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.

136 lines
4.2KB

  1. #include "plugin.hpp"
  2. struct VCA : Module {
  3. enum ParamIds {
  4. LEVEL1_PARAM,
  5. LEVEL2_PARAM,
  6. NUM_PARAMS
  7. };
  8. enum InputIds {
  9. EXP1_INPUT,
  10. LIN1_INPUT,
  11. IN1_INPUT,
  12. EXP2_INPUT,
  13. LIN2_INPUT,
  14. IN2_INPUT,
  15. NUM_INPUTS
  16. };
  17. enum OutputIds {
  18. OUT1_OUTPUT,
  19. OUT2_OUTPUT,
  20. NUM_OUTPUTS
  21. };
  22. VCA() {
  23. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS);
  24. configParam(LEVEL1_PARAM, 0.0, 1.0, 1.0, "Channel 1 level", "%", 0, 100);
  25. configParam(LEVEL2_PARAM, 0.0, 1.0, 1.0, "Channel 2 level", "%", 0, 100);
  26. configInput(EXP1_INPUT, "Channel 1 exponential CV");
  27. configInput(EXP2_INPUT, "Channel 2 exponential CV");
  28. configInput(LIN1_INPUT, "Channel 1 linear CV");
  29. configInput(LIN2_INPUT, "Channel 2 linear CV");
  30. configInput(IN1_INPUT, "Channel 1");
  31. configInput(IN2_INPUT, "Channel 2");
  32. configOutput(OUT1_OUTPUT, "Channel 1");
  33. configOutput(OUT2_OUTPUT, "Channel 2");
  34. configBypass(IN1_INPUT, OUT1_OUTPUT);
  35. configBypass(IN2_INPUT, OUT2_OUTPUT);
  36. }
  37. void processChannel(Input& in, Param& level, Input& lin, Input& exp, Output& out) {
  38. // Get input
  39. int channels = std::max(in.getChannels(), 1);
  40. simd::float_4 v[4];
  41. for (int c = 0; c < channels; c += 4) {
  42. v[c / 4] = simd::float_4::load(in.getVoltages(c));
  43. }
  44. // Apply knob gain
  45. float gain = level.getValue();
  46. for (int c = 0; c < channels; c += 4) {
  47. v[c / 4] *= gain;
  48. }
  49. // Apply linear CV gain
  50. if (lin.isConnected()) {
  51. if (lin.isPolyphonic()) {
  52. for (int c = 0; c < channels; c += 4) {
  53. simd::float_4 cv = simd::float_4::load(lin.getVoltages(c)) / 10.f;
  54. cv = clamp(cv, 0.f, 1.f);
  55. v[c / 4] *= cv;
  56. }
  57. }
  58. else {
  59. float cv = lin.getVoltage() / 10.f;
  60. cv = clamp(cv, 0.f, 1.f);
  61. for (int c = 0; c < channels; c += 4) {
  62. v[c / 4] *= cv;
  63. }
  64. }
  65. }
  66. // Apply exponential CV gain
  67. const float expBase = 50.f;
  68. if (exp.isConnected()) {
  69. if (exp.isPolyphonic()) {
  70. for (int c = 0; c < channels; c += 4) {
  71. simd::float_4 cv = simd::float_4::load(exp.getVoltages(c)) / 10.f;
  72. cv = clamp(cv, 0.f, 1.f);
  73. cv = rescale(pow(expBase, cv), 1.f, expBase, 0.f, 1.f);
  74. v[c / 4] *= cv;
  75. }
  76. }
  77. else {
  78. float cv = exp.getVoltage() / 10.f;
  79. cv = clamp(cv, 0.f, 1.f);
  80. cv = rescale(std::pow(expBase, cv), 1.f, expBase, 0.f, 1.f);
  81. for (int c = 0; c < channels; c += 4) {
  82. v[c / 4] *= cv;
  83. }
  84. }
  85. }
  86. // Set output
  87. out.setChannels(channels);
  88. for (int c = 0; c < channels; c += 4) {
  89. v[c / 4].store(out.getVoltages(c));
  90. }
  91. }
  92. void process(const ProcessArgs& args) override {
  93. processChannel(inputs[IN1_INPUT], params[LEVEL1_PARAM], inputs[LIN1_INPUT], inputs[EXP1_INPUT], outputs[OUT1_OUTPUT]);
  94. processChannel(inputs[IN2_INPUT], params[LEVEL2_PARAM], inputs[LIN2_INPUT], inputs[EXP2_INPUT], outputs[OUT2_OUTPUT]);
  95. }
  96. };
  97. struct VCAWidget : ModuleWidget {
  98. VCAWidget(VCA* module) {
  99. setModule(module);
  100. setPanel(createPanel(asset::plugin(pluginInstance, "res/VCA.svg")));
  101. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  102. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  103. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  104. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  105. addParam(createParam<RoundLargeBlackKnob>(mm2px(Vec(6.35, 19.11753)), module, VCA::LEVEL1_PARAM));
  106. addParam(createParam<RoundLargeBlackKnob>(mm2px(Vec(6.35, 74.80544)), module, VCA::LEVEL2_PARAM));
  107. addInput(createInput<PJ301MPort>(mm2px(Vec(2.5907, 38.19371)), module, VCA::EXP1_INPUT));
  108. addInput(createInput<PJ301MPort>(mm2px(Vec(14.59752, 38.19371)), module, VCA::LIN1_INPUT));
  109. addInput(createInput<PJ301MPort>(mm2px(Vec(2.5907, 52.80642)), module, VCA::IN1_INPUT));
  110. addInput(createInput<PJ301MPort>(mm2px(Vec(2.5907, 93.53435)), module, VCA::EXP2_INPUT));
  111. addInput(createInput<PJ301MPort>(mm2px(Vec(14.59752, 93.53435)), module, VCA::LIN2_INPUT));
  112. addInput(createInput<PJ301MPort>(mm2px(Vec(2.5907, 108.14706)), module, VCA::IN2_INPUT));
  113. addOutput(createOutput<PJ301MPort>(mm2px(Vec(14.59752, 52.80642)), module, VCA::OUT1_OUTPUT));
  114. addOutput(createOutput<PJ301MPort>(mm2px(Vec(14.59752, 108.14706)), module, VCA::OUT2_OUTPUT));
  115. }
  116. };
  117. Model* modelVCA = createModel<VCA, VCAWidget>("VCA");