Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

filter_variable.cpp 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // exp2 polynomial suggested by Stefan Stenzel on "music-dsp"
  100. // mail list, Wed, 3 Sep 2014 10:08:55 +0200
  101. int32_t x = n << 3;
  102. n = multiply_accumulate_32x32_rshift32_rounded(536870912, x, 1494202713);
  103. int32_t sq = multiply_32x32_rshift32_rounded(x, x);
  104. n = multiply_accumulate_32x32_rshift32_rounded(n, sq, 1934101615);
  105. n = n + (multiply_32x32_rshift32_rounded(sq,
  106. multiply_32x32_rshift32_rounded(x, 1358044250)) << 1);
  107. n = n << 1;
  108. #else
  109. // exp2 algorithm by Laurent de Soras
  110. // http://www.musicdsp.org/showone.php?id=106
  111. n = (n + 134217728) << 3;
  112. n = multiply_32x32_rshift32_rounded(n, n);
  113. n = multiply_32x32_rshift32_rounded(n, 715827883) << 3;
  114. n = n + 715827882;
  115. #endif
  116. n = n >> (6 - (control >> 27)); // 4 integer control bits
  117. fmult = multiply_32x32_rshift32_rounded(fcenter, n);
  118. if (fmult > 5378279) fmult = 5378279;
  119. fmult = fmult << 8;
  120. // fmult is within 0.4% accuracy for all but the top 2 octaves
  121. // of the audio band. This math improves accuracy above 5 kHz.
  122. // Without this, the filter still works fine for processing
  123. // high frequencies, but the filter's corner frequency response
  124. // can end up about 6% higher than requested.
  125. #ifdef IMPROVE_HIGH_FREQUENCY_ACCURACY
  126. // From "Fast Polynomial Approximations to Sine and Cosine"
  127. // Charles K Garrett, http://krisgarrett.net/
  128. fmult = (multiply_32x32_rshift32_rounded(fmult, 2145892402) +
  129. multiply_32x32_rshift32_rounded(
  130. multiply_32x32_rshift32_rounded(fmult, fmult),
  131. multiply_32x32_rshift32_rounded(fmult, -1383276101))) << 1;
  132. #endif
  133. // now do the state variable filter as normal, using fmult
  134. input = (*in++) << 12;
  135. lowpass = lowpass + MULT(fmult, bandpass);
  136. highpass = ((input + inputprev)>>1) - lowpass - MULT(damp, bandpass);
  137. inputprev = input;
  138. bandpass = bandpass + MULT(fmult, highpass);
  139. lowpasstmp = lowpass;
  140. bandpasstmp = bandpass;
  141. highpasstmp = highpass;
  142. lowpass = lowpass + MULT(fmult, bandpass);
  143. highpass = input - lowpass - MULT(damp, bandpass);
  144. bandpass = bandpass + MULT(fmult, highpass);
  145. lowpasstmp = signed_saturate_rshift(lowpass+lowpasstmp, 16, 13);
  146. bandpasstmp = signed_saturate_rshift(bandpass+bandpasstmp, 16, 13);
  147. highpasstmp = signed_saturate_rshift(highpass+highpasstmp, 16, 13);
  148. *lp++ = lowpasstmp;
  149. *bp++ = bandpasstmp;
  150. *hp++ = highpasstmp;
  151. } while (in < end);
  152. state_inputprev = inputprev;
  153. state_lowpass = lowpass;
  154. state_bandpass = bandpass;
  155. }
  156. void AudioFilterStateVariable::update(void)
  157. {
  158. audio_block_t *input_block=NULL, *control_block=NULL;
  159. audio_block_t *lowpass_block=NULL, *bandpass_block=NULL, *highpass_block=NULL;
  160. input_block = receiveReadOnly(0);
  161. control_block = receiveReadOnly(1);
  162. if (!input_block) {
  163. if (control_block) release(control_block);
  164. return;
  165. }
  166. lowpass_block = allocate();
  167. if (!lowpass_block) {
  168. release(input_block);
  169. if (control_block) release(control_block);
  170. return;
  171. }
  172. bandpass_block = allocate();
  173. if (!bandpass_block) {
  174. release(input_block);
  175. release(lowpass_block);
  176. if (control_block) release(control_block);
  177. return;
  178. }
  179. highpass_block = allocate();
  180. if (!highpass_block) {
  181. release(input_block);
  182. release(lowpass_block);
  183. release(bandpass_block);
  184. if (control_block) release(control_block);
  185. return;
  186. }
  187. if (control_block) {
  188. update_variable(input_block->data,
  189. control_block->data,
  190. lowpass_block->data,
  191. bandpass_block->data,
  192. highpass_block->data);
  193. release(control_block);
  194. } else {
  195. update_fixed(input_block->data,
  196. lowpass_block->data,
  197. bandpass_block->data,
  198. highpass_block->data);
  199. }
  200. release(input_block);
  201. transmit(lowpass_block, 0);
  202. release(lowpass_block);
  203. transmit(bandpass_block, 1);
  204. release(bandpass_block);
  205. transmit(highpass_block, 2);
  206. release(highpass_block);
  207. return;
  208. }