distort

distort — Distort an audio signal via waveshaping and optional clipping.

Description

Distort an audio signal via waveshaping and optional clipping.

Syntax

ar distort asig, kdist, ifn[, ihp, istor]

Initialization

ifn -- table number of a waveshaping function with extended guard point. The function can be of any shape, but it should pass through 0 with positive slope at the table mid-point. The table size need not be large, since it is read with interpolation.

ihp -- (optional) half-power point (in cps) of an internal low-pass filter. The default value is 10.

istor -- (optional) initial disposition of internal data space (see reson). The default value is 0.

Performance

asig -- Audio signal to be processed

kdist -- Amount of distortion (usually between 0 and 1)

This unit distorts an incoming signal using a waveshaping function ifn and a distortion index kdist. The input signal is first compressed using a running rms, then passed through a waveshaping function which may modify its shape and spectrum. Finally it is rescaled to approximately its original power.

The amount of distortion depends on the nature of the shaping function and on the value of kdist, which generally ranges from 0 to 1. For low values of kdist, we should like the shaping function to pass the signal almost unchanged. This will be the case if, at the mid-point of the table, the shaping function is near-linear and is passing through 0 with positive slope. A line function from -1 to +1 will satisfy this requirement; so too will a sigmoid (sinusoid from 270 to 90 degrees). As kdist is increased, the compressed signal is expanded to encounter more and more of the shaping function, and if this becomes non-linear the signal is increasingly bent on read-through to cause distortion.

When kdist becomes large enough, the read-through process will eventually hit the outer limits of the table. The table is not read with wrap-around, but will stick at the end-points as the incoming signal exceeds them; this introduces clipping, an additional form of signal distortion. The point at which clipping begins will depend on the complexity (rms-to-peak value) of the input signal. For a pure sinusoid, clipping will begin only as kdist exceeds 0.7; for a more complex input, clipping might begin at a kdist of 0.5 or much less. kdist can exceed the clip point by any amount, and may be greater than 1.

The shaping function can be made arbitrarily complex for extra effect. It should generally be continuous, though this is not a requirement. It should also be well-behaved near the mid-point, and roughly balanced positive-negative overall, else some excessive DC offset may result. The user might experiment with more aggressive functions to suit the purpose. A generally positive slope allows the distorted signal to be mixed with the source without phase cancellation.

distort is useful as an effects process, and is usually combined with reverb and chorusing on effects busses. However, it can alternatively be used to good effect within a single instrument.

Examples

Here is an example of the distort opcode. It uses the file distort.csd.

Example 199. Example of the distort opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
-odac     ;;;RT audio out
;-iadc    ;;;uncomment -iadc if RT audio input is needed too
; For Non-realtime ouput leave only the line below:
; -o distort.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

sr     = 44100
ksmps  = 32
nchnls = 2
0dbfs  = 1

gifn	ftgen	0,0, 257, 9, .5,1,270	; define a sigmoid, or better 
;gifn	ftgen	0,0, 257, 9, .5,1,270,1.5,.33,90,2.5,.2,270,3.5,.143,90,4.5,.111,270

instr 1

kdist	line	0, p3, 2		; and over 10 seconds
asig	poscil	0.3, 440, 1
aout	distort	asig, kdist, gifn	; gradually increase the distortion
	outs	aout, aout

endin
</CsInstruments>
<CsScore>
f 1 0 16384 10 1
i 1 0 10
e

</CsScore>
</CsoundSynthesizer>


Credits

Written by Barry L. Vercoe for Extended Csound and released in Csound5.