PolyOff16.cpp (12321B)
1 2 #include "PolyOff16.hpp" 3 4 #define OFFSET_FIRST "offset_first" 5 6 json_t* PolyOff16::saveToJson(json_t* root) { 7 root = OutputRangeModule<BGModule>::saveToJson(root); 8 json_object_set_new(root, OFFSET_FIRST, json_boolean(_offsetFirst)); 9 return root; 10 } 11 12 void PolyOff16::loadFromJson(json_t* root) { 13 OutputRangeModule<BGModule>::loadFromJson(root); 14 json_t* of = json_object_get(root, OFFSET_FIRST); 15 if (of) { 16 _offsetFirst = json_boolean_value(of); 17 } 18 } 19 20 void PolyOff16::processAll(const ProcessArgs& args) { 21 int cn = 1; 22 if (inputs[IN_INPUT].isConnected()) { 23 cn = clamp(inputs[IN_INPUT].getChannels(), 1, 16); 24 outputs[OUT_OUTPUT].setChannels(cn); 25 26 for (int c = 0; c < cn; ++c) { 27 float offset = clamp(params[OFFSET1_PARAM + 2 * c].getValue(), -1.0f, 1.0f); 28 if (inputs[CV1_INPUT + c].isConnected()) { 29 offset *= clamp(inputs[CV1_INPUT + c].getVoltage() / 5.0f, -1.0f, 1.0f); 30 } 31 offset += _rangeOffset; 32 offset *= _rangeScale; 33 34 float scale = clamp(params[SCALE1_PARAM + 2 * c].getValue(), -1.0f, 1.0f); 35 36 float out = inputs[IN_INPUT].getPolyVoltage(c); 37 if (_offsetFirst) { 38 out += offset; 39 out *= scale; 40 } 41 else { 42 out *= scale; 43 out += offset; 44 } 45 outputs[OUT_OUTPUT].setVoltage(clamp(out, -12.0f, 12.0f), c); 46 } 47 } 48 else { 49 cn = clamp(params[CHANNELS_PARAM].getValue(), 1.0f, 16.0f); 50 outputs[OUT_OUTPUT].setChannels(cn); 51 52 for (int c = 0; c < cn; ++c) { 53 float offset = clamp(params[OFFSET1_PARAM + 2 * c].getValue(), -1.0f, 1.0f); 54 offset += _rangeOffset; 55 offset *= _rangeScale; 56 57 float scale = clamp(params[SCALE1_PARAM + 2 * c].getValue(), -1.0f, 1.0f); 58 59 float out = inputs[CV1_INPUT + c].getVoltage(); 60 if (_offsetFirst) { 61 out += offset; 62 out *= scale; 63 } 64 else { 65 out *= scale; 66 out += offset; 67 } 68 outputs[OUT_OUTPUT].setVoltage(clamp(out, -12.0f, 12.0f), c); 69 } 70 } 71 72 for (int c = 0; c < 16; ++c) { 73 lights[CHANNEL1_LIGHT + c].value = (c < cn) * 1.0f; 74 } 75 } 76 77 struct PolyOff16Widget : BGModuleWidget { 78 static constexpr int hp = 16; 79 80 PolyOff16Widget(PolyOff16* module) { 81 setModule(module); 82 box.size = Vec(RACK_GRID_WIDTH * hp, RACK_GRID_HEIGHT); 83 setPanel(box.size, "PolyOff16"); 84 createScrews(); 85 86 // generated by svg_widgets.rb 87 auto offset1ParamPosition = Vec(62.5, 33.0); 88 auto scale1ParamPosition = Vec(93.5, 33.0); 89 auto offset2ParamPosition = Vec(62.5, 68.0); 90 auto scale2ParamPosition = Vec(93.5, 68.0); 91 auto offset3ParamPosition = Vec(62.5, 103.0); 92 auto scale3ParamPosition = Vec(93.5, 103.0); 93 auto offset4ParamPosition = Vec(62.5, 138.0); 94 auto scale4ParamPosition = Vec(93.5, 138.0); 95 auto offset5ParamPosition = Vec(62.5, 173.0); 96 auto scale5ParamPosition = Vec(93.5, 173.0); 97 auto offset6ParamPosition = Vec(62.5, 208.0); 98 auto scale6ParamPosition = Vec(93.5, 208.0); 99 auto offset7ParamPosition = Vec(62.5, 243.0); 100 auto scale7ParamPosition = Vec(93.5, 243.0); 101 auto offset8ParamPosition = Vec(62.5, 278.0); 102 auto scale8ParamPosition = Vec(93.5, 278.0); 103 auto offset9ParamPosition = Vec(182.5, 33.0); 104 auto scale9ParamPosition = Vec(213.5, 33.0); 105 auto offset10ParamPosition = Vec(182.5, 68.0); 106 auto scale10ParamPosition = Vec(213.5, 68.0); 107 auto offset11ParamPosition = Vec(182.5, 103.0); 108 auto scale11ParamPosition = Vec(213.5, 103.0); 109 auto offset12ParamPosition = Vec(182.5, 138.0); 110 auto scale12ParamPosition = Vec(213.5, 138.0); 111 auto offset13ParamPosition = Vec(182.5, 173.0); 112 auto scale13ParamPosition = Vec(213.5, 173.0); 113 auto offset14ParamPosition = Vec(182.5, 208.0); 114 auto scale14ParamPosition = Vec(213.5, 208.0); 115 auto offset15ParamPosition = Vec(182.5, 243.0); 116 auto scale15ParamPosition = Vec(213.5, 243.0); 117 auto offset16ParamPosition = Vec(182.5, 278.0); 118 auto scale16ParamPosition = Vec(213.5, 278.0); 119 auto channelsParamPosition = Vec(83.0, 332.0); 120 121 auto cv1InputPosition = Vec(10.5, 29.0); 122 auto cv2InputPosition = Vec(10.5, 64.0); 123 auto cv3InputPosition = Vec(10.5, 99.0); 124 auto cv4InputPosition = Vec(10.5, 134.0); 125 auto cv5InputPosition = Vec(10.5, 169.0); 126 auto cv6InputPosition = Vec(10.5, 204.0); 127 auto cv7InputPosition = Vec(10.5, 239.0); 128 auto cv8InputPosition = Vec(10.5, 274.0); 129 auto cv9InputPosition = Vec(130.5, 29.0); 130 auto cv10InputPosition = Vec(130.5, 64.0); 131 auto cv11InputPosition = Vec(130.5, 99.0); 132 auto cv12InputPosition = Vec(130.5, 134.0); 133 auto cv13InputPosition = Vec(130.5, 169.0); 134 auto cv14InputPosition = Vec(130.5, 204.0); 135 auto cv15InputPosition = Vec(130.5, 239.0); 136 auto cv16InputPosition = Vec(130.5, 274.0); 137 auto inInputPosition = Vec(118.5, 322.0); 138 139 auto outOutputPosition = Vec(149.5, 322.0); 140 141 auto channel1LightPosition = Vec(46.8, 43.0); 142 auto channel2LightPosition = Vec(46.8, 78.0); 143 auto channel3LightPosition = Vec(46.8, 113.0); 144 auto channel4LightPosition = Vec(46.8, 148.0); 145 auto channel5LightPosition = Vec(46.8, 183.0); 146 auto channel6LightPosition = Vec(46.8, 218.0); 147 auto channel7LightPosition = Vec(46.8, 253.0); 148 auto channel8LightPosition = Vec(46.8, 288.0); 149 auto channel9LightPosition = Vec(166.8, 43.0); 150 auto channel10LightPosition = Vec(166.8, 78.0); 151 auto channel11LightPosition = Vec(166.8, 113.0); 152 auto channel12LightPosition = Vec(166.8, 148.0); 153 auto channel13LightPosition = Vec(166.8, 183.0); 154 auto channel14LightPosition = Vec(166.8, 218.0); 155 auto channel15LightPosition = Vec(166.8, 253.0); 156 auto channel16LightPosition = Vec(166.8, 288.0); 157 // end generated by svg_widgets.rb 158 159 addParam(createParam<Knob16>(offset1ParamPosition, module, PolyOff16::OFFSET1_PARAM)); 160 addParam(createParam<Knob16>(scale1ParamPosition, module, PolyOff16::SCALE1_PARAM)); 161 addParam(createParam<Knob16>(offset2ParamPosition, module, PolyOff16::OFFSET2_PARAM)); 162 addParam(createParam<Knob16>(scale2ParamPosition, module, PolyOff16::SCALE2_PARAM)); 163 addParam(createParam<Knob16>(offset3ParamPosition, module, PolyOff16::OFFSET3_PARAM)); 164 addParam(createParam<Knob16>(scale3ParamPosition, module, PolyOff16::SCALE3_PARAM)); 165 addParam(createParam<Knob16>(offset4ParamPosition, module, PolyOff16::OFFSET4_PARAM)); 166 addParam(createParam<Knob16>(scale4ParamPosition, module, PolyOff16::SCALE4_PARAM)); 167 addParam(createParam<Knob16>(offset5ParamPosition, module, PolyOff16::OFFSET5_PARAM)); 168 addParam(createParam<Knob16>(scale5ParamPosition, module, PolyOff16::SCALE5_PARAM)); 169 addParam(createParam<Knob16>(offset6ParamPosition, module, PolyOff16::OFFSET6_PARAM)); 170 addParam(createParam<Knob16>(scale6ParamPosition, module, PolyOff16::SCALE6_PARAM)); 171 addParam(createParam<Knob16>(offset7ParamPosition, module, PolyOff16::OFFSET7_PARAM)); 172 addParam(createParam<Knob16>(scale7ParamPosition, module, PolyOff16::SCALE7_PARAM)); 173 addParam(createParam<Knob16>(offset8ParamPosition, module, PolyOff16::OFFSET8_PARAM)); 174 addParam(createParam<Knob16>(scale8ParamPosition, module, PolyOff16::SCALE8_PARAM)); 175 addParam(createParam<Knob16>(offset9ParamPosition, module, PolyOff16::OFFSET9_PARAM)); 176 addParam(createParam<Knob16>(scale9ParamPosition, module, PolyOff16::SCALE9_PARAM)); 177 addParam(createParam<Knob16>(offset10ParamPosition, module, PolyOff16::OFFSET10_PARAM)); 178 addParam(createParam<Knob16>(scale10ParamPosition, module, PolyOff16::SCALE10_PARAM)); 179 addParam(createParam<Knob16>(offset11ParamPosition, module, PolyOff16::OFFSET11_PARAM)); 180 addParam(createParam<Knob16>(scale11ParamPosition, module, PolyOff16::SCALE11_PARAM)); 181 addParam(createParam<Knob16>(offset12ParamPosition, module, PolyOff16::OFFSET12_PARAM)); 182 addParam(createParam<Knob16>(scale12ParamPosition, module, PolyOff16::SCALE12_PARAM)); 183 addParam(createParam<Knob16>(offset13ParamPosition, module, PolyOff16::OFFSET13_PARAM)); 184 addParam(createParam<Knob16>(scale13ParamPosition, module, PolyOff16::SCALE13_PARAM)); 185 addParam(createParam<Knob16>(offset14ParamPosition, module, PolyOff16::OFFSET14_PARAM)); 186 addParam(createParam<Knob16>(scale14ParamPosition, module, PolyOff16::SCALE14_PARAM)); 187 addParam(createParam<Knob16>(offset15ParamPosition, module, PolyOff16::OFFSET15_PARAM)); 188 addParam(createParam<Knob16>(scale15ParamPosition, module, PolyOff16::SCALE15_PARAM)); 189 addParam(createParam<Knob16>(offset16ParamPosition, module, PolyOff16::OFFSET16_PARAM)); 190 addParam(createParam<Knob16>(scale16ParamPosition, module, PolyOff16::SCALE16_PARAM)); 191 addParam(createParam<Knob16>(channelsParamPosition, module, PolyOff16::CHANNELS_PARAM)); 192 193 addInput(createInput<Port24>(cv1InputPosition, module, PolyOff16::CV1_INPUT)); 194 addInput(createInput<Port24>(cv2InputPosition, module, PolyOff16::CV2_INPUT)); 195 addInput(createInput<Port24>(cv3InputPosition, module, PolyOff16::CV3_INPUT)); 196 addInput(createInput<Port24>(cv4InputPosition, module, PolyOff16::CV4_INPUT)); 197 addInput(createInput<Port24>(cv5InputPosition, module, PolyOff16::CV5_INPUT)); 198 addInput(createInput<Port24>(cv6InputPosition, module, PolyOff16::CV6_INPUT)); 199 addInput(createInput<Port24>(cv7InputPosition, module, PolyOff16::CV7_INPUT)); 200 addInput(createInput<Port24>(cv8InputPosition, module, PolyOff16::CV8_INPUT)); 201 addInput(createInput<Port24>(cv9InputPosition, module, PolyOff16::CV9_INPUT)); 202 addInput(createInput<Port24>(cv10InputPosition, module, PolyOff16::CV10_INPUT)); 203 addInput(createInput<Port24>(cv11InputPosition, module, PolyOff16::CV11_INPUT)); 204 addInput(createInput<Port24>(cv12InputPosition, module, PolyOff16::CV12_INPUT)); 205 addInput(createInput<Port24>(cv13InputPosition, module, PolyOff16::CV13_INPUT)); 206 addInput(createInput<Port24>(cv14InputPosition, module, PolyOff16::CV14_INPUT)); 207 addInput(createInput<Port24>(cv15InputPosition, module, PolyOff16::CV15_INPUT)); 208 addInput(createInput<Port24>(cv16InputPosition, module, PolyOff16::CV16_INPUT)); 209 addInput(createInput<Port24>(inInputPosition, module, PolyOff16::IN_INPUT)); 210 211 addOutput(createOutput<Port24>(outOutputPosition, module, PolyOff16::OUT_OUTPUT)); 212 213 addChild(createLight<BGTinyLight<GreenLight>>(channel1LightPosition, module, PolyOff16::CHANNEL1_LIGHT)); 214 addChild(createLight<BGTinyLight<GreenLight>>(channel2LightPosition, module, PolyOff16::CHANNEL2_LIGHT)); 215 addChild(createLight<BGTinyLight<GreenLight>>(channel3LightPosition, module, PolyOff16::CHANNEL3_LIGHT)); 216 addChild(createLight<BGTinyLight<GreenLight>>(channel4LightPosition, module, PolyOff16::CHANNEL4_LIGHT)); 217 addChild(createLight<BGTinyLight<GreenLight>>(channel5LightPosition, module, PolyOff16::CHANNEL5_LIGHT)); 218 addChild(createLight<BGTinyLight<GreenLight>>(channel6LightPosition, module, PolyOff16::CHANNEL6_LIGHT)); 219 addChild(createLight<BGTinyLight<GreenLight>>(channel7LightPosition, module, PolyOff16::CHANNEL7_LIGHT)); 220 addChild(createLight<BGTinyLight<GreenLight>>(channel8LightPosition, module, PolyOff16::CHANNEL8_LIGHT)); 221 addChild(createLight<BGTinyLight<GreenLight>>(channel9LightPosition, module, PolyOff16::CHANNEL9_LIGHT)); 222 addChild(createLight<BGTinyLight<GreenLight>>(channel10LightPosition, module, PolyOff16::CHANNEL10_LIGHT)); 223 addChild(createLight<BGTinyLight<GreenLight>>(channel11LightPosition, module, PolyOff16::CHANNEL11_LIGHT)); 224 addChild(createLight<BGTinyLight<GreenLight>>(channel12LightPosition, module, PolyOff16::CHANNEL12_LIGHT)); 225 addChild(createLight<BGTinyLight<GreenLight>>(channel13LightPosition, module, PolyOff16::CHANNEL13_LIGHT)); 226 addChild(createLight<BGTinyLight<GreenLight>>(channel14LightPosition, module, PolyOff16::CHANNEL14_LIGHT)); 227 addChild(createLight<BGTinyLight<GreenLight>>(channel15LightPosition, module, PolyOff16::CHANNEL15_LIGHT)); 228 addChild(createLight<BGTinyLight<GreenLight>>(channel16LightPosition, module, PolyOff16::CHANNEL16_LIGHT)); 229 } 230 231 void contextMenu(Menu* menu) override { 232 OutputRangeOptionMenuItem::addOutputRangeOptionsToMenu(module, menu); 233 234 auto m = dynamic_cast<PolyOff16*>(module); 235 assert(m); 236 237 OptionsMenuItem* ooo = new OptionsMenuItem("Order of operations"); 238 ooo->addItem(OptionMenuItem("Scale, then offset", [m]() { return !m->_offsetFirst; }, [m]() { m->_offsetFirst = false; })); 239 ooo->addItem(OptionMenuItem("Offset, then scale", [m]() { return m->_offsetFirst; }, [m]() { m->_offsetFirst = true; })); 240 OptionsMenuItem::addToMenu(ooo, menu); 241 } 242 }; 243 244 Model* modelPolyOff16 = createModel<PolyOff16, PolyOff16Widget>("Bogaudio-PolyOff16", "POLYOFF16", "Polyphonic per-channel offset and scale", "Polyphonic");