<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP MIDI Tutorial 3: MIDI Sampler">
	<previous module="msp"  name="10_midichapter02"></previous>
	<next module="msp" name="11_polychapter01"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="03iMIDISampler" patch="03iMIDISampler.maxpat"/>

<h1>
	MIDI Tutorial 3: MIDI Sampler
</h1>

	<p>
		In this tutorial we talk about how to create a MIDI-controllable sampler in MSP.
		Along the way, we'll look at different features of many samplers, including looping,
		keymaps, and multi-timbral operation.
	</p>

	<bullet>
		To use the tutorial patchers in this section of the tutorial, make sure you
		have a correctly configured MIDI controller connected to your computer. The
		tutorials in this section use a variety of MIDI messages as example input; if your
		controller lacks any of these features, you can simulate their input with user
		interface objects in the tutorial.
	</bullet>


	<h2>
		Our fun sampler
	</h2>

	<bullet>
		Take a look at the patcher for this tutorial. The two sets of patcher logic
		(labeled <m>1</m> and <m>2</m> both deal with playing back samples stored in
		<o>buffer~</o> objects by using <o>groove~</o> objects under MIDI control.
		Look at patcher area <m>1</m>. Turn on the audio by clicking the <o>ezdac~</o>
		and turn up the <o>gain~</o> slider. From the <o>umenu</o> object at the top of
		the patch, select option <m>1</m> ('bd+hh.aiff'). Play some notes on your MIDI
		keyboard. You should hear a the bass drum sample at different pitches depending
		on which key you press. Select option <m>2</m> and take a listen; you should hear
		the snare drum.
	</bullet>

	<bullet>
		Select option <m>3</m> ('cym.aiff') from the <o>umenu</o>. Play a note
		on the keyboard and hold it down, or select a note from the <o>kslider</o> object.
		Notice that the sample loops. Select option <m>4</m>; notice that the bass guitar
		sound loops a swell. Play the sounds triggered by options <m>5</m> and
		<m>6</m>. Double-click the <o>coll</o> file named <m>sampler</m>.
	</bullet>

	<p>
		The <o>umenu</o> object in our tutorial patcher causes the <o>coll</o>
		object to dump out different data depending on which sample we select. Looking at
		its contents by double-clicking it reveals the entire database:
	</p>

	<p>
		1, 24 sample1 0 0 0; <i>(bd+hh.aiff)</i><br />
		2, 33 sample2 0 0 0; <i>(snare.aiff)</i><br />
		3, 50 sample3 0.136054 373.106537 1; <i>(cym.aiff)</i><br />
		4, 67 sample4 60.204079 70.476189 1; <i>(bass.aiff)</i><br />
		5, 84 sample5 0 0 0; <i>(epno.aiff)</i><br />
		6, 108 sample6 0 0 0; <i>(ahkey.aiff)</i><br />
	</p>

	<p>
		Each entry in the file sets up the <o>groove~</o> object in the patcher to
		play a sample. The format of each line in the <o>coll</o> file is:</p>
	<p>	<i>base_key &#x2002; buffer~_name  &#x2002; loop_start  &#x2002; loop_end  &#x2002; looping</i></p>
	<p>The <i>base key</i> refers to which MIDI note the sample will play at
		<i>normal</i> speed. The second item in the <o>coll</o> refers to which
		<o>buffer~</o> object will play the sample. The next three values determine the
		start and end points of an internal loop which the <o>groove~</o> object will play,
		and whether or not to use it at all (the last value).
	</p>

	<p>
		The point of a sampler is to play back recordings (audio samples), often from
		an acoustic instrument. If an acoustic instrumental source is used, it's
		inefficient to create a unique sample for every possible note on that instrument.
		Instead, every few notes are sampled, and in-between pitches are achieved by
		playing these sampled notes slightly fast or slow based on their base key. Similarly,
		a sampler is capable of playing notes that sustain for far longer than it's practical to
		record an instrumental sample. Instead, a sample of modest length is used, as an
		area is found <i>within</i> the sustaining part of the sample that can be safely
		looped. When you play a note and hold it, the sample starts playback at the
		beginning; once it moves into the loop zone, it repeats that area over and over again;
		when you pick up the note, it plays from the loop zone through to the end of the
		sample.
	</p>

	<bullet>
		Click through the <o>umenu</o> again and notice how the different
		parameters coming from the <o>coll</o> object set up the <o>groove~</o>
		object.
	</bullet>

	<p>
		The <i>base key</i> is converted from MIDI to frequency by the
		<o>mtof</o> object and used as a divisor for the frequency we want to play. In this
		way, we get a <i>ratio</i> of the desired frequency and the base frequency which
		we can use to set the speed of a <o>groove~</o> object. If we want the sound to
		come out an octave higher, we want the ratio to be <m>2.0</m>; an octave lower, it
		should be <m>0.5</m>. The second value out of the <o>coll</o> file is formatted
		with a <m>set</m> prefix by the <o>prepend</o> object and send to the
		<o>groove~</o> to select the appropriate <o>buffer~</o> to play. The loop values
		come out as numeric data, setting the loop start and end points and sending a loop
		value into an <o>int</o> box which is triggered each time a note plays. Note-on
		values (i.e. values with velocities greater than <m>0</m>) passed from the
		<o>stripnote</o> object trigger the <m>loop</m> message, set the value of the
		<o>sig~</o> object, and restart the sample by sending a <m>0</m> into the
		<o>groove~</o> object. Note-off objects set the <m>loop</m> state to <m>0</m>,
		allowing the <o>groove~</o> to play out the sample to the end and then
		stop.
	</p>

	<h2>
		Multi-timbral samplers and keymaps
	</h2>

	<bullet>
		Look at the patcher logic labeled <m>2</m> on the right of the tutorial
		patcher. As in the last tutorial, a <o>poly</o> object is used to route MIDI data to a
		number of instances of a single abstraction, this time called
		<m>samplervoice~</m>. However, the values out of the <o>poly</o> object do
		more than set the voice allocation.
	</bullet>

	<bullet>
		Turn down the <o>gain~</o> slider in patcher area <m>1</m> and turn
		up the one in patcher area <m>2</m>. Using an attached MIDI keyboard, play some
		notes all over the range of the keyboard. Notice that, depending on which notes you
		play, different samples are heard. Play some chords. Notice that you can have as
		many as four notes playing simultaneously. Look at the <o>message</o> box
		attached to the <o>funbuff</o> object:
	</bullet>

	<p>
		0 1,<br />
		41 2,<br />
		48 3,<br />
		53 4,<br />
		68 5,<br />
		96 6
	</p>

	<p>
		The <o>funbuff</o> object is loaded with these values to use them as a
		<i>key map</i> for a multi-timbral sampler. Any MIDI notes whose pitch values are
		between <m>0</m> and <m>40</m> will trigger sample <m>1</m> (as defined in
		the <o>coll</o> file); pitches between <m>41</m> and <m>47</m> will trigger
		sample <m>2</m>; and so on.
	</p>

	<bullet>
		Double-click any of the abstractions named <m>samplervoice~</m> and
		look at the patcher logic inside. Notice how it resembles the patcher logic in area
		<m>1</m> of the main patcher, with the addition of some objects to handle MIDI
		velocity to scale the amplitude output of the <o>groove~</o> object.
	</bullet>

	<p>
		The MIDI velocity of incoming notes is divided by <m>127.</m> to scale it
		between <m>0.</m> and <m>1.</m> It is then multiplied <i>by itself</i>, creating
		an exponential scaling wherein higher values on the MIDI velocity continuum yield
		far greater increases in volume than lower numbers. This simulates the behavior of
		logarithmic volume circuits (such as mixer faders) in analog audio
		equipment.
	</p>

	<bullet>
		Return to the main patcher, and click the <o>toggle</o> box labeled 'Play a
		sequence'. Double-click the <o>patcher</o> object named <m>sequence</m> as
		you rock out to the sequence playing out of our sampler.
	</bullet>

	<p>
		The <m>sequence</m> subpatch contains a <o>seq</o> object, a
		<o>midiflush</o>, and a <o>midiparse</o>. These objects load a MIDI file and
		output the raw bytes in response to <m>start</m> and <m>stop</m> messages
		(<o>seq</o>), shut off all sounding notes in a MIDI byte stream in response to a
		<m>bang</m> (<o>midiflush</o>), and parse and extract the pitch/velocity pairs
		from a MIDI stream so that they can be used elsewhere (<o>midiparse</o>). This
		allows our awesome MIDI sequence to be played by the sampler logic in the main
		patch.
	</p>

	<h2>
		Summary
	</h2>

	<p>
		MIDI-controllable samplers can be created using MSP <o>buffer~</o> and
		<o>groove~</o> objects. Different parameters of sampler data (loop points,
		sample name, base key) can be stored in <o>coll</o> files for easy access so that
		you can easily switch samples depending on MIDI events within the same MSP
		patcher logic.
	</p>

		</chapter>



