CmpDist.cpp (6343B)
1 2 #include "CmpDist.hpp" 3 4 bool CmpDist::active() { 5 return ( 6 outputs[MIX_OUTPUT].isConnected() || 7 outputs[GT_OUTPUT].isConnected() || 8 outputs[EQ_OUTPUT].isConnected() || 9 outputs[LT_OUTPUT].isConnected() 10 ); 11 } 12 13 int CmpDist::channels() { 14 return inputs[A_INPUT].getChannels(); 15 } 16 17 void CmpDist::addChannel(int c) { 18 _engines[c] = new Engine(); 19 } 20 21 void CmpDist::removeChannel(int c) { 22 delete _engines[c]; 23 _engines[c] = NULL; 24 } 25 26 void CmpDist::modulate() { 27 float aLevel = clamp(params[A_DRY_PARAM].getValue(), 0.0f, 1.0f); 28 aLevel = 1.0f - aLevel; 29 aLevel *= Amplifier::minDecibels; 30 _aDryAmplifier.setLevel(aLevel); 31 32 float bLevel = clamp(params[B_DRY_PARAM].getValue(), 0.0f, 1.0f); 33 bLevel = 1.0f - bLevel; 34 bLevel *= Amplifier::minDecibels; 35 _bDryAmplifier.setLevel(bLevel); 36 } 37 38 void CmpDist::modulateChannel(int c) { 39 Engine& e = *_engines[c]; 40 41 e.aScale = clamp(params[A_PARAM].getValue(), -1.0f, 1.0f); 42 if (inputs[A_SCALE_INPUT].isConnected()) { 43 e.aScale *= clamp(inputs[A_SCALE_INPUT].getPolyVoltage(c) / 5.0f, -1.0f, 1.0f); 44 } 45 e.bScale = clamp(params[B_PARAM].getValue(), -1.0f, 1.0f); 46 if (inputs[B_SCALE_INPUT].isConnected()) { 47 e.bScale *= clamp(inputs[B_SCALE_INPUT].getPolyVoltage(c) / 5.0f, -1.0f, 1.0f); 48 } 49 50 e.window = clamp(params[WINDOW_PARAM].getValue(), 0.0f, 1.0f); 51 if (inputs[WINDOW_INPUT].isConnected()) { 52 e.window *= clamp(inputs[WINDOW_INPUT].getPolyVoltage(c) / 10.0f, 0.0f, 1.0f); 53 } 54 e.window *= 10.0f; 55 56 e.gtMix = clamp(params[GT_MIX_PARAM].getValue(), -1.0f, 1.0f); 57 if (inputs[GT_MIX_INPUT].isConnected()) { 58 e.gtMix *= clamp(inputs[GT_MIX_INPUT].getPolyVoltage(c) / 5.0f, -1.0f, 1.0f); 59 } 60 e.eqMix = clamp(params[EQ_MIX_PARAM].getValue(), -1.0f, 1.0f); 61 e.ltMix = clamp(params[LT_MIX_PARAM].getValue(), -1.0f, 1.0f); 62 if (inputs[LT_MIX_INPUT].isConnected()) { 63 e.ltMix *= clamp(inputs[LT_MIX_INPUT].getPolyVoltage(c) / 5.0f, -1.0f, 1.0f); 64 } 65 66 float dw = clamp(params[DRY_WET_PARAM].getValue(), -1.0f, 1.0f); 67 if (inputs[DRY_WET_INPUT].isConnected()) { 68 e.ltMix *= clamp(inputs[DRY_WET_INPUT].getPolyVoltage(c) / 5.0f, -1.0f, 1.0f); 69 } 70 e.dryWet.setParams(dw, 0.9f, false); 71 } 72 73 void CmpDist::processAlways(const ProcessArgs& args) { 74 outputs[GT_OUTPUT].setChannels(_channels); 75 outputs[EQ_OUTPUT].setChannels(_channels); 76 outputs[LT_OUTPUT].setChannels(_channels); 77 outputs[MIX_OUTPUT].setChannels(_channels); 78 } 79 80 void CmpDist::processChannel(const ProcessArgs& args, int c) { 81 const float v = 5.0f; 82 Engine& e = *_engines[c]; 83 84 float a = inputs[A_INPUT].getPolyVoltage(c); 85 a *= e.aScale; 86 float b = v; 87 if (inputs[B_INPUT].isConnected()) { 88 b = inputs[B_INPUT].getPolyVoltage(c); 89 } 90 b *= e.bScale; 91 92 float gt = 0.0f; 93 float eq = 0.0f; 94 float lt = 0.0f; 95 if (fabsf(a - b) <= e.window) { 96 gt = -v; 97 eq = v; 98 lt = -v; 99 } 100 else if (a > b) { 101 gt = v; 102 eq = -v; 103 lt = -v; 104 } 105 else { 106 gt = -v; 107 eq = -v; 108 lt = v; 109 } 110 lt = -lt; 111 112 float dry = _aDryAmplifier.next(a) + _bDryAmplifier.next(b); 113 float wet = e.gtMix*gt + e.eqMix*eq + e.ltMix*lt; 114 float mix = e.dryWet.next(dry, wet); 115 outputs[GT_OUTPUT].setVoltage(gt, c); 116 outputs[EQ_OUTPUT].setVoltage(eq, c); 117 outputs[LT_OUTPUT].setVoltage(lt, c); 118 outputs[MIX_OUTPUT].setVoltage(mix, c); 119 } 120 121 struct CmpDistWidget : BGModuleWidget { 122 static constexpr int hp = 10; 123 124 CmpDistWidget(CmpDist* module) { 125 setModule(module); 126 box.size = Vec(RACK_GRID_WIDTH * hp, RACK_GRID_HEIGHT); 127 setPanel(box.size, "CmpDist"); 128 createScrews(); 129 130 // generated by svg_widgets.rb 131 auto aParamPosition = Vec(14.0, 46.0); 132 auto windowParamPosition = Vec(60.5, 46.0); 133 auto bParamPosition = Vec(107.0, 46.0); 134 auto gtMixParamPosition = Vec(14.0, 111.0); 135 auto eqMixParamPosition = Vec(60.5, 111.0); 136 auto ltMixParamPosition = Vec(107.0, 111.0); 137 auto dryWetParamPosition = Vec(60.5, 176.0); 138 auto aDryParamPosition = Vec(20.5, 182.5); 139 auto bDryParamPosition = Vec(113.5, 182.5); 140 141 auto gtMixInputPosition = Vec(15.0, 230.0); 142 auto ltMixInputPosition = Vec(47.0, 230.0); 143 auto windowInputPosition = Vec(79.0, 230.0); 144 auto dryWetInputPosition = Vec(111.0, 230.0); 145 auto aInputPosition = Vec(15.0, 274.0); 146 auto aScaleInputPosition = Vec(47.0, 274.0); 147 auto bInputPosition = Vec(15.0, 318.0); 148 auto bScaleInputPosition = Vec(47.0, 318.0); 149 150 auto gtOutputPosition = Vec(79.0, 274.0); 151 auto ltOutputPosition = Vec(111.0, 274.0); 152 auto eqOutputPosition = Vec(79.0, 318.0); 153 auto mixOutputPosition = Vec(111.0, 318.0); 154 // end generated by svg_widgets.rb 155 156 addParam(createParam<Knob29>(aParamPosition, module, CmpDist::A_PARAM)); 157 addParam(createParam<Knob29>(windowParamPosition, module, CmpDist::WINDOW_PARAM)); 158 addParam(createParam<Knob29>(bParamPosition, module, CmpDist::B_PARAM)); 159 addParam(createParam<Knob29>(gtMixParamPosition, module, CmpDist::GT_MIX_PARAM)); 160 addParam(createParam<Knob29>(eqMixParamPosition, module, CmpDist::EQ_MIX_PARAM)); 161 addParam(createParam<Knob29>(ltMixParamPosition, module, CmpDist::LT_MIX_PARAM)); 162 addParam(createParam<Knob29>(dryWetParamPosition, module, CmpDist::DRY_WET_PARAM)); 163 addParam(createParam<Knob16>(aDryParamPosition, module, CmpDist::A_DRY_PARAM)); 164 addParam(createParam<Knob16>(bDryParamPosition, module, CmpDist::B_DRY_PARAM)); 165 166 addInput(createInput<Port24>(gtMixInputPosition, module, CmpDist::GT_MIX_INPUT)); 167 addInput(createInput<Port24>(ltMixInputPosition, module, CmpDist::LT_MIX_INPUT)); 168 addInput(createInput<Port24>(windowInputPosition, module, CmpDist::WINDOW_INPUT)); 169 addInput(createInput<Port24>(dryWetInputPosition, module, CmpDist::DRY_WET_INPUT)); 170 addInput(createInput<Port24>(aInputPosition, module, CmpDist::A_INPUT)); 171 addInput(createInput<Port24>(aScaleInputPosition, module, CmpDist::A_SCALE_INPUT)); 172 addInput(createInput<Port24>(bInputPosition, module, CmpDist::B_INPUT)); 173 addInput(createInput<Port24>(bScaleInputPosition, module, CmpDist::B_SCALE_INPUT)); 174 175 addOutput(createOutput<Port24>(gtOutputPosition, module, CmpDist::GT_OUTPUT)); 176 addOutput(createOutput<Port24>(ltOutputPosition, module, CmpDist::LT_OUTPUT)); 177 addOutput(createOutput<Port24>(eqOutputPosition, module, CmpDist::EQ_OUTPUT)); 178 addOutput(createOutput<Port24>(mixOutputPosition, module, CmpDist::MIX_OUTPUT)); 179 } 180 }; 181 182 Model* modelCmpDist = createModel<CmpDist, CmpDistWidget>("Bogaudio-CmpDist", "CMPDIST", "Comparator-based distortion", "Distortion", "Effect", "Polyphonic");