<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Basics Tutorial 5: A Review of Fundamentals">
	<previous module="msp"  name="05_mspbasicchapter04"></previous>
	<next module="msp" name="06_synthesischapter01"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="05mReview" patch="05mReview.maxpat"/>

<h1>Basics Tutorial 5: A Review of Fundamentals</h1>



	<h2>
		Exercises in the fundamentals of MSP
	</h2>

	<p>
		In this chapter, we suggest some tasks for you to program that will
		test your understanding of the fundamentals of MSP presented in the
		tutorials so far. A few hints are included to get you started. Try
		these three progressive exercises on your own first, in new file of
		your own. Then check the example patch to see a possible solution,
		and read on in this chapter for an explanation of the solution patch.
	</p>

	<h3>
		Exercise 1
	</h3>

	<p>
		Write a patch that plays the note E above middle C for one second,
		ten times in a row, with an electric guitar-like timbre. Make it so
		that all you have to do is click once to turn audio on, and once to
		play the ten notes.
	</p>

	<p>
		Here are a few hints:
	</p>
	<ol>
	<li>
		The frequency of E above middle C is 329.627557 Hz.
	</li>

	<li>
		For an ‘electric guitar-like timbre’ you can use the AIFF
		file <i>gtr512.aiff</i> that was used in
		<link type="tutorial" module="msp" name="05_mspbasicchapter03">Basics Tutorial 3</link>.
		You'll need to read that file into a <o>buffer~</o> object, and
		access the <o>buffer~</o> with a <o>cycle~</o> object.
	</li>

	<li>
		Your sound will also need an amplitude envelope that is characteristic
		of a guitar: very fast attack, fast decay, and fairly steady (only
		slightly diminishing) sustain. Try using a list of line segments (target
		values and transition times) to a <o>line~</o> object, and using the output
		of <o>line~</o> to scale the amplitude of the <o>cycle~</o>.
	</li>

	<li>
		To play the note ten times in a row, you'll need to trigger the
		amplitude envelope repeatedly at a steady rate. The Max <o>metro</o> object
		is well suited for that task. To stop after ten notes, your patch should
		either count the notes or wait a specific amount of time, then turn
		the <o>metro</o> off.
	</li>
</ol>
	<h3>
		Exercise 2
	</h3>

	<p>
		Modify your first patch so that, over the course of the ten repeated
		notes, the electric guitar sound crossfades with a sinusoidal tone a
		perfect 12th higher. Use a linear crossfade, with the amplitude of
		one sound going from 1 to 0, while the other sound goes from 0 to 1.
		(We discuss other ways of crossfading in a future chapter.) Send the
		guitar tone to the left audio output channel, and the sine tone to
		the right channel.
	</p>

	<p>
		Hints:
	</p>
	<ol>

	<li>
		You will need a second <o>cycle~</o> object to produce the tone
		a 12th higher.
	</li>

	<li>
		To obtain the frequency that's a (just-tuned) perfect 12th above E,
		simply multiply 329.627557 times 3. The frequency that's an equal tempered
		perfect 12th above E is 987.7666 Hz. Use whichever tuning you prefer.
	</li>

	<li>
		In addition to the amplitude envelope for each note, you will need
		to change the over-all amplitude of each tone over the course of the
		ten seconds. This can be achieved using an additional <o>*~</o> object
		to scale the amplitude of each tone, slowly changing the scaling factor
		from 1 to 0 for one tone, and from 0 to 1 for the other.
	</li>
</ol>
	<h3>
		Exercise 3
	</h3>

	<p>
		Modify your second patch so that, over the course of the ten repeated
		notes, the two crossfading tones also perform an over-all <i>diminuendo</i>,
		diminishing to <sup>1</sup>/<sub>32</sub> their original amplitude (i.e., by 30 dB).
	</p>

	<p>
		Hints:
	</p>
	<ol>
<li>
		This will require yet another amplitude scaling factor (presumably
		another <o>*~</o> object) to reduce the amplitude gradually by a factor
		of .03125.
	</li>

	<li>
		Note that if you scale the amplitude linearly from 1 to .03125
		in ten seconds, the diminuendo will seem to start slowly and
		accelerate toward the end. That's because the linear distance
		between 1 and .5 (a reduction in half) is much greater than the
		linear distance between .0625 and .03125 (also a reduction in
		half). The first 6 dB of diminuendo will therefore occur in the
		first 5.16 seconds, but the last 6 dB reduction will occur in the
		last .32 seconds. So, if you want the diminuendo to be perceived
		as linear, you will have to adjust accordingly.
	</li>
</ol>
	<h3>
		Solution to Exercise 1
	</h3>

	<bullet>
		Double-click on the <b>p exercise_1</b> <o>patcher</o> object to see
		one possible solution to this exercise.
	</bullet>

		<br/>

	<p>
		To make an oscillator with a guitar-like waveform, you need to read the
		audio file <i>gtr512.aiff</i> (or some similar waveform) into a <o>buffer~</o>,
		and then refer to that <o>buffer~</o> with a <o>cycle~</o>.
		(See <link type="tutorial" module="msp" name="05_mspbasicchapter03">Basics Tutorial 3</link>.)
	</p>

	<p>
		Note that there is a limit to the precision with which Max can represent
		decimal numbers. When you save your patch, Max may change <m>float</m>
		values slightly. In this case, you won't hear the difference.
	</p>

	<p>
		If you want the audio file to be read into the <o>buffer~</o> immediately
		when the patch is loaded, you can type the filename in as a second
		argument in the <o>buffer~</o> object, or you can use a <o>loadbang</o> object to
		trigger a <m>read</m> message to <o>buffer~</o>. In our solution we also chose to
		provide the frequency from a <o>number</o> box - which allows you to play other
		pitches - rather than as an argument to <o>cycle~</o>, so we also send <o>cycle~</o>
		an initial frequency value with <o>loadbang</o>.
	</p>

	<p>
		Now that we have an oscillator producing the desired tone, we need to
		provide an amplitude envelope to shape a note.
	</p>

		<br/>

	<p>
		We chose the envelope shown below, composed of straight line segments.
		(See <link type="tutorial" module="msp" name="05_mspbasicchapter03">Basics Tutorial 3</link>.)
	</p>

<illustration><img  src="images/basicchapter05a.png"/></illustration>

	<caption>
		<i>‘Guitar-like’ amplitude envelope </i>
	</caption>

	<p>
		This amplitude envelope is imposed on the output of <o>cycle~</o> with a
		combination of <o>line~</o> and <o>*~</o>. A <o>metro</o> is used to
		trigger the envelope once per second, and the <o>metro</o> gets turned
		off after a 10-second delay.
	</p>

	<h2>
		Solution to Exercise 2
	</h2>
    
    <bullet>
        Double-click on the <b>p exercise_2</b> <o>patcher</o> object to see
        one possible solution to this exercise.
    </bullet>
    
    <br/>
	<p>
		For the right output channel we want a sinusoidal tone at three times
		the frequency (the third harmonic of the fundamental tone), with the
		same amplitude envelope. To crossfade between the two tones, the
		amplitude of the first tone must go from 1 to 0 while the amplitude
		of the second tone goes from 0 to 1. This can again be achieved with
		the combination of <o>line~</o> and <o>*~</o> for each tone. We
		used a little trick to economize. Rather than use a separate <o>line~</o> object
		to fade the second tone from 0 to 1, we just subtract 1 from the output of
		the existing <o>line~</o>, which gives us a ramp from 0 to -1. Perceptually
		this will have the same effect.
	</p>

	<p>
		This crossfade is triggered (via <link type="refpage" name="send">s</link> and
		<link type="refpage" name="receive">r</link> objects)
		by the same <o>button</o> that triggers the <o>metro</o>, so the crossfade starts
		at the same time as the ten individual notes do.
	</p>

	<h2>
		Solution to Exercise 3
	</h2>
    
    <bullet>
        Double-click on the <b>p exercise_3</b> <o>patcher</o> object to see
        one possible solution to this exercise.
    </bullet>
    
    <br/>
	<p>
		Finally, we need to use one more amplitude envelope to create a
		global <i>diminuendo</i>. The two tones go to yet another <o>*~</o> object,
		controlled by another <o>line~</o>. As noted earlier, a straight line
		decrease in amplitude will not give the perception of constant diminuendo
		in loudness.
	</p>

	<p>
		Therefore, we used five line segments to simulate a curve that decreases
		by half every two seconds. (The <o>curve~</o> object will do this automatically.)
	</p>

<illustration><img  src="images/basicchapter05b.png"/></illustration>

	<caption>
		<i>Global amplitude envelope decreasing by half every two seconds</i>
	</caption>

	<p>
		This global amplitude envelope is inserted in the signal network to scale
		both tones down smoothly by a factor of .03125 over 10 seconds.
	</p>

</chapter>


