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.

225 lines
6.4KB

  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. configInput(FREQ_INPUT, "Frequency");
  86. configInput(RES_INPUT, "Resonance");
  87. configInput(DRIVE_INPUT, "Drive");
  88. configInput(IN_INPUT, "Audio");
  89. configOutput(LPF_OUTPUT, "Lowpass filter");
  90. configOutput(HPF_OUTPUT, "Highpass filter");
  91. configBypass(IN_INPUT, LPF_OUTPUT);
  92. configBypass(IN_INPUT, HPF_OUTPUT);
  93. }
  94. void onReset() override {
  95. for (int i = 0; i < 4; i++)
  96. filters[i].reset();
  97. }
  98. void process(const ProcessArgs& args) override {
  99. if (!outputs[LPF_OUTPUT].isConnected() && !outputs[HPF_OUTPUT].isConnected()) {
  100. return;
  101. }
  102. float driveParam = params[DRIVE_PARAM].getValue();
  103. float resParam = params[RES_PARAM].getValue();
  104. float fineParam = params[FINE_PARAM].getValue();
  105. fineParam = dsp::quadraticBipolar(fineParam * 2.f - 1.f) * 7.f / 12.f;
  106. float freqCvParam = params[FREQ_CV_PARAM].getValue();
  107. freqCvParam = dsp::quadraticBipolar(freqCvParam);
  108. float freqParam = params[FREQ_PARAM].getValue();
  109. freqParam = freqParam * 10.f - 5.f;
  110. int channels = std::max(1, inputs[IN_INPUT].getChannels());
  111. for (int c = 0; c < channels; c += 4) {
  112. auto* filter = &filters[c / 4];
  113. float_4 input = float_4::load(inputs[IN_INPUT].getVoltages(c)) / 5.f;
  114. // Drive gain
  115. float_4 drive = driveParam + inputs[DRIVE_INPUT].getPolyVoltageSimd<float_4>(c) / 10.f;
  116. drive = clamp(drive, 0.f, 1.f);
  117. float_4 gain = simd::pow(1.f + drive, 5);
  118. input *= gain;
  119. // Add -120dB noise to bootstrap self-oscillation
  120. input += 1e-6f * (2.f * random::uniform() - 1.f);
  121. // Set resonance
  122. float_4 resonance = resParam + inputs[RES_INPUT].getPolyVoltageSimd<float_4>(c) / 10.f;
  123. resonance = clamp(resonance, 0.f, 1.f);
  124. filter->resonance = simd::pow(resonance, 2) * 10.f;
  125. // Get pitch
  126. float_4 pitch = freqParam + fineParam + inputs[FREQ_INPUT].getPolyVoltageSimd<float_4>(c) * freqCvParam;
  127. // Set cutoff
  128. float_4 cutoff = dsp::FREQ_C4 * simd::pow(2.f, pitch);
  129. cutoff = clamp(cutoff, 1.f, 8000.f);
  130. filter->setCutoff(cutoff);
  131. // Set outputs
  132. filter->process(input, args.sampleTime);
  133. float_4 lowpass = 5.f * filter->lowpass();
  134. lowpass.store(outputs[LPF_OUTPUT].getVoltages(c));
  135. float_4 highpass = 5.f * filter->highpass();
  136. highpass.store(outputs[HPF_OUTPUT].getVoltages(c));
  137. }
  138. outputs[LPF_OUTPUT].setChannels(channels);
  139. outputs[HPF_OUTPUT].setChannels(channels);
  140. /*
  141. // Process sample
  142. float dt = args.sampleTime / UPSAMPLE;
  143. float inputBuf[UPSAMPLE];
  144. float lowpassBuf[UPSAMPLE];
  145. float highpassBuf[UPSAMPLE];
  146. inputUpsampler.process(input, inputBuf);
  147. for (int i = 0; i < UPSAMPLE; i++) {
  148. // Step the filter
  149. filter.process(inputBuf[i], dt);
  150. lowpassBuf[i] = filter.lowpass;
  151. highpassBuf[i] = filter.highpass;
  152. }
  153. // Set outputs
  154. if (outputs[LPF_OUTPUT].isConnected()) {
  155. outputs[LPF_OUTPUT].setVoltage(5.f * lowpassDecimator.process(lowpassBuf));
  156. }
  157. if (outputs[HPF_OUTPUT].isConnected()) {
  158. outputs[HPF_OUTPUT].setVoltage(5.f * highpassDecimator.process(highpassBuf));
  159. }
  160. */
  161. }
  162. };
  163. struct VCFWidget : ModuleWidget {
  164. VCFWidget(VCF* module) {
  165. setModule(module);
  166. setPanel(createPanel(asset::plugin(pluginInstance, "res/VCF.svg")));
  167. addChild(createWidget<ScrewSilver>(Vec(15, 0)));
  168. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 30, 0)));
  169. addChild(createWidget<ScrewSilver>(Vec(15, 365)));
  170. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 30, 365)));
  171. addParam(createParam<RoundHugeBlackKnob>(Vec(33, 61), module, VCF::FREQ_PARAM));
  172. addParam(createParam<RoundLargeBlackKnob>(Vec(12, 143), module, VCF::FINE_PARAM));
  173. addParam(createParam<RoundLargeBlackKnob>(Vec(71, 143), module, VCF::RES_PARAM));
  174. addParam(createParam<RoundLargeBlackKnob>(Vec(12, 208), module, VCF::FREQ_CV_PARAM));
  175. addParam(createParam<RoundLargeBlackKnob>(Vec(71, 208), module, VCF::DRIVE_PARAM));
  176. addInput(createInput<PJ301MPort>(Vec(10, 276), module, VCF::FREQ_INPUT));
  177. addInput(createInput<PJ301MPort>(Vec(48, 276), module, VCF::RES_INPUT));
  178. addInput(createInput<PJ301MPort>(Vec(85, 276), module, VCF::DRIVE_INPUT));
  179. addInput(createInput<PJ301MPort>(Vec(10, 320), module, VCF::IN_INPUT));
  180. addOutput(createOutput<PJ301MPort>(Vec(48, 320), module, VCF::LPF_OUTPUT));
  181. addOutput(createOutput<PJ301MPort>(Vec(85, 320), module, VCF::HPF_OUTPUT));
  182. }
  183. };
  184. Model* modelVCF = createModel<VCF, VCFWidget>("VCF");