The softclip algorithm will apply soft cliping to an input signal so that it lies in the range of [-1, 1]. Under the hood, this implements a fast approximation of the tanhfunction with a drive parameter. This yields a reasonable sounding saturation effect.

The approximation used was found via the music-dsp mailing list.

Tangled Files

As per usual, softclip.c and softclip.h.

#include "softclip.h"

#ifndef SKFLT
#define SKFLT float

Stateless Function

The softclip algorithm can be implemented as a stateless function, called sk_stateless_tick. It takes in an input signal in and a multiplier amount drive. For a desired effect to occur, drive should be greater than 1.

SKFLT sk_softclip_tick(SKFLT in, SKFLT drive);

static SKFLT rational_tanh(SKFLT x)
    if( x < -3 )
        return -1;
    else if( x > 3 )
        return 1;
        return x * ( 27 + x * x ) / ( 27 + 9 * x * x );

SKFLT sk_softclip_tick(SKFLT in, SKFLT drive)
    return rational_tanh(in * drive);