Switch.cpp (4908B)
1 2 #include "Switch.hpp" 3 4 void bogaudio::Switch::reset() { 5 for (int i = 0; i < _channels; ++i) { 6 _trigger[i].reset(); 7 _latchedHigh[i] = false; 8 } 9 } 10 11 int bogaudio::Switch::channels() { 12 return inputs[GATE_INPUT].getChannels(); 13 } 14 15 void bogaudio::Switch::channelsChanged(int before, int after) { 16 for (; before < after; ++before) { 17 _trigger[before].reset(); 18 } 19 } 20 21 void bogaudio::Switch::modulate() { 22 _latch = params[LATCH_PARAM].getValue() > 0.5f; 23 } 24 25 void bogaudio::Switch::processAlways(const ProcessArgs& args) { 26 _high1LightSum = 0; 27 _low1LightSum = 0; 28 _high2LightSum = 0; 29 _low2LightSum = 0; 30 } 31 32 void bogaudio::Switch::processChannel(const ProcessArgs& args, int c) { 33 bool triggered = _trigger[c].process(10.0f*params[GATE_PARAM].getValue() + inputs[GATE_INPUT].getVoltage(c)); 34 if (_latch) { 35 if (triggered) { 36 _latchedHigh[c] = !_latchedHigh[c]; 37 } 38 } 39 else { 40 _latchedHigh[c] = false; 41 } 42 43 if (_latchedHigh[c] || (!_latch && _trigger[c].isHigh())) { 44 ++_high1LightSum; 45 ++_high2LightSum; 46 47 if (_channels == 1) { 48 outputs[OUT1_OUTPUT].setChannels(inputs[HIGH1_INPUT].getChannels()); 49 outputs[OUT1_OUTPUT].writeVoltages(inputs[HIGH1_INPUT].getVoltages()); 50 51 outputs[OUT2_OUTPUT].setChannels(inputs[HIGH2_INPUT].getChannels()); 52 outputs[OUT2_OUTPUT].writeVoltages(inputs[HIGH2_INPUT].getVoltages()); 53 } 54 else { 55 outputs[OUT1_OUTPUT].setChannels(_channels); 56 outputs[OUT1_OUTPUT].setVoltage(inputs[HIGH1_INPUT].getPolyVoltage(c), c); 57 58 outputs[OUT2_OUTPUT].setChannels(_channels); 59 outputs[OUT2_OUTPUT].setVoltage(inputs[HIGH2_INPUT].getPolyVoltage(c), c); 60 } 61 } 62 else { 63 ++_low1LightSum; 64 ++_low2LightSum; 65 66 if (_channels == 1) { 67 outputs[OUT1_OUTPUT].setChannels(inputs[LOW1_INPUT].getChannels()); 68 outputs[OUT1_OUTPUT].writeVoltages(inputs[LOW1_INPUT].getVoltages()); 69 70 outputs[OUT2_OUTPUT].setChannels(inputs[LOW2_INPUT].getChannels()); 71 outputs[OUT2_OUTPUT].writeVoltages(inputs[LOW2_INPUT].getVoltages()); 72 } 73 else { 74 outputs[OUT1_OUTPUT].setChannels(_channels); 75 outputs[OUT1_OUTPUT].setVoltage(inputs[LOW1_INPUT].getPolyVoltage(c), c); 76 77 outputs[OUT2_OUTPUT].setChannels(_channels); 78 outputs[OUT2_OUTPUT].setVoltage(inputs[LOW2_INPUT].getPolyVoltage(c), c); 79 } 80 } 81 } 82 83 void bogaudio::Switch::postProcessAlways(const ProcessArgs& args) { 84 lights[HIGH1_LIGHT].value = _high1LightSum * _inverseChannels; 85 lights[LOW1_LIGHT].value = _low1LightSum * _inverseChannels; 86 lights[HIGH2_LIGHT].value = _high2LightSum * _inverseChannels; 87 lights[LOW2_LIGHT].value = _low2LightSum * _inverseChannels; 88 } 89 90 struct SwitchWidget : SaveLatchToPatchModuleWidget { 91 static constexpr int hp = 3; 92 93 SwitchWidget(bogaudio::Switch* module) { 94 setModule(module); 95 box.size = Vec(RACK_GRID_WIDTH * hp, RACK_GRID_HEIGHT); 96 setPanel(box.size, "Switch"); 97 createScrews(); 98 99 // generated by svg_widgets.rb 100 auto gateParamPosition = Vec(13.5, 22.0); 101 auto latchParamPosition = Vec(31.5, 84.0); 102 103 auto gateInputPosition = Vec(10.5, 44.0); 104 auto high1InputPosition = Vec(10.5, 100.0); 105 auto low1InputPosition = Vec(10.5, 136.0); 106 auto high2InputPosition = Vec(10.5, 217.0); 107 auto low2InputPosition = Vec(10.5, 253.0); 108 109 auto out1OutputPosition = Vec(10.5, 174.0); 110 auto out2OutputPosition = Vec(10.5, 291.0); 111 112 auto high1LightPosition = Vec(7.5, 126.3); 113 auto low1LightPosition = Vec(7.5, 162.3); 114 auto high2LightPosition = Vec(7.5, 243.3); 115 auto low2LightPosition = Vec(7.5, 279.3); 116 // end generated by svg_widgets.rb 117 118 addParam(createParam<Button18>(gateParamPosition, module, bogaudio::Switch::GATE_PARAM)); 119 addParam(createParam<IndicatorButtonGreen9>(latchParamPosition, module, bogaudio::Switch::LATCH_PARAM)); 120 121 addInput(createInput<Port24>(gateInputPosition, module, bogaudio::Switch::GATE_INPUT)); 122 addInput(createInput<Port24>(high1InputPosition, module, bogaudio::Switch::HIGH1_INPUT)); 123 addInput(createInput<Port24>(low1InputPosition, module, bogaudio::Switch::LOW1_INPUT)); 124 addInput(createInput<Port24>(high2InputPosition, module, bogaudio::Switch::HIGH2_INPUT)); 125 addInput(createInput<Port24>(low2InputPosition, module, bogaudio::Switch::LOW2_INPUT)); 126 127 addOutput(createOutput<Port24>(out1OutputPosition, module, bogaudio::Switch::OUT1_OUTPUT)); 128 addOutput(createOutput<Port24>(out2OutputPosition, module, bogaudio::Switch::OUT2_OUTPUT)); 129 130 addChild(createLight<BGSmallLight<GreenLight>>(high1LightPosition, module, bogaudio::Switch::HIGH1_LIGHT)); 131 addChild(createLight<BGSmallLight<GreenLight>>(low1LightPosition, module, bogaudio::Switch::LOW1_LIGHT)); 132 addChild(createLight<BGSmallLight<GreenLight>>(high2LightPosition, module, bogaudio::Switch::HIGH2_LIGHT)); 133 addChild(createLight<BGSmallLight<GreenLight>>(low2LightPosition, module, bogaudio::Switch::LOW2_LIGHT)); 134 } 135 }; 136 137 Model* modelSwitch = bogaudio::createModel<bogaudio::Switch, SwitchWidget>("Bogaudio-Switch", "SWITCH", "2-way signal router", "Switch", "Polyphonic");