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.

207 satır
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 "effect_reverb.h"
  24. #include "utility/dspinst.h"
  25. #include "math_helper.h"
  26. void
  27. AudioEffectReverb::_do_comb_apf(struct comb_apf *apf, int32_t *in_buf, int32_t *out_buf)
  28. {
  29. int32_t acc_x, acc_y, g;
  30. int32_t w, z;
  31. uint32_t n, buf_len;
  32. g = apf->g;
  33. buf_len = apf->buf_len;
  34. for (n = 0; n < AUDIO_BLOCK_SAMPLES; n++) {
  35. acc_y = apf->buffer[apf->rd_idx%buf_len];
  36. acc_x = in_buf[n];
  37. w = multiply_32x32_rshift32_rounded(g, acc_y);
  38. acc_x += (w << 1);
  39. z = multiply_32x32_rshift32_rounded(g, acc_x);
  40. acc_y -= (z << 1);
  41. apf->buffer[apf->wr_idx%buf_len] = acc_x;
  42. out_buf[n] = acc_y;
  43. apf->rd_idx++;
  44. apf->wr_idx++;
  45. }
  46. }
  47. void
  48. AudioEffectReverb::_do_comb_lpf(struct comb_lpf *lpf, int32_t *in_buf, int32_t *out_buf)
  49. {
  50. int32_t x, y, w, z, g1, g2, z1;
  51. uint32_t n, buf_len;
  52. g1 = lpf->g1;
  53. g2 = lpf->g2;
  54. z1 = lpf->z1;
  55. buf_len = lpf->buf_len;
  56. for (n = 0; n < AUDIO_BLOCK_SAMPLES; n++) {
  57. y = lpf->buffer[lpf->rd_idx%buf_len];
  58. x = in_buf[n];
  59. w = multiply_accumulate_32x32_rshift32_rounded(y, g2, z1);
  60. z = multiply_accumulate_32x32_rshift32_rounded(x, g1, w);
  61. z1 = w;
  62. lpf->buffer[lpf->wr_idx%buf_len] = z;
  63. out_buf[n] = y;
  64. lpf->rd_idx++;
  65. lpf->wr_idx++;
  66. }
  67. lpf->z1 = z1;
  68. }
  69. void
  70. AudioEffectReverb::init_comb_filters(void)
  71. {
  72. int i;
  73. g_flt_apf[0] = 0.7;
  74. g_flt_apf[1] = -0.54;
  75. g_flt_apf[2] = 0.6;
  76. g2_flt_lpf = 0.985;
  77. arm_float_to_q31(g_flt_apf, g_q31_apf, 3);
  78. arm_float_to_q31(&g2_flt_lpf, &g2_q31_lpf, 1);
  79. apf[0].buffer = apf1_buf;
  80. apf[0].buf_len = APF1_BUF_LEN;
  81. apf[0].delay = APF1_DLY_LEN;
  82. apf[1].buffer = apf2_buf;
  83. apf[1].buf_len = APF2_BUF_LEN;
  84. apf[1].delay = APF2_DLY_LEN;
  85. apf[2].buffer = apf3_buf;
  86. apf[2].buf_len = APF3_BUF_LEN;
  87. apf[2].delay = APF3_DLY_LEN;
  88. for (i = 0; i < 3; i++) {
  89. apf[i].g = g_q31_apf[i];
  90. apf[i].wr_idx = 0;
  91. apf[i].rd_idx = apf[i].wr_idx - apf[i].delay -1;
  92. }
  93. lpf[0].buffer = lpf1_buf;
  94. lpf[0].buf_len = LPF1_BUF_LEN;
  95. lpf[0].delay = LPF1_DLY_LEN;
  96. lpf[1].buffer = lpf2_buf;
  97. lpf[1].buf_len = LPF2_BUF_LEN;
  98. lpf[1].delay = LPF2_DLY_LEN;
  99. lpf[2].buffer = lpf3_buf;
  100. lpf[2].buf_len = LPF3_BUF_LEN;
  101. lpf[2].delay = LPF3_DLY_LEN;
  102. lpf[3].buffer = lpf4_buf;
  103. lpf[3].buf_len = LPF4_BUF_LEN;
  104. lpf[3].delay = LPF4_DLY_LEN;
  105. for (i = 0; i < 4; i++) {
  106. lpf[i].g1 = g1_q31_lpf[i];
  107. lpf[i].g2 = g2_q31_lpf;
  108. lpf[i].wr_idx = 0;
  109. lpf[i].rd_idx = lpf[i].wr_idx - lpf[i].delay -1;
  110. }
  111. }
  112. void
  113. AudioEffectReverb::clear_buffers(void)
  114. {
  115. memset(apf1_buf, 0, APF1_BUF_LEN);
  116. memset(apf2_buf, 0, APF1_BUF_LEN);
  117. memset(apf3_buf, 0, APF1_BUF_LEN);
  118. memset(lpf1_buf, 0, LPF1_BUF_LEN);
  119. memset(lpf2_buf, 0, LPF2_BUF_LEN);
  120. memset(lpf3_buf, 0, LPF3_BUF_LEN);
  121. memset(lpf4_buf, 0, LPF4_BUF_LEN);
  122. }
  123. void
  124. AudioEffectReverb::reverbTime(float rt)
  125. {
  126. if (rt <= 0.0)
  127. return;
  128. reverb_time_sec = rt;
  129. g1_flt_lpf[0] = powf(10.0, -(3.0*LPF1_DLY_SEC)/(reverb_time_sec));
  130. g1_flt_lpf[1] = powf(10.0, -(3.0*LPF2_DLY_SEC)/(reverb_time_sec));
  131. g1_flt_lpf[2] = powf(10.0, -(3.0*LPF3_DLY_SEC)/(reverb_time_sec));
  132. g1_flt_lpf[3] = powf(10.0, -(3.0*LPF4_DLY_SEC)/(reverb_time_sec));
  133. arm_float_to_q31(g1_flt_lpf, g1_q31_lpf, 4);
  134. for (int i = 0; i < 4; i++)
  135. lpf[i].g1 = g1_q31_lpf[i];
  136. }
  137. void
  138. AudioEffectReverb::update(void)
  139. {
  140. audio_block_t *block;
  141. if (!(block = receiveWritable()))
  142. return;
  143. if (!block->data)
  144. return;
  145. arm_q15_to_q31(block->data, q31_buf, AUDIO_BLOCK_SAMPLES);
  146. _do_comb_apf(&apf[0], q31_buf, q31_buf);
  147. _do_comb_apf(&apf[1], q31_buf, q31_buf);
  148. _do_comb_lpf(&lpf[0], q31_buf, sum_buf);
  149. arm_shift_q31(sum_buf, -3, sum_buf, AUDIO_BLOCK_SAMPLES);
  150. _do_comb_lpf(&lpf[1], q31_buf, aux_buf);
  151. arm_shift_q31(aux_buf, -3, aux_buf, AUDIO_BLOCK_SAMPLES);
  152. arm_add_q31(sum_buf, aux_buf, sum_buf, AUDIO_BLOCK_SAMPLES);
  153. _do_comb_lpf(&lpf[2], q31_buf, aux_buf);
  154. arm_shift_q31(aux_buf, -3, aux_buf, AUDIO_BLOCK_SAMPLES);
  155. arm_add_q31(sum_buf, aux_buf, sum_buf, AUDIO_BLOCK_SAMPLES);
  156. _do_comb_lpf(&lpf[3], q31_buf, aux_buf);
  157. arm_shift_q31(aux_buf, -3, aux_buf, AUDIO_BLOCK_SAMPLES);
  158. arm_add_q31(sum_buf, aux_buf, sum_buf, AUDIO_BLOCK_SAMPLES);
  159. _do_comb_apf(&apf[2], sum_buf, q31_buf);
  160. arm_q31_to_q15(q31_buf, block->data, AUDIO_BLOCK_SAMPLES);
  161. transmit(block, 0);
  162. release(block);
  163. }
  164. /* EOF */