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.

102 lines
3.8KB

  1. #include "RJModules.hpp"
  2. #include <iostream>
  3. #include <cmath>
  4. namespace rack_plugin_RJModules {
  5. struct Sidechain: Module {
  6. enum ParamIds {
  7. RATIO_PARAM,
  8. DECAY_PARAM,
  9. NUM_PARAMS
  10. };
  11. enum InputIds {
  12. CH1_INPUT,
  13. TRIGGER_INPUT,
  14. RATIO_CV_INPUT,
  15. DECAY_CV_INPUT,
  16. NUM_INPUTS
  17. };
  18. enum OutputIds {
  19. CH1_OUTPUT,
  20. NUM_OUTPUTS
  21. };
  22. float decayAmount = 0.0;
  23. Sidechain() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {}
  24. void step() override;
  25. };
  26. #define ROUND(f) ((float)((f > 0.0) ? floor(f + 0.5) : ceil(f - 0.5)))
  27. void Sidechain::step() {
  28. // float combined_input_1 = params[CH1_PARAM].value * clamp(inputs[CH1_CV_INPUT].normalize(10.0) / 10.0, 0.0, 1.0);
  29. // float combined_input_2 = params[CH2_PARAM].value * clamp(inputs[CH2_CV_INPUT].normalize(10.0) / 10.0, 0.0, 1.0);
  30. // float combined_input_3 = params[CH3_PARAM].value * clamp(inputs[CH3_CV_INPUT].normalize(10.0) / 10.0, 0.0, 1.0);
  31. // // new_value = ( (old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min
  32. // float mapped_input_1 = ((combined_input_1 - 0.0) / (1.0 - 0.0) ) * (12.0 - -12.0) + -12.0;
  33. // float mapped_input_2 = ((combined_input_2 - 0.0) / (1.0 - 0.0) ) * (12.0 - -12.0) + -12.0;
  34. // float mapped_input_3 = ((combined_input_3 - 0.0) / (1.0 - 0.0) ) * (12.0 - -12.0) + -12.0;
  35. // int cast_input_1 = static_cast<int>(mapped_input_1);
  36. // int cast_input_2 = static_cast<int>(mapped_input_2);
  37. // int cast_input_3 = static_cast<int>(mapped_input_3);
  38. float ratio = params[RATIO_PARAM].value * clamp(inputs[RATIO_CV_INPUT].normalize(10.0f) / 10.0f, 0.0f, 1.0f);
  39. float decay = 1 - (params[DECAY_PARAM].value * clamp(inputs[DECAY_CV_INPUT].normalize(10.0f) / 10.0f, 0.0f, 1.0f)) + .00001;
  40. float decayLambda = .0001;
  41. if(inputs[TRIGGER_INPUT].value > 0 || inputs[TRIGGER_INPUT].value > 0){
  42. decayAmount = clamp(inputs[RATIO_CV_INPUT].normalize(10.0f) / 10.0f, 0.0f, 1.0f);
  43. }
  44. outputs[CH1_OUTPUT].value = inputs[CH1_OUTPUT].value * (1 - (ratio * decayAmount));
  45. decayAmount = (decayAmount - (decayLambda * (decay)));
  46. if(decayAmount < 0){
  47. decayAmount = 0;
  48. }
  49. }
  50. struct SidechainWidget: ModuleWidget {
  51. SidechainWidget(Sidechain *module);
  52. };
  53. SidechainWidget::SidechainWidget(Sidechain *module) : ModuleWidget(module) {
  54. box.size = Vec(15*10, 380);
  55. {
  56. SVGPanel *panel = new SVGPanel();
  57. panel->box.size = box.size;
  58. panel->setBackground(SVG::load(assetPlugin(plugin, "res/Sidechain.svg")));
  59. addChild(panel);
  60. }
  61. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  62. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 0)));
  63. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  64. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 365)));
  65. addParam(ParamWidget::create<RoundBlackKnob>(Vec(57, 159), module, Sidechain::RATIO_PARAM, 0.0, 1.0, 1.0));
  66. addParam(ParamWidget::create<RoundBlackKnob>(Vec(57, 239), module, Sidechain::DECAY_PARAM, 0.0, 1.0, 0.3));
  67. addInput(Port::create<PJ301MPort>(Vec(22, 100), Port::INPUT, module, Sidechain::CH1_INPUT));
  68. addInput(Port::create<PJ301MPort>(Vec(22, 180), Port::INPUT, module, Sidechain::RATIO_CV_INPUT));
  69. addInput(Port::create<PJ301MPort>(Vec(22, 260), Port::INPUT, module, Sidechain::DECAY_CV_INPUT));
  70. addInput(Port::create<PJ301MPort>(Vec(110, 100), Port::INPUT, module, Sidechain::TRIGGER_INPUT));
  71. addOutput(Port::create<PJ301MPort>(Vec(110, 305), Port::OUTPUT, module, Sidechain::CH1_OUTPUT));
  72. }
  73. } // namespace rack_plugin_RJModules
  74. using namespace rack_plugin_RJModules;
  75. RACK_PLUGIN_MODEL_INIT(RJModules, Sidechain) {
  76. Model *modelSidechain = Model::create<Sidechain, SidechainWidget>("RJModules", "Sidechain", "[FX] Sidechain", COMPRESSOR_TAG);
  77. return modelSidechain;
  78. }