Namespaces

Variants
Actions

MSP Basics Tutorial 2: Adjustable Oscillator

From Cycling '74 Wiki
(Difference between revisions)
Jump to: navigation, search
 
(One intermediate revision by one user not shown)
Line 1: Line 1:
Click here to open the tutorial patch: [[02mAdjustableOscillator.maxpat]]
+
Click here to open the tutorial patch: [[Media:02mAdjustableOscillator.maxpat]]
  
 
===Introduction===
 
===Introduction===
  
In this tutorial we'll look at how to control the amplitude of
+
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.
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.
+
  
 
===Amplifier: *~===
 
===Amplifier: *~===
  
As we saw in our last tutorial, the {{maxword|name=dac~}} object represents the
+
As we saw in our last tutorial, the {{maxword|name=dac~}} object represents the final ''output'' 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 {{maxword|name=ezdac~}} object. Now, any signal you want to listen to - a signal you send to {{maxword|name=ezdac~}} - must be in the amplitude range from -1.0 to +1.0. Any values exceeding those bounds will be clipped by the {{maxword|name=ezdac~}} (i.e. sharply limited to 1 or -1). This will cause (in most cases pretty objectionable) distortion of the sound. Most MSP objects that '''generate''' sound do so in the range of -1.0 to 1.0:
final ''output'' 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 {{maxword|name=ezdac~}} object. Now, any signal you want to
+
listen to - a signal you send to {{maxword|name=ezdac~}} - must be in the amplitude
+
range from -1.0 to +1.0. Any values exceeding those bounds will be
+
clipped by the {{maxword|name=ezdac~}} (i.e. sharply limited to 1 or -1). This
+
will cause (in most cases pretty objectionable) distortion of the sound.
+
Most MSP objects that '''generate''' sound do so in the range of -1.0 to 1.0:
+
  
 
[[Image:Basicchapter02a.png|border]]
 
[[Image:Basicchapter02a.png|border]]
 +
 
''The default output of cycle~ has amplitude of 1''
 
''The default output of cycle~ has amplitude of 1''
  
As a result, learning to adjust their amplitude is important so that we
+
As a result, learning to adjust their amplitude is important so that we have control over the volume that we're sending to the output.
have control over the volume that we're sending to the output.
+
  
To control the level of a signal you simply multiply each sample by a scaling
+
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.)
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.)
+
  
 
[[Image:Basicchapter02b.png|border]]
 
[[Image:Basicchapter02b.png|border]]
 +
 
''Amplitude adjusted by multiplication''
 
''Amplitude adjusted by multiplication''
  
The {{maxword|name=*~}} object in MSP multiplies the signal in its left inlet by
+
The {{maxword|name=*~}} 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 {{maxword|name=*~}}. By continuously changing the value in the right inlet of {{maxword|name=*~}}, we can fade the sound in or out, create a crescendo or diminuendo effect, etc.
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 {{maxword|name=*~}}. By continuously
+
changing the value in the right inlet of {{maxword|name=*~}}, we can fade the sound in
+
or out, create a crescendo or diminuendo effect, etc.
+
  
 
* In the tutorial patcher, start the audio by clicking the {{maxword|name=message}} box labeled <code>start</code>. You won't hear anything yet, because the {{maxword|name=*~}} objects in the patch are multiplying their input by <code>0</code>, resulting in silence. In the patcher area labeled <code>1</code>, type the number <code>300</code> into the floating-point {{maxword|name=number}} box labeled <code>Frequency</code>. In the {{maxword|name=number}} box labeled <code>Amplitude</code>, type <code>0.2</code>. You should hear a 300 Hz sine wave playing softly out of your audio device.
 
* In the tutorial patcher, start the audio by clicking the {{maxword|name=message}} box labeled <code>start</code>. You won't hear anything yet, because the {{maxword|name=*~}} objects in the patch are multiplying their input by <code>0</code>, resulting in silence. In the patcher area labeled <code>1</code>, type the number <code>300</code> into the floating-point {{maxword|name=number}} box labeled <code>Frequency</code>. In the {{maxword|name=number}} box labeled <code>Amplitude</code>, type <code>0.2</code>. You should hear a 300 Hz sine wave playing softly out of your audio device.
Line 47: Line 28:
  
 
[[Image:Basicchapter02c.png|border]]
 
[[Image:Basicchapter02c.png|border]]
''Instantaneous change of amplitude causes a noisy distortion of the
 
signal''
 
  
Before we address that problem, set the volume in the {{maxword|name=number}} box
+
''Instantaneous change of amplitude causes a noisy distortion of the signal''
to a comfortable amplitude, and drag in the {{maxword|name=number}} box
+
 
labeled <code>Frequency</code>. As you hear the oscillator's pitch rise and
+
Before we address that problem, set the volume in the {{maxword|name=number}} box to a comfortable amplitude, and drag in the {{maxword|name=number}} box labeled <code>Frequency</code>. 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 {{maxword|name=cycle~}} object or the amplitude output by the {{maxword|name=*~}} object), we may want to modify those parameters using a signal instead of sending Max messages.
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 {{maxword|name=cycle~}} object or the
+
amplitude output by the {{maxword|name=*~}} object), we may want to modify those
+
parameters using a signal instead of sending Max messages.
+
  
 
===Line segment generator: line~===
 
===Line segment generator: line~===
  
Take a look at the right-hand piece of patcher logic in the tutorial
+
Take a look at the right-hand piece of patcher logic in the tutorial (labeled <code>2</code>). If, instead of an instantaneous change of amplitude (which can cause an objectionable distortion of the signal), we supply a ''signal'' in the right inlet of the {{maxword|name=*~}} 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.
(labeled <code>2</code>). If, instead of an instantaneous change of amplitude
+
(which can cause an objectionable distortion of the signal), we supply
+
a ''signal'' in the right inlet of the {{maxword|name=*~}} 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.
+
  
The {{maxword|name=line~}} object functions similarly to the Max object {{maxword|name=line}}.
+
The {{maxword|name=line~}} object functions similarly to the Max object {{maxword|name=line}}. In its left inlet it receives a target value and a time (in ms) to reach that target. The {{maxword|name=line~}} 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.
In its left inlet it receives a target value and a time (in ms) to reach
+
that target. The {{maxword|name=line~}} 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.
+
  
 
* Turn down the volume on the left-hand audio circuit in the tutorial patcher by typing <code>0</code> into the <code>Amplitude</code> {{maxword|name=number}} box. In the right-hand part of the tutorial patcher, set the <code>Frequency</code> of the {{maxword|name=cycle~}} object to <code>300</code>. Then, clicking in the {{maxword|name=number}} box labeled <code>Amplitude</code>, set the volume to <code>0.3</code>. 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 {{maxword|name=number}} box, you should feel that the volume is ''chasing'' your actions, smoothly interpolating between values as if you were slowly manipulating a volume knob on an amplifier.
 
* Turn down the volume on the left-hand audio circuit in the tutorial patcher by typing <code>0</code> into the <code>Amplitude</code> {{maxword|name=number}} box. In the right-hand part of the tutorial patcher, set the <code>Frequency</code> of the {{maxword|name=cycle~}} object to <code>300</code>. Then, clicking in the {{maxword|name=number}} box labeled <code>Amplitude</code>, set the volume to <code>0.3</code>. 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 {{maxword|name=number}} box, you should feel that the volume is ''chasing'' your actions, smoothly interpolating between values as if you were slowly manipulating a volume knob on an amplifier.
  
The second value of the value/time pair we send to the {{maxword|name=line~}} object
+
The second value of the value/time pair we send to the {{maxword|name=line~}} 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.
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.
+
  
 
* Unlock the patcher and change the <code>message</code> box connected to the {{maxword|name=line~}} object controlling the <code>Amplitude</code>. If you'd like a quicker fade, try changing it to read <code>$1 50</code>. When you lock the patch and manipulate the {{maxword|name=number}} box again, you should feel that the volume is changing much more responsively to your commands.
 
* Unlock the patcher and change the <code>message</code> box connected to the {{maxword|name=line~}} object controlling the <code>Amplitude</code>. If you'd like a quicker fade, try changing it to read <code>$1 50</code>. When you lock the patch and manipulate the {{maxword|name=number}} box again, you should feel that the volume is changing much more responsively to your commands.
  
 
----
 
----
'''Technical detail: '''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
+
'''Technical detail: '''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.
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.
+
 
----
 
----
  
 
===Adjustable oscillator===
 
===Adjustable oscillator===
  
The patcher logic on the right uses a combination of {{maxword|name=*~}} and {{maxword|name=line~}} to
+
The patcher logic on the right uses a combination of {{maxword|name=*~}} and {{maxword|name=line~}} to make an adjustable amplifier for scaling the amplitude of the oscillator. However, the {{maxword|name=line~}} object can output signals that can be used to control a lot of things... not just volume.
make an adjustable amplifier for scaling the amplitude of the oscillator. However,
+
the {{maxword|name=line~}} object can output signals that can be used to control a lot of
+
things... not just volume.
+
  
 
* With the volume on the right-hand patcher logic raised, adjust the {{maxword|name=number box}} labeled <code>Frequency</code>. 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.
 
* With the volume on the right-hand patcher logic raised, adjust the {{maxword|name=number box}} labeled <code>Frequency</code>. 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.
  
As with the amplitude control, the {{maxword|name=line~}} object is sending a signal
+
As with the amplitude control, the {{maxword|name=line~}} 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 ''portamento''. If you think the transition is too slow for your tastes, unlock the patcher and change the <code>message</code> 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.
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 ''portamento''. If you think the transition
+
is too slow for your tastes, unlock the patcher and change the <code>message</code>
+
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.
+
  
 
===Summary===
 
===Summary===
  
Multiplying each sample of an audio signal by some number other than 1
+
Multiplying each sample of an audio signal by some number other than 1 changes its amplitude; therefore the {{maxword|name=*~}} 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 {{maxword|name=line~}} is comparable to the Max object {{maxword|name=line}} and is appropriate for providing a linearly changing value to the signal network. The combination of {{maxword|name=line~}} and {{maxword|name=*~}} can be used to make an ''envelope'' for controlling the over-all amplitude of a signal. In addition, a {{maxword|name=line~}} object can be used to smoothly control the parameters of nearly any MSP object, such as the frequency of a {{maxword|name=cycle~}} oscillator.
changes its amplitude; therefore the {{maxword|name=*~}} 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 {{maxword|name=line~}} is comparable to the Max object {{maxword|name=line}}
+
and is appropriate for providing a linearly changing value to the
+
signal network. The combination of {{maxword|name=line~}} and {{maxword|name=*~}} can
+
be used to make an ''envelope'' for controlling the over-all
+
amplitude of a signal. In addition, a {{maxword|name=line~}} object can be used
+
to smoothly control the parameters of nearly any MSP object, such as
+
the frequency of a {{maxword|name=cycle~}} oscillator.
+
  
 
===See Also===
 
===See Also===

Latest revision as of 15:26, 28 June 2012

Click here to open the tutorial patch: Media:02mAdjustableOscillator.maxpat

Contents

[edit] Introduction

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.

[edit] Amplifier: *~

As we saw in our last tutorial, the dac~ object represents the final output 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 ezdac~ object. Now, any signal you want to listen to - a signal you send to ezdac~ - must be in the amplitude range from -1.0 to +1.0. Any values exceeding those bounds will be clipped by the ezdac~ (i.e. sharply limited to 1 or -1). This will cause (in most cases pretty objectionable) distortion of the sound. Most MSP objects that generate sound do so in the range of -1.0 to 1.0:

Basicchapter02a.png

The default output of cycle~ has amplitude of 1

As a result, learning to adjust their amplitude is important so that we have control over the volume that we're sending to the output.

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.)

Basicchapter02b.png

Amplitude adjusted by multiplication

The *~ 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 *~. By continuously changing the value in the right inlet of *~, we can fade the sound in or out, create a crescendo or diminuendo effect, etc.

  • In the tutorial patcher, start the audio by clicking the message box labeled start. You won't hear anything yet, because the *~ objects in the patch are multiplying their input by 0, resulting in silence. In the patcher area labeled 1, type the number 300 into the floating-point number box labeled Frequency. In the number box labeled Amplitude, type 0.2. You should hear a 300 Hz sine wave playing softly out of your audio device.

Click in the Amplitude number box to the right 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 gain~ slider. However, you may hear something less pleasant as you change the volume, namely a series of clicks in the sound. This is called zipper noise, and results from sudden drastic changes in amplitude which cause discontinuities in the signal:

Basicchapter02c.png

Instantaneous change of amplitude causes a noisy distortion of the signal

Before we address that problem, set the volume in the number box to a comfortable amplitude, and drag in the number box labeled Frequency. 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 cycle~ object or the amplitude output by the *~ object), we may want to modify those parameters using a signal instead of sending Max messages.

[edit] Line segment generator: line~

Take a look at the right-hand piece of patcher logic in the tutorial (labeled 2). If, instead of an instantaneous change of amplitude (which can cause an objectionable distortion of the signal), we supply a signal in the right inlet of the *~ 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.

The line~ object functions similarly to the Max object line. In its left inlet it receives a target value and a time (in ms) to reach that target. The line~ 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.

  • Turn down the volume on the left-hand audio circuit in the tutorial patcher by typing 0 into the Amplitude number box. In the right-hand part of the tutorial patcher, set the Frequency of the cycle~ object to 300. Then, clicking in the number box labeled Amplitude, set the volume to 0.3. 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 number box, you should feel that the volume is chasing your actions, smoothly interpolating between values as if you were slowly manipulating a volume knob on an amplifier.

The second value of the value/time pair we send to the line~ 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.

  • Unlock the patcher and change the message box connected to the line~ object controlling the Amplitude. If you'd like a quicker fade, try changing it to read $1 50. When you lock the patch and manipulate the number box again, you should feel that the volume is changing much more responsively to your commands.

Technical detail: 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.


[edit] Adjustable oscillator

The patcher logic on the right uses a combination of *~ and line~ to make an adjustable amplifier for scaling the amplitude of the oscillator. However, the line~ object can output signals that can be used to control a lot of things... not just volume.

  • With the volume on the right-hand patcher logic raised, adjust the box number box labeled Frequency. 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.

As with the amplitude control, the line~ 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 portamento. If you think the transition is too slow for your tastes, unlock the patcher and change the message 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.

[edit] Summary

Multiplying each sample of an audio signal by some number other than 1 changes its amplitude; therefore the *~ 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 line~ is comparable to the Max object line and is appropriate for providing a linearly changing value to the signal network. The combination of line~ and *~ can be used to make an envelope for controlling the over-all amplitude of a signal. In addition, a line~ object can be used to smoothly control the parameters of nearly any MSP object, such as the frequency of a cycle~ oscillator.

[edit] See Also

cycle~ - Table lookup oscillator

ezdac~ - Audio output and on/off

line~ - Linear ramp generator