Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

223 linhas
7.5KB

  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. // A fixed point implementation of Freeverb by Jezar at Dreampoint
  27. // http://blog.bjornroche.com/2012/06/freeverb-original-public-domain-code-by.html
  28. // https://music.columbia.edu/pipermail/music-dsp/2001-October/045433.html
  29. #include <Arduino.h>
  30. #include "effect_freeverb.h"
  31. #include "utility/dspinst.h"
  32. AudioEffectFreeverb::AudioEffectFreeverb() : AudioStream(1, inputQueueArray)
  33. {
  34. memset(comb1buf, 0, sizeof(comb1buf));
  35. memset(comb2buf, 0, sizeof(comb2buf));
  36. memset(comb3buf, 0, sizeof(comb3buf));
  37. memset(comb4buf, 0, sizeof(comb4buf));
  38. memset(comb5buf, 0, sizeof(comb5buf));
  39. memset(comb6buf, 0, sizeof(comb6buf));
  40. memset(comb7buf, 0, sizeof(comb7buf));
  41. memset(comb8buf, 0, sizeof(comb8buf));
  42. comb1index = 0;
  43. comb2index = 0;
  44. comb3index = 0;
  45. comb4index = 0;
  46. comb5index = 0;
  47. comb6index = 0;
  48. comb7index = 0;
  49. comb8index = 0;
  50. comb1filter = 0;
  51. comb2filter = 0;
  52. comb3filter = 0;
  53. comb4filter = 0;
  54. comb5filter = 0;
  55. comb6filter = 0;
  56. comb7filter = 0;
  57. comb8filter = 0;
  58. combdamp1 = 6553;
  59. combdamp2 = 26215;
  60. combfeeback = 27524;
  61. memset(allpass1buf, 0, sizeof(allpass1buf));
  62. memset(allpass2buf, 0, sizeof(allpass2buf));
  63. memset(allpass3buf, 0, sizeof(allpass3buf));
  64. memset(allpass4buf, 0, sizeof(allpass4buf));
  65. allpass1index = 0;
  66. allpass2index = 0;
  67. allpass3index = 0;
  68. allpass4index = 0;
  69. }
  70. #if 1
  71. #define sat16(n, rshift) signed_saturate_rshift((n), 16, (rshift))
  72. #else
  73. static int16_t sat16(int32_t n, int rshift)
  74. {
  75. n = n >> rshift;
  76. if (n > 32767) {
  77. return 32767;
  78. }
  79. if (n < -32768) {
  80. return -32768;
  81. }
  82. return n;
  83. }
  84. #endif
  85. // TODO: move this to one of the data files, use in output_adat.cpp, output_tdm.cpp, etc
  86. static const audio_block_t zeroblock = {
  87. 0, 0, 0, {
  88. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  89. #if AUDIO_BLOCK_SAMPLES > 16
  90. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  91. #endif
  92. #if AUDIO_BLOCK_SAMPLES > 32
  93. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  94. #endif
  95. #if AUDIO_BLOCK_SAMPLES > 48
  96. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97. #endif
  98. #if AUDIO_BLOCK_SAMPLES > 64
  99. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  100. #endif
  101. #if AUDIO_BLOCK_SAMPLES > 80
  102. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  103. #endif
  104. #if AUDIO_BLOCK_SAMPLES > 96
  105. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  106. #endif
  107. #if AUDIO_BLOCK_SAMPLES > 112
  108. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  109. #endif
  110. } };
  111. void AudioEffectFreeverb::update()
  112. {
  113. #if defined(KINETISK)
  114. const audio_block_t *block;
  115. audio_block_t *outblock;
  116. int i;
  117. int16_t input, bufout, output;
  118. int32_t sum;
  119. outblock = allocate();
  120. if (!outblock) {
  121. audio_block_t *tmp = receiveReadOnly(0);
  122. if (tmp) release(tmp);
  123. return;
  124. }
  125. block = receiveReadOnly(0);
  126. if (!block) block = &zeroblock;
  127. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  128. // TODO: scale numerical range depending on roomsize & damping
  129. input = sat16(block->data[i] * 8738, 17); // for numerical headroom
  130. sum = 0;
  131. bufout = comb1buf[comb1index];
  132. sum += bufout;
  133. comb1filter = sat16(bufout * combdamp2 + comb1filter * combdamp1, 15);
  134. comb1buf[comb1index] = sat16(input + sat16(comb1filter * combfeeback, 15), 0);
  135. if (++comb1index >= sizeof(comb1buf)/sizeof(int16_t)) comb1index = 0;
  136. bufout = comb2buf[comb2index];
  137. sum += bufout;
  138. comb2filter = sat16(bufout * combdamp2 + comb2filter * combdamp1, 15);
  139. comb2buf[comb2index] = sat16(input + sat16(comb2filter * combfeeback, 15), 0);
  140. if (++comb2index >= sizeof(comb2buf)/sizeof(int16_t)) comb2index = 0;
  141. bufout = comb3buf[comb3index];
  142. sum += bufout;
  143. comb3filter = sat16(bufout * combdamp2 + comb3filter * combdamp1, 15);
  144. comb3buf[comb3index] = sat16(input + sat16(comb3filter * combfeeback, 15), 0);
  145. if (++comb3index >= sizeof(comb3buf)/sizeof(int16_t)) comb3index = 0;
  146. bufout = comb4buf[comb4index];
  147. sum += bufout;
  148. comb4filter = sat16(bufout * combdamp2 + comb4filter * combdamp1, 15);
  149. comb4buf[comb4index] = sat16(input + sat16(comb4filter * combfeeback, 15), 0);
  150. if (++comb4index >= sizeof(comb4buf)/sizeof(int16_t)) comb4index = 0;
  151. bufout = comb5buf[comb5index];
  152. sum += bufout;
  153. comb5filter = sat16(bufout * combdamp2 + comb5filter * combdamp1, 15);
  154. comb5buf[comb5index] = sat16(input + sat16(comb5filter * combfeeback, 15), 0);
  155. if (++comb5index >= sizeof(comb5buf)/sizeof(int16_t)) comb5index = 0;
  156. bufout = comb6buf[comb6index];
  157. sum += bufout;
  158. comb6filter = sat16(bufout * combdamp2 + comb6filter * combdamp1, 15);
  159. comb6buf[comb6index] = sat16(input + sat16(comb6filter * combfeeback, 15), 0);
  160. if (++comb6index >= sizeof(comb6buf)/sizeof(int16_t)) comb6index = 0;
  161. bufout = comb7buf[comb7index];
  162. sum += bufout;
  163. comb7filter = sat16(bufout * combdamp2 + comb7filter * combdamp1, 15);
  164. comb7buf[comb7index] = sat16(input + sat16(comb7filter * combfeeback, 15), 0);
  165. if (++comb7index >= sizeof(comb7buf)/sizeof(int16_t)) comb7index = 0;
  166. bufout = comb8buf[comb8index];
  167. sum += bufout;
  168. comb8filter = sat16(bufout * combdamp2 + comb8filter * combdamp1, 15);
  169. comb8buf[comb8index] = sat16(input + sat16(comb8filter * combfeeback, 15), 0);
  170. if (++comb8index >= sizeof(comb8buf)/sizeof(int16_t)) comb8index = 0;
  171. output = sat16(sum * 31457, 17);
  172. bufout = allpass1buf[allpass1index];
  173. allpass1buf[allpass1index] = output + (bufout >> 1);
  174. output = sat16(bufout - output, 1);
  175. if (++allpass1index >= sizeof(allpass1buf)/sizeof(int16_t)) allpass1index = 0;
  176. bufout = allpass2buf[allpass2index];
  177. allpass2buf[allpass2index] = output + (bufout >> 1);
  178. output = sat16(bufout - output, 1);
  179. if (++allpass2index >= sizeof(allpass2buf)/sizeof(int16_t)) allpass2index = 0;
  180. bufout = allpass3buf[allpass3index];
  181. allpass3buf[allpass3index] = output + (bufout >> 1);
  182. output = sat16(bufout - output, 1);
  183. if (++allpass3index >= sizeof(allpass3buf)/sizeof(int16_t)) allpass3index = 0;
  184. bufout = allpass4buf[allpass4index];
  185. allpass4buf[allpass4index] = output + (bufout >> 1);
  186. output = sat16(bufout - output, 1);
  187. if (++allpass4index >= sizeof(allpass4buf)/sizeof(int16_t)) allpass4index = 0;
  188. outblock->data[i] = sat16(output * 30, 0);
  189. }
  190. transmit(outblock);
  191. release(outblock);
  192. if (block != &zeroblock) release((audio_block_t *)block);
  193. #elif defined(KINETISL)
  194. audio_block_t *block;
  195. block = receiveReadOnly(0);
  196. if (block) release(block);
  197. #endif
  198. }