您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

effect_freeverb.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "effect_freeverb.h"
  28. #include "utility/dspinst.h"
  29. AudioEffectFreeverb::AudioEffectFreeverb() : AudioStream(1, inputQueueArray)
  30. {
  31. memset(comb1buf, 0, sizeof(comb1buf));
  32. memset(comb2buf, 0, sizeof(comb2buf));
  33. memset(comb3buf, 0, sizeof(comb3buf));
  34. memset(comb4buf, 0, sizeof(comb4buf));
  35. memset(comb5buf, 0, sizeof(comb5buf));
  36. memset(comb6buf, 0, sizeof(comb6buf));
  37. memset(comb7buf, 0, sizeof(comb7buf));
  38. memset(comb8buf, 0, sizeof(comb8buf));
  39. comb1index = 0;
  40. comb2index = 0;
  41. comb3index = 0;
  42. comb4index = 0;
  43. comb5index = 0;
  44. comb6index = 0;
  45. comb7index = 0;
  46. comb8index = 0;
  47. comb1filter = 0;
  48. comb2filter = 0;
  49. comb3filter = 0;
  50. comb4filter = 0;
  51. comb5filter = 0;
  52. comb6filter = 0;
  53. comb7filter = 0;
  54. comb8filter = 0;
  55. combdamp1 = 6553;
  56. combdamp2 = 26215;
  57. combfeeback = 27524;
  58. memset(allpass1buf, 0, sizeof(allpass1buf));
  59. memset(allpass2buf, 0, sizeof(allpass2buf));
  60. memset(allpass3buf, 0, sizeof(allpass3buf));
  61. memset(allpass4buf, 0, sizeof(allpass4buf));
  62. allpass1index = 0;
  63. allpass2index = 0;
  64. allpass3index = 0;
  65. allpass4index = 0;
  66. }
  67. #if 1
  68. #define sat16(n, rshift) signed_saturate_rshift((n), 16, (rshift))
  69. #else
  70. static int16_t sat16(int32_t n, int rshift)
  71. {
  72. n = n >> rshift;
  73. if (n > 32767) {
  74. return 32767;
  75. }
  76. if (n < -32768) {
  77. return -32768;
  78. }
  79. return n;
  80. }
  81. #endif
  82. void AudioEffectFreeverb::update()
  83. {
  84. #if defined(KINETISK)
  85. audio_block_t *block, *outblock;
  86. int i;
  87. int16_t input, bufout, output;
  88. int32_t sum;
  89. outblock = allocate();
  90. if (!outblock) {
  91. block = receiveReadOnly(0);
  92. if (block) release(block);
  93. return;
  94. }
  95. block = receiveReadOnly(0);
  96. if (!block) {
  97. release(outblock);
  98. return;
  99. // TODO: pointer to zero block
  100. }
  101. for (i=0; i < 128; i++) {
  102. input = sat16(block->data[i] * 21845, 17); // div by 6, for numerical headroom
  103. sum = 0;
  104. bufout = comb1buf[comb1index];
  105. sum += bufout;
  106. comb1filter = sat16(bufout * combdamp2 + comb1filter * combdamp1, 15);
  107. comb1buf[comb1index] = sat16(input + sat16(comb1filter * combfeeback, 15), 0);
  108. if (++comb1index >= sizeof(comb1buf)/sizeof(int16_t)) comb1index = 0;
  109. bufout = comb2buf[comb2index];
  110. sum += bufout;
  111. comb2filter = sat16(bufout * combdamp2 + comb2filter * combdamp1, 15);
  112. comb2buf[comb2index] = sat16(input + sat16(comb2filter * combfeeback, 15), 0);
  113. if (++comb2index >= sizeof(comb2buf)/sizeof(int16_t)) comb2index = 0;
  114. bufout = comb3buf[comb3index];
  115. sum += bufout;
  116. comb3filter = sat16(bufout * combdamp2 + comb3filter * combdamp1, 15);
  117. comb3buf[comb3index] = sat16(input + sat16(comb3filter * combfeeback, 15), 0);
  118. if (++comb3index >= sizeof(comb3buf)/sizeof(int16_t)) comb3index = 0;
  119. bufout = comb4buf[comb4index];
  120. sum += bufout;
  121. comb4filter = sat16(bufout * combdamp2 + comb4filter * combdamp1, 15);
  122. comb4buf[comb4index] = sat16(input + sat16(comb4filter * combfeeback, 15), 0);
  123. if (++comb4index >= sizeof(comb4buf)/sizeof(int16_t)) comb4index = 0;
  124. bufout = comb5buf[comb5index];
  125. sum += bufout;
  126. comb5filter = sat16(bufout * combdamp2 + comb5filter * combdamp1, 15);
  127. comb5buf[comb5index] = sat16(input + sat16(comb5filter * combfeeback, 15), 0);
  128. if (++comb5index >= sizeof(comb5buf)/sizeof(int16_t)) comb5index = 0;
  129. bufout = comb6buf[comb6index];
  130. sum += bufout;
  131. comb6filter = sat16(bufout * combdamp2 + comb6filter * combdamp1, 15);
  132. comb6buf[comb6index] = sat16(input + sat16(comb6filter * combfeeback, 15), 0);
  133. if (++comb6index >= sizeof(comb6buf)/sizeof(int16_t)) comb6index = 0;
  134. bufout = comb7buf[comb7index];
  135. sum += bufout;
  136. comb7filter = sat16(bufout * combdamp2 + comb7filter * combdamp1, 15);
  137. comb7buf[comb7index] = sat16(input + sat16(comb7filter * combfeeback, 15), 0);
  138. if (++comb7index >= sizeof(comb7buf)/sizeof(int16_t)) comb7index = 0;
  139. bufout = comb8buf[comb8index];
  140. sum += bufout;
  141. comb8filter = sat16(bufout * combdamp2 + comb8filter * combdamp1, 15);
  142. comb8buf[comb8index] = sat16(input + sat16(comb8filter * combfeeback, 15), 0);
  143. if (++comb8index >= sizeof(comb8buf)/sizeof(int16_t)) comb8index = 0;
  144. output = sat16(sum * 31457, 17);
  145. bufout = allpass1buf[allpass1index];
  146. allpass1buf[allpass1index] = output + (bufout >> 1);
  147. output = sat16(bufout - output, 1);
  148. if (++allpass1index >= sizeof(allpass1buf)/sizeof(int16_t)) allpass1index = 0;
  149. bufout = allpass2buf[allpass2index];
  150. allpass2buf[allpass2index] = output + (bufout >> 1);
  151. output = sat16(bufout - output, 1);
  152. if (++allpass2index >= sizeof(allpass2buf)/sizeof(int16_t)) allpass2index = 0;
  153. bufout = allpass3buf[allpass3index];
  154. allpass3buf[allpass3index] = output + (bufout >> 1);
  155. output = sat16(bufout - output, 1);
  156. if (++allpass3index >= sizeof(allpass3buf)/sizeof(int16_t)) allpass3index = 0;
  157. bufout = allpass4buf[allpass4index];
  158. allpass4buf[allpass4index] = output + (bufout >> 1);
  159. output = sat16(bufout - output, 1);
  160. if (++allpass4index >= sizeof(allpass4buf)/sizeof(int16_t)) allpass4index = 0;
  161. outblock->data[i] = sat16(output * 12, 0);
  162. }
  163. transmit(outblock);
  164. release(outblock);
  165. release(block);
  166. #elif defined(KINETISL)
  167. audio_block_t *block;
  168. block = receiveReadOnly(0);
  169. if (block) release(block);
  170. #endif
  171. }