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.

217 lines
6.1KB

  1. #include "plugin.hpp"
  2. using simd::float_4;
  3. template <typename T>
  4. static T clip(T x) {
  5. // return std::tanh(x);
  6. // Pade approximant of tanh
  7. x = simd::clamp(x, -3.f, 3.f);
  8. return x * (27 + x * x) / (27 + 9 * x * x);
  9. }
  10. template <typename T>
  11. struct LadderFilter {
  12. T omega0;
  13. T resonance = 1;
  14. T state[4];
  15. T input;
  16. LadderFilter() {
  17. reset();
  18. setCutoff(0);
  19. }
  20. void reset() {
  21. for (int i = 0; i < 4; i++) {
  22. state[i] = 0;
  23. }
  24. }
  25. void setCutoff(T cutoff) {
  26. omega0 = 2 * T(M_PI) * cutoff;
  27. }
  28. void process(T input, T dt) {
  29. dsp::stepRK4(T(0), dt, state, 4, [&](T t, const T x[], T dxdt[]) {
  30. T inputt = crossfade(this->input, input, t / dt);
  31. T inputc = clip(inputt - resonance * x[3]);
  32. T yc0 = clip(x[0]);
  33. T yc1 = clip(x[1]);
  34. T yc2 = clip(x[2]);
  35. T yc3 = clip(x[3]);
  36. dxdt[0] = omega0 * (inputc - yc0);
  37. dxdt[1] = omega0 * (yc0 - yc1);
  38. dxdt[2] = omega0 * (yc1 - yc2);
  39. dxdt[3] = omega0 * (yc2 - yc3);
  40. });
  41. this->input = input;
  42. }
  43. T lowpass() {
  44. return state[3];
  45. }
  46. T highpass() {
  47. // TODO This is incorrect when `resonance > 0`. Is the math wrong?
  48. return clip((input - resonance * state[3]) - 4 * state[0] + 6 * state[1] - 4 * state[2] + state[3]);
  49. }
  50. };
  51. static const int UPSAMPLE = 2;
  52. struct VCF : Module {
  53. enum ParamIds {
  54. FREQ_PARAM,
  55. FINE_PARAM,
  56. RES_PARAM,
  57. FREQ_CV_PARAM,
  58. DRIVE_PARAM,
  59. NUM_PARAMS
  60. };
  61. enum InputIds {
  62. FREQ_INPUT,
  63. RES_INPUT,
  64. DRIVE_INPUT,
  65. IN_INPUT,
  66. NUM_INPUTS
  67. };
  68. enum OutputIds {
  69. LPF_OUTPUT,
  70. HPF_OUTPUT,
  71. NUM_OUTPUTS
  72. };
  73. LadderFilter<float_4> filters[4];
  74. // Upsampler<UPSAMPLE, 8> inputUpsampler;
  75. // Decimator<UPSAMPLE, 8> lowpassDecimator;
  76. // Decimator<UPSAMPLE, 8> highpassDecimator;
  77. VCF() {
  78. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS);
  79. // Multiply and offset for backward patch compatibility
  80. configParam(FREQ_PARAM, 0.f, 1.f, 0.5f, "Frequency", " Hz", std::pow(2, 10.f), dsp::FREQ_C4 / std::pow(2, 5.f));
  81. configParam(FINE_PARAM, 0.f, 1.f, 0.5f, "Fine frequency");
  82. configParam(RES_PARAM, 0.f, 1.f, 0.f, "Resonance", "%", 0.f, 100.f);
  83. configParam(FREQ_CV_PARAM, -1.f, 1.f, 0.f, "Frequency modulation", "%", 0.f, 100.f);
  84. configParam(DRIVE_PARAM, 0.f, 1.f, 0.f, "Drive", "", 0, 11);
  85. }
  86. void onReset() override {
  87. for (int i = 0; i < 4; i++)
  88. filters[i].reset();
  89. }
  90. void process(const ProcessArgs& args) override {
  91. if (!outputs[LPF_OUTPUT].isConnected() && !outputs[HPF_OUTPUT].isConnected()) {
  92. return;
  93. }
  94. float driveParam = params[DRIVE_PARAM].getValue();
  95. float resParam = params[RES_PARAM].getValue();
  96. float fineParam = params[FINE_PARAM].getValue();
  97. fineParam = dsp::quadraticBipolar(fineParam * 2.f - 1.f) * 7.f / 12.f;
  98. float freqCvParam = params[FREQ_CV_PARAM].getValue();
  99. freqCvParam = dsp::quadraticBipolar(freqCvParam);
  100. float freqParam = params[FREQ_PARAM].getValue();
  101. freqParam = freqParam * 10.f - 5.f;
  102. int channels = std::max(1, inputs[IN_INPUT].getChannels());
  103. for (int c = 0; c < channels; c += 4) {
  104. auto* filter = &filters[c / 4];
  105. float_4 input = float_4::load(inputs[IN_INPUT].getVoltages(c)) / 5.f;
  106. // Drive gain
  107. float_4 drive = driveParam + inputs[DRIVE_INPUT].getPolyVoltageSimd<float_4>(c) / 10.f;
  108. drive = clamp(drive, 0.f, 1.f);
  109. float_4 gain = simd::pow(1.f + drive, 5);
  110. input *= gain;
  111. // Add -120dB noise to bootstrap self-oscillation
  112. input += 1e-6f * (2.f * random::uniform() - 1.f);
  113. // Set resonance
  114. float_4 resonance = resParam + inputs[RES_INPUT].getPolyVoltageSimd<float_4>(c) / 10.f;
  115. resonance = clamp(resonance, 0.f, 1.f);
  116. filter->resonance = simd::pow(resonance, 2) * 10.f;
  117. // Get pitch
  118. float_4 pitch = freqParam + fineParam + inputs[FREQ_INPUT].getPolyVoltageSimd<float_4>(c) * freqCvParam;
  119. // Set cutoff
  120. float_4 cutoff = dsp::FREQ_C4 * simd::pow(2.f, pitch);
  121. cutoff = clamp(cutoff, 1.f, 8000.f);
  122. filter->setCutoff(cutoff);
  123. // Set outputs
  124. filter->process(input, args.sampleTime);
  125. float_4 lowpass = 5.f * filter->lowpass();
  126. lowpass.store(outputs[LPF_OUTPUT].getVoltages(c));
  127. float_4 highpass = 5.f * filter->highpass();
  128. highpass.store(outputs[HPF_OUTPUT].getVoltages(c));
  129. }
  130. outputs[LPF_OUTPUT].setChannels(channels);
  131. outputs[HPF_OUTPUT].setChannels(channels);
  132. /*
  133. // Process sample
  134. float dt = args.sampleTime / UPSAMPLE;
  135. float inputBuf[UPSAMPLE];
  136. float lowpassBuf[UPSAMPLE];
  137. float highpassBuf[UPSAMPLE];
  138. inputUpsampler.process(input, inputBuf);
  139. for (int i = 0; i < UPSAMPLE; i++) {
  140. // Step the filter
  141. filter.process(inputBuf[i], dt);
  142. lowpassBuf[i] = filter.lowpass;
  143. highpassBuf[i] = filter.highpass;
  144. }
  145. // Set outputs
  146. if (outputs[LPF_OUTPUT].isConnected()) {
  147. outputs[LPF_OUTPUT].setVoltage(5.f * lowpassDecimator.process(lowpassBuf));
  148. }
  149. if (outputs[HPF_OUTPUT].isConnected()) {
  150. outputs[HPF_OUTPUT].setVoltage(5.f * highpassDecimator.process(highpassBuf));
  151. }
  152. */
  153. }
  154. };
  155. struct VCFWidget : ModuleWidget {
  156. VCFWidget(VCF* module) {
  157. setModule(module);
  158. setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/VCF.svg")));
  159. addChild(createWidget<ScrewSilver>(Vec(15, 0)));
  160. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 30, 0)));
  161. addChild(createWidget<ScrewSilver>(Vec(15, 365)));
  162. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 30, 365)));
  163. addParam(createParam<RoundHugeBlackKnob>(Vec(33, 61), module, VCF::FREQ_PARAM));
  164. addParam(createParam<RoundLargeBlackKnob>(Vec(12, 143), module, VCF::FINE_PARAM));
  165. addParam(createParam<RoundLargeBlackKnob>(Vec(71, 143), module, VCF::RES_PARAM));
  166. addParam(createParam<RoundLargeBlackKnob>(Vec(12, 208), module, VCF::FREQ_CV_PARAM));
  167. addParam(createParam<RoundLargeBlackKnob>(Vec(71, 208), module, VCF::DRIVE_PARAM));
  168. addInput(createInput<PJ301MPort>(Vec(10, 276), module, VCF::FREQ_INPUT));
  169. addInput(createInput<PJ301MPort>(Vec(48, 276), module, VCF::RES_INPUT));
  170. addInput(createInput<PJ301MPort>(Vec(85, 276), module, VCF::DRIVE_INPUT));
  171. addInput(createInput<PJ301MPort>(Vec(10, 320), module, VCF::IN_INPUT));
  172. addOutput(createOutput<PJ301MPort>(Vec(48, 320), module, VCF::LPF_OUTPUT));
  173. addOutput(createOutput<PJ301MPort>(Vec(85, 320), module, VCF::HPF_OUTPUT));
  174. }
  175. };
  176. Model* modelVCF = createModel<VCF, VCFWidget>("VCF");