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.

126 lines
4.0KB

  1. #include "HetrickCV.hpp"
  2. namespace rack_plugin_HetrickCV {
  3. struct Comparator : Module
  4. {
  5. enum ParamIds
  6. {
  7. AMOUNT_PARAM,
  8. SCALE_PARAM,
  9. NUM_PARAMS
  10. };
  11. enum InputIds
  12. {
  13. MAIN_INPUT,
  14. AMOUNT_INPUT,
  15. NUM_INPUTS
  16. };
  17. enum OutputIds
  18. {
  19. GT_GATE_OUTPUT,
  20. GT_TRIG_OUTPUT,
  21. LT_GATE_OUTPUT,
  22. LT_TRIG_OUTPUT,
  23. ZEROX_OUTPUT,
  24. NUM_OUTPUTS
  25. };
  26. enum LightIds
  27. {
  28. GT_LIGHT,
  29. LT_LIGHT,
  30. ZEROX_LIGHT,
  31. NUM_LIGHTS
  32. };
  33. Comparator() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS)
  34. {
  35. }
  36. TriggerGenWithSchmitt ltTrig, gtTrig;
  37. void step() override;
  38. // For more advanced Module features, read Rack's engine.hpp header file
  39. // - toJson, fromJson: serialization of internal data
  40. // - onSampleRateChange: event triggered by a change of sample rate
  41. // - reset, randomize: implements special behavior when user clicks these from the context menu
  42. };
  43. void Comparator::step()
  44. {
  45. float input = inputs[MAIN_INPUT].value;
  46. float compare = params[AMOUNT_PARAM].value + (inputs[AMOUNT_INPUT].value * params[SCALE_PARAM].value);
  47. compare = clampf(compare, -5.0f, 5.0f);
  48. const bool greaterThan = (input > compare);
  49. const bool lessThan = (input < compare);
  50. outputs[GT_TRIG_OUTPUT].value = gtTrig.process(greaterThan) ? 5.0f : 0.0f;
  51. outputs[LT_TRIG_OUTPUT].value = ltTrig.process(lessThan) ? 5.0f : 0.0f;
  52. outputs[GT_GATE_OUTPUT].value = greaterThan ? 5.0f : 0.0f;
  53. outputs[LT_GATE_OUTPUT].value = lessThan ? 5.0f : 0.0f;
  54. float allTrigs = outputs[GT_TRIG_OUTPUT].value + outputs[LT_TRIG_OUTPUT].value;
  55. allTrigs = clampf(allTrigs, 0.0f, 5.0f);
  56. outputs[ZEROX_OUTPUT].value = allTrigs;
  57. lights[GT_LIGHT].setBrightnessSmooth(outputs[GT_GATE_OUTPUT].value);
  58. lights[LT_LIGHT].setBrightnessSmooth(outputs[LT_GATE_OUTPUT].value);
  59. lights[ZEROX_LIGHT].setBrightnessSmooth(allTrigs);
  60. }
  61. struct ComparatorWidget : ModuleWidget { ComparatorWidget(Comparator *module); };
  62. ComparatorWidget::ComparatorWidget(Comparator* module) : ModuleWidget(module)
  63. {
  64. box.size = Vec(6 * RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  65. {
  66. auto *panel = new SVGPanel();
  67. panel->box.size = box.size;
  68. panel->setBackground(SVG::load(assetPlugin(plugin, "res/Comparator.svg")));
  69. addChild(panel);
  70. }
  71. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  72. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 30, 0)));
  73. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  74. addChild(Widget::create<ScrewSilver>(Vec(box.size.x - 30, 365)));
  75. //////PARAMS//////
  76. addParam(ParamWidget::create<Davies1900hBlackKnob>(Vec(27, 62), module, Comparator::AMOUNT_PARAM, -5.0, 5.0, 0.0));
  77. addParam(ParamWidget::create<Trimpot>(Vec(36, 112), module, Comparator::SCALE_PARAM, -1.0, 1.0, 1.0));
  78. //////INPUTS//////
  79. addInput(Port::create<PJ301MPort>(Vec(33, 195), Port::INPUT, module, Comparator::MAIN_INPUT));
  80. addInput(Port::create<PJ301MPort>(Vec(33, 145), Port::INPUT, module, Comparator::AMOUNT_INPUT));
  81. //////OUTPUTS//////
  82. addOutput(Port::create<PJ301MPort>(Vec(12, 285), Port::OUTPUT, module, Comparator::LT_GATE_OUTPUT));
  83. addOutput(Port::create<PJ301MPort>(Vec(53, 285), Port::OUTPUT, module, Comparator::GT_GATE_OUTPUT));
  84. addOutput(Port::create<PJ301MPort>(Vec(12, 315), Port::OUTPUT, module, Comparator::LT_TRIG_OUTPUT));
  85. addOutput(Port::create<PJ301MPort>(Vec(53, 315), Port::OUTPUT, module, Comparator::GT_TRIG_OUTPUT));
  86. addOutput(Port::create<PJ301MPort>(Vec(32.5, 245), Port::OUTPUT, module, Comparator::ZEROX_OUTPUT));
  87. //////BLINKENLIGHTS//////
  88. addChild(ModuleLightWidget::create<SmallLight<RedLight>>(Vec(22, 275), module, Comparator::LT_LIGHT));
  89. addChild(ModuleLightWidget::create<SmallLight<GreenLight>>(Vec(62, 275), module, Comparator::GT_LIGHT));
  90. addChild(ModuleLightWidget::create<SmallLight<RedLight>>(Vec(42, 275), module, Comparator::ZEROX_LIGHT));
  91. }
  92. } // namespace rack_plugin_HetrickCV
  93. using namespace rack_plugin_HetrickCV;
  94. RACK_PLUGIN_MODEL_INIT(HetrickCV, Comparator) {
  95. Model *modelComparator = Model::create<Comparator, ComparatorWidget>("HetrickCV", "Comparator", "Comparator", LOGIC_TAG);
  96. return modelComparator;
  97. }