<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Sampling Tutorial 6: Record and Play Audio Files">
	<previous module="msp"  name="07_samplingchapter05"></previous>
	<next module="msp" name="07_samplingchapter07"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="06sRecordAndPlayAIFFFiles" patch="06sRecordAndPlayAIFFFiles.maxpat"/>

<h1>
	Sampling Tutorial 6: Record and Play Audio Files
</h1>


	<h2>
		Playing from memory vs. playing from disk
	</h2>

	<p>
		You have already seen how to store sound in memory - in
		a <o>buffer~</o> - by recording into it directly or by
		reading in a pre-recorded audio file. Once the sound is
		in memory, it can be accessed in a variety of ways
		with <o>cycle~</o>, <o>lookup~</o>, <o>index~</o>,
		<o>play~</o>, <o>groove~</o>, <o>wave~</o>, etc.
	</p>

	<p>
		The main limitation of <o>buffer~</o> for storing samples, of
		course, is the amount of unused RAM available on your computer.
		You can only store as much sound in memory as you have memory
		to hold it. For playing and recording very large amounts of audio
		data, it is more reasonable to use the hard disk for storage.
		But it takes more time to access the hard disk than to access RAM;
		therefore, even when playing from the hard disk, MSP still needs
		to create a small buffer to preload some of the sound into memory.
		That way, MSP can play the preloaded sound <i>while</i> it is
		getting more sound from the hard disk, without undue delay or
		discontinuities due to the time needed to access the disk.
	</p>

	<h2>
		Record audio files: <o>sfrecord~</o>
	</h2>

	<p>
		MSP has objects for recording directly into, and playing directly
		from, an audio file: <o>sfrecord~</o> and <o>sfplay~</o>. Recording
		an audio file is particularly easy, you just open a file, begin recording,
		and stop recording. (You don't even need to close the file; the
		<o>sfrecord~</o> object takes care of that for you.) In the upper right
		corner of the Patcher window there is a patch for recording files.
	</p>

	<p>
		The <o>sfrecord~</o> object records to disk whatever signal data it
		receives in its inlets. The signal data can come directly from
		an <o>adc~</o> or <o>ezadc~</o> object, or from any other MSP object.
	</p>

	<bullet>
		Click on the <m>open</m> <o>message</o> box marked ‘Create an AIFF file’. You will
		be shown a Save As dialog box for naming your file. Navigate to the
		folder where you want to store the sound, name the file, and click
		Save. Turn audio on. Click on the <o>toggle</o> to begin recording;
		click on it again when you have finished.
	</bullet>

	<h2>
		Play audio files: <o>sfplay~</o>
	</h2>

	<p>
		In the left part of the tutorial patcher there is a patch for playing
		audio files. The basic usage of <o>sfplay~</o> requires only a few
		objects, as shown in the following example. To play a file, you
		just have to open it and start <o>sfplay~</o>. The audio output
		of <o>sfplay~</o> can be sent directly to <o>dac~</o>
		or <o>ezdac~</o>, and/or anywhere else in MSP.
	</p>

	<bullet>
		Click on the <m>open </m><o>message</o> box marked ‘Set the current file’,
		and open the audio file you have just recorded. Then (with audio on)
		click on the <o>toggle</o> marked ‘Play/Stop’ to hear your file.
	</bullet>
	<techdetail>
		<o>Sfplay~</o> can play .aif, .wav, and mp3 files.
	</techdetail>

	<h2>
		Play excerpts on cue
	</h2>

	<p>
		Because <o>sfplay~</o> does not need to load an entire audio file into
		memory, you can actually have many files open in the same <o>sfplay~</o> object,
		and play any of them (or any portion of them) on cue. The most recently
		opened file is considered by <o>sfplay~</o> to be the ‘current’
		file, and that is the file it will play when it receives the
		message <m>1</m>.
	</p>

	<bullet>
		Click on the remaining <m>open </m><o>message</o> boxes to open some other
		audio files, and then click on the <o>message</o> box
		marked ‘Define cues, 2 to 9’.
	</bullet>

	<p>
		The <m>preload</m> message to <o>sfplay~</o> specifies an entire file or a
		portion of a file, and assigns it a <i>cue number</i>. From then on, every
		time <o>sfplay~</o> receives that number, it will play that cue. In the
		example patch, cues <m>2</m>, <m>3</m>, and <m>4</m> play entire files,
		cue <m>5</m> plays the first 270 milliseconds of <i>sacre.aiff,</i> and
		so on. Cue <m>1</m> is always reserved for playing the current (most
		recently opened) file, and cue <m>0</m> is reserved for stopping <o>sfplay~</o>.
	</p>

	<p>
		Whenever <o>sfplay~</o> receives a cue, it stops whatever it is playing
		and immediately plays the new cue. (You can also send <o>sfplay~</o>
		a <i>queue of cues</i>, by sending it a <m>list</m> of numbers, and it
		will play each cue in succession.) Each <m>preload</m> message actually
		creates a small buffer containing the audio data for the beginning of
		the cue, so playback can start immediately upon receipt of the cue number.
	</p>

	<p>
		Now that cues 0 through 9 are defined, you can play different audio
		excerpts by sending <o>sfplay~</o> those numbers. The upper-left
		portion of the patch permits you to type those numbers directly from
		the computer keyboard.
	</p>

	<bullet>
		Click on the toggle marked ‘Keyplay On/Off’. Type number keys
		to play the different pre- defined cues. Turn ‘Keyplay’ off when you
		are done.
	</bullet>

	<h2>
		Try different file excerpts
	</h2>

	<p>
		Before you define a cue, you will probably need to listen to
		segments of the file to determine the precise start and end times
		you want. You can use the <m>seek</m> message to hear any segment
		of the current file.
	</p>

	<bullet>
		Open your own audio file again (or any other audio file) to make it
		the current file. In the right portion of this patch, enter an end time for
		the <m>seek</m> message. The excerpt you have specified will begin playing.
		Try different start and end times.
	</bullet><br/>

	<p>
		Once you find start and end times you like, you could use them in
		a <m>preload</m> message to establish a cue. Because <o>sfplay~</o>
		can't know in advance what excerpt it will be required to play in
		response to a <m>seek</m> message, it can't preload the excerpt.
		There will be a slight delay while it accesses the hard disk before
		it begins playing. For that reason, <m>seek</m> is best used as an
		auditioning tool; preloaded cues are better for performance
		situations where immediate playback is more critical.
	</p>

	<h2>
		Trigger an event at the end of a file
	</h2>

	<p>
		The patch in the lower right portion of the Patcher window demonstrates
		the use of the right outlet of <o>sfplay~</o>. When a cue is done
		playing (or when it is stopped with a <m>0</m> message), <o>sfplay~</o>
		sends a <m>bang</m> out the right outlet. In this example patch,
		the <m>bang</m> is used to trigger the next (randomly chosen) cue,
		so <o>sfplay~</o> effectively restarts itself when each cue is done.
	</p>

	<p>
		Note the importance of the <o>gate</o> object in this patch. If it were
		not present, there would be no way to stop <o>sfplay~</o> because
		each <m>0</m> cue would trigger another non-zero cue. The <o>gate</o>
		must be closed before the <m>0</m> cue is sent to <o>sfplay~</o>.
	</p>

	<bullet>
		In the patch marked ‘Play random excerpts’, click on the <o>message</o> box
		to preload the cues, then click on the <o>toggle</o> to start the process. To
		stop it, click on the <o>toggle</o> again. Turn audio off.
	</bullet>

<h2>Summary</h2>

	<p>
		For large and/or numerous audio samples, it is often better
		to read the samples from the hard disk than to try to load
		them all into RAM. The objects <o>sfrecord~</o> and <o>sfplay~</o>
		provide a simple way to record and play audio files to and from
		the hard disk. The <o>sfplay~</o> object can have many audio
		files open at once. Using the <m>preload</m> message, you
		can pre-define ready cues for playing specific files or
		sections of files. The <m>seek</m> message to <o>sfplay~</o> lets
		you try different start and end points for a cue. When a cue is
		done playing (or is stopped) <o>sfplay~</o> sends a <m>bang</m>
		out its right outlet. This <m>bang</m> can be used to trigger
		other processes, including sending <o>sfplay~</o> its next cue.
	</p>

<seealsolist>
<seealso name="sfplay~">Play audio file from disk</seealso>

<seealso name="sfrecord~">Record to audio file on disk</seealso>
</seealsolist>

</chapter>


