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.

MidSide.cpp 4.1KB

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "plugin.hpp"
  2. struct MidSide : Module {
  3. enum ParamIds {
  4. ENC_WIDTH_PARAM,
  5. DEC_WIDTH_PARAM,
  6. NUM_PARAMS
  7. };
  8. enum InputIds {
  9. ENC_WIDTH_INPUT,
  10. ENC_LEFT_INPUT,
  11. ENC_RIGHT_INPUT,
  12. DEC_WIDTH_INPUT,
  13. DEC_MID_INPUT,
  14. DEC_SIDES_INPUT,
  15. NUM_INPUTS
  16. };
  17. enum OutputIds {
  18. ENC_MID_OUTPUT,
  19. ENC_SIDES_OUTPUT,
  20. DEC_LEFT_OUTPUT,
  21. DEC_RIGHT_OUTPUT,
  22. NUM_OUTPUTS
  23. };
  24. enum LightIds {
  25. NUM_LIGHTS
  26. };
  27. MidSide() {
  28. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
  29. configParam(ENC_WIDTH_PARAM, 0.f, 2.f, 1.f, "Encoder width", "%", 0, 100);
  30. configParam(DEC_WIDTH_PARAM, 0.f, 2.f, 1.f, "Decoder width", "%", 0, 100);
  31. }
  32. void process(const ProcessArgs& args) override {
  33. using simd::float_4;
  34. // Encoder
  35. {
  36. int channels = std::max(inputs[ENC_LEFT_INPUT].getChannels(), inputs[ENC_RIGHT_INPUT].getChannels());
  37. outputs[ENC_MID_OUTPUT].setChannels(channels);
  38. outputs[ENC_SIDES_OUTPUT].setChannels(channels);
  39. for (int c = 0; c < channels; c += 4) {
  40. float_4 width = params[ENC_WIDTH_PARAM].getValue();
  41. width += inputs[ENC_WIDTH_INPUT].getPolyVoltageSimd<float_4>(c) / 10 * 2;
  42. width = simd::fmax(width, 0.f);
  43. float_4 left = inputs[ENC_LEFT_INPUT].getVoltageSimd<float_4>(c);
  44. float_4 right = inputs[ENC_RIGHT_INPUT].getVoltageSimd<float_4>(c);
  45. float_4 mid = (left + right) / 2;
  46. float_4 sides = (left - right) / 2 * width;
  47. outputs[ENC_MID_OUTPUT].setVoltageSimd(mid, c);
  48. outputs[ENC_SIDES_OUTPUT].setVoltageSimd(sides, c);
  49. }
  50. }
  51. // Decoder
  52. {
  53. int channels = std::max(inputs[DEC_MID_INPUT].getChannels(), inputs[DEC_SIDES_INPUT].getChannels());
  54. outputs[DEC_LEFT_OUTPUT].setChannels(channels);
  55. outputs[DEC_RIGHT_OUTPUT].setChannels(channels);
  56. for (int c = 0; c < channels; c += 4) {
  57. float_4 width = params[DEC_WIDTH_PARAM].getValue();
  58. width += inputs[DEC_WIDTH_INPUT].getPolyVoltageSimd<float_4>(c) / 10 * 2;
  59. width = simd::fmax(width, 0.f);
  60. float_4 mid = inputs[DEC_MID_INPUT].getVoltageSimd<float_4>(c);
  61. float_4 sides = inputs[DEC_SIDES_INPUT].getVoltageSimd<float_4>(c);
  62. float_4 left = mid + sides * width;
  63. float_4 right = mid - sides * width;
  64. outputs[DEC_LEFT_OUTPUT].setVoltageSimd(left, c);
  65. outputs[DEC_RIGHT_OUTPUT].setVoltageSimd(right, c);
  66. }
  67. }
  68. }
  69. };
  70. struct MidSideWidget : ModuleWidget {
  71. MidSideWidget(MidSide* module) {
  72. setModule(module);
  73. setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/MidSide.svg")));
  74. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, 0)));
  75. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, 0)));
  76. addChild(createWidget<ScrewSilver>(Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  77. addChild(createWidget<ScrewSilver>(Vec(box.size.x - 2 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT - RACK_GRID_WIDTH)));
  78. addParam(createParamCentered<RoundSmallBlackKnob>(mm2px(Vec(18.699, 26.234)), module, MidSide::ENC_WIDTH_PARAM));
  79. addParam(createParamCentered<RoundSmallBlackKnob>(mm2px(Vec(18.699, 81.235)), module, MidSide::DEC_WIDTH_PARAM));
  80. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.699, 26.234)), module, MidSide::ENC_WIDTH_INPUT));
  81. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.699, 41.234)), module, MidSide::ENC_LEFT_INPUT));
  82. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(18.699, 41.234)), module, MidSide::ENC_RIGHT_INPUT));
  83. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.699, 81.234)), module, MidSide::DEC_WIDTH_INPUT));
  84. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(6.699, 96.234)), module, MidSide::DEC_MID_INPUT));
  85. addInput(createInputCentered<PJ301MPort>(mm2px(Vec(18.699, 96.234)), module, MidSide::DEC_SIDES_INPUT));
  86. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(6.699, 57.253)), module, MidSide::ENC_MID_OUTPUT));
  87. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(18.699, 57.253)), module, MidSide::ENC_SIDES_OUTPUT));
  88. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(6.699, 112.252)), module, MidSide::DEC_LEFT_OUTPUT));
  89. addOutput(createOutputCentered<PJ301MPort>(mm2px(Vec(18.699, 112.252)), module, MidSide::DEC_RIGHT_OUTPUT));
  90. }
  91. };
  92. Model* modelMidSide = createModel<MidSide, MidSideWidget>("MidSide");