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.

176 lines
6.0KB

  1. /*
  2. The filter DSP code has been derived from
  3. Miller Puckette's code hosted at
  4. https://github.com/ddiakopoulos/MoogLadders/blob/master/src/RKSimulationModel.h
  5. which is licensed for use under the following terms (MIT license):
  6. Copyright (c) 2015, Miller Puckette. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this
  10. list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice,
  12. this list of conditions and the following disclaimer in the documentation
  13. and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "Fundamental.hpp"
  26. // The clipping function of a transistor pair is approximately tanh(x)
  27. // TODO: Put this in a lookup table. 5th order approx doesn't seem to cut it
  28. inline float clip(float x) {
  29. return tanhf(x);
  30. }
  31. struct LadderFilter {
  32. float cutoff = 1000.0f;
  33. float resonance = 1.0f;
  34. float state[4] = {};
  35. void calculateDerivatives(float input, float *dstate, const float *state) {
  36. float cutoff2Pi = 2*M_PI * cutoff;
  37. float satstate0 = clip(state[0]);
  38. float satstate1 = clip(state[1]);
  39. float satstate2 = clip(state[2]);
  40. dstate[0] = cutoff2Pi * (clip(input - resonance * state[3]) - satstate0);
  41. dstate[1] = cutoff2Pi * (satstate0 - satstate1);
  42. dstate[2] = cutoff2Pi * (satstate1 - satstate2);
  43. dstate[3] = cutoff2Pi * (satstate2 - clip(state[3]));
  44. }
  45. void process(float input, float dt) {
  46. float deriv1[4], deriv2[4], deriv3[4], deriv4[4], tempState[4];
  47. calculateDerivatives(input, deriv1, state);
  48. for (int i = 0; i < 4; i++)
  49. tempState[i] = state[i] + 0.5f * dt * deriv1[i];
  50. calculateDerivatives(input, deriv2, tempState);
  51. for (int i = 0; i < 4; i++)
  52. tempState[i] = state[i] + 0.5f * dt * deriv2[i];
  53. calculateDerivatives(input, deriv3, tempState);
  54. for (int i = 0; i < 4; i++)
  55. tempState[i] = state[i] + dt * deriv3[i];
  56. calculateDerivatives(input, deriv4, tempState);
  57. for (int i = 0; i < 4; i++)
  58. state[i] += (1.0f / 6.0f) * dt * (deriv1[i] + 2.0f * deriv2[i] + 2.0f * deriv3[i] + deriv4[i]);
  59. }
  60. void reset() {
  61. for (int i = 0; i < 4; i++) {
  62. state[i] = 0.0f;
  63. }
  64. }
  65. };
  66. struct VCF : Module {
  67. enum ParamIds {
  68. FREQ_PARAM,
  69. FINE_PARAM,
  70. RES_PARAM,
  71. FREQ_CV_PARAM,
  72. DRIVE_PARAM,
  73. NUM_PARAMS
  74. };
  75. enum InputIds {
  76. FREQ_INPUT,
  77. RES_INPUT,
  78. DRIVE_INPUT,
  79. IN_INPUT,
  80. NUM_INPUTS
  81. };
  82. enum OutputIds {
  83. LPF_OUTPUT,
  84. HPF_OUTPUT,
  85. NUM_OUTPUTS
  86. };
  87. LadderFilter filter;
  88. VCF() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {}
  89. void step() override;
  90. void onReset() override {
  91. filter.reset();
  92. }
  93. };
  94. void VCF::step() {
  95. float input = inputs[IN_INPUT].value / 5.0f;
  96. float drive = params[DRIVE_PARAM].value + inputs[DRIVE_INPUT].value / 10.0f;
  97. float gain = powf(100.0f, drive);
  98. input *= gain;
  99. // Add -60dB noise to bootstrap self-oscillation
  100. input += 1e-6f * (2.0f*randomUniform() - 1.0f);
  101. // Set resonance
  102. float res = params[RES_PARAM].value + inputs[RES_INPUT].value / 5.0f;
  103. res = 5.5f * clamp(res, 0.0f, 1.0f);
  104. filter.resonance = res;
  105. // Set cutoff frequency
  106. float cutoffExp = params[FREQ_PARAM].value + params[FREQ_CV_PARAM].value * inputs[FREQ_INPUT].value / 5.0f;
  107. cutoffExp = clamp(cutoffExp, 0.0f, 1.0f);
  108. const float minCutoff = 15.0f;
  109. const float maxCutoff = 8400.0f;
  110. filter.cutoff = minCutoff * powf(maxCutoff / minCutoff, cutoffExp);
  111. // Push a sample to the state filter
  112. filter.process(input, 1.0f/engineGetSampleRate());
  113. // Set outputs
  114. outputs[LPF_OUTPUT].value = 5.0f * filter.state[3];
  115. outputs[HPF_OUTPUT].value = 5.0f * (input - filter.state[3]);
  116. }
  117. struct VCFWidget : ModuleWidget {
  118. VCFWidget(VCF *module);
  119. };
  120. VCFWidget::VCFWidget(VCF *module) : ModuleWidget(module) {
  121. setPanel(SVG::load(assetPlugin(plugin, "res/VCF.svg")));
  122. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  123. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 0)));
  124. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  125. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 365)));
  126. addParam(ParamWidget::create<RoundHugeBlackKnob>(Vec(33, 61), module, VCF::FREQ_PARAM, 0.0f, 1.0f, 0.5f));
  127. addParam(ParamWidget::create<RoundLargeBlackKnob>(Vec(12, 143), module, VCF::FINE_PARAM, 0.0f, 1.0f, 0.5f));
  128. addParam(ParamWidget::create<RoundLargeBlackKnob>(Vec(71, 143), module, VCF::RES_PARAM, 0.0f, 1.0f, 0.0f));
  129. addParam(ParamWidget::create<RoundLargeBlackKnob>(Vec(12, 208), module, VCF::FREQ_CV_PARAM, -1.0f, 1.0f, 0.0f));
  130. addParam(ParamWidget::create<RoundLargeBlackKnob>(Vec(71, 208), module, VCF::DRIVE_PARAM, 0.0f, 1.0f, 0.0f));
  131. addInput(Port::create<PJ301MPort>(Vec(10, 276), Port::INPUT, module, VCF::FREQ_INPUT));
  132. addInput(Port::create<PJ301MPort>(Vec(48, 276), Port::INPUT, module, VCF::RES_INPUT));
  133. addInput(Port::create<PJ301MPort>(Vec(85, 276), Port::INPUT, module, VCF::DRIVE_INPUT));
  134. addInput(Port::create<PJ301MPort>(Vec(10, 320), Port::INPUT, module, VCF::IN_INPUT));
  135. addOutput(Port::create<PJ301MPort>(Vec(48, 320), Port::OUTPUT, module, VCF::LPF_OUTPUT));
  136. addOutput(Port::create<PJ301MPort>(Vec(85, 320), Port::OUTPUT, module, VCF::HPF_OUTPUT));
  137. }
  138. Model *modelVCF = Model::create<VCF, VCFWidget>("Fundamental", "VCF", "VCF", FILTER_TAG);