Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

185 lines
6.4KB

  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. #include "synth_waveform.h"
  27. #include "arm_math.h"
  28. #include "utility/dspinst.h"
  29. /******************************************************************/
  30. // PAH - add ramp-up and ramp-down to the onset of the wave
  31. // the length is specified in samples
  32. void AudioSynthWaveform::set_ramp_length(int16_t r_length)
  33. {
  34. if(r_length < 0) {
  35. ramp_length = 0;
  36. return;
  37. }
  38. // Don't set the ramp length longer than about 4 milliseconds
  39. if(r_length > 44*4) {
  40. ramp_length = 44*4;
  41. return;
  42. }
  43. ramp_length = r_length;
  44. }
  45. boolean AudioSynthWaveform::begin(float t_amp,int t_hi,short type)
  46. {
  47. tone_type = type;
  48. // tone_amp = t_amp;
  49. amplitude(t_amp);
  50. tone_freq = t_hi;
  51. if(t_hi < 1)return false;
  52. if(t_hi >= AUDIO_SAMPLE_RATE_EXACT/2)return false;
  53. tone_phase = 0;
  54. // tone_incr = (0x100000000LL*t_hi)/AUDIO_SAMPLE_RATE_EXACT;
  55. tone_incr = (0x80000000LL*t_hi)/AUDIO_SAMPLE_RATE_EXACT;
  56. if(0) {
  57. Serial.print("AudioSynthWaveform.begin(tone_amp = ");
  58. Serial.print(t_amp);
  59. Serial.print(", tone_hi = ");
  60. Serial.print(t_hi);
  61. Serial.print(", tone_incr = ");
  62. Serial.print(tone_incr,HEX);
  63. // Serial.print(", tone_hi = ");
  64. // Serial.print(t_hi);
  65. Serial.println(")");
  66. }
  67. return(true);
  68. }
  69. // PAH - 140313 fixed the calculation of the tone so that its spectrum
  70. // is much improved
  71. // PAH - 140313 fixed a problem with ramping
  72. void AudioSynthWaveform::update(void)
  73. {
  74. audio_block_t *block;
  75. short *bp;
  76. // temporary for ramp in sine
  77. uint32_t ramp_mag;
  78. // temporaries for TRIANGLE
  79. uint32_t mag;
  80. short tmp_amp;
  81. if(tone_freq == 0)return;
  82. // L E F T C H A N N E L O N L Y
  83. block = allocate();
  84. if(block) {
  85. bp = block->data;
  86. switch(tone_type) {
  87. case TONE_TYPE_SINE:
  88. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  89. // The value of ramp_up is always initialized to RAMP_LENGTH and then is
  90. // decremented each time through here until it reaches zero.
  91. // The value of ramp_up is used to generate a Q15 fraction which varies
  92. // from [0 - 1), and multiplies this by the current sample
  93. if(ramp_up) {
  94. // ramp up to the new magnitude
  95. // ramp_mag is the Q15 representation of the fraction
  96. // Since ramp_up can't be zero, this cannot generate +1
  97. ramp_mag = ((ramp_length-ramp_up)<<15)/ramp_length;
  98. ramp_up--;
  99. // adjust tone_phase to Q15 format and then adjust the result
  100. // of the multiplication
  101. // calculate the sample
  102. tmp_amp = (short)((arm_sin_q15(tone_phase>>16) * tone_amp) >> 17);
  103. *bp++ = (tmp_amp * ramp_mag)>>15;
  104. }
  105. else if(ramp_down) {
  106. // ramp down to zero from the last magnitude
  107. // The value of ramp_down is always initialized to RAMP_LENGTH and then is
  108. // decremented each time through here until it reaches zero.
  109. // The value of ramp_down is used to generate a Q15 fraction which varies
  110. // from [0 - 1), and multiplies this by the current sample
  111. // avoid RAMP_LENGTH/RAMP_LENGTH because Q15 format
  112. // cannot represent +1
  113. ramp_mag = ((ramp_down - 1)<<15)/ramp_length;
  114. ramp_down--;
  115. // adjust tone_phase to Q15 format and then adjust the result
  116. // of the multiplication
  117. tmp_amp = (short)((arm_sin_q15(tone_phase>>16) * last_tone_amp) >> 17);
  118. *bp++ = (tmp_amp * ramp_mag)>>15;
  119. } else {
  120. // adjust tone_phase to Q15 format and then adjust the result
  121. // of the multiplication
  122. tmp_amp = (short)((arm_sin_q15(tone_phase>>16) * tone_amp) >> 17);
  123. *bp++ = tmp_amp;
  124. }
  125. // phase and incr are both unsigned 32-bit fractions
  126. tone_phase += tone_incr;
  127. // If tone_phase has overflowed, truncate the top bit
  128. if(tone_phase & 0x80000000)tone_phase &= 0x7fffffff;
  129. }
  130. break;
  131. case TONE_TYPE_SQUARE:
  132. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  133. if(tone_phase & 0x80000000)*bp++ = -tone_amp;
  134. else *bp++ = tone_amp;
  135. // phase and incr are both unsigned 32-bit fractions
  136. tone_phase += tone_incr;
  137. }
  138. break;
  139. case TONE_TYPE_SAWTOOTH:
  140. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  141. *bp = ((short)(tone_phase>>16)*tone_amp) >> 15;
  142. bp++;
  143. // phase and incr are both unsigned 32-bit fractions
  144. tone_phase += tone_incr;
  145. }
  146. break;
  147. case TONE_TYPE_TRIANGLE:
  148. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  149. if(tone_phase & 0x80000000) {
  150. // negative half-cycle
  151. tmp_amp = -tone_amp;
  152. }
  153. else {
  154. // positive half-cycle
  155. tmp_amp = tone_amp;
  156. }
  157. mag = tone_phase << 2;
  158. // Determine which quadrant
  159. if(tone_phase & 0x40000000) {
  160. // negate the magnitude
  161. mag = ~mag + 1;
  162. }
  163. *bp++ = ((short)(mag>>17)*tmp_amp) >> 15;
  164. tone_phase += tone_incr;
  165. }
  166. break;
  167. }
  168. // send the samples to the left channel
  169. transmit(block,0);
  170. release(block);
  171. }
  172. }