Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

synth_sine.h 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef synth_sine_h_
  27. #define synth_sine_h_
  28. #include "AudioStream.h"
  29. #include "arm_math.h"
  30. // TODO: investigate making a high resolution sine wave
  31. // using Taylor series expansion.
  32. // http://www.musicdsp.org/showone.php?id=13
  33. class AudioSynthWaveformSine : public AudioStream
  34. {
  35. public:
  36. AudioSynthWaveformSine() : AudioStream(0, NULL), magnitude(16384) {}
  37. void frequency(float freq) {
  38. if (freq < 0.0) freq = 0.0;
  39. else if (freq > AUDIO_SAMPLE_RATE_EXACT/2) freq = AUDIO_SAMPLE_RATE_EXACT/2;
  40. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  41. }
  42. void phase(float angle) {
  43. if (angle < 0.0) angle = 0.0;
  44. else if (angle > 360.0) {
  45. angle = angle - 360.0;
  46. if (angle >= 360.0) return;
  47. }
  48. phase_accumulator = angle * (4294967296.0 / 360.0);
  49. }
  50. void amplitude(float n) {
  51. if (n < 0) n = 0;
  52. else if (n > 1.0) n = 1.0;
  53. magnitude = n * 65536.0;
  54. }
  55. virtual void update(void);
  56. private:
  57. uint32_t phase_accumulator;
  58. uint32_t phase_increment;
  59. int32_t magnitude;
  60. };
  61. class AudioSynthWaveformSineModulated : public AudioStream
  62. {
  63. public:
  64. AudioSynthWaveformSineModulated() : AudioStream(1, inputQueueArray), magnitude(16384) {}
  65. // maximum unmodulated carrier frequency is 11025 Hz
  66. // input = +1.0 doubles carrier
  67. // input = -1.0 DC output
  68. void frequency(float freq) {
  69. if (freq < 0.0) freq = 0.0;
  70. else if (freq > AUDIO_SAMPLE_RATE_EXACT/4) freq = AUDIO_SAMPLE_RATE_EXACT/4;
  71. phase_increment = freq * (4294967296.0 / AUDIO_SAMPLE_RATE_EXACT);
  72. }
  73. void phase(float angle) {
  74. if (angle < 0.0) angle = 0.0;
  75. else if (angle > 360.0) {
  76. angle = angle - 360.0;
  77. if (angle >= 360.0) return;
  78. }
  79. phase_accumulator = angle * (4294967296.0 / 360.0);
  80. }
  81. void amplitude(float n) {
  82. if (n < 0) n = 0;
  83. else if (n > 1.0) n = 1.0;
  84. magnitude = n * 65536.0;
  85. }
  86. virtual void update(void);
  87. private:
  88. uint32_t phase_accumulator;
  89. uint32_t phase_increment;
  90. audio_block_t *inputQueueArray[1];
  91. int32_t magnitude;
  92. };
  93. #endif