<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Sampling Tutorial 7: Sampling Review">
	<previous module="msp"  name="07_samplingchapter06"></previous>
	<next module="msp" name="08_filterchapter01"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="07sReviewSampleAndPlay" patch="07sReviewSampleAndPlay.maxpat"/>

<h1>
	Sampling Tutorial 7: Sampling Review
</h1>


	<h2>
		A sampling exercise
	</h2>

	<p>
		In this chapter we suggest an exercise to help you check
		your understanding of how to sample and play audio. Try
		completing this exercise in a new file of your own before
		you check the solution given in the example patch. (But don't
		have the example Patcher open while you design your own patch,
		or you will hear both patches when you turn audio on.) The
		exercise is to design a patch in which:
	</p>
	<ul>

	<li>
		Typing the <i>a</i> key on the computer keyboard turns audio on.
		Typing <i>a</i> again toggles audio off.
	</li>

	<li>
		Typing <i>r</i> on the computer keyboard makes a one-second
		recording of whatever audio is coming into the computer.
	</li>

	<li>
		Typing <i>p</i> plays the recording. Playback is to be at half
		speed, so that the sound lasts two seconds.
	</li>

	<li>
		An amplitude envelope is applied to the sample when it is
		played, tapering the amplitude slightly at the beginning and
		end so that there are no sudden clicks heard at either end of
		the sample.
	</li>

	<li>
		The sample is played back with a 3 Hz vibrato added to it.
		The depth of the vibrato is one semitone  up and down (a factor
		of 2<sup>&#0177;1/12</sup>).
	</li>
</ul>
	<h2>
		Hints
	</h2>

	<p>
		You will need to store the sound in a <o>buffer~</o> and
		play it back from memory.
	</p>

	<p>
		You can record directly into the <o>buffer~</o> with <o>record~</o>.
		(See <i><link type="tutorial" module="msp" name="07_samplingchapter01">Record and Play Samples</link></i>.) The input to <o>record~</o> will come
		from <o>adc~</o> (or <o>ezadc~</o>).
	</p>

	<p>
		The two obvious choices for playing a sample from a <o>buffer~</o>
		at half speed are <o>play~</o> and <o>groove~</o>. However,
		because we want to add vibrato to the sound - by continuously
		varying the playback speed - the better choice is <o>groove~</o>,
		which uses a (possibly time-varying) signal to control its playback
		speed directly. (See <i><link type="tutorial" module="msp" name="07_samplingchapter03">"Playback with Loops"</link></i>.)
	</p>

	<p>
		The amplitude envelope is best generated by a <o>line~</o> object
		which is sending its output to a <o>*~</o> object to scale the
		amplitude of the output signal (coming from <o>groove~</o>). You
		might want to use a <o>function</o> object to draw the envelope,
		and send its output to <o>line~</o> to describe the envelope.
		(See <i><link type="tutorial" module="msp" name="06_synthesischapter01">"Additive Synthesis"</link></i>.)
	</p>

	<p>
		The computer keyboard will need to trigger messages to the <o>adc~</o>,
		<o>record~</o>, <o>groove~</o>, and <o>line~</o> (or <o>function</o>)
		objects in order to perform the required tasks. Use the <o>key</o> object
		to get the keystrokes, and use <o>select</o> to detect the keys you want to use.
	</p>

	<p>
		Use a sinusoidal wave from a <o>cycle~</o> object to apply vibrato
		to the sample. The frequency of the <o>cycle~</o> will determine
		the <i>rate</i> of the vibrato, and the amplitude of the sinusoid
		will determine the <i>depth</i> of vibrato. Therefore, you will
		need to scale the <o>cycle~</o> object's amplitude with
		a <o>*~</o> object to achieve the proper vibrato depth.
	</p>

	<p>
		In the discussion of vibrato in the synthesis tutorials, we
		created vibrato by adding the output of the modulating oscillator
		to the frequency input of the carrier oscillator. However,
		two things are different in this exercise. First of all,
		the modulating oscillator needs to modulate the playback
		speed of <o>groove~</o> rather than the frequency of
		another <o>cycle~</o> object. Second, adding the output of
		the modulator to the input of the carrier - as in <i><link type="tutorial" module="msp" name="06_synthesischapter04">"Vibrato and Frequency Modulation"</link></i> -
		creates a vibrato of equal <i>frequency</i> above and below the
		carrier frequency, but does not create a vibrato of equal <i>pitch</i>
		up and down (as required in this exercise). A change in pitch is
		achieved by <i>multiplying</i> the carrier frequency by a certain
		amount, rather than by adding an amount to it.
	</p>

	<p>
		To raise the pitch of a tone by one semitone, you must multiply
		its frequency by the twelfth root of 2, which is a factor of 2
		to the <sup>1</sup>/<sub>12</sub> power (approximately 1.06). To
		lower the pitch of a tone by one semitone, you must multiply its
		frequency by 2 to the <sup>-1</sup>/<sub>12</sub> power (approximately 0.944).
		To calculate a signal value that changes continuously within this
		range, you may need to use an MSP object not yet discussed, <o>pow~</o>.
		Consult its description in the Objects section of this manual for
		details.
	</p>

<br/>

	<h2>
		Solution
	</h2>

	<bullet>
		Scroll the example Patcher window all the way to the right to
		see a solution to this exercise.
	</bullet>

	<p>
		The arguments to the <o>buffer~</o> object specify a length in
		milliseconds (<m>1000</m>) and a number of channels (<m>2</m>).
		This determines how much memory will initially be allocated to
		the <o>buffer~</o>.
	</p>

	<p>
		Since the memory allocated in the <o>buffer~</o> is limited to one second,
		there is no need to tell the <o>record~</o> object to stop when you record
		into the <o>buffer~</o>. It stops when it reaches the end of the <o>buffer~</o>.
	</p>

	<p>
		The keystrokes from the computer keyboard are reported by <o>key</o>, and
		the <o>select</o> object is used to detect the <i>a</i>, <i>r</i>,
		and <i>p</i> keys. The <m>bang</m>s from <o>select</o> trigger the
		necessary messages to <o>adc~</o>, <o>record~</o>, and <o>groove~</o>.
	</p>

	<p>
		The keystroke <i>p</i> is also used to trigger the amplitude envelope at the
		same time as the sample is played. This envelope is used to scale the output
		of <o>groove~</o>.
	</p>

	<p>
		A <o>sig~</o> <m>0.5</m> object sets the basic playback speed of <o>groove~</o>
		at half speed. The amplitude of a 3 Hz cosine wave is scaled by a factor of
		0.083333 (equal to <sup>1</sup>/<sub>12</sub>, but more computationally
		efficient than dividing by 12) so that it varies from <sup>-1</sup>/<sub>12</sub>
		to <sup>1</sup>/<sub>12</sub>. This sinusoidal signal is used as the exponent
		in a power function in <o>pow~</o> (2 to the power of the input), and the
		result is used as the factor by which to multiply the playback speed.
	</p>

</chapter>


