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.

178 lines
4.2KB

  1. #include "ML_modules.hpp"
  2. #include "dsp/digital.hpp"
  3. #define minLength 0.001f
  4. namespace rack_plugin_ML_modules {
  5. struct TrigDelay : Module {
  6. enum ParamIds {
  7. DELAY1_PARAM,
  8. DELAY2_PARAM,
  9. LENGTH1_PARAM,
  10. LENGTH2_PARAM,
  11. NUM_PARAMS
  12. };
  13. enum InputIds {
  14. GATE1_INPUT,
  15. GATE2_INPUT,
  16. DELAY1_INPUT,
  17. DELAY2_INPUT,
  18. LENGTH1_INPUT,
  19. LENGTH2_INPUT,
  20. NUM_INPUTS
  21. };
  22. enum OutputIds {
  23. OUT1_OUTPUT,
  24. OUT2_OUTPUT,
  25. NUM_OUTPUTS
  26. };
  27. enum LightIds {
  28. NUM_LIGHTS
  29. };
  30. TrigDelay() : Module( NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS ) {
  31. gSampleRate = engineGetSampleRate();
  32. // minLength = 0.001;
  33. };
  34. void step() override;
  35. bool gate1=false, gate2=false;
  36. SchmittTrigger gateTrigger1, gateTrigger2;
  37. PulseGenerator delay1, delay2;
  38. PulseGenerator on1, on2;
  39. float gSampleRate;
  40. void onSampleRateChange() { gSampleRate = engineGetSampleRate(); }
  41. void reset() override {
  42. gate1=false;
  43. gate2=false;
  44. };
  45. private:
  46. };
  47. void TrigDelay::step() {
  48. float delayTime1 = params[DELAY1_PARAM].value;
  49. float delayTime2 = params[DELAY2_PARAM].value;
  50. float length1 = params[LENGTH1_PARAM].value;
  51. float length2 = params[LENGTH2_PARAM].value;
  52. if( inputs[DELAY1_INPUT].active ) { delayTime1 *= clamp( inputs[DELAY1_INPUT].value / 10.0f, 0.0f, 1.0f );};
  53. if( inputs[DELAY2_INPUT].active ) { delayTime2 *= clamp( inputs[DELAY2_INPUT].value / 10.0f, 0.0f, 1.0f );};
  54. if( inputs[LENGTH1_INPUT].active ) { length1 *= clamp( inputs[LENGTH1_INPUT].value / 10.0f, minLength, 1.0f );};
  55. if( inputs[LENGTH2_INPUT].active ) { length2 *= clamp( inputs[LENGTH2_INPUT].value / 10.0f, minLength, 1.0f );};
  56. if( inputs[GATE1_INPUT].active ) {
  57. if(gateTrigger1.process(inputs[GATE1_INPUT].value)) {
  58. delay1.trigger(delayTime1);
  59. gate1 = true;
  60. };
  61. };
  62. if( inputs[GATE2_INPUT].active ) {
  63. if(gateTrigger2.process(inputs[GATE2_INPUT].value)) {
  64. delay2.trigger(delayTime2);
  65. gate2 = true;
  66. };
  67. };
  68. if( gate1 && !delay1.process(1.0/gSampleRate) ) {
  69. on1.trigger(length1);
  70. gate1 = false;
  71. };
  72. if( gate2 && !delay2.process(1.0/gSampleRate) ) {
  73. on2.trigger(length2);
  74. gate2 = false;
  75. };
  76. outputs[OUT1_OUTPUT].value = on1.process(1.0/gSampleRate) ? 10.0 : 0.0;
  77. outputs[OUT2_OUTPUT].value = on2.process(1.0/gSampleRate) ? 10.0 : 0.0;
  78. };
  79. struct TrigDelayWidget : ModuleWidget {
  80. TrigDelayWidget(TrigDelay *module);
  81. };
  82. TrigDelayWidget::TrigDelayWidget(TrigDelay *module) : ModuleWidget(module) {
  83. box.size = Vec(15*6, 380);
  84. {
  85. SVGPanel *panel = new SVGPanel();
  86. panel->box.size = box.size;
  87. panel->setBackground(SVG::load(assetPlugin(plugin,"res/TrigDelay.svg")));
  88. addChild(panel);
  89. }
  90. addChild(Widget::create<MLScrew>(Vec(15, 0)));
  91. addChild(Widget::create<MLScrew>(Vec(15, 365)));
  92. addParam(ParamWidget::create<SmallBlueMLKnob>(Vec(12, 69), module, TrigDelay::DELAY1_PARAM, 0.0, 2.0, 0.0));
  93. addInput(Port::create<MLPort>(Vec(52, 70), Port::INPUT, module, TrigDelay::DELAY1_INPUT));
  94. addParam(ParamWidget::create<SmallBlueMLKnob>(Vec(12, 112), module, TrigDelay::LENGTH1_PARAM, minLength, 2.0, 0.1));
  95. addInput(Port::create<MLPort>(Vec(52, 113), Port::INPUT, module, TrigDelay::LENGTH1_INPUT));
  96. addInput(Port::create<MLPort>(Vec(12, 164), Port::INPUT, module, TrigDelay::GATE1_INPUT));
  97. addOutput(Port::create<MLPort>(Vec(52, 164), Port::OUTPUT, module, TrigDelay::OUT1_OUTPUT));
  98. addParam(ParamWidget::create<SmallBlueMLKnob>(Vec(12, 153 + 69), module, TrigDelay::DELAY2_PARAM, 0.0, 2.0, 0.0));
  99. addInput(Port::create<MLPort>(Vec(52, 152 + 71), Port::INPUT, module, TrigDelay::DELAY2_INPUT));
  100. addParam(ParamWidget::create<SmallBlueMLKnob>(Vec(12, 153 + 112), module, TrigDelay::LENGTH2_PARAM, minLength, 2.0, 0.1));
  101. addInput(Port::create<MLPort>(Vec(52, 152 + 114), Port::INPUT, module, TrigDelay::LENGTH2_INPUT));
  102. addInput(Port::create<MLPort>(Vec(12, 152 + 165), Port::INPUT, module, TrigDelay::GATE2_INPUT));
  103. addOutput(Port::create<MLPort>(Vec(52, 152 + 165), Port::OUTPUT, module, TrigDelay::OUT2_OUTPUT));
  104. }
  105. } // namespace rack_plugin_ML_modules
  106. using namespace rack_plugin_ML_modules;
  107. RACK_PLUGIN_MODEL_INIT(ML_modules, TrigDelay) {
  108. Model *modelTrigDelay = Model::create<TrigDelay, TrigDelayWidget>("ML modules", "TrigDelay", "Trigger Delay", UTILITY_TAG, DELAY_TAG);
  109. return modelTrigDelay;
  110. }