<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="MSP Dynamics Tutorial 2: Dynamics Processing">
	<previous module="msp"  name="09_dynamicschapter03"></previous>
	<next module="msp" name="09_dynamicschapter03"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="02nDynamicsProcessing" patch="02nDynamicsProcessing.maxpat"/>
	<h1>
		Dynamics Tutorial 2: Dynamics Processing
	</h1>

	<p>
		This tutorial looks at using envelope following techniques to create
		<i>dynamics processors</i> in MSP. Once we've derived the amplitude envelope
		of a waveform as a control signal, we can add patcher logic to make decisions
		on the overall gain of a signal. This allows us to make compressors, limiters,
		and gates based on the time-varying amplitude of our audio.
	</p>


	<bullet>
		In the tutorial patcher, look at the area labeled <m>1</m>. This is a simple
		playback patcher that takes a <o>buffer~</o> and loops it using
		a <o>groove~</o> object. Turn on the audio by clicking the <o>ezdac~</o> and
		turn up the <o>gain~</o> slider to hear the sound. Note that the audio from
		the <o>groove~</o> object goes into a <o>send~</o> object named <m>audio</m>.
		We'll pick up this audio in the subpatchers within the tutorial patcher. Turn
		down the <o>gain~</o> slider and doubleclick the <o>patcher</o> object
		labeled 'Compressor/Limiter'.
	</bullet>

	<h2>
		Audio compression and limiting
	</h2>

	<p>
		If we have an audio signal containing a wide range of amplitudes (such
		as a drum loop or expressive vocals) it's often necessary to reduce,
		or <i>compress</i>, this range. A <i>compressor</i> algorithm reduces
		the dynamic range of an audio signal by tracking its envelope and checking
		that value against a <i>threshold</i> number. When the audio signal exceeds
		the threshold, its gain is reduced by a scaling factor called a
		compression <i>ratio</i>. We can change the behavior of a compressor by
		adjusting its <i>threshold</i> and <i>ratio</i>, as well as by making intelligent
		decisions regarding the way in which the envelope signal is derived.
	</p>

	<bullet>
		In the tutorial <o>patcher</o> labeled 'Compressor/Limiter', turn up
		the <o>gain~</o> slider. Note the difference between the sound and the sound
		of the uncompressed drum loop in the main patcher. Click in the <o>number</o> box
		labeled 'Threshold' and lower its value to <m>0.2</m>. Raise the <o>number</o> box
		labeled 'Ratio' to <m>30.</m>. Listen to the results. Change the 'Decay' value
		to <m>200</m>. Bring the ratio back down to <m>4.</m>, and the threshold to <m>0.5</m>.
		Try raising the 'Attack' value. Listen to the results as you play with the controls,
		and look at the <o>multislider</o> objects at the bottom of the patcher.
	</bullet>

	<p>
		Our compression patcher contains the standard controls that we would find
		on a hardware compressor box or a dynamics plug-in for a DAW program.
		The 'Attack' and 'Decay' controls set parameters for the envelope following
		of the audio signal. These values (converted from milliseconds to samples by
		the <o>*</o> objects) set how fast the <o>rampsmooth~</o> object allows the
		envelope to rise and fall based on the incoming signal. A long attack and
		decay will make the compressor circuit less responsive, but will also limit
		some of the artifacts associated with a highly responsive compressor, such as
		audible 'pumping' of the gain. The 'Threshold' and 'Ratio' controls alter how
		the compressor deals with scaling the gain. Our circuit implements the following
		equation to set the gain, where <m>g</m> is the gain, <m>e</m> is the envelope
		signal, and <m>t</m> and <m>r</m> are the threshold and ratio, respectively:
	</p>

	<p>
		g = ( (e-t)*(1/r) + t ) / e; 0&lt;=g&lt;=1.
	</p>

	<p>
		This gain value <m>g</m> is clipped in the range of <m>0.</m> to <m>1.</m> and
		then multiplied by our original audio to control its volume. The envelope
		value <m>e</m> and output gain <m>g</m> are shown in the patcher in
		the <o>multislider</o> objects labeled 'Enveloped' and 'Reduction'. The
		colors in the 'Reduction' <o>multislider</o> are flipped so that the colored
		area is greater as the gain is lower, visually cueing us into the fact that the
		audio volume is being <i>attenuated</i> by our compressor.
	</p>

	<bullet>
		Set the 'Attack', 'Decay', and 'Threshold' to <m>10.</m>, <m>100.</m>, and <m>0.5</m>,
		respectively. Click on the <m>message</m> box containing the value <m>0.</m> and
		labeled 'Limiter?'. Listen to the results.
	</bullet>

	<p>
		A <i>limiter</i> is a compression circuit with an infinite compression ratio,
		i.e. sound above the threshold is attenuated to never exceed that threshold.
		For a limiter, the <m>r</m> term is infinite, zeroing out part of the equation:
	</p>

	<p>
		g = t / e; 0&lt;=g&lt;=1.
	</p>

	<bullet>
		Experiment more with different compression settings to see how the
		circuit responds. Note that the circuit always attenuates the signal,
		so at very low thresholds it may be necessary to raise the <o>gain~</o> slider
		to hear the results. When you've finished, turn down the <o>gain~</o> slider,
		return to the main tutorial patcher, and open the <o>patcher</o> named 'Gate'.
	</bullet>

	<h2>
		Audio gating
	</h2>

	<bullet>
		Look at the contents of the subpatcher named 'Gate': the settings
		are similar to the compression example, but the results are quite
		different. Turn up the <o>gain~</o> slider and listen to the results
		of the circuit. Look at the <o>multislider</o> objects and see how
		they respond to the drum loop.
	</bullet>

	<p>
		The MSP circuit in this patcher starts on the same premise of a
		compressor: that of an envelope follower and a threshold. Rather
		than reducing the gain of audio energy that exceeds the threshold,
		however, this circuit attenuates signals that fall <i>below</i> it.
		This type of dynamics compression is called a <i>gate</i> (it is also
		sometimes referred to as a <i>noise gate</i>). Unlike a compressor,
		however, the gate has a <i>knee</i> setting instead of a compression
		ratio. This setting is a proportion of the threshold within which the
		audio is attenuated rather than cut altogether. So with the default
		settings in the tutorial patch, we can expect the following results:
	</p>

	<p>
		Sound above <m>0.5</m> are left alone (<m>g</m> = 1).<br />
		Sound below <m>0.5</m> and above <m>0.375</m> (the threshold * the knee) are scaled between 0 and 1.<br />
		Sound below <m>0.375</m> are gated out (<m>g</m> = 0)<br />
	</p>

	<p>
		In equation form, our gate looks something like this, where <m>g</m>
		is the gain, <m>e</m> is the envelope signal, and <m>t</m> and <m>k</m>
		are the threshold and knee:
	</p>

	<p>
		g = (e-(t*k)) / (t-(t*k); 0&lt;=g&lt;=1.
	</p>

	<p>
		As with our compressor, <m>g</m> is clipped between <m>0</m> and <m>1</m>.
	</p>

	<bullet>
		Familiarize yourself with the gate settings. Set the knee as high as
		it will go, and notice at the gate effect now simply cuts in and out.
		Set the knee to a low value, and the gate will fade in and out smoothly.
		See how the 'Attack' and 'Decay' controls on the envelope follower effect
		the ability of our gate to track the beat of the drum loop and gate out the
		silences.
	</bullet>
	<h2>
		Summary
	</h2>

	<p>
		Dynamics processors in MSP can be constructed using signal
		processing algorithms that take an envelope follower output
		as their control signal. Compressors and limiters attenuate
		audio signals that exceed a certain amplitude threshold according
		to their envelopes; gates attenuate audio that falls below a threshold.
		The parameters of the envelope follower itself control the responsiveness
		of the dynamics circuit, while parameters such as the threshold
		and ratio / kne. control the 'sound' of the processing.
	</p>
</chapter>

