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.

241 rinda
8.2KB

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