Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

synth_waveform.cpp 5.2KB

il y a 10 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. void AudioSynthWaveform::update(void)
  35. {
  36. audio_block_t *block;
  37. short *bp, *end;
  38. int32_t val1, val2, val3;
  39. uint32_t index, scale;
  40. // temporaries for TRIANGLE
  41. uint32_t mag;
  42. short tmp_amp;
  43. if(tone_amp == 0) return;
  44. block = allocate();
  45. if (block) {
  46. bp = block->data;
  47. switch(tone_type) {
  48. case WAVEFORM_SINE:
  49. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  50. // Calculate interpolated sin
  51. index = tone_phase >> 23;
  52. val1 = AudioWaveformSine[index];
  53. val2 = AudioWaveformSine[index+1];
  54. scale = (tone_phase >> 7) & 0xFFFF;
  55. val2 *= scale;
  56. val1 *= 0xFFFF - scale;
  57. val3 = (val1 + val2) >> 16;
  58. *bp++ = (short)((val3 * tone_amp) >> 15);
  59. // phase and incr are both unsigned 32-bit fractions
  60. tone_phase += tone_incr;
  61. // If tone_phase has overflowed, truncate the top bit
  62. if(tone_phase & 0x80000000)tone_phase &= 0x7fffffff;
  63. }
  64. break;
  65. case WAVEFORM_ARBITRARY:
  66. if (!arbdata) {
  67. release(block);
  68. return;
  69. }
  70. // len = 256
  71. for (int i = 0; i < AUDIO_BLOCK_SAMPLES;i++) {
  72. index = tone_phase >> 23;
  73. val1 = *(arbdata + index);
  74. val2 = *(arbdata + ((index + 1) & 255));
  75. scale = (tone_phase >> 7) & 0xFFFF;
  76. val2 *= scale;
  77. val1 *= 0xFFFF - scale;
  78. val3 = (val1 + val2) >> 16;
  79. *bp++ = (short)((val3 * tone_amp) >> 15);
  80. tone_phase += tone_incr;
  81. tone_phase &= 0x7fffffff;
  82. }
  83. break;
  84. case WAVEFORM_SQUARE:
  85. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  86. if(tone_phase & 0x40000000)*bp++ = -tone_amp;
  87. else *bp++ = tone_amp;
  88. // phase and incr are both unsigned 32-bit fractions
  89. tone_phase += tone_incr;
  90. }
  91. break;
  92. case WAVEFORM_SAWTOOTH:
  93. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  94. *bp++ = ((short)(tone_phase>>15)*tone_amp) >> 15;
  95. // phase and incr are both unsigned 32-bit fractions
  96. tone_phase += tone_incr;
  97. }
  98. break;
  99. case WAVEFORM_SAWTOOTH_REVERSE:
  100. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  101. *bp++ = ((short)(tone_phase>>15)*tone_amp) >> 15;
  102. // phase and incr are both unsigned 32-bit fractions
  103. tone_phase -= tone_incr;
  104. }
  105. break;
  106. case WAVEFORM_TRIANGLE:
  107. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  108. if(tone_phase & 0x80000000) {
  109. // negative half-cycle
  110. tmp_amp = -tone_amp;
  111. }
  112. else {
  113. // positive half-cycle
  114. tmp_amp = tone_amp;
  115. }
  116. mag = tone_phase << 2;
  117. // Determine which quadrant
  118. if(tone_phase & 0x40000000) {
  119. // negate the magnitude
  120. mag = ~mag + 1;
  121. }
  122. *bp++ = ((short)(mag>>17)*tmp_amp) >> 15;
  123. tone_phase += 2*tone_incr;
  124. }
  125. break;
  126. case WAVEFORM_PULSE:
  127. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  128. if(tone_phase < tone_width)*bp++ = -tone_amp;
  129. else *bp++ = tone_amp;
  130. tone_phase += tone_incr;
  131. }
  132. break;
  133. case WAVEFORM_SAMPLE_HOLD:
  134. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  135. if(tone_phase < tone_incr) {
  136. sample = random(-tone_amp, tone_amp);
  137. }
  138. *bp++ = sample;
  139. tone_phase += tone_incr;
  140. }
  141. break;
  142. }
  143. if (tone_offset) {
  144. bp = block->data;
  145. end = bp + AUDIO_BLOCK_SAMPLES;
  146. do {
  147. val1 = *bp;
  148. *bp++ = signed_saturate_rshift(val1 + tone_offset, 16, 0);
  149. } while (bp < end);
  150. }
  151. transmit(block,0);
  152. release(block);
  153. }
  154. }