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.

98 lines
3.8KB

  1. #include "ADSR.hpp"
  2. void ADSR::onReset() {
  3. _gateTrigger.reset();
  4. _envelope.reset();
  5. _modulationStep = modulationSteps;
  6. }
  7. void ADSR::onSampleRateChange() {
  8. _envelope.setSampleRate(engineGetSampleRate());
  9. _modulationStep = modulationSteps;
  10. }
  11. void ADSR::step() {
  12. lights[LINEAR_LIGHT].value = _linearMode = params[LINEAR_PARAM].value > 0.5f;
  13. if (!(outputs[OUT_OUTPUT].active || inputs[GATE_INPUT].active)) {
  14. return;
  15. }
  16. ++_modulationStep;
  17. if (_modulationStep >= modulationSteps) {
  18. _modulationStep = 0;
  19. _envelope.setAttack(powf(params[ATTACK_PARAM].value, 2.0f) * 10.f);
  20. _envelope.setDecay(powf(params[DECAY_PARAM].value, 2.0f) * 10.f);
  21. _envelope.setSustain(params[SUSTAIN_PARAM].value);
  22. _envelope.setRelease(powf(params[RELEASE_PARAM].value, 2.0f) * 10.f);
  23. _envelope.setLinearShape(_linearMode);
  24. }
  25. _gateTrigger.process(inputs[GATE_INPUT].value);
  26. _envelope.setGate(_gateTrigger.isHigh());
  27. outputs[OUT_OUTPUT].value = _envelope.next() * 10.0f;
  28. lights[ATTACK_LIGHT].value = _envelope.isStage(bogaudio::dsp::ADSR::ATTACK_STAGE);
  29. lights[DECAY_LIGHT].value = _envelope.isStage(bogaudio::dsp::ADSR::DECAY_STAGE);
  30. lights[SUSTAIN_LIGHT].value = _envelope.isStage(bogaudio::dsp::ADSR::SUSTAIN_STAGE);
  31. lights[RELEASE_LIGHT].value = _envelope.isStage(bogaudio::dsp::ADSR::RELEASE_STAGE);
  32. }
  33. struct ADSRWidget : ModuleWidget {
  34. static constexpr int hp = 3;
  35. ADSRWidget(ADSR* module) : ModuleWidget(module) {
  36. box.size = Vec(RACK_GRID_WIDTH * hp, RACK_GRID_HEIGHT);
  37. {
  38. SVGPanel *panel = new SVGPanel();
  39. panel->box.size = box.size;
  40. panel->setBackground(SVG::load(assetPlugin(plugin, "res/ADSR.svg")));
  41. addChild(panel);
  42. }
  43. addChild(Widget::create<ScrewSilver>(Vec(0, 0)));
  44. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 15, 365)));
  45. // generated by svg_widgets.rb
  46. auto attackParamPosition = Vec(8.0, 33.0);
  47. auto decayParamPosition = Vec(8.0, 90.0);
  48. auto sustainParamPosition = Vec(8.0, 147.0);
  49. auto releaseParamPosition = Vec(8.0, 204.0);
  50. auto linearParamPosition = Vec(32.0, 245.7);
  51. auto gateInputPosition = Vec(10.5, 265.0);
  52. auto outOutputPosition = Vec(10.5, 303.0);
  53. auto attackLightPosition = Vec(20.8, 65.0);
  54. auto decayLightPosition = Vec(20.8, 122.0);
  55. auto sustainLightPosition = Vec(20.8, 179.0);
  56. auto releaseLightPosition = Vec(20.8, 236.0);
  57. auto linearLightPosition = Vec(4.0, 247.0);
  58. // end generated by svg_widgets.rb
  59. addParam(ParamWidget::create<Knob29>(attackParamPosition, module, ADSR::ATTACK_PARAM, 0.0, 1.0, 0.12));
  60. addParam(ParamWidget::create<Knob29>(decayParamPosition, module, ADSR::DECAY_PARAM, 0.0, 1.0, 0.31623));
  61. addParam(ParamWidget::create<Knob29>(sustainParamPosition, module, ADSR::SUSTAIN_PARAM, 0.0, 1.0, 1.0));
  62. addParam(ParamWidget::create<Knob29>(releaseParamPosition, module, ADSR::RELEASE_PARAM, 0.0, 1.0, 0.31623));
  63. addParam(ParamWidget::create<StatefulButton9>(linearParamPosition, module, ADSR::LINEAR_PARAM, 0.0, 1.0, 0.0));
  64. addInput(Port::create<Port24>(gateInputPosition, Port::INPUT, module, ADSR::GATE_INPUT));
  65. addOutput(Port::create<Port24>(outOutputPosition, Port::OUTPUT, module, ADSR::OUT_OUTPUT));
  66. addChild(ModuleLightWidget::create<TinyLight<GreenLight>>(attackLightPosition, module, ADSR::ATTACK_LIGHT));
  67. addChild(ModuleLightWidget::create<TinyLight<GreenLight>>(decayLightPosition, module, ADSR::DECAY_LIGHT));
  68. addChild(ModuleLightWidget::create<TinyLight<GreenLight>>(sustainLightPosition, module, ADSR::SUSTAIN_LIGHT));
  69. addChild(ModuleLightWidget::create<TinyLight<GreenLight>>(releaseLightPosition, module, ADSR::RELEASE_LIGHT));
  70. addChild(ModuleLightWidget::create<SmallLight<GreenLight>>(linearLightPosition, module, ADSR::LINEAR_LIGHT));
  71. }
  72. };
  73. RACK_PLUGIN_MODEL_INIT(Bogaudio, ADSR) {
  74. Model *modelADSR = createModel<ADSR, ADSRWidget>("Bogaudio-ADSR", "ADSR", "utility envelope", ENVELOPE_GENERATOR_TAG);
  75. return modelADSR;
  76. }