PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

208 rindas
5.4KB

  1. /*
  2. * Copyright (c) 2016 Joao Rossi Filho
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. // https://github.com/joaoRossiFilho/teensy_reverb
  23. #include <Arduino.h>
  24. #include "effect_reverb.h"
  25. #include "utility/dspinst.h"
  26. #include "math_helper.h"
  27. void
  28. AudioEffectReverb::_do_comb_apf(struct comb_apf *apf, int32_t *in_buf, int32_t *out_buf)
  29. {
  30. int32_t acc_x, acc_y, g;
  31. int32_t w, z;
  32. uint32_t n, buf_len;
  33. g = apf->g;
  34. buf_len = apf->buf_len;
  35. for (n = 0; n < AUDIO_BLOCK_SAMPLES; n++) {
  36. acc_y = apf->buffer[apf->rd_idx%buf_len];
  37. acc_x = in_buf[n];
  38. w = multiply_32x32_rshift32_rounded(g, acc_y);
  39. acc_x += (w << 1);
  40. z = multiply_32x32_rshift32_rounded(g, acc_x);
  41. acc_y -= (z << 1);
  42. apf->buffer[apf->wr_idx%buf_len] = acc_x;
  43. out_buf[n] = acc_y;
  44. apf->rd_idx++;
  45. apf->wr_idx++;
  46. }
  47. }
  48. void
  49. AudioEffectReverb::_do_comb_lpf(struct comb_lpf *lpf, int32_t *in_buf, int32_t *out_buf)
  50. {
  51. int32_t x, y, w, z, g1, g2, z1;
  52. uint32_t n, buf_len;
  53. g1 = lpf->g1;
  54. g2 = lpf->g2;
  55. z1 = lpf->z1;
  56. buf_len = lpf->buf_len;
  57. for (n = 0; n < AUDIO_BLOCK_SAMPLES; n++) {
  58. y = lpf->buffer[lpf->rd_idx%buf_len];
  59. x = in_buf[n];
  60. w = multiply_accumulate_32x32_rshift32_rounded(y, g2, z1);
  61. z = multiply_accumulate_32x32_rshift32_rounded(x, g1, w);
  62. z1 = w;
  63. lpf->buffer[lpf->wr_idx%buf_len] = z;
  64. out_buf[n] = y;
  65. lpf->rd_idx++;
  66. lpf->wr_idx++;
  67. }
  68. lpf->z1 = z1;
  69. }
  70. void
  71. AudioEffectReverb::init_comb_filters(void)
  72. {
  73. int i;
  74. g_flt_apf[0] = 0.7;
  75. g_flt_apf[1] = -0.54;
  76. g_flt_apf[2] = 0.6;
  77. g2_flt_lpf = 0.985;
  78. arm_float_to_q31(g_flt_apf, g_q31_apf, 3);
  79. arm_float_to_q31(&g2_flt_lpf, &g2_q31_lpf, 1);
  80. apf[0].buffer = apf1_buf;
  81. apf[0].buf_len = APF1_BUF_LEN;
  82. apf[0].delay = APF1_DLY_LEN;
  83. apf[1].buffer = apf2_buf;
  84. apf[1].buf_len = APF2_BUF_LEN;
  85. apf[1].delay = APF2_DLY_LEN;
  86. apf[2].buffer = apf3_buf;
  87. apf[2].buf_len = APF3_BUF_LEN;
  88. apf[2].delay = APF3_DLY_LEN;
  89. for (i = 0; i < 3; i++) {
  90. apf[i].g = g_q31_apf[i];
  91. apf[i].wr_idx = 0;
  92. apf[i].rd_idx = apf[i].wr_idx - apf[i].delay -1;
  93. }
  94. lpf[0].buffer = lpf1_buf;
  95. lpf[0].buf_len = LPF1_BUF_LEN;
  96. lpf[0].delay = LPF1_DLY_LEN;
  97. lpf[1].buffer = lpf2_buf;
  98. lpf[1].buf_len = LPF2_BUF_LEN;
  99. lpf[1].delay = LPF2_DLY_LEN;
  100. lpf[2].buffer = lpf3_buf;
  101. lpf[2].buf_len = LPF3_BUF_LEN;
  102. lpf[2].delay = LPF3_DLY_LEN;
  103. lpf[3].buffer = lpf4_buf;
  104. lpf[3].buf_len = LPF4_BUF_LEN;
  105. lpf[3].delay = LPF4_DLY_LEN;
  106. for (i = 0; i < 4; i++) {
  107. lpf[i].g1 = g1_q31_lpf[i];
  108. lpf[i].g2 = g2_q31_lpf;
  109. lpf[i].wr_idx = 0;
  110. lpf[i].rd_idx = lpf[i].wr_idx - lpf[i].delay -1;
  111. }
  112. }
  113. void
  114. AudioEffectReverb::clear_buffers(void)
  115. {
  116. memset(apf1_buf, 0, sizeof(apf1_buf));
  117. memset(apf2_buf, 0, sizeof(apf2_buf));
  118. memset(apf3_buf, 0, sizeof(apf3_buf));
  119. memset(lpf1_buf, 0, sizeof(lpf1_buf));
  120. memset(lpf2_buf, 0, sizeof(lpf2_buf));
  121. memset(lpf3_buf, 0, sizeof(lpf3_buf));
  122. memset(lpf4_buf, 0, sizeof(lpf4_buf));
  123. }
  124. void
  125. AudioEffectReverb::reverbTime(float rt)
  126. {
  127. if (rt <= 0.0)
  128. return;
  129. reverb_time_sec = rt;
  130. g1_flt_lpf[0] = powf(10.0, -(3.0*LPF1_DLY_SEC)/(reverb_time_sec));
  131. g1_flt_lpf[1] = powf(10.0, -(3.0*LPF2_DLY_SEC)/(reverb_time_sec));
  132. g1_flt_lpf[2] = powf(10.0, -(3.0*LPF3_DLY_SEC)/(reverb_time_sec));
  133. g1_flt_lpf[3] = powf(10.0, -(3.0*LPF4_DLY_SEC)/(reverb_time_sec));
  134. arm_float_to_q31(g1_flt_lpf, g1_q31_lpf, 4);
  135. for (int i = 0; i < 4; i++)
  136. lpf[i].g1 = g1_q31_lpf[i];
  137. }
  138. void
  139. AudioEffectReverb::update(void)
  140. {
  141. audio_block_t *block;
  142. if (!(block = receiveWritable()))
  143. return;
  144. if (!block->data)
  145. return;
  146. arm_q15_to_q31(block->data, q31_buf, AUDIO_BLOCK_SAMPLES);
  147. _do_comb_apf(&apf[0], q31_buf, q31_buf);
  148. _do_comb_apf(&apf[1], q31_buf, q31_buf);
  149. _do_comb_lpf(&lpf[0], q31_buf, sum_buf);
  150. arm_shift_q31(sum_buf, -3, sum_buf, AUDIO_BLOCK_SAMPLES);
  151. _do_comb_lpf(&lpf[1], q31_buf, aux_buf);
  152. arm_shift_q31(aux_buf, -3, aux_buf, AUDIO_BLOCK_SAMPLES);
  153. arm_add_q31(sum_buf, aux_buf, sum_buf, AUDIO_BLOCK_SAMPLES);
  154. _do_comb_lpf(&lpf[2], q31_buf, aux_buf);
  155. arm_shift_q31(aux_buf, -3, aux_buf, AUDIO_BLOCK_SAMPLES);
  156. arm_add_q31(sum_buf, aux_buf, sum_buf, AUDIO_BLOCK_SAMPLES);
  157. _do_comb_lpf(&lpf[3], q31_buf, aux_buf);
  158. arm_shift_q31(aux_buf, -3, aux_buf, AUDIO_BLOCK_SAMPLES);
  159. arm_add_q31(sum_buf, aux_buf, sum_buf, AUDIO_BLOCK_SAMPLES);
  160. _do_comb_apf(&apf[2], sum_buf, q31_buf);
  161. arm_q31_to_q15(q31_buf, block->data, AUDIO_BLOCK_SAMPLES);
  162. transmit(block, 0);
  163. release(block);
  164. }
  165. /* EOF */