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.

222 lines
6.3KB

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