<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Basics Tutorial 2: Adjustable Oscillator">

	<previous module="msp"  name="05_mspbasicchapter01"></previous>
	<next module="msp" name="05_mspbasicchapter03"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="02mAdjustableOscillator" patch="02mAdjustableOscillator.maxpat"/>

<h1>Basics Tutorial 2: Adjustable Oscillator</h1>



	<h2>
		Introduction
	</h2>

	<p>
		In this tutorial we'll look at how to control the amplitude of
		signals we work with in MSP. In addition, we'll look at an object
		that helps us generate signal ramps from Max messages that we can
		use to smoothly control the parameters of MSP objects.
	</p>

	<h2>
		Amplifier: <o>*~</o>
	</h2>

	<p>
		As we saw in our last tutorial, the <o>dac~</o> object represents the
		final <i>output</i> of our audio signal network; anything you want to
		come out of your speakers (or into your headphones) needs to eventually
		find its way to a <o>dac~</o> object. Now, any signal you want to
		listen to - a signal you send to <o>dac~</o> - must be in the amplitude
		range from -1.0 to +1.0. Any values exceeding those bounds will be
		clipped by the <o>dac~</o> (i.e. sharply limited to 1 or -1). This
		will cause (in most cases pretty objectionable) distortion of the sound.
		Most MSP objects that <b>generate</b> sound do so in the range of -1.0 to 1.0:
	</p>

<illustration><img  src="images/basicchapter02a.png"/></illustration>

	<caption>
		<i>The default output of cycle~ has amplitude of 1</i></caption>

	<p>
		As a result, learning to adjust signal amplitude is important so that we
		have control over the volume that we're sending to the output.
	</p>

	<p>
		To control the level of a signal you simply multiply each sample by a scaling
		factor. For example, to halve the amplitude of a signal you simply multiply
		it by 0.5. (Although it would be mathematically equivalent to divide the
		amplitude of the signal by 2, multiplication is a more efficient computation
		procedure than division.)
	</p>

<illustration><img  src="images/basicchapter02b.png"/></illustration>

	<caption>
		<i>Amplitude adjusted by multiplication</i></caption>

	<p>
		The <o>*~</o> object in MSP multiplies the signal in its left inlet by
		whatever comes into the right. This can be a constant number or a signal.
		If we wish to change the amplitude of a signal continuously over time, we
		can supply a changing signal in the right inlet of <o>*~</o>. By continuously
		changing the value in the right inlet of <o>*~</o>, we can fade the sound in
		or out, create a crescendo or diminuendo effect, etc.
	</p>

	<bullet>
		In the tutorial patcher, start the audio by clicking the <o>message</o> box
		labeled <m>start</m>. You won't hear anything yet, because the <o>*~</o>
		objects in the patch are multiplying their input by <m>0</m>, resulting in
		silence. In the patcher area labeled <m>1</m>, type the number <m>300</m>
		into the floating-point <o>number</o> box labeled <m>Frequency</m>. In
		the <o>number</o> box labeled <m>Amplitude</m>, type <m>0.2</m>. You
		should hear a 300 Hz sine wave playing softly out of your audio device.
		Click in the <m>Amplitude</m> <o>number</o> box to the <b>right</b> of
		the decimal point and drag the value up and down. You should hear the sine
		wave fade in and out in volume, just as it did in the last tutorial when
		we manipulated the <o>gain~</o> slider. However, you may hear something
		less pleasant as you change the volume, namely a series of clicks in the
		sound. This is called <i>zipper noise</i>, and results from sudden drastic
		changes in amplitude which cause discontinuities in the signal:
	</bullet>

<illustration><img  src="images/basicchapter02c.png"/></illustration>

	<caption>
		<i>Instantaneous change of amplitude causes a noisy distortion of the
		signal</i></caption>

	<p>
		Before we address that problem, set the volume in the <o>number</o> box
		to a comfortable amplitude, and drag in the <o>number</o> box
		labeled <m>Frequency</m>. As you hear the oscillator's pitch rise and
		fall, you may also hear a form of zipper noise do to the sudden changes
		in frequency. If we're going to be continuously controlling either of
		these parameters (the frequency of the <o>cycle~</o> object or the
		amplitude output by the <o>*~</o> object), we may want to modify those
		parameters using a signal instead of sending Max messages.
	</p>

	<h2>
		Line segment generator: <o>line~</o>
	</h2>

	<p>
		Take a look at the right-hand piece of patcher logic in the tutorial
		(labeled <m>2</m>). If, instead of an instantaneous change of amplitude
		(which can cause an objectionable distortion of the signal), we supply
		a <i>signal</i> in the right inlet of the <o>*~</o> object that changes
		over the course of 500 milliseconds, we interpolate between the starting
		amplitude and the target amplitude with each sample, creating a smooth
		amplitude change.
	</p>

	<p>
		The <o>line~</o> object functions similarly to the Max object <o>line</o>.
		In its left inlet it receives a target value and a time (in ms) to reach
		that target. The <o>line~</o> object calculates the proper intermediate value
		for each sample in order to change in a straight line from its current
		value to the target value.
	</p>

	<bullet>
		Turn down the volume on the left-hand audio circuit in the tutorial patcher
		by typing <m>0</m> into the <m>Amplitude</m> <o>number</o> box. In the
		right-hand part of the tutorial patcher, set the <m>Frequency</m> of
		the <o>cycle~</o> object to <m>300</m>. Then, clicking in the <o>number</o> box
		labeled <m>Amplitude</m>, set the volume to <m>0.3</m>. Rather than the sound
		immediately coming on as before, you should hear the sine wave fade in over
		half a second. If you drag within the <o>number</o> box, you should feel that
		the volume is <i>chasing</i> your actions, smoothly interpolating between
		values as if you were slowly manipulating a volume knob on an amplifier.
	</bullet>

	<p>
		The second value of the value/time pair we send to the <o>line~</o> object
		determines the smoothness of the transition. If we feel that the volume control
		isn't quite responsive enough, it's easily changed by playing with the
		transition value.
	</p>

	<bullet>
		Unlock the patcher and change the <m>message</m> box connected to the <o>line~</o>
		object controlling the <m>Amplitude</m>. If you'd like a quicker fade, try
		changing it to read <m>$1 50</m>. When you lock the patch and manipulate
		the <o>number</o> box again, you should feel that the volume is changing much
		more responsively to your commands.
	</bullet>

	<techdetail>
		<b>Technical detail: </b>Any change in the over-all amplitude of a signal introduces
		some amount of distortion during the time when the amplitude is changing. (The shape
		of the waveform is actually changed during that time, compared with the original
		signal.) Whether this distortion is objectionable depends on how sudden the
		change is, how great the change in amplitude is, and how complex the original
		signal is. A small amount of such distortion introduced into an already complex
		signal may go largely unnoticed by the listener. Conversely, even a slight
		distortion of a very pure original signal will add partials to the tone, thus
		changing its timbre.
	</techdetail>

	<h2>
		Adjustable oscillator
	</h2>

	<p>
		The patcher logic on the right uses a combination of <o>*~</o> and <o>line~</o> to
		make an adjustable amplifier for scaling the amplitude of the oscillator. However,
		the <o>line~</o> object can output signals that can be used to control a lot of
		things... not just volume.
	</p>

	<bullet>
		With the volume on the right-hand patcher logic raised, adjust the <o>number</o> box
		labeled <m>Frequency</m>. Try typing in a value distant from the current one, e.g.
		make the frequency jump from 300 to 1000 Hz. Notice that, unlike in the left-hand
		example, the frequency smoothly transitions over 500 milliseconds.
	</bullet>

	<p>
		As with the amplitude control, the <o>line~</o> object is sending a signal
		that smoothly transitions based on value/time commands sent to it by Max
		messages. By controlling the frequency of the oscillator in this way,
		we've created a gliding effect referred to in synthesizer design (and
		instrument performance) as <i>portamento</i>. If you think the transition
		is too slow for your tastes, unlock the patcher and change the <m>message</m>
		box so that you have a shorter transition time. See how short you can make it
		so that the portamento disappears but you still avoid hearing any clicks in
		during a frequency change.
	</p>

<h2>
	Summary
</h2>

	<p>
		Multiplying each sample of an audio signal by some number other than 1
		changes its amplitude; therefore the <o>*~</o> object is effectively
		an amplifier. A sudden drastic change of amplitude can cause a click,
		so a more gradual fade of amplitude - by controlling the amplitude with
		another signal - is usually advisable. The segment signal
		generator <o>line~</o> is comparable to the Max object <o>line</o>
		and is appropriate for providing a linearly changing value to the
		signal network. The combination of <o>line~</o> and <o>*~</o> can
		be used to make an <i>envelope</i> for controlling the over-all
		amplitude of a signal. In addition, a <o>line~</o> object can be used
		to smoothly control the parameters of nearly any MSP object, such as
		the frequency of a <o>cycle~</o> oscillator.
	</p>

<seealsolist>
	<seealso name="cycle~">Table lookup oscillator</seealso>

	<seealso name="dac~">Audio output and on/off</seealso>

	<seealso name="line~">Linear ramp generator</seealso>
</seealsolist>

</chapter>
