|
| 1 | +// AnalogChew.cpp |
| 2 | +// Mads Kjeldgaard (mail@madskjeldgaard.dk) |
| 3 | + |
| 4 | +#include "AnalogChew.hpp" |
| 5 | + |
| 6 | +#include "SC_PlugIn.hpp" |
| 7 | + |
| 8 | +static InterfaceTable *ft; |
| 9 | + |
| 10 | +namespace AnalogChew { |
| 11 | + |
| 12 | +AnalogChew::AnalogChew() { |
| 13 | + samplerate = sampleRate(); |
| 14 | + |
| 15 | + filt.reset(samplerate, int(samplerate * 0.02f)); |
| 16 | + dropout.prepare((double)samplerate); |
| 17 | + cookParams(samplerate, 0.5, 0.5, 0.5); |
| 18 | + |
| 19 | + mCalcFunc = make_calc_function<AnalogChew, &AnalogChew::next>(); |
| 20 | + next(1); |
| 21 | +} |
| 22 | + |
| 23 | +AnalogChew::~AnalogChew() {} |
| 24 | + |
| 25 | +float AnalogChew::uniform() { return ((float)rand() / (RAND_MAX)); } |
| 26 | + |
| 27 | +void AnalogChew::cookParams(float fs, float depthParam, float freqParam, |
| 28 | + float varParam) { |
| 29 | + const float highFreq = std::min(22000.0f, 0.49f * fs); |
| 30 | + const float freqChange = highFreq - 5000.0f; |
| 31 | + |
| 32 | + auto depth = depthParam; |
| 33 | + auto freq = freqParam; |
| 34 | + if (freq == 0.0f) { |
| 35 | + mix = 0.0f; |
| 36 | + filt.setFreq(highFreq); |
| 37 | + } else if (freq == 1.0f) { |
| 38 | + mix = 1.0f; |
| 39 | + power = 3.0f * depth; |
| 40 | + filt.setFreq(highFreq - freqChange * depth); |
| 41 | + } else if (sampleCounter >= samplesUntilChange) { |
| 42 | + sampleCounter = 0; |
| 43 | + isCrinkled = !isCrinkled; |
| 44 | + |
| 45 | + if (isCrinkled) // start crinkle |
| 46 | + { |
| 47 | + mix = 1.0f; |
| 48 | + power = (1.0f + 2.0f * uniform()) * depth; |
| 49 | + filt.setFreq(highFreq - freqChange * depth); |
| 50 | + samplesUntilChange = getWetTime(freq, depth, varParam); |
| 51 | + } else // end crinkle |
| 52 | + { |
| 53 | + mix = 0.0f; |
| 54 | + filt.setFreq(highFreq); |
| 55 | + samplesUntilChange = getDryTime(freq, varParam); |
| 56 | + } |
| 57 | + } else { |
| 58 | + power = (1.0f + 2.0f * uniform()) * depth; |
| 59 | + if (isCrinkled) { |
| 60 | + filt.setFreq(highFreq - freqChange * depth); |
| 61 | + filt.setFreq(highFreq - freqChange * depth); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + dropout.setMix(mix); |
| 66 | + dropout.setPower(1.0f + power); |
| 67 | +} |
| 68 | + |
| 69 | +void AnalogChew::next(int nSamples) { |
| 70 | + float depth = in0(Depth); |
| 71 | + float freq = in0(Frequency); |
| 72 | + float var = in0(Variance); |
| 73 | + |
| 74 | + cookParams(samplerate, depth, freq, var); |
| 75 | + |
| 76 | + const float *input = in(Input); |
| 77 | + float *outbuf = out(Out1); |
| 78 | + |
| 79 | + for (int i = 0; i < nSamples; ++i) { |
| 80 | + // get input |
| 81 | + float x = mkutils::constrain(input[i], -1.0f, 1.0f); |
| 82 | + x = dropout.processSample(x); |
| 83 | + x = filt.processSample(x); |
| 84 | + outbuf[i] = x; |
| 85 | + sampleCounter++; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void AnalogChew::clear(int nSamples) { ClearUnitOutputs(this, nSamples); } |
| 90 | + |
| 91 | +} // namespace AnalogChew |
| 92 | + |
| 93 | +PluginLoad(AnalogChewUGens) { |
| 94 | + // Plugin magic |
| 95 | + ft = inTable; |
| 96 | + registerUnit<AnalogChew::AnalogChew>(ft, "AnalogChew", false); |
| 97 | +} |
0 commit comments