<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Basics Tutorial 4: Routing Signals">
	<previous module="msp"  name="05_mspbasicchapter03"></previous>
	<next module="msp" name="05_mspbasicchapter05"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="04mRoutingSignals" patch="04mRoutingSignals.maxpat"/>

<h1>Basics Tutorial 4: Routing Signals</h1>


	<h2>
		Introduction
	</h2>

	<p>
		In this tutorial we'll look at different ways to route MSP audio
		connections, as well as review sending messages remotely within
		Max with a focus on how this can be useful in signal network
		design. Along the way we'll learn about how gain can be expressed
		in decibels and how the <o>phasor~</o> object works.
	</p>

	<h2>
		Remote signal connections: <o>send~</o> and <o>receive~</o>
	</h2>

	<p>
		The patch cords that connect MSP objects look different from normal
		patch cords because they actually do something different. They describe
		the order of calculations in a signal network. These connected objects
		will be used to calculate a whole block of samples for the next portion
		of sound output by your computer.
	</p>

	<p>
		Max objects can communicate remotely without patch cords by using the
		<o>send</o> and <o>receive</o> objects (and some similar objects such
		as <o>value</o> and <o>pv</o>). You can also use semicolons in <o>message</o> boxes
		to transmit values to <o>receive</o> objects by their name. You can transmit
		MSP signals remotely as well. Rather than using normal <o>send</o>
		and <o>receive</o> objects, however, two MSP objects exist specifically
		for remote transmission of signals: <o>send~</o> and <o>receive~</o>.
	</p>

	<p>
		The two objects <o>send~</o> and <o>receive~</o> work very similarly
		to <o>send</o> and <o>receive</o>, but are only for use with MSP
		signals. Max will allow you to connect normal patch cords to <o>send~</o>
		and <o>receive~</o>, but only signals will get passed through <o>send~</o> to
		the corresponding <o>receive~</o>.
	</p>

	<p>
	Similar to how the names of <o>send</o> and <o>receive</o> can be shortened
	to <link type="refpage" name="send">s</link> and <link type="refpage" name="receive">r</link>,
	the names of <o>send~</o> and <o>receive~</o> can be shortened to
	<link type="refpage" name="send~">s~</link> and <link type="refpage" name="receive~">r~</link>.
	</p>

	<p>
		There are a few important differences between the Max
		objects <o>send</o> and <o>receive</o> and the MSP objects <o>send~</o>
		and <o>receive~</o>.
	</p>
	<ol>

	<li>
		A Max message can be sent to a <o>receive</o> object from several
		other objects besides <o>send</o>, such as <o>float</o>, <o>forward</o>,
		<o>grab</o>, <o>if</o>, <o>int</o>, and <o>message</o>; <o>receive~</o>
		can receive a signal only from a <o>send~</o> object that shares the same name.
	</li>

	<li>
		If <o>receive</o> has no typed-in argument, it has an inlet for
		receiving <m>set</m> messages to set or change its name; <o>receive~</o> also
		has an inlet for that purpose, but is nevertheless required to have a typed-in
		argument to give it an initial destination name.
	</li>

	<li>
		The Max <o>send</o> object, once created, cannot change destinations
		(the Max <o>forward</o> object does this). The MSP <o>send~</o> can
		change destinations with a <m>set</m> message.
	</li>
</ol>
	<p>
		Examples of each of these usages can be seen in the tutorial patch.
	</p>

	<h2>
		Routing a signal: <o>gate~</o>
	</h2>

	<p>
		The MSP object <o>gate~</o> works very similarly to the Max <o>gate</o> object.
		Just as <o>gate</o> is used to direct messages to one of several destinations,
		or to shut the flow of messages off entirely, <o>gate~</o> directs a signal to
		different places, or shuts it off from the rest of the signal network.
	</p>

	<p>
		In the example patch, the <o>gate~</o> objects are used to route signals
		to the left audio output, the right audio output, both, or neither,
		according to what number is received from the <o>umenu</o> object.
	</p>

	<p>
		It is worth noting that changing the chosen outlet of a <o>gate~</o> while
		an audio signal is playing through it can cause an audible click because
		the signal shifts abruptly from one outlet to another. To avoid this,
		you should generally design your patch in such a way that the <o>gate~</o> object's
		outlet will only be changed when the audio signal going through it is at zero
		or when audio is off. (No such precaution was taken in this tutorial patch.)
	</p>
	<p>
		The closed outlets of <o>gate~</o> do not stop sending signals, they continue to send a signal with a constant value of 0. If the idea of a constant signal seems strange to you, think of it as 44,100 0s a second.
	</p>

	<h2>
		Wave interference
	</h2>

	<p>
		It's a fundamental physical fact that when we add together two sinusoidal
		waves with different frequencies we create <i>interference</i> between
		the two waves. Since they have different frequencies, they will usually
		not be exactly in phase with each other; at some times they will be
		sufficiently in phase that they add together constructively, but at other
		times they add together destructively, canceling each other out to some
		extent. They only arrive precisely in phase with each other at a rate
		equal to the difference in their frequencies. For example, a sinusoid at
		1000 Hz and another at 1002 Hz come into phase exactly 2 times per second.
		In this case, they are sufficiently close in frequency that we don't hear
		them as two separate tones. Instead, we hear their recurring pattern of
		constructive and destructive interference as <i>beats</i> occurring at a
		sub-audio rate of 2 Hz, a rate known as the <i>difference frequency</i>
		or <i>beat frequency</i>. (Interestingly, we hear the two waves as a single
		tone with a sub-audio beat frequency of 2 Hz and an audio frequency of 1001 Hz.)
	</p>

	<p>
		When the example patch is opened, a <o>loadbang</o> object sends initial
		frequency values to the two <o>cycle~</o> objects in the patch, setting
		them to 1000 Hz and 1002 Hz; so we expect that these two tones sounded
		together will cause a beat frequency of 2 Hz. It also sends initial values
		to the <o>umenu</o> objects which in turn set the <o>gate~</o> objects,
		directing one tone to the left audio output and one to the right audio
		output. A fourth value called <m>Depth</m> is also set. All of this is
		accomplished by using a <m>message</m> box to remotely set values picked
		up by Max <o>receive</o> objects in the patcher.
	</p>

	<bullet>
		In the tutorial patcher, click on <o>ezdac~</o> to turn audio on, then
		use the <o>slider</o> marked <m>Volume</m> to adjust the loudness of the
		sound to a comfortable level. Note that the beats occur exactly twice per
		second. Try changing the frequency of Oscillator B to various other
		numbers close to 1000 (using the <o>number</o> box labeled <m>frequency</m>), and
		note the effect. As the difference frequency approaches an audio rate (say,
		in the range of 20-30 Hz) you can no longer distinguish individual beats, and
		the effect becomes more of a timbral change. Increase the difference still further,
		and you begin to hear two distinct frequencies.
	</bullet>

	<bullet>
		 <i>Philosophical tangent: </i>It can be shown mathematically and
		 empirically that when two sinusoidal tones are added, their
		 interference pattern recurs at a rate equal to the difference
		 in their frequencies. This apparently explains why we hear beats;
		 the amplitude demonstrably varies at the difference rate. However,
		 if you listen to this patch through headphones so that the two tones
		 never have an opportunity to interfere mathematically, electrically,
		 or in the air, you still hear the beats! This phenomenon, known as <i>binaural beating</i>
		 is caused by ‘interference’ occurring in the nervous system. Although such
		 interference is of a very different physical nature than the interference
		 of sound waves in the air, we experience it as similar. An experiment like
		 this demonstrates that our auditory system actively shapes the world we hear.
	</bullet>

	<h2>
		Amplitude and relative amplitude
	</h2>

	<p>
		The <o>slider</o> marked ‘Volume’ has been set to have a range of
		101 values, from <m>0</m> to <m>100</m>, which makes it easy to
		convert its output to a float ranging from <m>0</m> to <m>1</m> just
		by dividing by 100. (The decimal point in argument typed into the <o>/</o> object
		ensures a float division.)
	</p>

	<p>
		The <o>*~</o> objects use the specified amplitude value to scale
		the audio signal before it goes to the <o>ezdac~</o>. If both oscillators
		get sent to the same inlet of <o>ezdac~</o>, their combined amplitude
		will be 2. Therefore, it is prudent to keep the amplitude scaling
		factor at or below 0.5. For that reason, the amplitude value which
		the user thinks of as being between 0 and 1 is actually kept between
		0 and 0.5 by the <o>*</o> <m>0.5</m> object below the <o>slider</o>.
	</p>

	<p>
		Because of the wide range of possible audible amplitudes, it may be
		more meaningful in some cases to display volume numerically in terms
		of the logarithmic scale of decibels (<i>dB</i>), rather than in
		terms of absolute amplitude. The decibel scale refers to <i>relative</i>
		amplitude, i.e. the amplitude of a signal relative to some reference
		amplitude. The formula for calculating amplitude in decibels is:
	</p>

	<caption>
		<i>dB = 20(log10(A/Aref))</i>
	</caption>

	<p>
		where <i>A</i> is the amplitude being measured and <i>Aref</i> is
		a fixed reference amplitude.
	</p>

	<p>
		The <o>AtodB</o> object uses a reference amplitude of 1 in the formula shown
		above, and converts the amplitude to dB. If you turn off the audio
		(for safety) and raise the <o>slider</o> to its maximum value (ensuring
		an output of <m>1</m>), you will see the decibel output of
		the <o>AtodB</o> object read <m>0</m> (i.e. unity gain). Each
		halving of the amplitude in the <o>slider</o> is approximately equal
		to a 6 dB reduction in decibels.
	</p>

	<h2>
		Constant signal value: <o>sig~</o>
	</h2>

	<p>
		Most signal networks require some changing values (such as an
		amplitude envelope to vary the amplitude over time) and some
		constant values (such as a frequency value to keep an oscillator
		at a steady pitch). In general, one provides a constant value to
		an MSP object in the form of a <m>float</m> message, as we have
		done in these examples when sending a frequency in the left inlet of
		a <o>cycle~</o> object.
	</p>

	<p>
		However, there are some cases when one wants to combine both constant
		and changing values in the same inlet of an MSP object. Inlets that
		accept either a <m>float</m> or a <m>signal</m> (such as the left
		inlet of <o>cycle~</o>) do not successfully combine the two. In general,
		MSP objects will <i>ignore</i> a floating-point value in deference to a
		signal input. As a result, it's necessary to have an object that outputs
		a constant value as a signal if you need to combine a fixed and changing
		value in an MSP signal chain.
	</p>

	<p>
		One way to combine a numerical Max message (an <m>int</m> or a <m>float</m>)
		with a signal is to convert the number into a steady signal with the <o>sig~</o>
		object. The output of <o>sig~</o> is a signal with a constant value, determined
		by the number received in its inlet.
	</p>

	<p>
		In the example patch, <m>Oscillator B</m> combines a constant frequency
		(supplied as a <m>float</m> to <o>sig~</o>) with a varying frequency
		offset (signal values coming in from a <o>receive~</o> object
		called <m>FreqB+</m>). The sum of these signals
		will be the frequency of the oscillator at any given instant.
	</p>

	<h2>
		Changing the phase of a waveform: <o>phasor~</o>
	</h2>

	<p>
		For the most part, the phase offset of an isolated audio wave doesn't
		have a substantial effect perceptually. For example, a sine wave in
		the audio range sounds exactly like a cosine wave, even though there
		is a theoretical phase difference of a quarter cycle. For that reason,
		we have not been concerned with the rightmost phase inlet of the <o>cycle~</o>
		object until now.
	</p>

<illustration><img src="images/basicchapter04a.png"/></illustration>

	<caption>
		<i>A sine wave offset by a quarter cycle is a cosine wave</i>
	</caption>

	<p>
		However, there are some very useful reasons to control the phase offset
		of a wave. For example, by leaving the frequency of <o>cycle~</o>
		at <m>0</m>, and continuously increasing its phase offset, you can
		change its instantaneous value (just as if it had a positive frequency).
		The phase offset of a sinusoid is usually referred to in degrees (a full
		cycle is 360°) or <i>radians</i> (a full cycle is 2*&#x3C0; radians in length).
		In the <o>cycle~</o> object, phase is referred to in wave cycles; so an
		offset of &#x3C0; radians is 1/2 of a cycle, or <m>0.5</m>. In other words,
		as the phase varies from 0 to 2&#x3C0; radians, it varies from <m>0</m> to <m>1</m> wave
		cycles. This way of describing the phase is handy since it allows us to use the
		common signal range from 0 to 1.
	</p>

	<p>
		So, if we vary the phase offset of a stationary (0 Hz) <o>cycle~</o> object
		continuously from 0 to 1 over the course of one second, the resulting
		output is a cosine wave with a frequency of 1 Hz.
	</p>

	<p>
		The <o>phasor~</o> object is a very useful MSP signal generator that
		simply outputs a ramp from <m>0</m> to <m>1</m> at a given frequency.
		As with the <o>cycle~</o> object, it's frequency can be set by an
		argument to the object, a floating-point value in its left inlet, or
		a signal. Later in our tutorial, we'll hear what it sounds like (it's
		a sawtooth wave, and not an especially pleasant one), but for now let's
		use it for changing the <i>phase</i> (hence <o>phasor~</o> of
		the <o>cycle~</o> object in the lower-left of the tutorial patcher.
	</p>

	<bullet>
        Click on the <o>message</o> box above the <o>line~</o> objects in the lower-left
        portion of the tutorial patch. You should hear one of the sine waves begin
		to slowly drop and rise in pitch, causing the beating between the two sines
		connected to the audio output to change over time.
	</bullet><br/>

	<p>
		The <o>cycle~</o> in the bottom-left of the patcher is <i>modulating</i>
		the <o>cycle~</o> object in the upper-right by adding a value to its current
		(constant) frequency, set by the <o>sig~</o> object.
	</p>
	<techdetail>
		<m>Historical note:</m> In the long ago days of MusicN synthesis, the <i>phasor</i> was the only <i>unit generator</i> that could produce a continuous tone. A phasor had to be connected to a wavetable to generate other waveforms. In MSP, <o>cycle~</o>, <o>triangle~</o> and the other oscillators have internal phasors, but the phasor concept is useful for so many operations we have provided the stand alone version.
	</techdetail>
	<h2>
		Receiving a different signal
	</h2>

	<p>
		The remaining portion of the tutorial patch exists simply to demonstrate the
		use of the <m>set</m> message to the <o>receive~</o> object. This is another
		way to alter the signal flow in a network. As with the <o>send~</o> object,
		you can change the name of the <o>receive~</o> object with a <m>set</m> message,
		instructing it to get its input from a different <o>send~</o> object (or objects).
	</p>

	<bullet>
		Click on the <o>message</o> box containing <m>set sawtooth</m>. Both of the
		connected <o>receive~</o> objects now get their signal from the <o>phasor~</o>
		object in the lower-right corner of the window. Click on the <o>message</o> boxes
		containing <m>set outL</m> and <m>set outR</m> messages to receive the sinusoidal
		tones once again. With the <m>sawtooth</m> destination selected, change the frequency
		of the <o>phasor~</o> in the lower-right of the patcher. Notice that when set to
		an audible frequency, the ramp of the <o>phasor~</o> object causes it to sound like
		an oscillator rich in harmonics. This is a <i>sawtooth</i> wave, a common waveform
		used in synthesizer design. Because the <o>phasor~</o> object only ramps from <m>0</m>
		to <m>1</m> (and not <m>-1</m> to <m>1</m>), using a <o>phasor~</o> as an audio
		source isn't the best way to get this sound: the MSP <o>saw~</o> wave will do
		the trick instead.
	</bullet>

<h2>Summary</h2>

	<p>
		It is possible to make signal connections without patch cords by using the
		MSP objects <o>send~</o> and <o>receive~</o>, which are similar to the Max
		objects <o>send</o> and <o>receive</o>. The <m>set </m>message can be used
		to change the name of a <o>send~</o> or <o>receive~</o> object, thus
		switching how signals are routed. Signal flow can be routed to different
		destinations within a patcher, or shut off entirely, using the <o>gate~</o>
		object, which is the MSP equivalent of the Max object <o>gate</o>.
	</p>

	<p>
		The <o>cycle~</o> object can be used not only for periodic audio waves,
		but also for sub-audio control functions: you can read through the waveform
		of a <o>cycle~</o> object at any rate you wish by keeping its frequency at
		0 Hz and changing its phase continuously from <m>0</m> to <m>1</m>.
		The <o>phasor~</o> object is appropriate for changing the phase of
		a <o>cycle~</o> waveform in this way; you could also use a <o>line~</o> object
		to create a ramp that goes through the object's wavetable.
	</p>

	<p>
		The <o>sig~</o> object converts a number to a constant signal; it receives a number
		in its inlet and sends out a signal of that value. This is useful for combining
		constant values with varying signals. Mixing together tones with slightly
		different frequencies creates interference between waves, which can create
		beats and other timbral effects.
	</p>

	<seealsolist>
		<seealso name="gate~">Route a signal to one of several outlets</seealso>

		<seealso name="receive~">Receive signals without patch cords </seealso>

		<seealso name="send~">Transmit signals without patch cords</seealso>

		<seealso name="sig~">Constant signal of a number</seealso>

		<seealso name="phasor~">Sawtooth wave generator</seealso>
	</seealsolist>

</chapter>
