<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Sampling Tutorial 2: Playing Back From Multiple Sample Points">
	<previous module="msp"  name="07_samplingchapter01"></previous>
	<next module="msp" name="07_samplingchapter03"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="02sPlayingFromMultiplePoints" patch="02sPlayingFromMultiplePoints.maxpat"/>

<h1>
	Sampling Tutorial 2: Playing Back From Multiple Sample Points Simultaneously
</h1>

	<p>
		Part of the logic behind the way MSP works with samples is that
		multiple objects within a Max patcher can access the same sample
		memory of a single <o>buffer~</o> object. This makes it quite easy
		to implement <i>polyphony</i> within a sampler playback patcher,
		as shown in this tutorial.
	</p>

	<h2>
		Loading a sample
	</h2>

	<p>
		Take a look at the tutorial patcher. As with the previous tutorial,
		this one uses a <o>buffer~</o> object (named <m>gerald</m>, in this case),
		to store a sample that can be accessed by other objects. Rather than
		recording into the <o>buffer~</o> we've instructed it to load in
		a sample (called "drumLoop.aif") when the patcher loads. The <m>replace</m>
		message to <o>buffer~</o> differs from the <m>read</m> message in that
		it <i>resizes</i> the <o>buffer~</o> object's memory to accomodate the
		entire soundfile being read; while in this tutorial this would make no
		difference, the two messages behave differently once you begin loading
		and unloading samples into the same <o>buffer~</o> object.
	</p>

	<bullet>
		Double-click the <o>buffer~</o> object to view the contents of the
		sample called <m>gerald</m>.
	</bullet>

<illustration><img  src="images/samplingchapter02a.png"/></illustration>

	<caption>
		<i>a sample called gerald</i>
	</caption>

	<p>
		If we take a look at the loaded sample, we see that it seems
		to contain four sounds, equally spaced in time about a second apart.
		In this patcher, this sample is accessed by a series of three different
		<o>play~</o> objects, all named after our <o>buffer~</o> object...
		these three <o>play~</o> objects are integrated into the patcher logic
		to play back different parts of the sample.
	</p>

	<h2>
		A simple drum machine
	</h2>

	<bullet>
		Turn on the audio for the tutorial patcher and turn up the <o>gain~</o>
		slider. Click on the <o>button</o> objects in the patch and listen to
		the results. These <o>button</o> objects trigger messages instructing
		the different <o>play~</o> objects to access different parts of the
		<o>buffer~</o> we've loaded. The four different ramps generated
		by the <o>line~</o> objects each play a different one-second part of
		the <o>buffer~</o>. Because of the way we designed the audio file that
		the <o>buffer~</o> has loaded, these sounds correspond to the four sounds
		we saw in the waveform display of the <o>buffer~</o>: four drum hits from
		a TR-808 drum machine... a bass drum, a snare drum, a closed hi-hat, and
		an open hi-hat.
	</bullet><br/>

	<p>
		Because all three of our <o>play~</o> objects are capable of accessing
		the same <o>buffer~</o> contents, they can be triggered simultaneously
		and create a polyphonic sound. The rest of the patcher logic
		simulates a very simple aleatoric drum machine, which uses <o>random</o>
		objects driven from a <o>metro</o> to trigger parts of the <o>buffer~</o>
		based on the <m>message</m> boxes. The drum machine uses <o>random</o>
		objects to define a probability of a sound event for the bass drum (one
		chance in three), the snare (one chance in four), and the hi-hats (two
		chances in three, equally divided between the two sounds).
	</p>

	<bullet>
		Click on the <o>toggle</o> box at the top of the patcher to start
		the <o>metro</o>. Once the drum machine starts going, notice that
		it's possible to hear a bass drum, a snare drum, and one of the hi-hat
		samples simultaneously, even though all three sounds live within the
		same <o>buffer~</o>. Notice that, because of the way we've constructed
		our patcher, it's impossible to hear an open and closed hi-hat at the
		same time; both of those sample triggers go to the same <o>play~</o> object,
		which can only sound one 'voice' of our drum machine. As a result, they
		will interrupt one another if they trigger too quickly.
	</bullet>

	<h2>
		Summary
	</h2>

	<p>
		The MSP sampling architecture allows for any number of sample playback objects to access the contents of the same <o>buffer~</o> object. As a result, you can use multiple <o>play~</o> objects to simultaneously access sample data to create a polyphonic texture. Creating audio files that contain samples at regular time intervals makes it easy to set up "banks" of samples within a single file, accessible from MSP by reading at different points in a <o>buffer~</o> sample.
	</p>

</chapter>


