You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 satır
5.2KB

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