Dropout.h (1604B)
1 #ifndef DROPOUT_H_INCLUDED 2 #define DROPOUT_H_INCLUDED 3 4 #include "JuceHeader.h" 5 6 class Dropout 7 { 8 public: 9 Dropout() {} 10 11 void setMix (float newMix) 12 { 13 for (auto& mix : mixSmooth) 14 mix.setTargetValue (newMix); 15 } 16 17 void setPower (float newPow) 18 { 19 for (auto& power : powerSmooth) 20 power.setTargetValue (newPow); 21 } 22 23 void prepare (double sr, int numChannels) 24 { 25 mixSmooth.resize ((size_t) numChannels); 26 for (auto& mix : mixSmooth) 27 mix.reset (sr, 0.01); 28 29 powerSmooth.resize ((size_t) numChannels); 30 for (auto& power : powerSmooth) 31 power.reset (sr, 0.005); 32 } 33 34 void process (AudioBuffer<float>& buffer) 35 { 36 if (mixSmooth[0].getTargetValue() == 0.0f && ! mixSmooth[0].isSmoothing()) 37 return; 38 39 for (size_t ch = 0; ch < (size_t) buffer.getNumChannels(); ++ch) 40 { 41 auto* x = buffer.getWritePointer ((int) ch); 42 for (int n = 0; n < buffer.getNumSamples(); ++n) 43 { 44 auto mix = mixSmooth[ch].getNextValue(); 45 x[n] = mix * dropout (x[n], ch) + (1.0f - mix) * x[n]; 46 } 47 } 48 } 49 50 inline float dropout (float x, size_t ch) 51 { 52 auto sign = (float) chowdsp::Math::sign (x); 53 return pow (abs (x), powerSmooth[ch].getNextValue()) * sign; 54 } 55 56 private: 57 std::vector<LinearSmoothedValue<float>> mixSmooth; 58 std::vector<LinearSmoothedValue<float>> powerSmooth; 59 60 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Dropout) 61 }; 62 63 #endif // DROPOUT_H_INCLUDED