The term waveshaping is used to describe a technique where the instantaneous amplitude of the input signal is used as an index into a waveshaping function, and in this manner reshape the resulting waveform. With a linear and symmetric waveshaping function, no change is made to the waveform. Like this:
;***************************************************
; Waveshaper
;***************************************************
instr 5
iamp = ampdbfs(p4) ; Amp in -dB
; audio generator
a1 oscili 1, 440, giSine
; waveshaper
iShape ftgentmp 0, 0, 65537, 19, 0.5, 1, 270, 0 ; rising sigmoid (bipolar)
a2 tablei a1*0.5, iShape, 1, 0.5, 0
; audio out
out a2
endin
;***************************************************
For common “soft clip” processing, we could use a waveshaping function like this:
The soft clipping function used above generates harmonic distortion with only odd harmonics. We can control how much distortion we’ll get by adjusting the input amplitude. In order to compensate for the change in output volume, we mix in a bit of the dry signal, so the waveshaper drive amount also control dry/wet balance.
;***************************************************
; Waveshaper
;***************************************************
instr 5
iamp = ampdbfs(p4) ; Amp in -dB
; audio generator
a1 oscili 1, 440, giSine
; waveshaper
iShape ftgentmp 0, 0, 65537, 19, 0.5, 1, 270, 0 ; rising sigmoid (bipolar)
kdrive line 0, p3, 1 ; waveshaper drive (distortion amount)
a2 tablei a1*0.5*kdrive, iShape, 1, 0.5, 0
aout = (a2+(a1*(1-kdrive)))*0.7 ; mix drive/dry
; audio out
out aout
endin
;***************************************************
The previous example created odd harmonics. We can create a specific pattern of harmonics by using Chebyshew polynominals. For more info on this, see e.g. http://www.csounds.com/manual/html/GEN13.html Below is a shaping function that will create the two first odd harmonics, then another one for creating the two first even harmonics.
Rectifying a signal is a common method of distortion. It will flip the negative part of the waveform around to become positive. This will in most cases remove the fundamental frequency (make the perceived pitch go up an octave), as the waveform shape will effectively become half as long. In the following examples, we have a linear rectifier function and a “soft rectifier” function. The soft version was made by smoothing the edges of the shaping function a bit, to avoid too harsh aliasing.
We can use a waveshaping function of any form, so this technique gives us great flexibility. In most cases it is advisable to have a shaping function that has zero at its midpoint. That ensures that silence at the input also creates silence at the output, with no DC offset. In these cases it is advisable to use a dc blocking filter after waveshaping. A dc blocking filter is essentially a high pass filter with a very low cutoff frequency (letting through everything above 5 Hz or so).
This is a form of distortion that can occur in a badly adjusted transistor amplifier (or one that is about to break). The effect arise because the positive and the negative part of the signal is amplified by different transistors, and the switching (crossover) between these can cause artifacts unless it is carefully compensated for. With waveshaping, we can use the following shaping function to simulate crossover distortion. In the sound example, we keep the drive constant, but let the sound fade out towards the ends, so you can hear how this kind of distortion creates artifacts even at low levels.
For creative purposes, one could just as easily do another form of crossover distortion, literally inverting the crossover function. Listen to how this changes the overall level and presence of the sound, and also how it reacts differently to quiet levels (in the release of the sound).