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.

234 lines
7.2KB

  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, // removed in 2.0
  56. RES_PARAM,
  57. FREQ_CV_PARAM,
  58. DRIVE_PARAM,
  59. // Added in 2.0
  60. RES_CV_PARAM,
  61. DRIVE_CV_PARAM,
  62. NUM_PARAMS
  63. };
  64. enum InputIds {
  65. FREQ_INPUT,
  66. RES_INPUT,
  67. DRIVE_INPUT,
  68. IN_INPUT,
  69. NUM_INPUTS
  70. };
  71. enum OutputIds {
  72. LPF_OUTPUT,
  73. HPF_OUTPUT,
  74. NUM_OUTPUTS
  75. };
  76. LadderFilter<float_4> filters[4];
  77. // Upsampler<UPSAMPLE, 8> inputUpsampler;
  78. // Decimator<UPSAMPLE, 8> lowpassDecimator;
  79. // Decimator<UPSAMPLE, 8> highpassDecimator;
  80. VCF() {
  81. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS);
  82. // Multiply and offset for backward patch compatibility
  83. configParam(FREQ_PARAM, 0.f, 1.f, 0.5f, "Cutoff frequency", " Hz", std::pow(2, 10.f), dsp::FREQ_C4 / std::pow(2, 5.f));
  84. configParam(FINE_PARAM, 0.f, 1.f, 0.5f, "Fine frequency");
  85. configParam(RES_PARAM, 0.f, 1.f, 0.f, "Resonance", "%", 0.f, 100.f);
  86. configParam(RES_CV_PARAM, -1.f, 1.f, 0.f, "Resonance CV", "%", 0.f, 100.f);
  87. configParam(FREQ_CV_PARAM, -1.f, 1.f, 0.f, "Cutoff frequency CV", "%", 0.f, 100.f);
  88. configParam(DRIVE_PARAM, 0.f, 1.f, 0.f, "Drive", "", 0, 11);
  89. configParam(DRIVE_CV_PARAM, -1.f, 1.f, 0.f, "Drive CV", "%", 0, 100);
  90. configInput(FREQ_INPUT, "Frequency");
  91. configInput(RES_INPUT, "Resonance");
  92. configInput(DRIVE_INPUT, "Drive");
  93. configInput(IN_INPUT, "Audio");
  94. configOutput(LPF_OUTPUT, "Lowpass filter");
  95. configOutput(HPF_OUTPUT, "Highpass filter");
  96. configBypass(IN_INPUT, LPF_OUTPUT);
  97. configBypass(IN_INPUT, HPF_OUTPUT);
  98. }
  99. void onReset() override {
  100. for (int i = 0; i < 4; i++)
  101. filters[i].reset();
  102. }
  103. void process(const ProcessArgs& args) override {
  104. if (!outputs[LPF_OUTPUT].isConnected() && !outputs[HPF_OUTPUT].isConnected()) {
  105. return;
  106. }
  107. float driveParam = params[DRIVE_PARAM].getValue();
  108. float driveCvParam = params[DRIVE_CV_PARAM].getValue();
  109. float resParam = params[RES_PARAM].getValue();
  110. float resCvParam = params[RES_CV_PARAM].getValue();
  111. float fineParam = params[FINE_PARAM].getValue();
  112. fineParam = dsp::quadraticBipolar(fineParam * 2.f - 1.f) * 7.f / 12.f;
  113. float freqCvParam = params[FREQ_CV_PARAM].getValue();
  114. freqCvParam = dsp::quadraticBipolar(freqCvParam);
  115. float freqParam = params[FREQ_PARAM].getValue();
  116. freqParam = freqParam * 10.f - 5.f;
  117. int channels = std::max(1, inputs[IN_INPUT].getChannels());
  118. for (int c = 0; c < channels; c += 4) {
  119. auto* filter = &filters[c / 4];
  120. float_4 input = float_4::load(inputs[IN_INPUT].getVoltages(c)) / 5.f;
  121. // Drive gain
  122. // TODO Make center of knob unity gain, 0 is off like a VCA
  123. float_4 drive = driveParam + inputs[DRIVE_INPUT].getPolyVoltageSimd<float_4>(c) / 10.f * driveCvParam;
  124. drive = clamp(drive, 0.f, 1.f);
  125. float_4 gain = simd::pow(1.f + drive, 5);
  126. input *= gain;
  127. // Add -120dB noise to bootstrap self-oscillation
  128. input += 1e-6f * (2.f * random::uniform() - 1.f);
  129. // Set resonance
  130. float_4 resonance = resParam + inputs[RES_INPUT].getPolyVoltageSimd<float_4>(c) / 10.f * resCvParam;
  131. resonance = clamp(resonance, 0.f, 1.f);
  132. filter->resonance = simd::pow(resonance, 2) * 10.f;
  133. // Get pitch
  134. float_4 pitch = freqParam + fineParam + inputs[FREQ_INPUT].getPolyVoltageSimd<float_4>(c) * freqCvParam;
  135. // Set cutoff
  136. float_4 cutoff = dsp::FREQ_C4 * simd::pow(2.f, pitch);
  137. cutoff = clamp(cutoff, 1.f, 8000.f);
  138. filter->setCutoff(cutoff);
  139. // Set outputs
  140. filter->process(input, args.sampleTime);
  141. float_4 lowpass = 5.f * filter->lowpass();
  142. lowpass.store(outputs[LPF_OUTPUT].getVoltages(c));
  143. float_4 highpass = 5.f * filter->highpass();
  144. highpass.store(outputs[HPF_OUTPUT].getVoltages(c));
  145. }
  146. outputs[LPF_OUTPUT].setChannels(channels);
  147. outputs[HPF_OUTPUT].setChannels(channels);
  148. /*
  149. // Process sample
  150. float dt = args.sampleTime / UPSAMPLE;
  151. float inputBuf[UPSAMPLE];
  152. float lowpassBuf[UPSAMPLE];
  153. float highpassBuf[UPSAMPLE];
  154. inputUpsampler.process(input, inputBuf);
  155. for (int i = 0; i < UPSAMPLE; i++) {
  156. // Step the filter
  157. filter.process(inputBuf[i], dt);
  158. lowpassBuf[i] = filter.lowpass;
  159. highpassBuf[i] = filter.highpass;
  160. }
  161. // Set outputs
  162. if (outputs[LPF_OUTPUT].isConnected()) {
  163. outputs[LPF_OUTPUT].setVoltage(5.f * lowpassDecimator.process(lowpassBuf));
  164. }
  165. if (outputs[HPF_OUTPUT].isConnected()) {
  166. outputs[HPF_OUTPUT].setVoltage(5.f * highpassDecimator.process(highpassBuf));
  167. }
  168. */
  169. }
  170. };
  171. struct VCFWidget : ModuleWidget {
  172. VCFWidget(VCF* module) {
  173. setModule(module);
  174. setPanel(createPanel(asset::plugin(pluginInstance, "res/VCF.svg")));
  175. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  176. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  177. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  178. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  179. addParam(createParamCentered<RoundHugeBlackKnob>(mm2px(Vec(17.587, 29.808)), module, VCF::FREQ_PARAM));
  180. addParam(createParamCentered<RoundLargeBlackKnob>(mm2px(Vec(8.895, 56.388)), module, VCF::RES_PARAM));
  181. addParam(createParamCentered<RoundLargeBlackKnob>(mm2px(Vec(26.665, 56.388)), module, VCF::DRIVE_PARAM));
  182. addParam(createParamCentered<Trimpot>(mm2px(Vec(6.996, 80.603)), module, VCF::FREQ_CV_PARAM));
  183. addParam(createParamCentered<Trimpot>(mm2px(Vec(17.833, 80.603)), module, VCF::RES_CV_PARAM));
  184. addParam(createParamCentered<Trimpot>(mm2px(Vec(28.67, 80.603)), module, VCF::DRIVE_CV_PARAM));
  185. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.996, 96.813)), module, VCF::FREQ_INPUT));
  186. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(17.833, 96.813)), module, VCF::RES_INPUT));
  187. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(28.67, 96.813)), module, VCF::DRIVE_INPUT));
  188. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.996, 113.115)), module, VCF::IN_INPUT));
  189. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(17.833, 113.115)), module, VCF::LPF_OUTPUT));
  190. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(28.67, 113.115)), module, VCF::HPF_OUTPUT));
  191. }
  192. };
  193. Model* modelVCF = createModel<VCF, VCFWidget>("VCF");