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.

155 lines
4.7KB

  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. //#define MULT(a, b) (int32_t)(((int64_t)(a) * (b)) >> 30)
  31. #define MULT(a, b) (multiply_32x32_rshift32_rounded(a, b) << 2)
  32. static bool first = true;
  33. void AudioFilterStateVariable::update_fixed(const int16_t *in,
  34. int16_t *lp, int16_t *bp, int16_t *hp)
  35. {
  36. const int16_t *end = in + AUDIO_BLOCK_SAMPLES;
  37. int32_t input, inputprev;
  38. int32_t lowpass, bandpass, highpass;
  39. int32_t lowpasstmp, bandpasstmp, highpasstmp;
  40. int32_t fmult, damp;
  41. fmult = setting_fmult;
  42. damp = setting_damp;
  43. inputprev = state_inputprev;
  44. lowpass = state_lowpass;
  45. bandpass = state_bandpass;
  46. do {
  47. input = (*in++) << 12;
  48. lowpass = lowpass + MULT(fmult, bandpass);
  49. highpass = ((input + inputprev)>>1) - lowpass - MULT(damp, bandpass);
  50. inputprev = input;
  51. bandpass = bandpass + MULT(fmult, highpass);
  52. lowpasstmp = lowpass;
  53. bandpasstmp = bandpass;
  54. highpasstmp = highpass;
  55. lowpass = lowpass + MULT(fmult, bandpass);
  56. highpass = input - lowpass - MULT(damp, bandpass);
  57. bandpass = bandpass + MULT(fmult, highpass);
  58. lowpasstmp = signed_saturate_rshift(lowpass+lowpasstmp, 16, 13);
  59. bandpasstmp = signed_saturate_rshift(bandpass+bandpasstmp, 16, 13);
  60. highpasstmp = signed_saturate_rshift(highpass+highpasstmp, 16, 13);
  61. *lp++ = lowpasstmp;
  62. *bp++ = bandpasstmp;
  63. *hp++ = highpasstmp;
  64. #if 0
  65. if (first) {
  66. Serial.print(input >> 12);
  67. Serial.print(", ");
  68. Serial.print(lowpasstmp);
  69. Serial.print(", ");
  70. Serial.print(bandpasstmp);
  71. Serial.print(", ");
  72. Serial.print(highpasstmp);
  73. Serial.println();
  74. }
  75. #endif
  76. } while (in < end);
  77. state_inputprev = inputprev;
  78. state_lowpass = lowpass;
  79. state_bandpass = bandpass;
  80. first = false;
  81. }
  82. void AudioFilterStateVariable::update_variable(const int16_t *in,
  83. const int16_t *ctl, int16_t *lp, int16_t *bp, int16_t *hp)
  84. {
  85. // TODO: implement control signal modulation of corner frequency
  86. update_fixed(in, lp, bp, hp);
  87. }
  88. void AudioFilterStateVariable::update(void)
  89. {
  90. audio_block_t *input_block=NULL, *control_block=NULL;
  91. audio_block_t *lowpass_block=NULL, *bandpass_block=NULL, *highpass_block=NULL;
  92. input_block = receiveReadOnly(0);
  93. control_block = receiveReadOnly(1);
  94. if (!input_block) {
  95. if (control_block) release(control_block);
  96. return;
  97. }
  98. lowpass_block = allocate();
  99. if (!lowpass_block) {
  100. release(input_block);
  101. if (control_block) release(control_block);
  102. return;
  103. }
  104. bandpass_block = allocate();
  105. if (!bandpass_block) {
  106. release(input_block);
  107. release(lowpass_block);
  108. if (control_block) release(control_block);
  109. return;
  110. }
  111. highpass_block = allocate();
  112. if (!highpass_block) {
  113. release(input_block);
  114. release(lowpass_block);
  115. release(bandpass_block);
  116. if (control_block) release(control_block);
  117. return;
  118. }
  119. if (control_block) {
  120. update_variable(input_block->data,
  121. control_block->data,
  122. lowpass_block->data,
  123. bandpass_block->data,
  124. highpass_block->data);
  125. release(control_block);
  126. } else {
  127. update_fixed(input_block->data,
  128. lowpass_block->data,
  129. bandpass_block->data,
  130. highpass_block->data);
  131. }
  132. release(input_block);
  133. transmit(lowpass_block, 0);
  134. release(lowpass_block);
  135. transmit(bandpass_block, 1);
  136. release(bandpass_block);
  137. transmit(highpass_block, 2);
  138. release(highpass_block);
  139. return;
  140. }