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ů.

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