<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Dynamics Tutorial 1: Envelope Following">
	<previous module="msp"  name="08_filterchapter05"></previous>
	<next module="msp" name="09_dynamicschapter02"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="01nEnvelopeFollowing" patch="01nEnvelopeFollowing.maxpat"/>

	<h1>
		Dynamics Tutorial 1: Envelope Following
	</h1>

	<p>
		In this group of tutorials, we'll look at different ways to work
		with <i>dynamics</i> in audio signals. The ability to use and control
		the <i>amplitude</i> of an audio waveform is important for many applications,
		from the macro level (audio compression) to the micro level (distortion).
		In this first tutorial, we'll learn to derive control values from amplitude
		parameters of an audio signal which we can use to control parameters
		elsewhere in the signal chain. This technique is called <i>envelope following</i>.
	</p>


	<h2>
		The envelope, please...
	</h2>

	<p>
		We've learned from working with MSP that digital audio is represented as
		a stream of discrete samples, each of which represent the <i>amplitude</i> of
		a signal at a given time. However, when we actually think about (and use)
		the term amplitude, we aren't necessarily thinking of it on a sample-by-sample
		basis. Instead, we discuss the amplitude of a sound based on how <i>loud</i> it
		seems to us as listeners. Alternately, we create objective measurement systems
		to attempt to quantify this (subjective) loudness, such as level meters in
		audio mixing software. Both of these systems look at the amplitude of an
		audio signal not on a sample-by-sample level, but <i>averaged</i> over time.
		This technique of deriving the macro-amplitude of a sound's loudness or
		volume (as opposed to the micro-amplitude of the sample values) is
		called <i>envelope following</i>.
	</p>

	<p>
		We use the term envelope in synthesizer design to refer to the overall dynamic
		shape of the sound. For example, a sound that fades in smoothly and then fades
		out over the same amount of time could be said to have a triangular envelope.
		A sound with a short attack, a long sustain, and a short fade might look like
		a trapezoid. Similarly, we can take any sound source and, with a little bit
		of work, abstract its envelope:
	</p>
		<illustration><img src="images/dynamicschapter01a.png"/></illustration>

	<caption>
		<i>"Is that you?": waveform (top) and envelope (bottom)</i>
	</caption>

	<p>
		To create the envelope above, we looked at the samples within the waveform and
		tracked their <i>average</i> amplitude over time. There are several strategies
		to do this in MSP, involving a trio of objects that allow us to smooth audio
		signals based on different parameters.
	</p>

	<bullet>
		In the tutorial patcher, familiarize yourself with the area labeled <m>1</m>.
		This is a basic sample playback patcher that loads an audio file into a <o>buffer~</o> object
		and plays it in an endless loop using the <o>groove~</o> object. Start the audio, turn
		up the <o>gain~</o> slider, and make sure you can hear the drum loop as it plays.
	</bullet>

	<h2>
		Envelope following in MSP
	</h2>

	<bullet>
		Look at the two objects in yellow attached to the output of the <o>groove~</o> object.
	</bullet>

	<p>
		The first thing that needs to be accomplished when deriving the smoothed amplitude
		of a sound is to convert the signal into a <i>rectified</i> wave; that is, to
		ignore the difference between negative and positive sample values and only
		concentrate on their distance from <m>0</m>. We can do this by taking the absolute
		value of a signal, which can be accomplished using the <o>abs~</o> object in MSP.
	</p>

	<p>
		The <o>snapshot~</o> object allows us to convert one sample of an MSP signal
		into a floating-point number output from the object as a Max event. Every time
		the object receives a <m>bang</m>, it takes the current audio sample and outputs
		it into Max as a number which can be viewed, scaled, or used to trigger Max events.
		In the case of our patcher, the absolute value of our <o>groove~</o> object's
		output is being used to drive the height of a <o>multislider</o> object, with
		the <o>snapshot~</o> providing the intermediary translation from an MSP signal
		t. a Max message. As we can see, the bouncing up and down of the <o>multislider</o>
		gives us a vague impression of the amplitude of the drum loop. However, a more
		accurate impression of how we <i>hear</i> the sound can be accomplished by smoothing
		those values.
	</p>

	<bullet>
		Look at the patcher logic labeled <m>2</m> in our tutorial. In the <o>number</o> box
		labeled 'Up' type the value <m>1.</m> and hit return. In the <o>number</o> box
		labeled 'Down' type the value <m>100.</m> and hit return. Observe the results as
		plotted in the <o>multislider</o> objects below. Now do the reverse (type <m>1.</m> as
		the 'Down' value and <m>100.</m> as the 'Up' value). Notice the difference?
	</bullet>

	<p>
		The output of the <o>send~</o> object (which consists of our drum loop) is
		converted to an absolute value, run through an object called <o>rampsmooth~</o>,
		and plotted in a <o>number</o> box and two <o>multislider</o> objects. The
		right-hand <o>multislider</o> is in one of its scrolling modes, which allows
		us to see values as they unfold over time. The <o>rampsmooth~</o> object
		performs a linear smoothing operation on an input signal. Its two arguments
		(or numbers given at its middle and right inlets) control the way in which the
		smoothing behaves. The middle inlet controls over how many samples the object
		will smooth a changing value when it is <i>rising</i>; the right inlet controls
		how many samples it takes for a signal to <i>fall</i>. These two values allow
		us to independently smooth increasing and decreasing sample values, so that
		sharp onsets can be kept in the envelope but abrupt silences generate a tail.
	</p>

	<bullet>
		Look at the area of the tutorial labeled <m>3</m>. As before, enter <m>1.</m>
		in the <o>number</o> box labeled 'Up' and <m>100.</m> in the <o>number</o> box
		labeled 'Down'. Observe the envelope drawn in the <o>multislider</o>. Now do
		the reverse (type <m>1.</m> as the 'Down' value and <m>100.</m> as the 'Up' value).
		Type <m>100.</m> for both values.
	</bullet>

	<p>
		The <o>slide~</o> object smooths audio signals logarithmically. The two values provided work as denominators for new rising or falling energy entering the (smoothed) signal. For example, an 'Up' value of <m>1000</m> means it will take 1000 samples for a single-sample transition from <m>0</m> to <m>1</m> to occur. The larger the 'Up' and 'Down' values, the smoother the curve.
	</p>

	<bullet>
		Look at patcher area <m>4</m>. In the <o>number</o> box labeled 'Delta',
		type <m>0.01</m> and hit return. Observe the results. Type an even smaller
		number, such as <m>0.0001</m>.
	</bullet>

	<p>
		The <o>deltaclip~</o> object works in a different manner from <o>rampsmooth~</o>
		and <o>slide~</o>. It sets a threshold for the amount an audio signal can change,
		and clips the outgoing signal to that amount. Setting the <m>min</m>
		and <m>max</m> values for the delta to <m>-1</m> and <m>1</m> will allow
		samples to pass normally provided they don't change by more than that amount.
		Smaller values will smooth the audio signal in a <i>relative</i> way so that
		the resulting envelope traces a much narrower set of values than the incoming signal.
	</p>

	<p>
		If we put a burst of noise through the different envelope following
		techniques shown in our tutorial patcher, we can see the difference
		between the curves they generate:
	</p>
		<illustration><img src="images/dynamicschapter01b.png"/></illustration>

	<caption>
		<i>Different envelope followers on a noise burst.</i>
	</caption>

	<p>
		Each of these envelope-following techniques have different musical and
		sonic applications and have different signatures. A logarithmic envelope
		follower controlling the volume of a synthesizer, for example, would
		have a very different sound than a linear one.
	</p>

	<h2>
		Using envelopes as control signals
	</h2>

	<bullet>
		Look at the patcher logic labeled <m>5</m> in the tutorial. Turn up
		the <o>gain~</o> slider to hear the output of the <o>cycle~</o> object.
		Using the <o>umenu</o>, select the different envelope followers as a control
		input. If you like, change their parameters as well to see what types of
		different effects you can achieve.
	</bullet>

	<p>
		The three envelope following patcher areas send their output signals
		through <o>send~</o> objects to any <o>receive~</o> set to the appropriate
		name. In the case of patcher logic <m>5</m>, this signal (ranging
		from <m>0</m> to <m>1</m> controls the frequency of a <o>cycle~</o> object,
		mapping the control signal to between <m>100</m> and <m>1100</m> Hz. The
		difference between the three envelope type should be apparent if they are
		set to fairly strong smoothing values.
	</p>

	<bullet>
		Turn down the <o>gain~</o> slider in patcher area <m>5</m> and look at
		the patcher logic labeled <m>6</m>. Turn up the <o>gain~</o> slider and
		select different sources for a control signal from the <o>umenu</o>.
	</bullet>

	<p>
		In this example, we use our envelope control signal to modulate the cutoff
		frequency of a <o>lores~</o> filter, creating a dynamic wah-wah effect on a
		mixed and detuned waveform.
	</p>

	<bullet>
		Turn down patcher area <m>6</m> and look at the patcher logic labeled <m>7</m>.
		Turn up the <o>gain~</o> slider and select different envelopes from
		the <o>umenu</o>.
	</bullet>

	<p>
		In this example, we are using our envelope to create an 'auto-wah' on the
		drum loop itself. This is a very common use of envelope following in music
		production, where a control signal derived from an audio signal is used to
		control a parameter of an effect applied to that same audio clip further
		down the chain. Using this logic, we could use our envelope follower to
		produce control signals for any signal-processing procedure we want in MSP.
	</p>
		<h2>
		Summary
		</h2>

	<p>
		When talking about the <i>amplitude</i> of an audio signal, we distinguish
		between the sample-by-sample amplitude and the overall loudness of a sound.
		This second shape is called the <i>envelope</i> of a sound, and the signal
		processing involved in deriving it is called <i>envelope following</i>.
		Envelopes of audio signals can be created by looking at the absolute (rectified)
		waveform (created with the <o>abs~</o>) object and smoothing it using MSP objects
		such as <o>rampsmooth~</o>, <o>slide~</o>, or <o>deltaclip~</o>. These smoothed
		control signals can then be scaled and used to control parameters on any synthesis
		or signal processing operation you like. The MSP <o>snapshot~</o> object is useful
		for converting MSP audio signals into floating-point Max numbers which can be
		viewed using objects such as <o>multislider</o>.
	</p>
	<seealsolist>
		<seealso name="abs~">Absolute value of a signal</seealso>
		<seealso name="snapshot~">Convert signal values to numbers</seealso>
		<seealso name="rampsmooth~">Smooth an incoming signal</seealso>
		<seealso name="slide~">Filter a signal logarithmically</seealso>
		<seealso name="deltaclip~">Limit changes in signal amplitude</seealso>
	</seealsolist>
</chapter>

