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.

synth_waveform.cpp 4.7KB

10 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_TRIANGLE:
  100. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  101. if(tone_phase & 0x80000000) {
  102. // negative half-cycle
  103. tmp_amp = -tone_amp;
  104. }
  105. else {
  106. // positive half-cycle
  107. tmp_amp = tone_amp;
  108. }
  109. mag = tone_phase << 2;
  110. // Determine which quadrant
  111. if(tone_phase & 0x40000000) {
  112. // negate the magnitude
  113. mag = ~mag + 1;
  114. }
  115. *bp++ = ((short)(mag>>17)*tmp_amp) >> 15;
  116. tone_phase += 2*tone_incr;
  117. }
  118. break;
  119. case WAVEFORM_PULSE:
  120. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  121. if(tone_phase < tone_width)*bp++ = -tone_amp;
  122. else *bp++ = tone_amp;
  123. tone_phase += tone_incr;
  124. }
  125. break;
  126. }
  127. if (tone_offset) {
  128. bp = block->data;
  129. end = bp + AUDIO_BLOCK_SAMPLES;
  130. do {
  131. val1 = *bp;
  132. *bp++ = signed_saturate_rshift(val1 + tone_offset, 16, 0);
  133. } while (bp < end);
  134. }
  135. transmit(block,0);
  136. release(block);
  137. }
  138. }