<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Sampling Tutorial 3: Playback with Loops">
	<previous module="msp"  name="07_samplingchapter02"></previous>
	<next module="msp" name="07_samplingchapter04"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="03sSamplePlaybackWithLoops" patch="03sSamplePlaybackWithLoops.maxpat"/>

<h1>
	Sampling Tutorial 3: Playback with Loops
</h1>

	<p>
		As we've seen in the last two sampling tutorials, there
		are a variety of objects for accessing data from an MSP <o>buffer~</o>
		object. This tutorial looks at another one which is specialized for
		working with looping sounds: <o>groove~</o>.
	</p>


	<h2>
		Playing samples with <o>groove~</o>
	</h2>

	<p>
		The <o>groove~</o> object is one of the most versatile
		objects for playing sound from a <o>buffer~</o>. You can
		specify the <o>buffer~</o> to read, the starting point,
		the playback speed (either forward or backward), and
		start and end points for a repeating loop within the
		sample. As with other objects that read from a <o>buffer~</o>,
		<o>groove~</o> accesses the <o>buffer~</o> remotely,
		without patch cords, by sharing its name.
	</p>

	<p>
		Take a look at <o>groove~</o> object in the tutorial patcher.
		The <o>groove~</o> object has three inlets which take both
		signals and standard Max messages. The left-hand inlet
		interprets a signal as its playback <i>speed</i>, and a Max
		floating-point numeric message as its playback <i>position</i>.
		This allows for event-level control of the playback point (to
		jump to an area of the sample) as well as a time-varying signal
		controlling the playback rate of the sound. The second and
		third inlets allow you to specify start and end points in the
		<o>buffer~</o> to be used as loops.
	</p>

	<p>
		The <o>sig~</o> object is an MSP object that outputs a constant
		signal based on a floating-point numeric input. This signal
		drives the playback speed of the <o>groove~</o>. A signal
		of <m>1</m> causes normal-speed forward playback; a signal
		of <m>0.5</m> halves the speed (and drops the pitch of the
		sample by an octave); a signal of <m>-2</m> plays the sample
		at double-speed in reverse, and so on. While our tutorial
		uses constant-value signals for playback speed (supplied
		by the <o>sig~</o> object), there is no reason why we couldn't
		use a <o>line~</o>, <o>cycle~</o> or other MSP object as a
		control signal for the speed of the playback.
	</p>

	<p>
		The <o>groove~</o> object also understands a number of
		messages in its left inlet to control its behavior. The
		message <m>loop 1</m> turns looping on, which is to say
		that when the <o>groove~</o> object reaches the end of the
		sample (or the time specified in the third inlet) it will
		return instantly to the beginning of the sample (or the point
		specified in the second inlet). In addition, you can change
		which <o>buffer~</o> the <o>groove~</o> object accesses as its
		sample memory at any time by sending the message <m>set</m>
		followed by the name of a valid <o>buffer~</o> object --- in fact,
		you can do this with any <o>buffer~</o>-accessing MSP object,
		including <o>index~</o> and <o>play~</o>. In the tutorial patcher,
		a <o>umenu</o> object is loaded with the names of the three <o>buffer~</o>
		objects we've got in our patcher.
	</p>

	<h2>
		Play some loops
	</h2>

	<bullet>
		Turn on the audio with the <o>toggle</o> object and adjust the output
		volume of the patch using the floating-point <o>number</o> box on the
		right of the tutorial (labeled <m>Amplitude</m>). Click on the different
		circles in the <o>preset</o> object to play the samples in different ways.
	</bullet>

	<p>
		The first preset just functions as an ‘Off’ button. The next three presets
		play the three <o>buffer~</o> objects at normal speed without looping.
		The rest of the presets demonstrate a variety of sound possibilities using
		different playback speeds on different excerpts of the buffered files, with
		or without looping.
	</p>

	<bullet>
		You may want to experiment with your own settings by changing the user
		interface objects directly.
	</bullet><br/>

	<techdetail>
		<b>Technical detail:</b> In traditional sampler design, the system that
		plays back the samples is very similar in operation to a <o>groove~</o> object.
		Typically (for instrumental samples that are meant to loop) the
		sampler contains a handful of recorded samples of a few seconds
		duration, each of which is bound to a specific <i>root key</i>,
		a note value at which it plays normally. Note values without samples
		play the nearest note that has a sample assigned to it either slightly
		fast or slightly slow by adjusting its playback rate. For notes that are
		meant to sustain (e.g. violins or synthesizer pads), the sample has
		metadata stored with it setting loop points within the sample which
		the synthesizer loops inside during sustained notes. Upon releasing the
		note, the sample plays out to the end.

	</techdetail>

	<p>
		If you want to create smooth undetectable loops with <o>groove~</o>,
		you can use the <m>loopinterp</m> message to enable crossfades between
		the end of a loop and the beginning of the next pass through the loop
		to smooth out the transition back to the start point (see the <o>groove~</o>
		<link type="vignette" module="core" name="docrefpages">Object reference page</link>
		for more information on this message). If the <o>buffer~</o>
		contains an AIFF file that has its own loop points - points established
		in a separate audio editing program - there is a way to use those loop
		points to set the loop points of <o>groove~</o>.
	</p>

	<p>
		The <o>info~</o> object can report the loop points of an AIFF file
		contained in a <o>buffer~</o>, and you can send those loop start and
		end times directly into <o>groove~</o>. You can also use the the <o>info~</o>
		object to find out the length (in milliseconds) of any file you load into a buffer.
	</p>

<illustration><img  src="images/samplingchapter03a.png"/></illustration>

	<caption>
		<i>Using info~ to get loop point information from an AIFF file</i>
	</caption>

	<h2>
		Summary
	</h2>

	<p>
		The <o>groove~</o> object is the most versatile way to play sound
		from a <o>buffer~</o>. You can specify the <o>buffer~</o> to read, the
		starting point, the playback speed (either forward or backward), and
		starting and ending points for a repeating loop within the sample. If
		the <o>buffer~</o> contains an AIFF file that has its own pre-established
		loop points, you can use the <o>info~</o> object to get those loop times
		and send them to <o>groove~</o>. The playback speed of <o>groove~</o> is
		determined by the value of the signal coming in its left inlet. You can
		set the current buffer position of <o>groove~</o> by sending a <m>float</m>
		time value in the left inlet.
	</p>

<seealsolist>
<seealso name="buffer~">Store audio samples</seealso>

<seealso name="groove~">Variable-rate looping sample playback</seealso>
<seealso name="info~">Information about a buffer~</seealso>

<seealso name="sig~">Constant signal of a number</seealso>
</seealsolist>

</chapter>


