You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.1KB

  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_waveform_h_
  27. #define synth_waveform_h_
  28. #include "AudioStream.h"
  29. #include "arm_math.h"
  30. //#define ORIGINAL_AUDIOSYNTHWAVEFORM
  31. #ifdef ORIGINAL_AUDIOSYNTHWAVEFORM
  32. // waveforms.c
  33. extern "C" {
  34. extern const int16_t AudioWaveformSine[257];
  35. extern const int16_t AudioWaveformTriangle[257];
  36. extern const int16_t AudioWaveformSquare[257];
  37. extern const int16_t AudioWaveformSawtooth[257];
  38. }
  39. class AudioSynthWaveform : public AudioStream
  40. {
  41. public:
  42. AudioSynthWaveform(const int16_t *waveform)
  43. : AudioStream(0, NULL), wavetable(waveform), magnitude(0), phase(0)
  44. , ramp_down(0), ramp_up(0), ramp_mag(0), ramp_length(0)
  45. { }
  46. void frequency(float freq) {
  47. if (freq > AUDIO_SAMPLE_RATE_EXACT / 2 || freq < 0.0) return;
  48. phase_increment = (freq / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  49. }
  50. void amplitude(float n) { // 0 to 1.0
  51. if (n < 0) n = 0;
  52. else if (n > 1.0) n = 1.0;
  53. // Ramp code
  54. if(magnitude && (n == 0)) {
  55. ramp_down = ramp_length;
  56. ramp_up = 0;
  57. last_magnitude = magnitude;
  58. }
  59. else if((magnitude == 0) && n) {
  60. ramp_up = ramp_length;
  61. ramp_down = 0;
  62. }
  63. // set new magnitude
  64. magnitude = n * 32767.0;
  65. }
  66. virtual void update(void);
  67. void set_ramp_length(uint16_t r_length);
  68. private:
  69. const int16_t *wavetable;
  70. uint16_t magnitude;
  71. uint16_t last_magnitude;
  72. uint32_t phase;
  73. uint32_t phase_increment;
  74. uint32_t ramp_down;
  75. uint32_t ramp_up;
  76. uint32_t ramp_mag;
  77. uint16_t ramp_length;
  78. };
  79. #else
  80. // waveforms.c
  81. extern "C" {
  82. extern const int16_t AudioWaveformSine[257];
  83. }
  84. #define AUDIO_SAMPLE_RATE_ROUNDED (44118)
  85. #define DELAY_PASSTHRU -1
  86. #define TONE_TYPE_SINE 0
  87. #define TONE_TYPE_SAWTOOTH 1
  88. #define TONE_TYPE_SQUARE 2
  89. #define TONE_TYPE_TRIANGLE 3
  90. class AudioSynthWaveform :
  91. public AudioStream
  92. {
  93. public:
  94. AudioSynthWaveform(void) :
  95. AudioStream(0,NULL),
  96. tone_freq(0), tone_phase(0), tone_incr(0), tone_type(0),
  97. ramp_down(0), ramp_up(0), ramp_length(0)
  98. {
  99. }
  100. void frequency(float t_hi)
  101. {
  102. if (t_hi > AUDIO_SAMPLE_RATE_EXACT / 2 || t_hi < 0.0) return;
  103. tone_incr = ((0x80000000LL*t_hi)/AUDIO_SAMPLE_RATE_EXACT) + 0.5;
  104. }
  105. // If ramp_length is non-zero this will set up
  106. // either a rmap up or a ramp down when a wave
  107. // first starts or when the amplitude is set
  108. // back to zero.
  109. // Note that if the ramp_length is N, the generated
  110. // wave will be N samples longer than when it is not
  111. // ramp
  112. void amplitude(float n) { // 0 to 1.0
  113. if (n < 0) n = 0;
  114. else if (n > 1.0) n = 1.0;
  115. // Ramp code
  116. if(tone_amp && (n == 0)) {
  117. ramp_down = ramp_length;
  118. ramp_up = 0;
  119. last_tone_amp = tone_amp;
  120. }
  121. else if((tone_amp == 0) && n) {
  122. ramp_up = ramp_length;
  123. ramp_down = 0;
  124. // reset the phase when the amplitude was zero
  125. // and has now been increased. Note that this
  126. // happens even if the wave is not ramped
  127. // so that the signal starts at zero
  128. tone_phase = 0;
  129. }
  130. // set new magnitude
  131. tone_amp = n * 32767.0;
  132. }
  133. boolean begin(float t_amp,float t_hi,short t_type);
  134. virtual void update(void);
  135. void set_ramp_length(int16_t r_length);
  136. private:
  137. short tone_amp;
  138. short last_tone_amp;
  139. short tone_freq;
  140. uint32_t tone_phase;
  141. // volatile prevents the compiler optimizing out the frequency function
  142. volatile uint32_t tone_incr;
  143. short tone_type;
  144. uint32_t ramp_down;
  145. uint32_t ramp_up;
  146. uint16_t ramp_length;
  147. };
  148. #endif
  149. #if 0
  150. class AudioSineWaveMod : public AudioStream
  151. {
  152. public:
  153. AudioSineWaveMod() : AudioStream(1, inputQueueArray) {}
  154. void frequency(float freq);
  155. //void amplitude(q15 n);
  156. virtual void update(void);
  157. private:
  158. uint32_t phase;
  159. uint32_t phase_increment;
  160. uint32_t modulation_factor;
  161. audio_block_t *inputQueueArray[1];
  162. };
  163. #endif
  164. #endif