<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="MSP Polyphony Tutorial 2: Granular Synthesis">
	<previous module="msp"  name="11_polychapter01"></previous>
	<next module="msp" name="11_polychapter03"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="02hGranularSynthesis" patch="02hGranularSynthesis.maxpat"/>
	<h1>
		Polyphony Tutorial 2: Granular Synthesis
	</h1>

	<h2>
		Granular synthesis
	</h2>
	<p>
		In this tutorial we'll look at using the <o>poly~</o> object
		to generate large amounts of polyphony in order to play the
		contents of one <o>buffer~</o> of sample data. We'll
		leverage the ability of MSP to play sample data from the
		same <o>buffer~</o> at multiple arbitrary speeds and time
		points to explore the technique of <i>granular synthesis</i>
	</p>
	<p>
		Put simply, granular synthesis is the use of very short
		(or, sometimes, less short) sonic events called 'grains'
		to generate complex textures. While the musical and written
		literature on the technique is beyond the scope of this
		tutorial (see Curtis Roads' <i>Microsound</i> (MIT Press: 2004)
		for a great exploration of this topic), we'll cover the basics
		here. While classic granular synthesis relies on the use of
		very small amounts of wavetable data, the technique we'll
		explore in this tutorial uses sample data taken arbitrarily
		from soundfiles.
	</p>
	<p>
		In our tutorial patcher, we'll create an algorithmic playback
		system based on constrained random values to control the
		following parameters of a polyphonic sample playback engine:
		rate, onset point, duration, pitch, amplitude. We'll also
		look at how adjusting envelopes changes the sonic output.
	</p>
	<h2>
		Experimenting with the patcher
	</h2>
	<p>
		Take a look at the tutorial patcher. There are several numbered
		areas, each of which controls part of our granular synthesis
		engine. The patcher area labeled <m>1</m> is the <i>grain emitter</i>
		proper: a <o>metro</o> object schedules and fires <m>bang</m> messages
		into a <o>poly~</o> object that has loaded 100 voices of an
		abstraction named <m>polygrain~</m>. Area <m>2</m> allows us to
		check our CPU usage depending on the parameters of our synthesizer.
		Area <m>3</m> and <m>4</m> set the synthesis parameters - the
		sample we're using, which area of it to draw from for grains,
		and the parameters of the grain playback system in
		the <m>polygrain~</m> abstraction.
	</p>
	<bullet>
		In patcher area <m>1</m>, turn on the audio and turn up the <o>gain~</o> slider. At the top of the patcher, click the <o>button</o> object a few times and listen to the results. Sending a <m>bang</m> into the <o>poly~</o> object generates a single 'grain' of audio. Turn on the <o>metro</o> object by clicking the <o>toggle</o> at the top of the patcher.
	</bullet><br/>
	<p>
		The <o>poly~</o> object in our patcher generates grains: single
		bursts of sample playback which we can control dynamically by
		adjusting parameters. The <o>metro</o> and <o>button</o> objects
		control the grain emitter. Each time the <o>metro</o> fires,
		it sends a <m>bang</m> into the <o>poly~</o>, prepended by
		the <m>note</m> message, which assigns the <m>bang</m> to the
		first available voice within the <o>poly~</o>. In addition,
		each <m>bang</m> from the <o>metro</o> object schedules the
		next one by adjusting the speed of the <o>metro</o>.
		The <o>random</o> object generates a random value which is
		then put through a <o>scale</o> object with a variable output
		range, defined by the <m>speedmin</m> and <m>speedmax</m> parameters
		found in patcher area <m>4</m>.
	</p>
	<h2>
		Checking CPU
	</h2>
	<bullet>
		With the grain emitter enabled (i.e. the <o>metro</o> object set
		to run), turn on the <o>metro</o> in patcher area <m>2</m>.
		The <o>number</o> box at the bottom of the patcher logic should
		output a number. Turn off the grain emitter at the top and watch
		the results. Turn it on again.
	</bullet>
	<p>
		The <o>adstatus</o> object allows us to control and view aspects
		of the MSP audio driver currently running. All of the viewable
		attributes of the <i>Audio Status</i> window (available under the
		Max <b>Options</b> menu) can be accessed via the <o>adstatus</o> object.
		The <m>cpu</m> mode of the <o>adstatus</o> object (set by its argument)
		instructs the object to receive <m>bang</m> messages and output
		the current CPU usage of MSP. Notice that when the grain emitter
		is turned off, the CPU usage drops to <m>0.</m>. This is because
		our <o>poly~</o> abstraction mutes itself when its playback has
		finished. When no notes are firing, all of the copies of
		the <o>poly~</o> abstraction should be muted.
	</p>
	<h2>
		Adjusting parameters
	</h2>
	<bullet>
		In patcher area <m>3</m>, highlight an area of the <o>waveform~</o> objects
		to select part of the <o>buffer~</o> named <m>thegrain</m>.
		Notice that when you drag on either of the <o>waveform~</o> objects,
		both of them highlight in the same regions. The rightmost outlet
		of the <o>waveform~</o> object allows us to <i>link</i> them
		together so that you can use more than one of the objects to work
		with a multi-channel <o>buffer~</o>. Load a different sample using
		the <o>message</o> boxes above the <o>buffer~</o> object and
		highlight different regions of the sample. The highlighted regions
		of the <o>buffer~</o> controls where the grain emitter draws its
		sample data.
	</bullet>
	<bullet>
		In patcher area <m>4</m>, use the <o>preset</o> object to try out
		different parameters for our grain emitter, then try entering your
		own values. The <i>Grain rate</i> <o>number</o> boxes control the
		speed range of the <o>metro</o> in patcher area <m>1</m>.
		The <i>Grain duration</i> controls the ranges for how long each
		grain plays for inside the <o>poly~</o>. The <i>Grain pitch</i>
		values provide a range for what speed the grains play at.
		The <i>Grain amplitude</i> controls set the volume range of the
		grain emitter, and the <i>Grain slope</i> sets the sharpness of
		the attack and decay on each grain's envelope. Notice how different
		densities of grains changes the sound as well as the CPU usage of
		the grains.
	</bullet>
	<p>
		Before we look at our <o>poly~</o> abstraction, notice the
		effect of longer and shorter grain rates and durations on the
		CPU usage. Longer grain durations and shorter grain rates result
		in more voices inside the <o>poly~</o> being active at any
		one time - either they are fired more frequently, or they
		take longer to 'free' themselves, or both. The result is a
		higher CPU usage.
	</p>
	<bullet>
		In patcher area <m>1</m>, enable the <o>toggle</o> object
		attached to the <o>message</o> box labeled <m>parallel $1</m>.
		Restart the audio by turning the <o>dac~</o> off and on. Notice
		the effect, if any, on the CPU.
	</bullet>
	<p>
		Depending on your computer architecture, you can take advantage
		of multiple cores in your computer's CPU (or multiple processors
		if you have a multi-processor machine) by dividing the <o>poly~</o> object's
		resources over multiple <i>threads</i>. In essence, this divides
		the instances of the <o>poly~</o> object across the different
		cores or processors of your computer, allowing sets of voices
		to run in parallel. Depending on your computer's CPU architecture,
		this may provide a significant boost in performance.
	</p>
	<h2>
		Inside the patch
	</h2>
	<bullet>
		Double-click the <o>poly~</o> object to view an instance of the
		abstraction named <m>polygrain~</m>. Take a look around the patcher.
	</bullet>
	<p>
		The <m>polygrain~</m> abstraction recieves a single <m>bang</m>
		(via the <o>in</o> object at the top of the patcher) and uses it
		to generate a grain of audio, using the MSP logic at the bottom
		of the abstraction. The <o>trigger</o> object at the top of the
		patch clearly sets up the order of events for generating our grain:
	</p>
	<p>
		First, the <o>thispoly~</o> object receives a <m>mute 0</m>
		and <m>1</m> message in immediate succession. This turns <i>on</i> (unmutes)
		the signal processing in the instance, and sets it's state to 'busy',
		so that it won't receive any more note messages until the grain is finished.
	</p>
	<p>
		Next, a <m>bang</m> is dispatched to generate a random <i>amplitude</i>
		for the grain, which goes into the right side of the <o>*~</o> object
		labeled 'how loud is this grain?'. This <o>*~</o> controls the
		scaling for the output of the <o>line~</o> object above that sets
		the grain envelope.
	</p>
	<p>
		Third, a random <i>pitch</i> is selected which is transformed into
		a duration multiplier for the <o>line~</o> objects controlling
		the playback of the sample and its amplitude envelope. The <o>!/</o> object
		divides the incoming pitch into <m>1.</m>, so that a requested
		pitch of <m>2.</m> tells the objects downstream to multiply their
		durations by <m>0.5</m> (half as long, and up an octave).
	</p>
	<p>
		Fourth, a random <i>duration</i> is generated, which sets up the
		parameters for the <o>line~</o> objects so that they generate
		the appropriately scaled and offset values for the grain length.
	</p>
	<p>
		Finally, a grain is triggered by generating a random <i>start point</i>
		based on the highlighted areas in the <o>waveform~</o> object in the
		main patcher. This <m>bang</m> eventually generates two messages
		which command the two <o>line~</o> objects to generate the playback
		curve for the <o>play~</o> object and the amplitude envelope for
		the <o>*~</o> objects.
	</p>
	<p>
		Once the 'envelope' <o>line~</o> is finished, it sends a <m>bang</m>
		to <m>mute</m> the instance and set it to 'free' (<m>0</m>), so it
		can receive a new message.
	</p>
	<bullet>
		Under the <b>File</b> menu in Max, select <i>Modify Read Only</i>.
		This will allow you to unlock the copy of the <m>simplegrain~</m> abstraction
		you are viewing. Unlock the patcher, and place 'watchpoints' on some
		of the patchcords to monitor their values. In the <i>Watchpoints</i>
		window, you should see how different values in the grain settings
		in the main patcher translate into values for the synthesis algorithm
		at work here.
	</bullet>
	<h2>
		Summary
	</h2>
	<p>
		The <o>poly~</o> object allows you to have a large number of instances
		of a single, simple MSP patcher. You can use <o>send</o>
		and <o>receive</o> to communicate to all instances of a <o>poly~</o>
		abstraction, which can be distributed across multiple cores or
		processors with the <m>parallel</m> message.
		The <o>adstatus</o> object allows you to access and change
		aspects of the MSP audio driver; the <m>cpu</m> argument to the
		object lets you see how much of your computer's CPU you are using
		with a patcher.
	</p>
	<seealsolist>
		<seealso name="adstatus">Report and control audio driver settings</seealso>
	</seealsolist>
</chapter>

