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.

filter_ladder.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Audio Library for Teensy, Ladder Filter
  2. * Copyright (c) 2021, Richard van Hoesel
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice, development funding notice, and this permission
  12. * notice shall be included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. //-----------------------------------------------------------
  23. // Huovilainen New Moog (HNM) model as per CMJ jun 2006
  24. // Implemented as Teensy Audio Library compatible object
  25. // Richard van Hoesel, Feb. 9 2021
  26. // v.1.02 now includes both cutoff and resonance "CV" modulation inputs
  27. // please retain this header if you use this code.
  28. //-----------------------------------------------------------
  29. // https://forum.pjrc.com/threads/60488?p=269755&viewfull=1#post269755
  30. // https://forum.pjrc.com/threads/60488?p=269609&viewfull=1#post269609
  31. #include <Arduino.h>
  32. #include "filter_ladder.h"
  33. #include <math.h>
  34. #include <stdint.h>
  35. #define MOOG_PI ((float)3.14159265358979323846264338327950288)
  36. #define MAX_RESONANCE ((float)1.1)
  37. float AudioFilterLadder::LPF(float s, int i)
  38. {
  39. float ft = s * (1.0f/1.3f) + (0.3f/1.3f) * z0[i] - z1[i];
  40. ft = ft * alpha + z1[i];
  41. z1[i] = ft;
  42. z0[i] = s;
  43. return ft;
  44. }
  45. void AudioFilterLadder::resonance(float res)
  46. {
  47. // maps resonance = 0->1 to K = 0 -> 4
  48. if (res > MAX_RESONANCE) {
  49. res = MAX_RESONANCE;
  50. } else if (res < 0.0f) {
  51. res = 0.0f;
  52. }
  53. K = 4.0f * res;
  54. }
  55. void AudioFilterLadder::frequency(float c)
  56. {
  57. Fbase = c;
  58. compute_coeffs(c);
  59. }
  60. void AudioFilterLadder::compute_coeffs(float c)
  61. {
  62. if (c > 0.49f * AUDIO_SAMPLE_RATE_EXACT) {
  63. c = 0.49f * AUDIO_SAMPLE_RATE_EXACT;
  64. } else if (c < 1.0f) {
  65. c = 1.0f;
  66. }
  67. float wc = c * (float)(2.0f * MOOG_PI / AUDIO_SAMPLE_RATE_EXACT);
  68. float wc2 = wc * wc;
  69. alpha = 0.9892f * wc - 0.4324f * wc2 + 0.1381f * wc * wc2 - 0.0202f * wc2 * wc2;
  70. }
  71. bool AudioFilterLadder::resonating()
  72. {
  73. for (int i=0; i < 4; i++) {
  74. if (fabsf(z0[i]) > 0.0001f) return true;
  75. if (fabsf(z1[i]) > 0.0001f) return true;
  76. }
  77. return false;
  78. }
  79. static inline float fast_tanh(float x)
  80. {
  81. float x2 = x * x;
  82. return x * (27.0f + x2) / (27.0f + 9.0f * x2);
  83. }
  84. void AudioFilterLadder::update(void)
  85. {
  86. audio_block_t *blocka, *blockb, *blockc;
  87. float Ktot;
  88. bool FCmodActive = true;
  89. bool QmodActive = true;
  90. blocka = receiveWritable(0);
  91. blockb = receiveReadOnly(1);
  92. blockc = receiveReadOnly(2);
  93. if (!blocka) {
  94. if (resonating()) {
  95. // When no data arrives but the filter is still
  96. // resonating, we must continue computing the filter
  97. // with zero input to sustain the resonance
  98. blocka = allocate();
  99. }
  100. if (!blocka) {
  101. if (blockb) release(blockb);
  102. if (blockc) release(blockc);
  103. return;
  104. }
  105. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  106. blocka->data[i] = 0;
  107. }
  108. }
  109. if (!blockb) {
  110. FCmodActive = false;
  111. }
  112. if (!blockc) {
  113. QmodActive = false;
  114. Ktot = K;
  115. }
  116. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  117. float input = blocka->data[i] * (1.0f/32768.0f);
  118. if (FCmodActive) {
  119. float FCmod = blockb->data[i] * (1.0f/32768.0f);
  120. // TODO: should this be "volts per octave"?
  121. float ftot = Fbase + Fbase * FCmod;
  122. if (FCmod != 0) compute_coeffs(ftot);
  123. }
  124. if (QmodActive) {
  125. float Qmod = blockc->data[i] * (1.0f/32768.0f);
  126. Ktot = K + (MAX_RESONANCE * 1.1f) * Qmod;
  127. if (Ktot < 0.0f) Ktot = 0.0f;
  128. }
  129. float u = input - (z1[3] - 0.5f * input) * Ktot;
  130. u = fast_tanh(u);
  131. float stage1 = LPF(u, 0);
  132. float stage2 = LPF(stage1, 1);
  133. float stage3 = LPF(stage2, 2);
  134. float stage4 = LPF(stage3, 3);
  135. blocka->data[i] = stage4 * 32767.0f;
  136. }
  137. transmit(blocka);
  138. release(blocka);
  139. if (blockb) release(blockb);
  140. if (blockc) release(blockc);
  141. }