Solved Create Simple Saturation Effect

Hey!
I was wondering how you could make a saturation effect in scriptnode, I noticed the that math operators can be used, but how do I use these operators to change the sound?
Thanks!

The best way to do it I think is with the SNEX shaper node, there are some articles in the Forum with a few examples.

The best way to do it I think is with the SNEX shaper node, there are some articles in the Forum with a few examples.

@Soundavid I found this:
https://www.musicdsp.org/en/latest/Effects/42softsaturation.htmlx < a: f(x) = x x > a: f(x) = a + (xa)/(1+((xa)/(1a))^2) x > 1: f(x) = (a+1)/2
There’s some dsp code on that and I was wondering if that could be used for Snex, or if there’s any conversion needed what the final result would look like.. I also noticed that there’s a math.expr node and was wondering if that’d be easier for this task.
Thanks!

This post is deleted! 

@Casmat Need to get my head around this a bit more! Did you get a good saturation effect in the end?

@DanH Yup! It’s still hard for me to understand it completely but I can piece together the dsp better now after trying to convert existing waveshaping models out there:
For my hard saturation I used the basic algo in this site:
https://www.hackaudio.com/digitalsignalprocessing/distortioneffects/hardclipping/It’s not the most detailed but it’ll get the job done.
On the other hand my soft sat I just used musicdsp code which in my usecase sounded better than some other algorithms i found on the internet. I just ported that over to snex in the shaper node and it sounds amazing!
Check the forum for a new post in 1520 minutes, I'll post some goodies!
Edit: Just posted!
https://forum.hise.audio/topic/8958/foryouhardsaturationinscriptnode/1
https://forum.hise.audio/topic/8959/foryousoftsaturationinscriptnode/1 
@Casmat the alternative is to use a Faust node... lots of saturation, distortion and clipping options there...:
https://faustlibraries.grame.fr/libs/misceffects/#saturators

@Lindon Indeed, nice to be a bit more malleable though!

@Casmat Very kind, thank you! How do you figure out how to convert the equations into snex code?

@DanH Lets take a look at soft sat for example, the musicdsp dsp was this:
x < a: f(x) = x x > a: f(x) = a + (xa)/(1+((xa)/(1a))^2) x > 1: f(x) = (a+1)/2
There is also a note that is important to consider that is also posted by the author of the dsp above:
This only works for positive values of x. a should be in the range 0..1
We'll use that later
Now after creating a snexshaper file and creating the amount parameter (with range 01 as said in note) and in the snex code editor of the shaper, you'll see that there's already a base template that is provided. This template basically takes care of most of the stuff you'd need.
In the dsp, there's three values
x
,a
, andf(x)
. Nowa
is commonly used as a variable for gain, andx
is input whilef(x)
ory
is the output. Next I initialized two variablesgain
fora
andout
forf(x)
. We don't need an input variable since there's ainput
parameter in the template that we can use. I created a separate methodsaturate(input)
to separate things out to fix the problem mentioned in the dsp note. From there, you can see the code in the saturate method is exactly the same as the dsp but the dsp variables swapped out plus the return statement at the end.Next in the
getSample(input)
method that was provided in the template, I made an if statement to solve the problem in the dsp note where only a positive value worked for the input, which wouldn't result in complete waveshaping. If the input was greater than 0, then saturate using the input, otherwise make the input negative and run it through the saturate function and then reverse the output to affect negative values as well.Everything is set except the parameter linking. To link the parameter, I looked up on the forum for some snex shaper problems and someone had a video and I used the code they used. I don't know why it's not included in template but its the same for everything, create an if statement and if P (parameter?) is equal to 0 (index position of parameter), then link it!
That's pretty much it, the hard sat is the same but without the needing to make sure negative values of input are factored in