<?xml version="1.0" encoding="utf-8" standalone="yes"?>
		<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
		<chapter name="MSP Dynamics Tutorial 3: Distortion">
			<previous module="msp"  name="09_dynamicschapter02"></previous>
			<next module="msp" name="10_midichapter01"></next>
			<parent name="00_mspindex">MSP Tutorials</parent>

		<setdocpatch name="03nDistortion" patch="03nDistortion.maxpat"/>
		<h1>Dynamics Tutorial 3: Distortion</h1>

	<p>
		In this tutorial, we'll look at the use of <i>waveshaping</i> to
		modify an input signal, simulating the distortion present in overdriven
		amplifiers. In the realm of analogue audio, all amplifiers introduce
		some form of distortion into the signal. The ability to creatively
		harness this by deliberately overdriving distortion circuits is
		difficult to simulate in a digital audio environment, as the
		natural artifacts and nonlinearities that render tube and transistor
		amplifier distortion so interesting are absent in the digital signal
		path. One way to overcome this is to simulate the distortion caused
		by amplifiers by using lookup tables to change the dynamic response
		of an input signal. Unlike the previous tutorials, which look at
		dynamics from a macro- (or envelope) perspective, this tutorial looks
		at sound amplitudes on a sample-by-sample basis.
	</p>

	<p>
		A review of the tutorial that covers
		<link type="tutorial" module="msp" name="07_samplingchapter05">waveshaping synthesis</link>
		may be useful to understand how the <o>lookup~</o> object works in MSP.
	</p>



	<h2>
		Splitting bands
	</h2>

	<p>
		One of the attributes of cool-sounding distortion circuits is that they
		are <i>frequency-dependent</i> as well as <i>amplitude-dependent</i> in
		their behavior; that is to say, the way in which they shape an input
		signal depends not only on how loud the sound comes in, but also the
		frequency components in that sound. Some distortion circuits process high
		frequencies far more harshly than bass frequencies; some distort a narrow
		range of frequencies in the mid-range and leave high sounds relatively pure.
		In order to simulate this, we create an MSP signal chain that splits our
		input signal into three bands for low, medium, and high frequencies.
	</p>

	<bullet>
		Take a look at the tutorial patcher. Start the audio by clicking
		the <o>ezdac~</o> and turn on the <o>metro</o> object labeled '1)' by
		clicking the <o>toggle</o> object. There are three <o>gain~</o> sliders
		at the bottom of the patch. Raise these each in turn, and you should hear
		a random sequence of vibraphone notes, with the first <o>gain~</o> slider
		controlling the bass, the second controlling the mid-range, and the third
		controlling the high frequencies.
	</bullet>

	<p>
		The state-variable (<o>svf~</o> --- colored red) object in our patcher
		takes the output of our sample playback logic and applies four filters to
		the signal simultaneously: a lowpass filter (output from the left outlet),
		a highpass filter (output from the second outlet), a bandpass filter (output
		from the third outlet), and a bandreject or notch filter (output form the last
		outlet). We're only interested in the first three, which roughly correspond
		to the bass, middle, and treble of our input signal.
	</p>

	<h2>
		Applying distortion
	</h2>

	<bullet>
		Turn down all but the <o>gain~</o> slider controlling the low frequencies.
		On the right of the tutorial patcher, draw in the <o>waveform~</o> object
		labeled 'Low'. Start by manually retracing the shape that's already in there
		(a diagonal line running from bottom to top). Notice that even the slightest
		deviation changes the sound and adds noise. Try drawing a zig-zag shape across
		the waveform:
	</bullet>
		<illustration><img src="images/dynamicschapter03a.png"/></illustration>

	<caption>
		<i>A freehand waveshape</i>
	</caption>

	<p>
		A transfer function with multiple zero-crossing points will have the result of
		adding additional harmonics to any relatively periodic waveform. In amplifier
		distortion, this is an important component of the 'warm' effect of tube amplification.
	</p>

	<bullet>
		Click the <o>button</o> object labeled 'Reset' under the 'Low' <o>waveform~</o>.
		The sound (and the shape) should return to normal. Turn up the <o>gain~</o> slider
		representing the mid-range frequencies. In the <o>waveform~</o> object labeled 'Mid',
		draw a diagonal slash across the middle of the waveform:
	</bullet>
		<illustration><img src="images/dynamicschapter03b.png"/></illustration>

	<caption>
		<i>Distortion around zero-crossings</i>
	</caption>

	<p>
		Notice how the mid-range now has a harsh overdriven sound to it.
		What we've done in drawing that shape is introduce a set of additional
		zero-crossings around the normal zero point in the waveform. The result
		of this will be nonlinearities in the mid-range signal whenever its
		sample amplitude comes close to zero. This simulates the properties of
		many solid state transistor distortion circuits (such as guitar pedals),
		which 'kink' the signal at specific intervals to generate high harmonics
		from the signal.
	</p>

	<bullet>
		'Reset' the 'Mid' waveshape by clicking the <o>button</o>, and turn
		up the <o>gain~</o> slider representing the high frequencies. Draw
		a series of sharp peaks along the waveform opposite from the normal curve:
	</bullet>
		<illustration><img src="images/dynamicschapter03c.png"/></illustration>

	<caption>
		<i>Introducing random non-linearities</i>
	</caption>

	<p>
		This type of waveshape creates a harsh distortion similar to the
		way digital distortion effects work. By introducing noise (random
		activity) into the waveshape, we create the potential for completely
		arbitrary distortion effects that resemble less an amplifier circuit
		than a 'digital' effects process.
	</p>

	<h2>
		Resetting and smoothing
	</h2>

	<bullet>
		Double-click any one of the <o>patcher</o> objects that are triggered
		by the 'Reset' <o>button</o> objects (<m>resetlow, resetmid, resethi</m>).
		Look at the contents of the subpatch.
	</bullet>

	<p>
		The MSP <o>peek~</o> object, you may recall, allows us to programmatically
		fill <o>buffer~</o> objects with samples according to patcher logic in Max.
		The <o>uzi</o> object, when it receives a <m>bang</m>, sets up a chain
		of <m>8192</m> events that fill the appropriate <o>buffer~</o> object with
		an ascending ramp of values from <m>-1</m> to <m>1</m>. When the <o>lookup~</o> object
		uses this curve, the incoming signal gets passed unchanged.
	</p>

	<bullet>
		Close this patcher and open any of the <o>patcher</o> objects that are
		triggered by the 'Smooth' <o>button</o> objects (<m>smoothlow, smoothmid, smoothhi</m>)
	</bullet>

	<p>
		The smoothing subpatches, which are triggered whenever you release
		the mouse from drawing in the <o>waveform~</o> object, triggers
		an <o>uzi</o> object that takes each sample currently in the <o>buffer~</o> and
		averages it with its previous sample, creating a smoother curve than would be
		possible by freehand drawing.
	</p>

	<bullet>
		Close the subpatch, and unlock the main tutorial patcher. Disconnect
		the <o>waveform~</o> objects from the <o>zl</o> objects below them.
		Lock the patcher and draw some curves. You'll find that the distortion
		effects you create are much much more pronounced. If you want to,
		click the <o>button</o> objects that smooth the shapes manually. The
		more you click them, the more averaged out our waveshapes become.
	</bullet><br/>

	<bullet>
		In the middle of the tutorial patcher, click the <o>message</o> box
		that reads <m>replace bass.aiff</m>. Now the sound generated by our
		sampler will be that of an electric bass guitar. Play with the different
		waveshaping techniques we looked at earlier. If you like, modify
		the <m>Cutoff frequency</m> of the <o>svf~</o> object with the <o>number</o> box
		in the middle of the patcher. This will let you choose where the midrange
		distortion effect is most prominent.
	</bullet>
	<h2>
		Summary
	</h2>

	<p>
		Waveshaping is an exciting synthesis technique that allows you to
		create complex timbres by running an oscillator through a lookup table;
		when used as a signal-processing technique with complex audio input,
		it can be used to simulate all manner of distortion effects. Because
		real-life amplifier distortion changes depending on the frequency
		content of the input signal, one way to simulate this distortion is
		to split an audio signal into several frequency bands and waveshape
		each one independently. While it's possible to scientifically measure
		and model the responses of different distortion circuits, freehand
		drawing in the <o>waveform~</o> object allows us to experiment with
		different curves and hear them directly.
	</p>
</chapter>

