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.

143 lines
4.8KB

  1. #include "plugin.hpp"
  2. using simd::float_4;
  3. template <typename T>
  4. static T clip(T x) {
  5. // Pade approximant of x/(1 + x^12)^(1/12)
  6. const T limit = 1.16691853009184f;
  7. x = clamp(x * 0.1f, -limit, limit);
  8. return 10.0f * (x + 1.45833f * simd::pow(x, 13) + 0.559028f * simd::pow(x, 25) + 0.0427035f * simd::pow(x, 37))
  9. / (1.0f + 1.54167f * simd::pow(x, 12) + 0.642361f * simd::pow(x, 24) + 0.0579909f * simd::pow(x, 36));
  10. }
  11. struct ABC : Module {
  12. enum ParamIds {
  13. B1_LEVEL_PARAM,
  14. C1_LEVEL_PARAM,
  15. B2_LEVEL_PARAM,
  16. C2_LEVEL_PARAM,
  17. NUM_PARAMS
  18. };
  19. enum InputIds {
  20. A1_INPUT,
  21. B1_INPUT,
  22. C1_INPUT,
  23. A2_INPUT,
  24. B2_INPUT,
  25. C2_INPUT,
  26. NUM_INPUTS
  27. };
  28. enum OutputIds {
  29. OUT1_OUTPUT,
  30. OUT2_OUTPUT,
  31. NUM_OUTPUTS
  32. };
  33. enum LightIds {
  34. ENUMS(OUT1_LIGHT, 3),
  35. ENUMS(OUT2_LIGHT, 3),
  36. NUM_LIGHTS
  37. };
  38. ABC() {
  39. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
  40. configParam(B1_LEVEL_PARAM, -1.0, 1.0, 0.0, "B1 Level");
  41. configParam(C1_LEVEL_PARAM, -1.0, 1.0, 0.0, "C1 Level");
  42. configParam(B2_LEVEL_PARAM, -1.0, 1.0, 0.0, "B2 Level");
  43. configParam(C2_LEVEL_PARAM, -1.0, 1.0, 0.0, "C2 Level");
  44. }
  45. void processSection(const ProcessArgs& args, int& lastChannels, float_4* lastOut, ParamIds levelB, ParamIds levelC, InputIds inputA, InputIds inputB, InputIds inputC, OutputIds output, LightIds outLight) {
  46. // Compute polyphony channels
  47. int channels = std::max(lastChannels, inputs[inputA].getChannels());
  48. channels = std::max(channels, inputs[inputB].getChannels());
  49. channels = std::max(channels, inputs[inputC].getChannels());
  50. lastChannels = channels;
  51. // Get param levels
  52. float gainB = 2.f * exponentialBipolar80Pade_5_4(params[levelB].getValue());
  53. float gainC = exponentialBipolar80Pade_5_4(params[levelC].getValue());
  54. for (int c = 0; c < channels; c += 4) {
  55. // Get inputs
  56. float_4 inA = inputs[inputA].getPolyVoltageSimd<float_4>(c);
  57. float_4 inB = inputs[inputB].getNormalPolyVoltageSimd<float_4>(5.f, c) * gainB;
  58. float_4 inC = inputs[inputC].getNormalPolyVoltageSimd<float_4>(10.f, c) * gainC;
  59. // Compute and set output
  60. float_4 out = inA * inB / 5.f + inC;
  61. lastOut[c / 4] += out;
  62. if (outputs[output].isConnected()) {
  63. outputs[output].setChannels(channels);
  64. outputs[output].setVoltageSimd(clip(lastOut[c / 4]), c);
  65. }
  66. }
  67. // Set lights
  68. if (channels == 1) {
  69. float b = lastOut[0][0];
  70. lights[outLight + 0].setSmoothBrightness(b / 5.f, args.sampleTime);
  71. lights[outLight + 1].setSmoothBrightness(-b / 5.f, args.sampleTime);
  72. lights[outLight + 2].setBrightness(0.f);
  73. }
  74. else {
  75. // RMS of output
  76. float b = 0.f;
  77. for (int c = 0; c < channels; c++)
  78. b += std::pow(lastOut[c / 4][c % 4], 2);
  79. b = std::sqrt(b);
  80. lights[outLight + 0].setBrightness(0.0f);
  81. lights[outLight + 1].setBrightness(0.0f);
  82. lights[outLight + 2].setBrightness(b);
  83. }
  84. }
  85. void process(const ProcessArgs& args) override {
  86. int channels = 1;
  87. float_4 out[4] = {};
  88. // Section A
  89. processSection(args, channels, out, B1_LEVEL_PARAM, C1_LEVEL_PARAM, A1_INPUT, B1_INPUT, C1_INPUT, OUT1_OUTPUT, OUT1_LIGHT);
  90. // Break summing if output A is patched
  91. if (outputs[OUT1_OUTPUT].isConnected()) {
  92. channels = 1;
  93. std::memset(out, 0, sizeof(out));
  94. }
  95. // Section B
  96. processSection(args, channels, out, B2_LEVEL_PARAM, C2_LEVEL_PARAM, A2_INPUT, B2_INPUT, C2_INPUT, OUT2_OUTPUT, OUT2_LIGHT);
  97. }
  98. };
  99. struct ABCWidget : ModuleWidget {
  100. ABCWidget(ABC* module) {
  101. setModule(module);
  102. setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/ABC.svg")));
  103. addChild(createWidget<Knurlie>(Vec(15, 0)));
  104. addChild(createWidget<Knurlie>(Vec(15, 365)));
  105. addParam(createParam<Davies1900hRedKnob>(Vec(45, 37), module, ABC::B1_LEVEL_PARAM));
  106. addParam(createParam<Davies1900hWhiteKnob>(Vec(45, 107), module, ABC::C1_LEVEL_PARAM));
  107. addParam(createParam<Davies1900hRedKnob>(Vec(45, 204), module, ABC::B2_LEVEL_PARAM));
  108. addParam(createParam<Davies1900hWhiteKnob>(Vec(45, 274), module, ABC::C2_LEVEL_PARAM));
  109. addInput(createInput<BefacoInputPort>(Vec(7, 28), module, ABC::A1_INPUT));
  110. addInput(createInput<BefacoInputPort>(Vec(7, 70), module, ABC::B1_INPUT));
  111. addInput(createInput<BefacoInputPort>(Vec(7, 112), module, ABC::C1_INPUT));
  112. addOutput(createOutput<BefacoOutputPort>(Vec(7, 154), module, ABC::OUT1_OUTPUT));
  113. addInput(createInput<BefacoInputPort>(Vec(7, 195), module, ABC::A2_INPUT));
  114. addInput(createInput<BefacoInputPort>(Vec(7, 237), module, ABC::B2_INPUT));
  115. addInput(createInput<BefacoInputPort>(Vec(7, 279), module, ABC::C2_INPUT));
  116. addOutput(createOutput<BefacoOutputPort>(Vec(7, 321), module, ABC::OUT2_OUTPUT));
  117. addChild(createLight<MediumLight<RedGreenBlueLight>>(Vec(37, 162), module, ABC::OUT1_LIGHT));
  118. addChild(createLight<MediumLight<RedGreenBlueLight>>(Vec(37, 329), module, ABC::OUT2_LIGHT));
  119. }
  120. };
  121. Model* modelABC = createModel<ABC, ABCWidget>("ABC");