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.

130 lines
3.7KB

  1. #include "DGate.hpp"
  2. void DGate::onReset() {
  3. _trigger.reset();
  4. _triggerOuptutPulseGen.process(10.0);
  5. _stage = STOPPED_STAGE;
  6. _stageProgress = 0.0;
  7. }
  8. void DGate::step() {
  9. float envelope = 0.0;
  10. bool complete = false;
  11. if (
  12. _trigger.process(params[TRIGGER_PARAM].value + inputs[TRIGGER_INPUT].value) ||
  13. (_firstStep && _triggerOnLoad && _shouldTriggerOnLoad && params[LOOP_PARAM].value <= 0.0)
  14. ) {
  15. _stage = DELAY_STAGE;
  16. _stageProgress = 0.0;
  17. }
  18. else {
  19. switch (_stage) {
  20. case STOPPED_STAGE: {
  21. break;
  22. }
  23. case DELAY_STAGE: {
  24. if (stepStage(params[DELAY_PARAM])) {
  25. _stage = GATE_STAGE;
  26. _stageProgress = 0.0;
  27. }
  28. break;
  29. }
  30. case GATE_STAGE: {
  31. if (stepStage(params[GATE_PARAM])) {
  32. complete = true;
  33. if (params[LOOP_PARAM].value <= 0.0 || _trigger.isHigh()) {
  34. _stage = DELAY_STAGE;
  35. _stageProgress = 0.0;
  36. }
  37. else {
  38. _stage = STOPPED_STAGE;
  39. }
  40. }
  41. else {
  42. envelope = 1.0;
  43. }
  44. break;
  45. }
  46. }
  47. }
  48. outputs[GATE_OUTPUT].value = envelope * 10.0;
  49. if (complete) {
  50. _triggerOuptutPulseGen.trigger(0.001);
  51. }
  52. outputs[END_OUTPUT].value = _triggerOuptutPulseGen.process(engineGetSampleTime()) ? 5.0 : 0.0;
  53. lights[DELAY_LIGHT].value = _stage == DELAY_STAGE;
  54. lights[GATE_LIGHT].value = _stage == GATE_STAGE;
  55. _firstStep = false;
  56. }
  57. bool DGate::stepStage(Param& knob) {
  58. float t = knob.value;
  59. t = pow(t, 2);
  60. t = fmaxf(t, 0.001);
  61. t *= 10.0;
  62. _stageProgress += engineGetSampleTime() / t;
  63. return _stageProgress > 1.0;
  64. }
  65. struct DGateWidget : ModuleWidget {
  66. static constexpr int hp = 3;
  67. DGateWidget(DGate* module) : ModuleWidget(module) {
  68. box.size = Vec(RACK_GRID_WIDTH * hp, RACK_GRID_HEIGHT);
  69. {
  70. SVGPanel *panel = new SVGPanel();
  71. panel->box.size = box.size;
  72. panel->setBackground(SVG::load(assetPlugin(plugin, "res/DGate.svg")));
  73. addChild(panel);
  74. }
  75. addChild(Widget::create<ScrewSilver>(Vec(0, 0)));
  76. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 15, 365)));
  77. // generated by svg_widgets.rb
  78. auto delayParamPosition = Vec(8.0, 33.0);
  79. auto gateParamPosition = Vec(8.0, 92.0);
  80. auto loopParamPosition = Vec(15.0, 144.5);
  81. auto triggerParamPosition = Vec(13.5, 191.0);
  82. auto triggerInputPosition = Vec(10.5, 213.0);
  83. auto gateOutputPosition = Vec(10.5, 252.0);
  84. auto endOutputPosition = Vec(10.5, 287.0);
  85. auto delayLightPosition = Vec(20.8, 65.0);
  86. auto gateLightPosition = Vec(20.8, 124.0);
  87. // end generated by svg_widgets.rb
  88. addParam(ParamWidget::create<Knob29>(delayParamPosition, module, DGate::DELAY_PARAM, 0.0, 1.0, 0.0));
  89. addParam(ParamWidget::create<Knob29>(gateParamPosition, module, DGate::GATE_PARAM, 0.0, 1.0, 0.32));
  90. addParam(ParamWidget::create<SliderSwitch2State14>(loopParamPosition, module, DGate::LOOP_PARAM, 0.0, 1.0, 1.0));
  91. addParam(ParamWidget::create<Button18>(triggerParamPosition, module, DGate::TRIGGER_PARAM, 0.0, 1.0, 0.0));
  92. addInput(Port::create<Port24>(triggerInputPosition, Port::INPUT, module, DGate::TRIGGER_INPUT));
  93. addOutput(Port::create<Port24>(gateOutputPosition, Port::OUTPUT, module, DGate::GATE_OUTPUT));
  94. addOutput(Port::create<Port24>(endOutputPosition, Port::OUTPUT, module, DGate::END_OUTPUT));
  95. addChild(ModuleLightWidget::create<TinyLight<GreenLight>>(delayLightPosition, module, DGate::DELAY_LIGHT));
  96. addChild(ModuleLightWidget::create<TinyLight<GreenLight>>(gateLightPosition, module, DGate::GATE_LIGHT));
  97. }
  98. void appendContextMenu(Menu* menu) override {
  99. DGate* dgate = dynamic_cast<DGate*>(module);
  100. assert(dgate);
  101. menu->addChild(new MenuLabel());
  102. menu->addChild(new TriggerOnLoadMenuItem(dgate, "Resume Loop on Load"));
  103. }
  104. };
  105. RACK_PLUGIN_MODEL_INIT(Bogaudio, DGate) {
  106. Model *modelDGate = createModel<DGate, DGateWidget>("Bogaudio-DGate", "DGate", "trigger-to-gate with delay");
  107. return modelDGate;
  108. }