Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

240 Zeilen
8.1KB

  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. #if defined(KINETISK)
  43. void AudioFilterStateVariable::update_fixed(const int16_t *in,
  44. int16_t *lp, int16_t *bp, int16_t *hp)
  45. {
  46. const int16_t *end = in + AUDIO_BLOCK_SAMPLES;
  47. int32_t input, inputprev;
  48. int32_t lowpass, bandpass, highpass;
  49. int32_t lowpasstmp, bandpasstmp, highpasstmp;
  50. int32_t fmult, damp;
  51. fmult = setting_fmult;
  52. damp = setting_damp;
  53. inputprev = state_inputprev;
  54. lowpass = state_lowpass;
  55. bandpass = state_bandpass;
  56. do {
  57. input = (*in++) << 12;
  58. lowpass = lowpass + MULT(fmult, bandpass);
  59. highpass = ((input + inputprev)>>1) - lowpass - MULT(damp, bandpass);
  60. inputprev = input;
  61. bandpass = bandpass + MULT(fmult, highpass);
  62. lowpasstmp = lowpass;
  63. bandpasstmp = bandpass;
  64. highpasstmp = highpass;
  65. lowpass = lowpass + MULT(fmult, bandpass);
  66. highpass = input - lowpass - MULT(damp, bandpass);
  67. bandpass = bandpass + MULT(fmult, highpass);
  68. lowpasstmp = signed_saturate_rshift(lowpass+lowpasstmp, 16, 13);
  69. bandpasstmp = signed_saturate_rshift(bandpass+bandpasstmp, 16, 13);
  70. highpasstmp = signed_saturate_rshift(highpass+highpasstmp, 16, 13);
  71. *lp++ = lowpasstmp;
  72. *bp++ = bandpasstmp;
  73. *hp++ = highpasstmp;
  74. } while (in < end);
  75. state_inputprev = inputprev;
  76. state_lowpass = lowpass;
  77. state_bandpass = bandpass;
  78. }
  79. void AudioFilterStateVariable::update_variable(const int16_t *in,
  80. const int16_t *ctl, int16_t *lp, int16_t *bp, int16_t *hp)
  81. {
  82. const int16_t *end = in + AUDIO_BLOCK_SAMPLES;
  83. int32_t input, inputprev, control;
  84. int32_t lowpass, bandpass, highpass;
  85. int32_t lowpasstmp, bandpasstmp, highpasstmp;
  86. int32_t fcenter, fmult, damp, octavemult;
  87. int32_t n;
  88. fcenter = setting_fcenter;
  89. octavemult = setting_octavemult;
  90. damp = setting_damp;
  91. inputprev = state_inputprev;
  92. lowpass = state_lowpass;
  93. bandpass = state_bandpass;
  94. do {
  95. // compute fmult using control input, fcenter and octavemult
  96. control = *ctl++; // signal is always 15 fractional bits
  97. control *= octavemult; // octavemult range: 0 to 28671 (12 frac bits)
  98. n = control & 0x7FFFFFF; // 27 fractional control bits
  99. #ifdef IMPROVE_EXPONENTIAL_ACCURACY
  100. // exp2 polynomial suggested by Stefan Stenzel on "music-dsp"
  101. // mail list, Wed, 3 Sep 2014 10:08:55 +0200
  102. int32_t x = n << 3;
  103. n = multiply_accumulate_32x32_rshift32_rounded(536870912, x, 1494202713);
  104. int32_t sq = multiply_32x32_rshift32_rounded(x, x);
  105. n = multiply_accumulate_32x32_rshift32_rounded(n, sq, 1934101615);
  106. n = n + (multiply_32x32_rshift32_rounded(sq,
  107. multiply_32x32_rshift32_rounded(x, 1358044250)) << 1);
  108. n = n << 1;
  109. #else
  110. // exp2 algorithm by Laurent de Soras
  111. // http://www.musicdsp.org/showone.php?id=106
  112. n = (n + 134217728) << 3;
  113. n = multiply_32x32_rshift32_rounded(n, n);
  114. n = multiply_32x32_rshift32_rounded(n, 715827883) << 3;
  115. n = n + 715827882;
  116. #endif
  117. n = n >> (6 - (control >> 27)); // 4 integer control bits
  118. fmult = multiply_32x32_rshift32_rounded(fcenter, n);
  119. if (fmult > 5378279) fmult = 5378279;
  120. fmult = fmult << 8;
  121. // fmult is within 0.4% accuracy for all but the top 2 octaves
  122. // of the audio band. This math improves accuracy above 5 kHz.
  123. // Without this, the filter still works fine for processing
  124. // high frequencies, but the filter's corner frequency response
  125. // can end up about 6% higher than requested.
  126. #ifdef IMPROVE_HIGH_FREQUENCY_ACCURACY
  127. // From "Fast Polynomial Approximations to Sine and Cosine"
  128. // Charles K Garrett, http://krisgarrett.net/
  129. fmult = (multiply_32x32_rshift32_rounded(fmult, 2145892402) +
  130. multiply_32x32_rshift32_rounded(
  131. multiply_32x32_rshift32_rounded(fmult, fmult),
  132. multiply_32x32_rshift32_rounded(fmult, -1383276101))) << 1;
  133. #endif
  134. // now do the state variable filter as normal, using fmult
  135. input = (*in++) << 12;
  136. lowpass = lowpass + MULT(fmult, bandpass);
  137. highpass = ((input + inputprev)>>1) - lowpass - MULT(damp, bandpass);
  138. inputprev = input;
  139. bandpass = bandpass + MULT(fmult, highpass);
  140. lowpasstmp = lowpass;
  141. bandpasstmp = bandpass;
  142. highpasstmp = highpass;
  143. lowpass = lowpass + MULT(fmult, bandpass);
  144. highpass = input - lowpass - MULT(damp, bandpass);
  145. bandpass = bandpass + MULT(fmult, highpass);
  146. lowpasstmp = signed_saturate_rshift(lowpass+lowpasstmp, 16, 13);
  147. bandpasstmp = signed_saturate_rshift(bandpass+bandpasstmp, 16, 13);
  148. highpasstmp = signed_saturate_rshift(highpass+highpasstmp, 16, 13);
  149. *lp++ = lowpasstmp;
  150. *bp++ = bandpasstmp;
  151. *hp++ = highpasstmp;
  152. } while (in < end);
  153. state_inputprev = inputprev;
  154. state_lowpass = lowpass;
  155. state_bandpass = bandpass;
  156. }
  157. void AudioFilterStateVariable::update(void)
  158. {
  159. audio_block_t *input_block=NULL, *control_block=NULL;
  160. audio_block_t *lowpass_block=NULL, *bandpass_block=NULL, *highpass_block=NULL;
  161. input_block = receiveReadOnly(0);
  162. control_block = receiveReadOnly(1);
  163. if (!input_block) {
  164. if (control_block) release(control_block);
  165. return;
  166. }
  167. lowpass_block = allocate();
  168. if (!lowpass_block) {
  169. release(input_block);
  170. if (control_block) release(control_block);
  171. return;
  172. }
  173. bandpass_block = allocate();
  174. if (!bandpass_block) {
  175. release(input_block);
  176. release(lowpass_block);
  177. if (control_block) release(control_block);
  178. return;
  179. }
  180. highpass_block = allocate();
  181. if (!highpass_block) {
  182. release(input_block);
  183. release(lowpass_block);
  184. release(bandpass_block);
  185. if (control_block) release(control_block);
  186. return;
  187. }
  188. if (control_block) {
  189. update_variable(input_block->data,
  190. control_block->data,
  191. lowpass_block->data,
  192. bandpass_block->data,
  193. highpass_block->data);
  194. release(control_block);
  195. } else {
  196. update_fixed(input_block->data,
  197. lowpass_block->data,
  198. bandpass_block->data,
  199. highpass_block->data);
  200. }
  201. release(input_block);
  202. transmit(lowpass_block, 0);
  203. release(lowpass_block);
  204. transmit(bandpass_block, 1);
  205. release(bandpass_block);
  206. transmit(highpass_block, 2);
  207. release(highpass_block);
  208. return;
  209. }
  210. #elif defined(KINETISL)
  211. void AudioFilterStateVariable::update(void)
  212. {
  213. audio_block_t *block;
  214. block = receiveReadOnly(0);
  215. if (block) release(block);
  216. block = receiveReadOnly(1);
  217. if (block) release(block);
  218. }
  219. #endif