<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Sampling Tutorial 1: Recording and Playback">
	<previous module="msp"  name="06_synthesischapter05"></previous>
	<next module="msp" name="07_samplingchapter02"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="01sRecordAndPlaySamples"
patch="01sRecordAndPlaySamples.maxpat"/>

<h1>
	Sampling Tutorial 1: Recording and Playback
</h1>

	<p>
		The next group of tutorials introduce concepts for the playback within
		MSP of audio <i>samples</i>. Samples can be recorded and played back in a variety
		of ways, depending on your requirements, and can be accessed either from
		computer memory or as an audio file on your hard drive.
	</p>


	<h2>
		Sound input: adc~
	</h2>

	<p>
		Just as the <o>dac~</o> object exists for getting sound out of your
		computer (to your headphones or speakers), an object exists for getting audio from
		the real world <i>into</i> your computer: the analog-to-digital conversion object
		called <o>adc~</o>. It recognizes all the same messages as the <o>dac~</o>
		object, but instead of sending signal to the audio output jacks of the computer,
		<o>adc~</o> receives signal from the audio input jacks, and sends the incoming
		signal out its outlets. Just as <o>dac~</o> has a user interface version called
		<o>ezdac~</o>, there is an iconic version of <o>adc~</o> called <o>ezadc~</o>.
	</p>

<illustration><img src="images/samplingchapter01a.png"/></illustration>

	<caption>
		<i>adc~ and ezadc~ get sound from the audio input jacks and send it out as a signal</i>
	</caption>

	<p>
		To use the <o>adc~</o> object, you need to send sound from some source into the computer. The sound may come from any line level source such as a media player, or from a microphone - your computer might have a built-in microphone, or you can use a standard microphone via a preamplifer. If you have a external audio interface, you can select it from the Audio Status window and use the inputs on that device as well.
	</p>

	<bullet>
		Double click on the <o>adc~</o> object to open the Audio Status
		window. Make sure that the <i>Input Source</i> popup menu displays the input
		device you want. Depending on your computer system, audio card and driver, you
		may not have a choice of input device-this is nothing to be concerned about.
	</bullet>

<br/>

	<bullet>
		In the section of the tutorial patcher labeled <m>1</m>, click on the
		toggle above the <o>adc~</o> object to turn audio on. If you want to hear the input
		sound played directly out the output jacks, adjust the <o>gain~</o> object marked
		<i>Audio thruput level</i>. If you are using the internal microphone on your
		computer, be careful for feedback when playing sound out of the built-in speakers at
		the same time.
	</bullet>

	<h2>
		Storing audio in MSP: buffer~
	</h2>

	<p>
		In the lower part of the patch notice the object called
		<o>buffer~</o>. Though it isn't connected to the rest of the patcher logic, the
		<o>buffer~</o> object provides the central functionality of this patcher - it stores
		the sample data from the sound we'll be working with. In MSP, all sample playback
		from computer memory takes place via <o>buffer~</o> objects. The object is
		created with a <m>name</m> as its argument, which is arbitrary. This name is
		used by other objects to <i>access</i> the audio data in the <o>buffer~</o> object.
		So in our case, our <o>buffer~</o> is named <m>soundbyte</m>; any objects in the
		patcher which need to play or record into that chunk of sample memory must also
		be set to use <m>soundbyte</m> as their <o>buffer~</o> name. The second
		argument to <o>buffer~</o> sets the initial length of the sample in milliseconds. In
		our tutorial, we'll be working with a 2-second (2000 millisecond) sample of audio.
	</p>


	<h2>
		Recording a sound: record~
	</h2>

	<p>
		A variety of objects exist to access the data in a <o>buffer~</o>. In
		the section of the patch labeled <m>2</m> (in the upper-left), the <o>record~</o>
		object takes an MSP signal and <i>writes</i> it into the <o>buffer~</o> designated
		by the object's argument.
	</p>

	<p>
		When <o>record~</o> receives a non-zero integer in its left inlet, it
		begins recording the signal connected to its record inlet; a <m>0</m> stops the
		recording. You can specify recording start and end points within the
		<o>buffer~</o> by sending numbers in the two right inlets of <o>record~</o>. If
		you don't specify start and end points, recording will fill the entire <o>buffer~</o>.
		Notice that the length of the recording is limited by the length of the
		<o>buffer~</o>.
	</p>

	<p>
		In the tutorial patch, <o>record~</o> will stop recording after 2
		seconds (2000 ms). We have included a delayed <m>bang</m> to turn off the
		<o>toggle</o> after two seconds, but this is just to make the <o>toggle</o>
		accurately display the state of <o>record~</o>. It is not necessary to stop
		<o>record~</o> explicitly, because it will stop automatically when it reaches its
		end point or the end of the <o>buffer~</o>.
	</p>

	<bullet>
		Make sure that you have sound coming into the computer, then click
		on the <o>toggle</o> to record two seconds of the incoming sound. If you want to,
		you can double-click on the <o>buffer~</o> afterward to see the recorded signal.
	</bullet>

	<h2>
		Reading through a buffer~: index~
	</h2>

	<p>
		Once you get sound into a <o>buffer~</o> object, there are several
		objects that exist in MSP to <i>read</i> the data back out as an MSP signal. In
		addition, you can save your record to an you an audio file by clicking the
		<m>write</m> message attached to the <o>buffer~</o> in the lower-right.
	</p>

	<p>
		The simplest way to play back the contents of a <o>buffer~</o> is to
		use the <o>index~</o> object, shown in the area of the tutorial patcher labeled
		<m>3</m>. The <o>index~</o> object receives a <m>signal</m> as its input,
		which represents a sample number. It looks up that sample in its associated
		<o>buffer~</o>, and sends the value of that sample out its outlet as a signal. The
		<o>count~</o> object just sends out a signal value that increases by one with each
		sample. So, if you send the output of <o>count~</o> - a steady stream of increasing
		numbers - to the input of <o>index~</o> - which will treat them as sample
		numbers - <o>index~</o> will read straight through the <o>buffer~</o>, playing
		it back at the current sampling rate.
	</p>

	<bullet>
		Turn up the <o>gain~</o> slider labeled <m>Indexed level</m>, and
		click on the <o>button</o> marked <m>Play</m> to play the sound in the
		<o>buffer~</o>. You can change the starting sample number by sending a different
		starting number into <o>count~</o>. Remember that these values are in
		<i>samples</i>, not milliseconds, so depending on your audio hardware's sampling
		rate different numbers will mean different things. For example, to play the sample
		starting halfway into the <o>buffer~</o> on regular CD-quality computer
		hardware, you would use the value <m>44100</m>.
	</bullet>

	<h2>
		Variable speed playback: play~
	</h2>

	<p>
		While the <o>index~</o> object is a useful one to simply play a
		sample forwards at normal speed, part of the creative fun of sampling is the ability
		to manipulate sample plackback in different ways. The <o>play~</o> object
		(shown in the section of the patcher labeled <m>4</m> allows you to access a
		<o>buffer~</o> object's contents much as if it were an analogue tape, with the
		signal input to <o>play~</o> setting the position of the play head.
	</p>

	<p>
		As with <o>index~</o>, the <o>play~</o> object receives a signal in
		its inlet which indicates a position which it uses to play audio out of its associated
		<o>buffer~</o>. The <o>play~</o> object differs from <o>index~</o> in two
		important ways, however. First, the unit of time for the <o>play~</o> object is in
		milliseconds, allowing for an easier understanding of where you're playing in the
		sample. Second, when <o>play~</o> receives a position that falls between two
		samples in the <o>buffer~</o> it interpolates between those two values. For this
		reason, you can read through a <o>buffer~</o> at any speed by sending an
		increasing or decreasing signal to <o>play~</o>, and it will interpolate between
		samples as necessary.
	</p>

	<p>
		Because the <o>play~</o> object puts out the sample value that
		correlates to the time given at its signal inlet, in order to hear anything, that time
		needs to be changing constantly; this is similar to the way a tape recorder works... a
		constant signal sent to a <o>play~</o> object is the equivalent of a tape record set
		to "pause". The most obvious way to use the <o>play~</o> object is to send it a
		linearly increasing (or decreasing) signal from a <o>line~</o> object, as shown in
		the tutorial patcher.
	</p>

	<p>
		Reading from 0 to 2000 (millisecond position in the <o>buffer~</o>)
		in a time of 2000 ms produces normal playback. Reading from 0 to 2000 in 4000 ms
		produces half-speed playback, and so on. Generating a ramp with the <o>line~</o>
		object that runs from high to low (e.g. from 2000 ms back to 0 ms) will play the
		audio backwards.
	</p>

	<bullet>
		Click on the different <o>message</o> box objects to hear the sound
		played in various speed/direction combinations. Turn audio off when you have
		finished.
	</bullet>

	<p>
		Although not demonstrated in this tutorial patch, it's worth noting
		that you could use other signals as input to <o>play~</o> in order to achieve
		accelerations and decelerations, such as an exponential curve from a
		<o>curve~</o> object or even an appropriately scaled  sinusoid from a
		<o>cycle~</o> object.
	</p>

	<h2>
		Summary
	</h2>

	<p>
		Sound coming into the computer enters MSP via the <o>adc~</o>
		object. The <o>record~</o> object stores the incoming sound - or any other signal
		-- in a <o>buffer~</o>. You can record into the entire <o>buffer~</o>, or you can
		record into any portion of it by specifying start and end buffer positions in the two
		rightmost inlets of <o>record~</o>. For simple normal-speed playback of the
		sound in a <o>buffer~</o>, you can use the <o>count~</o> and <o>index~</o>
		objects to read through it at the current sampling rate. Use the <o>line~</o> and
		<o>play~</o> objects for variable-speed playback and/or for reading through the
		<o>buffer~</o> in both directions.
	</p>

<seealsolist>
	<seealso name="adc~">Audio input and on/off</seealso>

	<seealso name="ezadc~">Audio on/off; analog-to-digital converter</seealso>

	<seealso name="buffer~">Store audio samples</seealso>

	<seealso name="index~">Sample playback without interpolation</seealso>

	<seealso name="play~">Position-based sample playback</seealso>

	<seealso name="record~">Record sound into a buffer</seealso>
</seealsolist>

</chapter>



