diff --git a/src/Random.cpp b/src/Random.cpp index 4496cdb..1bda878 100644 --- a/src/Random.cpp +++ b/src/Random.cpp @@ -44,8 +44,8 @@ struct Random : Module { NUM_LIGHTS }; - float lastVoltage = 0.f; - float nextVoltage = 0.f; + float lastVoltage[16] = {}; + float nextVoltage[16] = {}; /** Waits at 1 until reset */ float phase = 0.f; float clockFreq = 0.f; @@ -54,7 +54,7 @@ struct Random : Module { /** External clock timer */ dsp::Timer clockTimer; dsp::SchmittTrigger clockTrigger; - dsp::PulseGenerator trigGenerator; + dsp::PulseGenerator pulseGenerator; Random() { config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS); @@ -91,11 +91,9 @@ struct Random : Module { } void process(const ProcessArgs& args) override { - // Params - float shape = params[SHAPE_PARAM].getValue(); - shape += inputs[SHAPE_INPUT].getVoltage() / 10.f * params[SHAPE_CV_PARAM].getValue(); - shape = clamp(shape, 0.f, 1.f); + int channels = std::max(1, inputs[EXTERNAL_INPUT].getChannels()); + // Params float rand = 0.f; if (!inputs[EXTERNAL_INPUT].isConnected()) { rand = params[RAND_PARAM].getValue(); @@ -117,21 +115,23 @@ struct Random : Module { return; // Generate next random voltage - lastVoltage = nextVoltage; + // lastVoltage = nextVoltage; + std::copy_n(nextVoltage, channels, lastVoltage); + if (inputs[EXTERNAL_INPUT].isConnected()) { - nextVoltage = inputs[EXTERNAL_INPUT].getVoltage(); + inputs[EXTERNAL_INPUT].readVoltages(nextVoltage); } else { // Crossfade new random voltage with last float v = 10.f * random::uniform(); if (!uni) v -= 5.f; - nextVoltage = crossfade(nextVoltage, v, rand); + nextVoltage[0] = crossfade(nextVoltage[0], v, rand); } phase = 0.f; - trigGenerator.trigger(1e-3f); + pulseGenerator.trigger(1e-3f); lights[PROB_LIGHT].setBrightness(3.f); }; @@ -167,12 +167,24 @@ struct Random : Module { phase += deltaPhase; phase = std::min(1.f, phase); + // Shape param + float shape = params[SHAPE_PARAM].getValue(); + shape += inputs[SHAPE_INPUT].getVoltage() / 10.f * params[SHAPE_CV_PARAM].getValue(); + shape = clamp(shape, 0.f, 1.f); + + auto interpolateOutput = [&](Output& output, float v) { + for (int c = 0; c < channels; c++) { + float vc = rescale(v, 0.f, 1.f, lastVoltage[c], nextVoltage[c]); + output.setVoltage(vc, c); + } + output.setChannels(channels); + }; + // Stepped if (outputs[STEPPED_OUTPUT].isConnected()) { float steps = std::ceil(std::pow(shape, 2) * 15 + 1); float v = std::ceil(phase * steps) / steps; - v = rescale(v, 0.f, 1.f, lastVoltage, nextVoltage); - outputs[STEPPED_OUTPUT].setVoltage(v); + interpolateOutput(outputs[STEPPED_OUTPUT], v); } // Linear @@ -185,8 +197,7 @@ struct Random : Module { else { v = 1.f; } - v = rescale(v, 0.f, 1.f, lastVoltage, nextVoltage); - outputs[LINEAR_OUTPUT].setVoltage(v); + interpolateOutput(outputs[LINEAR_OUTPUT], v); } // Smooth @@ -200,8 +211,7 @@ struct Random : Module { else { v = -1.f; } - v = rescale(v, 1.f, -1.f, lastVoltage, nextVoltage); - outputs[SMOOTH_OUTPUT].setVoltage(v); + interpolateOutput(outputs[SMOOTH_OUTPUT], v); } // Exp @@ -217,12 +227,12 @@ struct Random : Module { else { v = 1.f; } - v = rescale(v, 0.f, 1.f, lastVoltage, nextVoltage); - outputs[EXPONENTIAL_OUTPUT].setVoltage(v); + interpolateOutput(outputs[EXPONENTIAL_OUTPUT], v); } // Trigger output - outputs[TRIG_OUTPUT].setVoltage(trigGenerator.process(args.sampleTime) ? 10.f : 0.f); + bool pulse = pulseGenerator.process(args.sampleTime); + outputs[TRIG_OUTPUT].setVoltage(pulse ? 10.f : 0.f); // Lights lights[RATE_LIGHT].setSmoothBrightness(0.f, args.sampleTime);