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.

216 lines
6.3KB

  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_sine.h"
  27. #include "utility/dspinst.h"
  28. // data_waveforms.c
  29. extern "C" {
  30. extern const int16_t AudioWaveformSine[257];
  31. }
  32. void AudioSynthWaveformSine::update(void)
  33. {
  34. audio_block_t *block;
  35. uint32_t i, ph, inc, index, scale;
  36. int32_t val1, val2;
  37. if (magnitude) {
  38. block = allocate();
  39. if (block) {
  40. ph = phase_accumulator;
  41. inc = phase_increment;
  42. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  43. index = ph >> 24;
  44. val1 = AudioWaveformSine[index];
  45. val2 = AudioWaveformSine[index+1];
  46. scale = (ph >> 8) & 0xFFFF;
  47. val2 *= scale;
  48. val1 *= 0x10000 - scale;
  49. #if defined(KINETISK)
  50. block->data[i] = multiply_32x32_rshift32(val1 + val2, magnitude);
  51. #elif defined(KINETISL)
  52. block->data[i] = (((val1 + val2) >> 16) * magnitude) >> 16;
  53. #endif
  54. ph += inc;
  55. }
  56. phase_accumulator = ph;
  57. transmit(block);
  58. release(block);
  59. return;
  60. }
  61. }
  62. phase_accumulator += phase_increment * AUDIO_BLOCK_SAMPLES;
  63. }
  64. #if defined(KINETISK)
  65. // High accuracy 11th order Taylor Series Approximation
  66. // input is 0 to 0xFFFFFFFF, representing 0 to 360 degree phase
  67. // output is 32 bit signed integer, top 25 bits should be very good
  68. static int32_t taylor(uint32_t ph)
  69. {
  70. int32_t angle, sum, p1, p2, p3, p5, p7, p9, p11;
  71. if (ph >= 0xC0000000 || ph < 0x40000000) { // ph: 0.32
  72. angle = (int32_t)ph; // valid from -90 to +90 degrees
  73. } else {
  74. angle = (int32_t)(0x80000000u - ph); // angle: 2.30
  75. }
  76. p1 = multiply_32x32_rshift32_rounded(angle, 1686629713) << 2; // p1: 2.30
  77. p2 = multiply_32x32_rshift32_rounded(p1, p1) << 1; // p2: 3.29
  78. p3 = multiply_32x32_rshift32_rounded(p2, p1) << 2; // p3: 3.29
  79. sum = multiply_subtract_32x32_rshift32_rounded(p1, p3, 1431655765); // sum: 2.30
  80. p5 = multiply_32x32_rshift32_rounded(p3, p2); // p5: 6.26
  81. sum = multiply_accumulate_32x32_rshift32_rounded(sum, p5, 572662306);
  82. p7 = multiply_32x32_rshift32_rounded(p5, p2); // p7: 9.23
  83. sum = multiply_subtract_32x32_rshift32_rounded(sum, p7, 109078534);
  84. p9 = multiply_32x32_rshift32_rounded(p7, p2); // p9: 12.20
  85. sum = multiply_accumulate_32x32_rshift32_rounded(sum, p9, 12119837);
  86. p11 = multiply_32x32_rshift32_rounded(p9, p2); // p11: 15.17
  87. sum = multiply_subtract_32x32_rshift32_rounded(sum, p11, 881443);
  88. return sum <<= 1; // return: 1.31
  89. }
  90. #endif
  91. void AudioSynthWaveformSineHires::update(void)
  92. {
  93. #if defined(KINETISK)
  94. audio_block_t *msw, *lsw;
  95. uint32_t i, ph, inc;
  96. int32_t val;
  97. if (magnitude) {
  98. msw = allocate();
  99. lsw = allocate();
  100. if (msw && lsw) {
  101. ph = phase_accumulator;
  102. inc = phase_increment;
  103. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  104. val = taylor(ph);
  105. msw->data[i] = val >> 16;
  106. lsw->data[i] = val & 0xFFFF;
  107. ph += inc;
  108. }
  109. phase_accumulator = ph;
  110. transmit(msw, 0);
  111. release(msw);
  112. transmit(lsw, 1);
  113. release(lsw);
  114. return;
  115. } else {
  116. if (msw) release(msw);
  117. if (lsw) release(lsw);
  118. }
  119. }
  120. phase_accumulator += phase_increment * AUDIO_BLOCK_SAMPLES;
  121. #endif
  122. }
  123. #if defined(KINETISK)
  124. void AudioSynthWaveformSineModulated::update(void)
  125. {
  126. audio_block_t *block, *modinput;
  127. uint32_t i, ph, inc, index, scale;
  128. int32_t val1, val2;
  129. int16_t mod;
  130. modinput = receiveReadOnly();
  131. ph = phase_accumulator;
  132. inc = phase_increment;
  133. block = allocate();
  134. if (!block) {
  135. // unable to allocate memory, so we'll send nothing
  136. if (modinput) {
  137. // but if we got modulation data, update the phase
  138. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  139. mod = modinput->data[i];
  140. ph += inc + (multiply_32x32_rshift32(inc, mod << 16) << 1);
  141. }
  142. release(modinput);
  143. } else {
  144. ph += phase_increment * AUDIO_BLOCK_SAMPLES;
  145. }
  146. phase_accumulator = ph;
  147. return;
  148. }
  149. if (modinput) {
  150. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  151. index = ph >> 24;
  152. val1 = AudioWaveformSine[index];
  153. val2 = AudioWaveformSine[index+1];
  154. scale = (ph >> 8) & 0xFFFF;
  155. val2 *= scale;
  156. val1 *= 0x10000 - scale;
  157. //block->data[i] = (((val1 + val2) >> 16) * magnitude) >> 16;
  158. block->data[i] = multiply_32x32_rshift32(val1 + val2, magnitude);
  159. // -32768 = no phase increment
  160. // 32767 = double phase increment
  161. mod = modinput->data[i];
  162. ph += inc + (multiply_32x32_rshift32(inc, mod << 16) << 1);
  163. //ph += inc + (((int64_t)inc * (mod << 16)) >> 31);
  164. }
  165. release(modinput);
  166. } else {
  167. ph = phase_accumulator;
  168. inc = phase_increment;
  169. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  170. index = ph >> 24;
  171. val1 = AudioWaveformSine[index];
  172. val2 = AudioWaveformSine[index+1];
  173. scale = (ph >> 8) & 0xFFFF;
  174. val2 *= scale;
  175. val1 *= 0x10000 - scale;
  176. block->data[i] = (val1 + val2) >> 16;
  177. ph += inc;
  178. }
  179. }
  180. phase_accumulator = ph;
  181. transmit(block);
  182. release(block);
  183. }
  184. #elif defined(KINETISL)
  185. void AudioSynthWaveformSineModulated::update(void)
  186. {
  187. audio_block_t *block;
  188. block = receiveReadOnly();
  189. if (block) release(block);
  190. }
  191. #endif