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.

146 lines
4.4KB

  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. static inline float fast_tanh(float x)
  72. {
  73. float x2 = x * x;
  74. return x * (27.0f + x2) / (27.0f + 9.0f * x2);
  75. }
  76. void AudioFilterLadder::update(void)
  77. {
  78. audio_block_t *blocka, *blockb, *blockc;
  79. float Ktot;
  80. bool FCmodActive = true;
  81. bool QmodActive = true;
  82. blocka = receiveWritable(0);
  83. blockb = receiveReadOnly(1);
  84. blockc = receiveReadOnly(2);
  85. if (!blocka) {
  86. blocka = allocate();
  87. if (!blocka) {
  88. if (blockb) release(blockb);
  89. if (blockc) release(blockc);
  90. return;
  91. }
  92. // When no data arrives, we must treat it as if zeros had
  93. // arrived. Because of resonance, we need to keep computing
  94. // output. Perhaps we could examine the filter state here
  95. // and just return without any work when it's below some
  96. // threshold we know produces no more sound/resonance?
  97. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  98. blocka->data[i] = 0;
  99. }
  100. }
  101. if (!blockb) {
  102. FCmodActive = false;
  103. }
  104. if (!blockc) {
  105. QmodActive = false;
  106. Ktot = K;
  107. }
  108. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  109. float input = blocka->data[i] * (1.0f/32768.0f);
  110. if (FCmodActive) {
  111. float FCmod = blockb->data[i] * (1.0f/32768.0f);
  112. // TODO: should this be "volts per octave"?
  113. float ftot = Fbase + Fbase * FCmod;
  114. if (FCmod != 0) compute_coeffs(ftot);
  115. }
  116. if (QmodActive) {
  117. float Qmod = blockc->data[i] * (1.0f/32768.0f);
  118. Ktot = K + (MAX_RESONANCE * 1.1f) * Qmod;
  119. if (Ktot < 0.0f) Ktot = 0.0f;
  120. }
  121. float u = input - (z1[3] - 0.5f * input) * Ktot;
  122. u = fast_tanh(u);
  123. float stage1 = LPF(u, 0);
  124. float stage2 = LPF(stage1, 1);
  125. float stage3 = LPF(stage2, 2);
  126. float stage4 = LPF(stage3, 3);
  127. blocka->data[i] = stage4 * 32767.0f;
  128. }
  129. transmit(blocka);
  130. release(blocka);
  131. if (blockb) release(blockb);
  132. if (blockc) release(blockc);
  133. }