<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Basics Tutorial 3: Wavetable Oscillator">
	<previous module="msp"  name="05_mspbasicchapter02"></previous>
	<next module="msp" name="05_mspbasicchapter04"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>


<setdocpatch name="03mWavetableOscillator" patch="03mWavetableOscillator.maxpat"/>

<h1>Basics Tutorial 3: Wavetable Oscillator</h1>


	<h2>
		Introduction
	</h2>

	<p>
		In this tutorial patch, we'll look at how to use a wavetable other than a sine
		wave to generate sound using the <o>cycle~</o> object. Along the way, we'll
		look at how to generate more complex curves using the <o>line~</o> object, and
		get a peek at how MSP stores audio data in computer memory using
		the <o>buffer~</o> object.
	</p>

	<h2>
		Audio on/off switch: <o>ezdac~</o>
	</h2>

	<p>
		In this tutorial patch, the <o>dac~</o> object which was used in earlier
		examples has been replaced by a button with a speaker icon. This is
		the <o>ezdac~</o> object, a user interface object available from the
		<link type="vignette" module="core" name="top_patcher_toolbar">Top Patcher Window Toolbar</link>.
	</p>

	<illustration><img  src="images/basicchapter03a.png"/></illustration>

	<caption>
		<i>ezdac~ is an on/off button for audio, available from the
		<link type="vignette" module="core" name="top_patcher_toolbar">Top Patcher Window Toolbar</link></i>
	</caption>

	<p>
		The <o>ezdac~</o> works much like <o>dac~</o>, except that clicking on it
		turns the audio on or off. It can also respond to <m>start</m> and <m>stop</m> messages
		in its left inlet, like the <o>dac~</o>. Unlike <o>dac~</o>, however, it is
		hard-wired to output channels 1 and 2 on your audio device, which might not
		make it appropriate for working with multi-channel sound. For our tutorials,
		however, it should be fine. The <o>ezdac~</o> button is highlighted when audio is on.
	</p>

	<h2>
		A stored sound: <o>buffer~</o>
	</h2>

	<p>
		In the previous examples, the <o>cycle~</o> object was used to read repeatedly
		through 16K values describing a cycle of a cosine wave. As it happens, a <o>cycle~</o> object
		can read through <i>any</i> set of values, treating them as a single cycle of a waveform. These
		 numbers must be stored in our computer's memory, and can be loaded from a soundfile,
		generated programmatically by an algorithm, or even drawn by hand. The MSP object
		that maintains these pieces of memory is called <o>buffer~</o>.
	</p>

	<p>
		The <o>buffer~</o> object can be used easily; it requires a single argument:
		a name to uniquely identify it. Any audio data stored in that <o>buffer~</o> becomes
		associated with that name. The name of the <o>buffer~</o> is arbitrary and doesn't
		have to match the name of the audio file loaded into it; the only requirement is
		that it not conflict with other <i>named</i> items in open Max patches (such
		as <o>table</o> objects or <o>send</o>/<o>receive</o> pairs). As you can see,
		we've named the <o>buffer~</o> <i>richtone</i>.
	</p>

	<p>
		Messages sent to the object allow you to load in audio files, resize, or
		clear the memory of the <o>buffer~</o>. The trick is this: the <o>buffer~</o> object
		itself doesn't <i>play back</i> any audio; it simply holds onto the sample data
		and associates a name with it that <i>other</i> MSP objects can use to get at the
		data. A <o>cycle~</o> object can be made to read from that <o>buffer~</o> by
		typing the same name in as its argument. The initial frequency value for the <o>cycle~</o> object,
		just before the buffer name, is optional.
	</p>

	<p>
		To get the sound into the <o>buffer~</o>, send it a <m>replace</m> or <m>read</m> message, followed by the name of an audio file in the search path of the Max program. If you leave out the name of a file, Max will open an Open Document dialog box, allowing you to select an audio file to load. The <m>replace</m> or <m>read</m> message reads in an audio file and automatically <i>sizes</i> the <o>buffer~</o> object's memory to match the length of the audio file.
	</p>

	<p>
		Regardless of the length of the sound in the <o>buffer~</o>, the <o>cycle~</o> object will default to use 512 samples from it for its waveform. (If you like, you can specify a starting point in the <o>buffer~</o> for <o>cycle~</o> to begin its waveform, either with an additional argument to <o>cycle~</o> or with a <m>buffer_ofset</m> message to <o>cycle~</o>. You can also change the <m>buffer_sizeinsamps</m> attribute to set the size of the wave table.) In the example patch, we use an audio file that contains exactly 512 samples called <m>gtr512.aiff</m>.
	</p>

	<techdetail>
		<b>Technical detail:</b> In fact, <o>cycle~</o> Chris uses 513 samples. The
		513th sample is used only for interpolation from the 512th sample.
		When <o>cycle~</o> is being used to create a periodic waveform, as in this
		example patch, the last sample should be the same as the 1st sample. If
		the <o>buffer~</o> contains only 512 samples, as in this example, <o>cycle~</o> supplies
		a 513th sample that is the same as the 1st sample.
	</techdetail><br/>

	<bullet>
		In the tutorial patcher, click on the <o>message</o> box that says <m>read gtr512.aiff</m>.
		This loads in the audio file into our <o>buffer~</o> (named <m>richtone</m>).
		Then click on the <o>ezdac~</o> object to turn the audio on. Click on
		the <o>message</o> box labeled <m>A</m>. You should hear a short burst of what
		sounds like noise. Click on the <o>message</o> labeled <m>B</m>. You should hear a
		rich oscillating tone fade in and out over a second. Click the other <m>message</m> boxes
		in turn, listening the the result. Notice the different values in the lists being sent
		to the <o>line~</o> objects, as well as the different frequencies that
		the <o>cycle~</o> objects in the tutorial are set to.
	</bullet>

	<p>
		There are several other objects that can use the data in a <o>buffer~</o>,
		as you will see in later chapters.
	</p>

	<h2>
		Creating complex envelopes with <o>line~</o>
	</h2>

	<p>
		In the previous example patch, we used <o>line~</o> to make a linearly
		changing signal by sending it a list of two numbers. The first number in
		the list was a target value and the second was the amount of time, in milliseconds,
		for <o>line~</o> to arrive at the target value.
	</p>

	<illustration><img src="images/basicchapter03b.png"/></illustration>

	<caption>
		<i>line~ is given a target value (1.) and an amount of time to get there (100 ms)</i>
	</caption>

	<p>
		If we want to, we can send <o>line~</o> a longer list containing many value-time
		pairs of numbers (up to 64 pairs of numbers). In this way, we can make a <o>line~</o> object
		perform a more elaborate function composed of many adjoining line segments.
		After completing the first line segment, <o>line~</o> proceeds immediately
		toward the next target value in the list, taking the specified amount of time
		to get there. In this way, we can great function curves for synthesizers that
		are commonly referred to as <i>envelopes</i>.
	</p>

<illustration><img  src="images/basicchapter03c.png"/></illustration>

	<caption>

		<i>A function made up of line segments - a classic 'envelope'</i>
	</caption>

	<p>
		Synthesizer users are familiar with using this type of function to generate
		envelopes such as the ‘ADSR’ curves that control the attack, decay, sustain,
		and release of a sound's amplitude independently. That is what we're doing in
		this example patch, although we can choose how many line segments we wish to
		use for the envelope.
	</p>

	<bullet>
		Click on the <o>message</o> boxes again in sequence and decipher how the
		lists for each <o>line~</o> object affect the sound. Note that each <m>message</m> box
		begins with a <m>0</m> followed by a comma, this sends an individual message
		of <m>0</m> to the <o>line~</o> object immediately followed by the subsequent list.
		Sending a number by itelf to a <o>line~</o> object causes it to change <i>immediately</i> to
		a value. An equivalent notation would be to start our envelope lists with <m>0 0</m>
		(go to 0 in 0 milliseconds, i.e. right now).
	</bullet>

	<h2>
		Add signals to produce a composite sound
	</h2>

	<p>
		Any time two or more signals are connected to the same signal inlet, those
		signals are added together and their sum is used by the receiving object.
	</p>

<illustration><img  src="images/basicchapter03d.png"/></illustration>

	<caption>
		<i>Multiple signals are added (mixed) in a signal inlet</i>
	</caption>

	<p>
		Addition of digital signals is equivalent to unity gain mixing in analog
		audio. It is important to note that even if all your signals have
		amplitude less than or equal to 1, the sum of such signals can easily
		exceed 1. In MSP it's fine to have a signal with an amplitude that
		exceeds 1 anywhere within the signal chain, but before sending the signal
		to <o>dac~</o> you must scale it (usually with a <o>*~</o> object) to
		keep its amplitude less than or equal to 1. A signal with amplitude greater
		than 1 will be distorted by <o>dac~</o>.
	</p>

	<p>
		In the example patch we're using three different <o>cycle~</o> objects
		that are oscillating the waveform stored in the <o>buffer~</o> named <m>richtone</m>.
		While up to now we're been playing them all one at a time, they could all
		be mixed together to produce a composite instrument sound.
	</p>

	<bullet>
		Set the volume of our tutorial patch to a maximum level of <m>0.3</m> to
		prevent clipping (remember we're using three different sounds, each with a
		hypothetical maximum output of 1). Click on the <o>button</o> at the top
		of the patcher to play all three signals simultaneously.
	</bullet>

	<p>
		Each of the three tones has a different amplitude envelope, causing
		the timbre of the note to evolve over the course of its 1-second duration.
		At the same time, even though all three tones are playing from the
		same sample, they are set to different frequencies, creating a much
		richer spectrum than exists in the original audio file being used for
		the wavetable. As we'll see in the next tutorial, mixing wavetables of
		different frequencies is a key technique in something called <i>additive synthesis</i>.
	</p>

<h2>
	Summary
</h2>

	<p>
		The <o>ezdac~</o> object is a button for switching the audio on and
		off. The <o>buffer~</o> object stores a sound in the computer's
		memory. You can load an audio file into <o>buffer~</o> with
		a <m>replace</m> message. If a <o>cycle~</o> object has a typed-in
		argument which gives it the same name as a <o>buffer~</o> object,
		the <o>cycle~</o> will use 512 samples from that <o>buffer~</o> as its
		waveform instead of the default cosine wave.
	</p>

	<p>
		Whenever you connect more than one signal to a given signal inlet,
		the receiving object adds those signals together and uses the sum
		as its input in that inlet. Exercise care when mixing (adding) audio
		signals, to avoid distortion caused by sending a signal with amplitude
		greater than 1 to the <o>dac~</o>.
	</p>

	<p>
		The <o>line~</o> object can receive a list in its left inlet that
		consists of up to 64 pairs of numbers representing target values and
		transition times. It will produce a signal that changes linearly from
		one target value to another in the specified amounts of time. This
		can be used to make a function of line segments describing any shape
		desired, which is particularly useful as a control signal for amplitude
		envelopes. You can achieve crossfades between signals by using different
		amplitude envelopes from different <o>line~</o> objects.
	</p>

<seealsolist>
	<seealso name="buffer~">Store audio samples</seealso>

	<seealso name="ezdac~">Audio output and on/off button</seealso>

	</seealsolist>

</chapter>
