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.

218 lines
7.6KB

  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 "filter_variable.h"
  27. #include "utility/dspinst.h"
  28. // State Variable Filter (Chamberlin) with 2X oversampling
  29. // http://www.musicdsp.org/showArchiveComment.php?ArchiveID=92
  30. // The fast 32x32 with rshift32 discards 2 bits, which probably
  31. // never matter.
  32. //#define MULT(a, b) (int32_t)(((int64_t)(a) * (b)) >> 30)
  33. #define MULT(a, b) (multiply_32x32_rshift32_rounded(a, b) << 2)
  34. // It's very unlikely anyone could hear any difference, but if you
  35. // care deeply about numerical precision in seldom-used cases,
  36. // uncomment this to improve the control signal accuracy
  37. //#define IMPROVE_HIGH_FREQUENCY_ACCURACY
  38. // This increases the exponential approximation accuracy from
  39. // about 0.341% error to only 0.012% error, which probably makes
  40. // no audible difference.
  41. //#define IMPROVE_EXPONENTIAL_ACCURACY
  42. void AudioFilterStateVariable::update_fixed(const int16_t *in,
  43. int16_t *lp, int16_t *bp, int16_t *hp)
  44. {
  45. const int16_t *end = in + AUDIO_BLOCK_SAMPLES;
  46. int32_t input, inputprev;
  47. int32_t lowpass, bandpass, highpass;
  48. int32_t lowpasstmp, bandpasstmp, highpasstmp;
  49. int32_t fmult, damp;
  50. fmult = setting_fmult;
  51. damp = setting_damp;
  52. inputprev = state_inputprev;
  53. lowpass = state_lowpass;
  54. bandpass = state_bandpass;
  55. do {
  56. input = (*in++) << 12;
  57. lowpass = lowpass + MULT(fmult, bandpass);
  58. highpass = ((input + inputprev)>>1) - lowpass - MULT(damp, bandpass);
  59. inputprev = input;
  60. bandpass = bandpass + MULT(fmult, highpass);
  61. lowpasstmp = lowpass;
  62. bandpasstmp = bandpass;
  63. highpasstmp = highpass;
  64. lowpass = lowpass + MULT(fmult, bandpass);
  65. highpass = input - lowpass - MULT(damp, bandpass);
  66. bandpass = bandpass + MULT(fmult, highpass);
  67. lowpasstmp = signed_saturate_rshift(lowpass+lowpasstmp, 16, 13);
  68. bandpasstmp = signed_saturate_rshift(bandpass+bandpasstmp, 16, 13);
  69. highpasstmp = signed_saturate_rshift(highpass+highpasstmp, 16, 13);
  70. *lp++ = lowpasstmp;
  71. *bp++ = bandpasstmp;
  72. *hp++ = highpasstmp;
  73. } while (in < end);
  74. state_inputprev = inputprev;
  75. state_lowpass = lowpass;
  76. state_bandpass = bandpass;
  77. }
  78. void AudioFilterStateVariable::update_variable(const int16_t *in,
  79. const int16_t *ctl, int16_t *lp, int16_t *bp, int16_t *hp)
  80. {
  81. const int16_t *end = in + AUDIO_BLOCK_SAMPLES;
  82. int32_t input, inputprev, control;
  83. int32_t lowpass, bandpass, highpass;
  84. int32_t lowpasstmp, bandpasstmp, highpasstmp;
  85. int32_t fcenter, fmult, damp, octavemult;
  86. int32_t n;
  87. fcenter = setting_fcenter;
  88. octavemult = setting_octavemult;
  89. damp = setting_damp;
  90. inputprev = state_inputprev;
  91. lowpass = state_lowpass;
  92. bandpass = state_bandpass;
  93. do {
  94. // compute fmult using control input, fcenter and octavemult
  95. control = *ctl++; // signal is always 15 fractional bits
  96. control *= octavemult; // octavemult range: 0 to 28671 (12 frac bits)
  97. n = control & 0x7FFFFFF; // 27 fractional control bits
  98. #ifdef IMPROVE_EXPONENTIAL_ACCURACY
  99. int32_t x = n << 3;
  100. n = multiply_accumulate_32x32_rshift32_rounded(536870912, x, 1494202713);
  101. int32_t sq = multiply_32x32_rshift32_rounded(x, x);
  102. n = multiply_accumulate_32x32_rshift32_rounded(n, sq, 1934101615);
  103. n = n + (multiply_32x32_rshift32_rounded(sq,
  104. multiply_32x32_rshift32_rounded(x, 1358044250)) << 1);
  105. n = n << 1;
  106. #else
  107. n = (n + 134217728) << 3;
  108. n = multiply_32x32_rshift32_rounded(n, n);
  109. n = multiply_32x32_rshift32_rounded(n, 715827883) << 3;
  110. n = n + 715827882;
  111. #endif
  112. n = n >> (6 - (control >> 27)); // 4 integer control bits
  113. fmult = multiply_32x32_rshift32_rounded(fcenter, n);
  114. if (fmult > 5378279) fmult = 5378279;
  115. fmult = fmult << 8;
  116. // fmult is within 0.4% accuracy for all but the top 2 octaves
  117. // of the audio band. This math improves accuracy above 5 kHz.
  118. // Without this, the filter still works fine for processing
  119. // high frequencies, but the filter's corner frequency response
  120. // can end up about 6% higher than requested.
  121. #ifdef IMPROVE_HIGH_FREQUENCY_ACCURACY
  122. fmult = (multiply_32x32_rshift32_rounded(fmult, 2145892402) +
  123. multiply_32x32_rshift32_rounded(
  124. multiply_32x32_rshift32_rounded(fmult, fmult),
  125. multiply_32x32_rshift32_rounded(fmult, -1383276101))) << 1;
  126. #endif
  127. // now do the state variable filter as normal, using fmult
  128. input = (*in++) << 12;
  129. lowpass = lowpass + MULT(fmult, bandpass);
  130. highpass = ((input + inputprev)>>1) - lowpass - MULT(damp, bandpass);
  131. inputprev = input;
  132. bandpass = bandpass + MULT(fmult, highpass);
  133. lowpasstmp = lowpass;
  134. bandpasstmp = bandpass;
  135. highpasstmp = highpass;
  136. lowpass = lowpass + MULT(fmult, bandpass);
  137. highpass = input - lowpass - MULT(damp, bandpass);
  138. bandpass = bandpass + MULT(fmult, highpass);
  139. lowpasstmp = signed_saturate_rshift(lowpass+lowpasstmp, 16, 13);
  140. bandpasstmp = signed_saturate_rshift(bandpass+bandpasstmp, 16, 13);
  141. highpasstmp = signed_saturate_rshift(highpass+highpasstmp, 16, 13);
  142. *lp++ = lowpasstmp;
  143. *bp++ = bandpasstmp;
  144. *hp++ = highpasstmp;
  145. } while (in < end);
  146. state_inputprev = inputprev;
  147. state_lowpass = lowpass;
  148. state_bandpass = bandpass;
  149. }
  150. void AudioFilterStateVariable::update(void)
  151. {
  152. audio_block_t *input_block=NULL, *control_block=NULL;
  153. audio_block_t *lowpass_block=NULL, *bandpass_block=NULL, *highpass_block=NULL;
  154. input_block = receiveReadOnly(0);
  155. control_block = receiveReadOnly(1);
  156. if (!input_block) {
  157. if (control_block) release(control_block);
  158. return;
  159. }
  160. lowpass_block = allocate();
  161. if (!lowpass_block) {
  162. release(input_block);
  163. if (control_block) release(control_block);
  164. return;
  165. }
  166. bandpass_block = allocate();
  167. if (!bandpass_block) {
  168. release(input_block);
  169. release(lowpass_block);
  170. if (control_block) release(control_block);
  171. return;
  172. }
  173. highpass_block = allocate();
  174. if (!highpass_block) {
  175. release(input_block);
  176. release(lowpass_block);
  177. release(bandpass_block);
  178. if (control_block) release(control_block);
  179. return;
  180. }
  181. if (control_block) {
  182. update_variable(input_block->data,
  183. control_block->data,
  184. lowpass_block->data,
  185. bandpass_block->data,
  186. highpass_block->data);
  187. release(control_block);
  188. } else {
  189. update_fixed(input_block->data,
  190. lowpass_block->data,
  191. bandpass_block->data,
  192. highpass_block->data);
  193. }
  194. release(input_block);
  195. transmit(lowpass_block, 0);
  196. release(lowpass_block);
  197. transmit(bandpass_block, 1);
  198. release(bandpass_block);
  199. transmit(highpass_block, 2);
  200. release(highpass_block);
  201. return;
  202. }