Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

172 Zeilen
4.6KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2018, 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. int16_t *bp, *end;
  39. int32_t val1, val2;
  40. int16_t magnitude15;
  41. uint32_t i, ph, index, index2, scale;
  42. const uint32_t inc = phase_increment;
  43. ph = phase_accumulator + phase_offset;
  44. if (magnitude == 0) {
  45. phase_accumulator += inc * AUDIO_BLOCK_SAMPLES;
  46. return;
  47. }
  48. block = allocate();
  49. if (!block) {
  50. phase_accumulator += inc * AUDIO_BLOCK_SAMPLES;
  51. return;
  52. }
  53. bp = block->data;
  54. switch(tone_type) {
  55. case WAVEFORM_SINE:
  56. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  57. index = ph >> 24;
  58. val1 = AudioWaveformSine[index];
  59. val2 = AudioWaveformSine[index+1];
  60. scale = (ph >> 8) & 0xFFFF;
  61. val2 *= scale;
  62. val1 *= 0x10000 - scale;
  63. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  64. ph += inc;
  65. }
  66. break;
  67. case WAVEFORM_ARBITRARY:
  68. if (!arbdata) {
  69. release(block);
  70. phase_accumulator += inc * AUDIO_BLOCK_SAMPLES;
  71. return;
  72. }
  73. // len = 256
  74. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  75. index = ph >> 24;
  76. index2 = index + 1;
  77. if (index2 >= 256) index2 = 0;
  78. val1 = *(arbdata + index);
  79. val2 = *(arbdata + index2);
  80. scale = (ph >> 8) & 0xFFFF;
  81. val2 *= scale;
  82. val1 *= 0x10000 - scale;
  83. *bp++ = multiply_32x32_rshift32(val1 + val2, magnitude);
  84. ph += inc;
  85. }
  86. break;
  87. case WAVEFORM_SQUARE:
  88. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  89. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  90. if (ph & 0x80000000) {
  91. *bp++ = -magnitude15;
  92. } else {
  93. *bp++ = magnitude15;
  94. }
  95. ph += inc;
  96. }
  97. break;
  98. case WAVEFORM_SAWTOOTH:
  99. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  100. *bp++ = signed_multiply_32x16t(magnitude, ph);
  101. ph += inc;
  102. }
  103. break;
  104. case WAVEFORM_SAWTOOTH_REVERSE:
  105. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  106. *bp++ = signed_multiply_32x16t(0xFFFFFFFFu - magnitude, ph);
  107. ph += inc;
  108. }
  109. break;
  110. case WAVEFORM_TRIANGLE:
  111. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  112. uint32_t phtop = ph >> 30;
  113. if (phtop == 1 || phtop == 2) {
  114. *bp++ = ((0x10000 - (ph >> 15)) * magnitude) >> 16;
  115. } else {
  116. *bp++ = ((ph >> 15) * magnitude) >> 16;
  117. }
  118. ph += inc;
  119. }
  120. break;
  121. case WAVEFORM_PULSE:
  122. magnitude15 = signed_saturate_rshift(magnitude, 16, 1);
  123. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  124. if (ph < pulse_width) {
  125. *bp++ = magnitude15;
  126. } else {
  127. *bp++ = -magnitude15;
  128. }
  129. ph += inc;
  130. }
  131. break;
  132. case WAVEFORM_SAMPLE_HOLD:
  133. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  134. *bp++ = sample;
  135. uint32_t newph = ph + inc;
  136. if (newph < ph) {
  137. sample = random(magnitude) - (magnitude >> 1);
  138. }
  139. ph = newph;
  140. }
  141. break;
  142. }
  143. phase_accumulator = ph - phase_offset;
  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. }