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.

164 lines
4.9KB

  1. #include "AudibleInstruments.hpp"
  2. struct Branches : Module {
  3. enum ParamIds {
  4. THRESHOLD1_PARAM,
  5. THRESHOLD2_PARAM,
  6. MODE1_PARAM,
  7. MODE2_PARAM,
  8. NUM_PARAMS
  9. };
  10. enum InputIds {
  11. IN1_INPUT,
  12. IN2_INPUT,
  13. P1_INPUT,
  14. P2_INPUT,
  15. NUM_INPUTS
  16. };
  17. enum OutputIds {
  18. OUT1A_OUTPUT,
  19. OUT2A_OUTPUT,
  20. OUT1B_OUTPUT,
  21. OUT2B_OUTPUT,
  22. NUM_OUTPUTS
  23. };
  24. enum LightIds {
  25. MODE1_LIGHT,
  26. MODE2_LIGHT,
  27. STATE1_POS_LIGHT, STATE1_NEG_LIGHT,
  28. STATE2_POS_LIGHT, STATE2_NEG_LIGHT,
  29. NUM_LIGHTS
  30. };
  31. SchmittTrigger gateTriggers[2];
  32. SchmittTrigger modeTriggers[2];
  33. bool modes[2] = {};
  34. bool outcomes[2] = {};
  35. Branches() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  36. json_t *dataToJson() override {
  37. json_t *rootJ = json_object();
  38. json_t *modesJ = json_array();
  39. for (int i = 0; i < 2; i++) {
  40. json_array_insert_new(modesJ, i, json_boolean(modes[i]));
  41. }
  42. json_object_set_new(rootJ, "modes", modesJ);
  43. return rootJ;
  44. }
  45. void dataFromJson(json_t *rootJ) override {
  46. json_t *modesJ = json_object_get(rootJ, "modes");
  47. if (modesJ) {
  48. for (int i = 0; i < 2; i++) {
  49. json_t *modeJ = json_array_get(modesJ, i);
  50. if (modeJ)
  51. modes[i] = json_boolean_value(modeJ);
  52. }
  53. }
  54. }
  55. void step() override;
  56. void onReset() override {
  57. for (int i = 0; i < 2; i++) {
  58. modes[i] = false;
  59. outcomes[i] = false;
  60. }
  61. }
  62. };
  63. void Branches::step() {
  64. float gate = 0.0;
  65. for (int i = 0; i < 2; i++) {
  66. // mode button
  67. if (modeTriggers[i].process(params[MODE1_PARAM + i].value))
  68. modes[i] = !modes[i];
  69. if (inputs[IN1_INPUT + i].active)
  70. gate = inputs[IN1_INPUT + i].value;
  71. if (gateTriggers[i].process(gate)) {
  72. // trigger
  73. float r = randomUniform();
  74. float threshold = clamp(params[THRESHOLD1_PARAM + i].value + inputs[P1_INPUT + i].value / 10.f, 0.f, 1.f);
  75. bool toss = (r < threshold);
  76. if (!modes[i]) {
  77. // direct modes
  78. outcomes[i] = toss;
  79. }
  80. else {
  81. // toggle modes
  82. outcomes[i] = (outcomes[i] != toss);
  83. }
  84. if (!outcomes[i])
  85. lights[STATE1_POS_LIGHT + 2*i].value = 1.0;
  86. else
  87. lights[STATE1_NEG_LIGHT + 2*i].value = 1.0;
  88. }
  89. lights[STATE1_POS_LIGHT + 2*i].value *= 1.0 - engineGetSampleTime() * 15.0;
  90. lights[STATE1_NEG_LIGHT + 2*i].value *= 1.0 - engineGetSampleTime() * 15.0;
  91. lights[MODE1_LIGHT + i].value = modes[i] ? 1.0 : 0.0;
  92. outputs[OUT1A_OUTPUT + i].value = outcomes[i] ? 0.0 : gate;
  93. outputs[OUT1B_OUTPUT + i].value = outcomes[i] ? gate : 0.0;
  94. }
  95. }
  96. struct BranchesWidget : ModuleWidget {
  97. BranchesWidget(Branches *module) : ModuleWidget(module) {
  98. setPanel(SVG::load(assetPlugin(pluginInstance, "res/Branches.svg")));
  99. addChild(createWidget<ScrewSilver>(Vec(15, 0)));
  100. addChild(createWidget<ScrewSilver>(Vec(15, 365)));
  101. addParam(createParam<Rogan1PSRed>(Vec(24, 64), module, Branches::THRESHOLD1_PARAM, 0.0, 1.0, 0.5));
  102. addParam(createParam<TL1105>(Vec(69, 58), module, Branches::MODE1_PARAM, 0.0, 1.0, 0.0));
  103. addInput(createPort<PJ301MPort>(Vec(9, 122), PortWidget::INPUT, module, Branches::IN1_INPUT));
  104. addInput(createPort<PJ301MPort>(Vec(55, 122), PortWidget::INPUT, module, Branches::P1_INPUT));
  105. addOutput(createPort<PJ301MPort>(Vec(9, 160), PortWidget::OUTPUT, module, Branches::OUT1A_OUTPUT));
  106. addOutput(createPort<PJ301MPort>(Vec(55, 160), PortWidget::OUTPUT, module, Branches::OUT1B_OUTPUT));
  107. addParam(createParam<Rogan1PSGreen>(Vec(24, 220), module, Branches::THRESHOLD2_PARAM, 0.0, 1.0, 0.5));
  108. addParam(createParam<TL1105>(Vec(69, 214), module, Branches::MODE2_PARAM, 0.0, 1.0, 0.0));
  109. addInput(createPort<PJ301MPort>(Vec(9, 278), PortWidget::INPUT, module, Branches::IN2_INPUT));
  110. addInput(createPort<PJ301MPort>(Vec(55, 278), PortWidget::INPUT, module, Branches::P2_INPUT));
  111. addOutput(createPort<PJ301MPort>(Vec(9, 316), PortWidget::OUTPUT, module, Branches::OUT2A_OUTPUT));
  112. addOutput(createPort<PJ301MPort>(Vec(55, 316), PortWidget::OUTPUT, module, Branches::OUT2B_OUTPUT));
  113. addChild(createLight<SmallLight<GreenRedLight>>(Vec(40, 169), module, Branches::STATE1_POS_LIGHT));
  114. addChild(createLight<SmallLight<GreenRedLight>>(Vec(40, 325), module, Branches::STATE2_POS_LIGHT));
  115. }
  116. void appendContextMenu(Menu *menu) override {
  117. Branches *branches = dynamic_cast<Branches*>(module);
  118. assert(branches);
  119. struct BranchesModeItem : MenuItem {
  120. Branches *branches;
  121. int channel;
  122. void onAction(const event::Action &e) override {
  123. branches->modes[channel] ^= 1;
  124. }
  125. void step() override {
  126. rightText = branches->modes[channel] ? "Toggle" : "Latch";
  127. MenuItem::step();
  128. }
  129. };
  130. menu->addChild(construct<MenuLabel>());
  131. menu->addChild(construct<MenuLabel>(&MenuLabel::text, "Channels"));
  132. menu->addChild(construct<BranchesModeItem>(&MenuItem::text, "Channel 1 modes", &BranchesModeItem::branches, branches, &BranchesModeItem::channel, 0));
  133. menu->addChild(construct<BranchesModeItem>(&MenuItem::text, "Channel 2 modes", &BranchesModeItem::branches, branches, &BranchesModeItem::channel, 1));
  134. }
  135. };
  136. Model *modelBranches = createModel<Branches, BranchesWidget>("Branches");