For You: Soft Saturation in ScriptNode
-
Hey!
As a way to express my thanks for the massive help in this community, I'm making this post along with another hard saturation post to share my saturation! All the original dsp was based off of:
https://www.musicdsp.org/en/latest/Effects/42-soft-saturation.html
x < a:
f(x) = x
x > a:
f(x) = a + (x-a)/(1+((x-a)/(1-a))^2)
x > 1:
f(x) = (a+1)/2
To create the saturation, add the
snex_shaper
node in scriptnode to your project, press the three dots -> Create new file. Name it and then add a parameter the same way and name it something like drive. Then open the code editor from the snex nodeThe main course:
template <int NumVoices> struct snex_softsat { SNEX_NODE(snex_softsat); // Implement the Waveshaper here... float gain = 0.0f; float out = 0.0f; float saturate(float input) { if(input < gain) { out = input; } else if(input > gain) { out = gain + (input-gain) / (1.0f+Math.pow(((input-gain)/(1.0f-gain)), 2.0f)); } else if(input > 1.0f) { out = (gain + 1.0f) / 2.0f; } return out; } float getSample(float input) { if(input>0.0f) { saturate(input); } else { input = input * -1.0f; saturate(input); out = out * -1.0f; } return out; } // These functions are the glue code that call the function above template <typename T> void process(T& data) { for(auto ch: data) { for(auto& s: data.toChannelData(ch)) { s = getSample(s); } } } template <typename T> void processFrame(T& data) { for(auto& s: data) s = getSample(s); } void reset() { } void prepare(PrepareSpecs ps) { } void setExternalData(const ExternalData& d, int index) { } template <int P> void setParameter(double v) { if(P==0) { gain = (float)v; } } };
What I recommend is just downloading snex_softsat.zip and then in your project folder -> DspNetworks -> CodeLibrary -> snex_shaper (create one if not there) and place the
.h
and.xml
files in there.. they should show up in the dropdown in the snex shaper all ready to go.There you go! Feel free to use it however you want and enjoy!
Note: this isn't going to give you the full saturation, just the waveshaping (hard part). You still would need to wrap the shaper node in an oversampling node as well as having filters, gain staging, xfaders, etc to make a release ready saturation, shouldn't be too hard to do however since they're all nodes in scriptnode
Thanks!
-