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.

160 lines
5.3KB

  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. configInput(A1_INPUT, "A1");
  45. configInput(B1_INPUT, "B1");
  46. configInput(C1_INPUT, "C1");
  47. configInput(A2_INPUT, "A2");
  48. configInput(B2_INPUT, "B2");
  49. configInput(C2_INPUT, "C2");
  50. getInputInfo(B1_INPUT)->description = "Normalled to 5V";
  51. getInputInfo(C1_INPUT)->description = "Normalled to 10V";
  52. getInputInfo(B2_INPUT)->description = "Normalled to 5V";
  53. getInputInfo(C2_INPUT)->description = "Normalled to 10V";
  54. configOutput(OUT1_OUTPUT, "Out 1");
  55. configOutput(OUT2_OUTPUT, "Out 2");
  56. getOutputInfo(OUT1_OUTPUT)->description = "Normalled to Out 2";
  57. }
  58. void processSection(const ProcessArgs& args, int& lastChannels, float_4* lastOut, ParamIds levelB, ParamIds levelC, InputIds inputA, InputIds inputB, InputIds inputC, OutputIds output, LightIds outLight) {
  59. // Compute polyphony channels
  60. int channels = std::max(lastChannels, inputs[inputA].getChannels());
  61. channels = std::max(channels, inputs[inputB].getChannels());
  62. channels = std::max(channels, inputs[inputC].getChannels());
  63. lastChannels = channels;
  64. // Get param levels
  65. float gainB = 2.f * exponentialBipolar80Pade_5_4(params[levelB].getValue());
  66. float gainC = exponentialBipolar80Pade_5_4(params[levelC].getValue());
  67. for (int c = 0; c < channels; c += 4) {
  68. // Get inputs
  69. float_4 inA = inputs[inputA].getPolyVoltageSimd<float_4>(c);
  70. float_4 inB = inputs[inputB].getNormalPolyVoltageSimd<float_4>(5.f, c) * gainB;
  71. float_4 inC = inputs[inputC].getNormalPolyVoltageSimd<float_4>(10.f, c) * gainC;
  72. // Compute and set output
  73. float_4 out = inA * inB / 5.f + inC;
  74. lastOut[c / 4] += out;
  75. if (outputs[output].isConnected()) {
  76. outputs[output].setChannels(channels);
  77. outputs[output].setVoltageSimd(clip(lastOut[c / 4]), c);
  78. }
  79. }
  80. // Set lights
  81. if (channels == 1) {
  82. float b = lastOut[0][0];
  83. lights[outLight + 0].setSmoothBrightness(b / 5.f, args.sampleTime);
  84. lights[outLight + 1].setSmoothBrightness(-b / 5.f, args.sampleTime);
  85. lights[outLight + 2].setBrightness(0.f);
  86. }
  87. else {
  88. // RMS of output
  89. float b = 0.f;
  90. for (int c = 0; c < channels; c++)
  91. b += std::pow(lastOut[c / 4][c % 4], 2);
  92. b = std::sqrt(b);
  93. lights[outLight + 0].setBrightness(0.0f);
  94. lights[outLight + 1].setBrightness(0.0f);
  95. lights[outLight + 2].setBrightness(b);
  96. }
  97. }
  98. void process(const ProcessArgs& args) override {
  99. int channels = 1;
  100. float_4 out[4] = {};
  101. // Section A
  102. processSection(args, channels, out, B1_LEVEL_PARAM, C1_LEVEL_PARAM, A1_INPUT, B1_INPUT, C1_INPUT, OUT1_OUTPUT, OUT1_LIGHT);
  103. // Break summing if output A is patched
  104. if (outputs[OUT1_OUTPUT].isConnected()) {
  105. channels = 1;
  106. std::memset(out, 0, sizeof(out));
  107. }
  108. // Section B
  109. processSection(args, channels, out, B2_LEVEL_PARAM, C2_LEVEL_PARAM, A2_INPUT, B2_INPUT, C2_INPUT, OUT2_OUTPUT, OUT2_LIGHT);
  110. }
  111. };
  112. struct ABCWidget : ModuleWidget {
  113. ABCWidget(ABC* module) {
  114. setModule(module);
  115. setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/panels/ABC.svg")));
  116. addChild(createWidget<Knurlie>(Vec(15, 0)));
  117. addChild(createWidget<Knurlie>(Vec(15, 365)));
  118. addParam(createParam<Davies1900hRedKnob>(Vec(45, 37), module, ABC::B1_LEVEL_PARAM));
  119. addParam(createParam<Davies1900hWhiteKnob>(Vec(45, 107), module, ABC::C1_LEVEL_PARAM));
  120. addParam(createParam<Davies1900hRedKnob>(Vec(45, 204), module, ABC::B2_LEVEL_PARAM));
  121. addParam(createParam<Davies1900hWhiteKnob>(Vec(45, 274), module, ABC::C2_LEVEL_PARAM));
  122. addInput(createInput<BefacoInputPort>(Vec(7, 28), module, ABC::A1_INPUT));
  123. addInput(createInput<BefacoInputPort>(Vec(7, 70), module, ABC::B1_INPUT));
  124. addInput(createInput<BefacoInputPort>(Vec(7, 112), module, ABC::C1_INPUT));
  125. addOutput(createOutput<BefacoOutputPort>(Vec(7, 154), module, ABC::OUT1_OUTPUT));
  126. addInput(createInput<BefacoInputPort>(Vec(7, 195), module, ABC::A2_INPUT));
  127. addInput(createInput<BefacoInputPort>(Vec(7, 237), module, ABC::B2_INPUT));
  128. addInput(createInput<BefacoInputPort>(Vec(7, 279), module, ABC::C2_INPUT));
  129. addOutput(createOutput<BefacoOutputPort>(Vec(7, 321), module, ABC::OUT2_OUTPUT));
  130. addChild(createLight<MediumLight<RedGreenBlueLight>>(Vec(37, 162), module, ABC::OUT1_LIGHT));
  131. addChild(createLight<MediumLight<RedGreenBlueLight>>(Vec(37, 329), module, ABC::OUT2_LIGHT));
  132. }
  133. };
  134. Model* modelABC = createModel<ABC, ABCWidget>("ABC");