For You: Hard Saturation in ScriptNode
-
Hey!
To express my gratitude, I'm making this post along with another soft saturation post to share my saturation! All the original dsp was based off of:
https://www.hackaudio.com/digital-signal-processing/distortion-effects/hard-clipping/
(Site seems to be ridden with popups and ads now so I'll put the main dsp below)out = thresh, when in > thresh
out = -thresh, when in < -thresh
out = in, otherwise
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 thresh. Then open the code editor from the snex nodeHere's the fun stuff:
template <int NumVoices> struct snex_hardsat { SNEX_NODE(snex_hardsat); // Implement the Waveshaper here... float thresh = 0.0f; float out = 0.0f; float getSample(float input) { if(input > thresh) { out = thresh; } else if(input < thresh*-1.0f) { out = thresh * -1.0f; } else { out = input; } 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) { thresh = (float)v; } } };
What I recommend is just downloading snex_hardsat.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!