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.

144 lines
4.3KB

  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. float AudioFilterLadder::LPF(float s, int i)
  37. {
  38. float ft = s * (1.0f/1.3f) + (0.3f/1.3f) * z0[i] - z1[i];
  39. ft = ft * alpha + z1[i];
  40. z1[i] = ft;
  41. z0[i] = s;
  42. return ft;
  43. }
  44. void AudioFilterLadder::resonance(float res)
  45. {
  46. // maps resonance = 0->1 to K = 0 -> 4
  47. if (res > 1.1f) {
  48. res = 1.1f;
  49. } else if (res < 0.0f) {
  50. res = 0.0f;
  51. }
  52. K = 4.0f * res;
  53. }
  54. void AudioFilterLadder::frequency(float c)
  55. {
  56. Fbase = c;
  57. compute_coeffs(c);
  58. }
  59. void AudioFilterLadder::compute_coeffs(float c)
  60. {
  61. if (c > 0.49f * AUDIO_SAMPLE_RATE_EXACT) {
  62. c = 0.49f * AUDIO_SAMPLE_RATE_EXACT;
  63. } else if (c < 1.0f) {
  64. c = 1.0f;
  65. }
  66. float wc = c * (float)(2.0f * MOOG_PI / AUDIO_SAMPLE_RATE_EXACT);
  67. float wc2 = wc * wc;
  68. alpha = 0.9892f * wc - 0.4324f * wc2 + 0.1381f * wc * wc2 - 0.0202f * wc2 * wc2;
  69. }
  70. static inline float fast_tanh(float x)
  71. {
  72. float x2 = x * x;
  73. return x * (27.0f + x2) / (27.0f + 9.0f * x2);
  74. }
  75. void AudioFilterLadder::update(void)
  76. {
  77. audio_block_t *blocka, *blockb, *blockc;
  78. float Ktot;
  79. bool FCmodActive = true;
  80. bool QmodActive = true;
  81. blocka = receiveWritable(0);
  82. blockb = receiveReadOnly(1);
  83. blockc = receiveReadOnly(2);
  84. if (!blocka) {
  85. blocka = allocate();
  86. if (!blocka) {
  87. if (blockb) release(blockb);
  88. if (blockc) release(blockc);
  89. return;
  90. }
  91. // When no data arrives, we must treat it as if zeros had
  92. // arrived. Because of resonance, we need to keep computing
  93. // output. Perhaps we could examine the filter state here
  94. // and just return without any work when it's below some
  95. // threshold we know produces no more sound/resonance?
  96. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  97. blocka->data[i] = 0;
  98. }
  99. }
  100. if (!blockb) {
  101. FCmodActive = false;
  102. }
  103. if (!blockc) {
  104. QmodActive = false;
  105. Ktot = K;
  106. }
  107. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  108. float input = blocka->data[i] * (1.0f/32768.0f);
  109. if (FCmodActive) {
  110. float FCmod = blockb->data[i] * (1.0f/32768.0f);
  111. // TODO: should this be "volts per octave"?
  112. float ftot = Fbase + Fbase * FCmod;
  113. if (FCmod != 0) compute_coeffs(ftot);
  114. }
  115. if (QmodActive) {
  116. float Qmod = blockc->data[i] * (1.0f/32768.0f);
  117. Ktot = K + 4.4f * Qmod;
  118. if (Ktot < 0.0f) Ktot = 0.0f;
  119. }
  120. float u = input - (z1[3] - 0.5f * input) * Ktot;
  121. u = fast_tanh(u);
  122. float stage1 = LPF(u, 0);
  123. float stage2 = LPF(stage1, 1);
  124. float stage3 = LPF(stage2, 2);
  125. float stage4 = LPF(stage3, 3);
  126. blocka->data[i] = stage4 * 32767.0f;
  127. }
  128. transmit(blocka);
  129. release(blocka);
  130. if (blockb) release(blockb);
  131. if (blockc) release(blockc);
  132. }