<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Basics Tutorial 1: Test Tone">
	<previous module="msp"  name="04_mspaudioio">How MSP Works</previous>
	<next module="msp" name="05_mspbasicchapter02">Test Tone</next>
	<parent name="00_mspindex">MSP Tutorials</parent>
<setdocpatch name="01mTestTone" patch="01mTestTone.maxpat"/>

<h1>Basics Tutorial 1: Test Tone</h1>



	<h2>
		MSP objects are pretty much like Max objects
	</h2>

	<p>
		MSP objects are for processing digital audio (i.e., sound) to be played
		by your computer. MSP objects look just like Max objects, have inlets
		and outlets just like Max objects, and are connected together with
		patch cords just like Max objects. They are created the same way as
		Max objects - just by placing an object box in the Patcher window
		and typing in the desired name - and they co-exist quite happily with
		Max objects in the same Patcher window.
	</p>

	<h2>
		...but they're a little different.
	</h2>

	<p>
		A patcher containing interconnected MSP objects works a little differently
		from a patcher containing standard Max objects.
	</p>

	<p>
		One way to think of the difference is just to think of MSP objects as
		working much faster than ordinary Max objects. Since MSP objects need
		to produce enough numbers to generate a high fidelity audio signal
		(commonly 44,100 numbers per second), they must work faster than the
		scheduler used by standard Max objects, which typically runs around a
		thousand times per second.
	</p>

	<p>
		Here's another helpful way to think of the difference. Think of a patch
		of MSP objects not as a program in which events occur at specific
		instants (as in a standard Max patch), but rather as a description
		of an instrument design - a synthesizer, sampler, or effect processor.
		It's like a mathematical formula, with each object constantly providing
		numerical values to the object(s) connected to its outlet. At any
		given instant in time, this formula has a result, which is the
		instantaneous amplitude of the audio signal. This is why we frequently
		refer to an ensemble of inter-connected MSP objects as a <i>signal network</i>.
	</p>

	<p>
		So, whereas a patch made up of standard Max objects sits idle and does
		nothing until something occurs (a mouse click, an incoming MIDI
		message, the clock firing on a <o>metro</o> object, etc.) causing
		one object to send a message to another object, a signal network of
		MSP objects, by contrast, is always active (from the time it's turned
		on to the time it's turned off), with all its objects constantly
		communicating to calculate the appropriate amplitude for the sound at
		that instant.
	</p>

	<h2>
		...so they look a little different
	</h2>

	<p>
		The names of all MSP objects end with the tilde character (<i>~</i>).
		This character, which looks like a cycle of a sine wave, just serves
		as an indicator to help you distinguish MSP objects from other
		Max objects.
	</p>

	<p>
		The patch cords between MSP objects have stripes. This helps you
		distinguish the MSP signal network from the rest of the Max patch.
	</p>

	<illustration><img  src="images/basicchapter01a.png"/></illustration>

	<caption><i>
		MSP objects are connected by striped patch cords</i>
	</caption>

	<p>
		Take a look at the tutorial patcher. You'll see that we have six
		items of our patcher's canvas. Three of them should be familiar
		to you: we've got a Max <o>comment</o> box ("warning:loud") and
		two <o>message</o> boxes labelled <m>start</m> and <m>stop</m>.
		The bulk of the patcher logic consists of three objects we've
		never seen before: a <o>cycle~</o> object, a <o>dac~</o> object,
		and something that looks suspiciously like a <o>slider</o> object
		but is actually a <o>gain~</o> object. Let's look at these objects
		in turn, starting with the <o>dac~</o> at the bottom of the patcher
		and returning from there to the top of the chain.
	</p>

	<h2>
		Digital-to-analog converter: <o>dac~</o>
	</h2>

	<p>
		<i>The digital-to-analog converter</i> (DAC) is the part of your computer
		that translates the stream of discrete numbers in a digital audio signal
		into a continuous fluctuating voltage which will drive your loudspeaker.
	</p>

	<p>
		Once you have calculated a digital signal to make a computer-generated
		sound, you must send the numbers to the DAC. So, MSP has an object
		called <o>dac~</o>. This object is generally the terminal object in
		any signal network, and is required for any signals generated by MSP
		to make it out of your computer as sound. The <o>dac~</o> object receives
		the signals you wish to hear in its inlets. It can have as many inlets
		as there are available channels of audio playback on your computer, though
		it defaults to a stereo configuration talking to the first two channels
		of your audio hardware - this is why there are two inlets on the <o>dac~</o>
		in our patcher. If you are using more elaborate audio output hardware,
		you can type in arguments to specify other audio channels.
	</p>

	<p>
		<b>Important!</b> <o>dac~</o> must be receiving a signal of non-zero
		amplitude in order for you to hear anything. <o>dac~</o> expects to
		receive signal values in the range -1.0 to 1.0. Numbers that exceed
		that range will cause distortion when the sound is played.
	</p>

	<h2>
		A simple wavetable oscillator: <o>cycle~</o>
	</h2>

	<p>
		The best way to produce a periodic waveform is with <o>cycle~</o>.
		This object uses the technique known as ‘wavetable synthesis’. It
		reads through a list of 512 values at a specified rate, looping back
		to the beginning of the list when it reaches the end. This simulates
		a periodically repeating waveform.
	</p>

	<p>
		You can direct <o>cycle~</o> to read from a list of values that you
		supply (in the form of an audio file), or if you don't supply one,
		it will read through its own table which represents a cycle of a
		cosine wave with an amplitude of 1. We'll show you how to supply
		your own waveform in a later tutorial; for now we'll use the cosine
		waveform.
	</p>

	<illustration><img  src="images/basicchapter01b.png"/></illustration>

	<caption>
		<i>Graph of 512 numbers describing one cycle of a cosine wave with amplitude 1</i>
	</caption>

	<p>
		The <o>cycle~</o> object receives a frequency value (in Hz) in its left
		inlet, and it determines on its own how fast it should read through the
		list in order to send out a signal with the desired frequency.
	</p>

	<techdetail>
		<b>Technical detail: </b>To figure out how far to step through the list
		for each consecutive sample, <o>cycle~</o> uses the basic formula

		<caption>I=ƒL/R</caption>

		where <i>I</i> is the amount to increment through the list, <i>ƒ</i>
		is the signal's frequency, <i>L</i> is the length of the list (512 in
		this case), and <i>R</i> is the audio sampling rate. <o>cycle~</o>
		is an ‘interpolating oscillator’, which means that if <i>I</i> does
		not land exactly on an integer index in the list for a given sample,
		the <o>cycle~</o> object interpolates between the two closest numbers in the list to find the proper output value. Performing interpolation in a wavetable oscillator makes a substantial improvement in audio quality. The <o>cycle~</o> object uses linear interpolation, while other MSP objects use very high-quality (and more computationally expensive) polynomial interpolation.
	</techdetail>

	<p>
		The <o>cycle~</o> object has a default frequency of 0 Hz. So in order
		to hear the signal, we need to supply an audible frequency value. This
		can be done with a number argument as in the example patch, or by
		sending a number in the left inlet, or by connecting another MSP object
		to the left inlet. If we listen to the output of this signal with its
		frequency set in an audioble range, we should hear a sine wave, the purest
		(in terms of spectrum) sound that we can make.
	</p>

	<h2>
		A volume control: <o>gain~</o>
	</h2>

	<p>
		If we were to directly connect our <o>cycle~</o> object to a <o>dac~</o>
		object and start the audio, we could easily damage our speakers or, more
		importantly, our ears. MSP objects that generate audio (such as <o>cycle~</o>)
		generally do so at a normalized dynamic range of -1 to 1, which the <o>dac~</o>
		object will interpret as being the loudest signal possible. To avoid
		these dangers, it's important to have an object in-between to control
		the amplitude of the signal going to our speakers. The <o>gain~</o>
		slider performs just such a function:
	</p>

		<illustration><img  src="images/basicchapter01c.png"/></illustration>

	<caption>
		The <o>gain~</o> slider in the sliders section of the Object Palette
	</caption>

	<p>
		The <o>gain~</o> slider <i>multiplies</i> its incoming signal by a
		factor based on the position of the slider which, for all but the
		uppermost reaches of the object, is a number less than 1. As a result,
		the <o>gain~</o> object almost always <i>attenuates</i> (or turns down)
		the volume of what comes in, much like a fader on an analog mixing
		desk. The <o>gain~</o> object is in several ways a graphical version
		of a <o>*~</o> object, which we'll look at in a later tutorial.
	</p>
	<techdetail>
		It's important to note that the <o>gain~</o> slider "goes up to 11". In fact it goes above 12, in that the higest step of the slider is 157 compared to the unity gain step of 128. This extreme level multiplies the signal by more than 7, almost guaranteeing distortion.
	</techdetail>

	<h2>
		Starting and stopping signal processing
	</h2>
	<p>
		The MSP audio network is off when Max boots up. It can be turned on with the on/off icon in the lower right corner of any window that contains MSP objects. There is a slider just above this button that controls the level of the window in the output mix, as well as a pair of VU bars that show the signal level. When the window is opened, this slider is at the unity gain point, and can be used to balance the mix, but you should keep in mind that reducing the level with this slider may mask distortion that is caused by excessive levels within the patch.
	</p>

	<p>
		Audio processing can also be controlled programmatically. The <o>dac~</o> object is one of five MSP objects (along with <o>adc~</o>, their graphical cousins <o>ezadc~</o> and <o>ezdac~</o>, and <o>adstatus</o>) that can turn on and off the MSP audio network from <i>within</i> your patcher. It is very common to provide the <m>start</m> message with a <o>loadmess</o> object so that audio begins as soon as the patch is opened.
	</p>

	<p>
		<b>Important!</b> Sending <m>start</m> or <m>stop</m> to any <o>dac~</o> or <o>adc~</o> object enables or disables processing for <b>all</b> open signal networks in <b>all</b> currently open Max patchers. So if you turn on audio in a patcher and hear things you don't expect, a good place to look is under the <b>Window</b> menu of Max; it's possible you have other documents open that are generating audio using MSP. You can use the <m>startwindow</m> message to start processing only in the affected window.
	</p>


	<h2>
		Listening to the Test Tone
	</h2>

	<p>
		The first time you start up Max, it will try to use your computer's
		default sound card and driver (CoreAudio on Macintosh or MME on Windows)
		for audio input and output. If you have the audio output of your computer
		connected to headphones or an amplifier, you should hear the output of
		MSP through it. If you don't have an audio cable connected to your
		computer, you'll hear the sound through the computer's internal speaker.
	</p>

	<p>
		In order to get MSP up and running properly, we recommend that you start
		the tutorials using your computer's built-in sound hardware. If you
		want to use an external audio interface or sound card, please refer to
		the Audio Input and Output chapter for details on configuring MSP to
		work with audio hardware.
	</p>

	<bullet>
		Set your audio amplifier (or amplified speakers) to their minimum
		setting, then click on the <m>start</m> <o>message</o> in the tutorial
		patcher. Click and drag on the <o>gain~</o> slider until it's set to
		somewhere in the middle of its length. Adjust your audio amplifier to
		a comfortable setting. Once you're satisfied that the patcher is working,
		click on the <m>stop</m> message box to turn off that annoying test tone.
	</bullet>

	<h2>
		Troubleshooting
	</h2>

	<p>
		If you don't hear any sound coming from your computer when you start
		the <o>dac~</o> in this example and turn up the <o>gain~</o> slider,
		check the level setting on your amplifier or mixer, and check all your
		audio connections. Check that the sound output isn't currently muted.
		On Macintosh, the sound output level is set using the Sound preferences
		in the System Preferences application. On Windows, the sound output
		level is set using the Sounds and Audio Devices setup
		(Start - Control Panels - Sounds and Audio Devices).
	</p>

	<p>
		If you are still are not hearing anything, choose <b>Audio Status</b> from
		the Options Menu and verify that a valid driver and output are chosens in the
		pop-up menus. If one isn't, choose it. (See the <link type="tutorial" module="msp" name="04_mspaudioio">Audio I/O tutorial</link> for more information.)
	</p>

		<illustration><img  src="images/basicchapter01d.png"/></illustration>

	<techdetail>
		You can always check to see if audio problems originate in your patch or in the audio system by opening the <i>Audiotester</i> patch in the <m>Extras</m> menu. This patch provides a simple audio source as well as displays of any active input. If selecting <i>Oscillator</i> or <i>Pink noise</i> from the signal source menu does not produce a sound, the problem is somewhere in the audio setup.
	</techdetail>



	<h2>
		Summary
	</h2>

	<p>
		A <i>signal network</i> of connected MSP objects describes an audio instrument. The digital-to-analog converter of the instrument is represented by the <o>dac~</o> object; <o>dac~</o> must be receiving a signal of non-zero amplitude (in the range -1.0 to 1.0) in order for you to hear anything. The <o>cycle~</o> object is a wavetable oscillator which reads cyclically through a list of 512 amplitude values, at a rate determined by the supplied frequency value. Signal processing is turned on and off with a button in the patcher window or by by sending a <m>start</m> or <m>stop</m> message to any <o>dac~</o> or <o>adc~</o> object.
	</p>

	<bullet>
		Because sending a <m>start</m> message to a <o>dac~</o> turns on audio
		in all open Max patches that contain MSP objects, when going through these
		tutorials its best to only have them open one at a time. Close the Patcher
		window before proceeding to the next chapter.
	</bullet>

<seealsolist>
	<seealso name="cycle~">Table lookup oscillator</seealso>
	<seealso name="gain~">Exponential scaling volume slider</seealso>
	<seealso name="dac~">Audio output and on/off</seealso>

	<seealso type="tutorial" module="msp" name="04_mspaudioio">I/O Audio input and output with MSP</seealso>
</seealsolist>

</chapter>
