HISE Logo Forum
    • Categories
    • Register
    • Login

    For You: Soft Saturation in ScriptNode

    Scheduled Pinned Locked Moved Presets / Scripts / Ideas
    1 Posts 1 Posters 607 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • CasmatC
      Casmat
      last edited by Casmat

      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:

      Soft saturation — Musicdsp.org documentation

      favicon

      (www.musicdsp.org)

      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 node

      The 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!

      i make music

      1 Reply Last reply Reply Quote 5
      • 1 11235813 referenced this topic on
      • First post
        Last post

      57

      Online

      1.7k

      Users

      11.7k

      Topics

      101.8k

      Posts