Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

153 lines
3.5KB

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