Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

179 lines
4.9KB

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