MSP Polyphony Tutorial 1: Turning Signals On and Off
<openpatchlink>Open the tutorial</openpatchlink>.
Turning audio on and off selectively
There are two elementary ways of turning audio processing on and off in an MSP patcher:
For the first tutorial that introduces the poly~ object and the idea of polyphony in general, we are going to take a step back and think more broadly about allocating resources in a signal processing patch - when you think about it, that is what polyphony is about, in a general way.
There are two other ways of turning audio processing on and off in an MSP patcher we have not yet mentioned:
- Send an
intto a dac~, adc~, ezdac~, or ezadc~ object.
0is the same as
stop, and a non-zero number is the same as
- Double-click on a dac~ or adc~ object to open the Audio Status window, then use its Audio on/ off pop-up menu. You can also choose Audio Status... from the Options menu to open the Audio Status window.
Any of those methods of starting MSP will turn the audio on in all open Patcher windows and their subpatches. There are also a several ways to turn audio processing on and off in a single Patcher.
Local control with the startwindow message
Sending the message
startwindow -- instead of
start -- to a ezdac~, ezadc~, dac~, or adc~ object turns the audio on only in the Patcher window that contains that object, and in its subpatches. It turns audio off in all other Patchers. The
startwindow message is very useful because it allows you to have many different signal networks loaded in different Patchers, yet turn audio on only in the Patcher that you want to hear. If you encapsulate different signal networks in separate patches, you can have many of them loaded and available but only turn on one at a time, which helps avoid overtaxing your computer's processing power. (Note that
startwindow is used in all MSP help files so that you can try the help file's demonstration without hearing your other work at the same time.)
In some cases (e.g. when you have multiple helpfile patcher windows open), using the
startwindow message is more appropriate than start.
Using the Mixer Engine to control audio
Every top-level patcher in Max has its own DSP chain, and is equipped with a mixer engine that lets you turn audio on and off in the patcher. You'll find the Mixer engine controls in the patcher window toolbar. By default, the Mixer control panel controls for the dac~ or ezdac~ object in your patcher window (you can add additional dac~ objects to your patch as described <link type="vignette" module="core" name="mixer" anchor="multidac">here</link>).
To display the Mixer engine control panel for your patcher, click on the Mixer button in the patcher toolbar.
You can click and drag on the volume knob to set the output volume for the patcher window, and mute or solo the audio output using the M (Mute) or S (Solo) buttons.
The Mixer Engine also allows you to fade the audio in or out when you turn audio on and off in a patcher window by adding an amount of latency to the patcher outputs. You can set the length of the fade in/out in milliseconds by setting mixer latency and crossfade time in the <link module="core" name="mixer_preferences" type="vignette">Mixer Preferences</link>.
Selecting one of several signals: selector~
In the previous chapter, we saw the gate~ object used to route a signal to one of several possible destinations. Another useful object for routing signals is selector~, which is comparable to the Max object switch. Several different signals can be sent into selector~, but it will pass only one of them -- or none at all -- out its outlet. The left inlet of selector~ receives an
int specifying which of the other inlets should be opened. Only the signal coming in the opened inlet gets passed on out the outlet.
As with gate~, switching signals with selector~ can cause a very abrupt change in the signal being sent out, resulting in unwanted clicks. So if you want to avoid such clicks it's best to change the open inlet of selector~ only when audio is off or when all of its input signal levels are 0.
In the tutorial patch, selector~ is used to choose one of four different classic synthesizer wave types: sine, sawtooth, triangle, or square. The umenu contains the names of the wave types, and sends the correct number to the control inlet of selector~ to open the desired inlet.
- Choose a wave type from the pop-up menu, then click on the
startwindowmessage. Use the pop-up menu to listen to the four different waves. Click on the
stopmessage to turn audio off.
Turning off part of a signal network: pcontrol
If you want to enable or disable signal processing in a patcher or abstraction, you can use the pcontrol object. The
enable message to pcontrol turns off all activity within a subpatcher - both MSP signals and Max scheduling.
Technical detail: If you double-click on the pcontrol object named harmonics and look inside, you'll hotice that we have a number of cycle~ objects computing different harmonics of the fundamental frequency used throughout the tutorial patcher. In the subpatch, the selector~ object allows us to disable the fundamental frequency. Even with the fundamental missing, our ears still resolve the same pitch.
Turning off part of a signal network: poly~
You have seen that the selector~ and gate~ objects can be used to listen selectively to a particular part of the signal network. The parts of the signal network that are being ignored — for example, any parts of the network that are going into a closed inlet of selector~ — continue to run even though they have been effectively disconnected. That means MSP continues to calculate all the numbers necessary for that part of the signal network, even though it has no effect on what you hear. It would be preferable if one could actually shut down the processing for the parts of the signal network that are not needed at a given time.
The poly~ object allows us to do just that.
The poly~ object is very powerful - to describe it simply, it lets us create and manage multiple copies of the same MSP subpatch within one object, and is very useful for creating polyphonic synths. We'll describe its use for that purpose in greater detail in a <link type="tutorial" module="msp" name="mspchapter21">later tutorial</link>. For now, we'll show you how to use it to conserve CPU usage.
The tutorial patch contains an example of the poly~ object used to enable and disable signal processing in a patch.
The poly~ object takes as its argument the name of a patcher file, followed by a number that specifies the number of copies (or instances) of the patch to be managed (in this case, it's only a single instance). When you launch the patch, a toggle object automatically sends the message
mute 0 1 to the poly~ object.
To look inside the poly~ object, double-click on it to open a new patcher window.
The in and out~ objects function in a matter similar to inlet and outlet objects in a subpatcher. The second in object receives the
mute message and routes it to the thispatcher object. Sending the
mute 0 1 message to the thispatcher object located in the patch will shut down the processing of the loaded subpatcher completely (the first zero indicates that you are sending the message to the first and only instance of the subpatcher loaded in the poly~ object).
When the tutorial patch opens for the first time, toggle object connected to the message box that contains the control message is on, and the oscillators in the wobbler abstraction inside the poly~ object are shut down. If you click on the toggle to send the message
mute 0 0 to the thispatcher object, you will hear the oscillators. If you have the Mixer Engine window open, you will also notice that the percentage CPU usage increases, indicating that there is more audio processing going on.
Creating your own stompbox
The final portion of our tutorial patch - the stompbox patcher - combines two different techniques used in this tutorial with some clever patching to imitate a stompbox that not only selects direct or processed output, but also turns processing off when it is not selected.
Our patcher contains a trigger object to route and control the order of events. The right outlet of the trigger object takes a 0 or 1 input value and uses it with the
mute 0 message to turn processing on or off inside the patcher loaded in the poly~ object. Since we want to send the
mute 0 1 message that mutes the subpatcher when the stompbox button is on, we use the !- object to invert the switch input.
Our patcher also contains a selector~ object that switches between the straight patcher input (the center inlet) and the output of a poly~ object that contains a simple distortion patch. Since the selector~ switch uses a zero indicate that no output is selected, we'll need to add 1 to our stompbox toggle object's output.
So when we press the toggle attached to the right inlet of the stompbox patcher file, we do two things:
- We send the message
mute 0 0to turn the processing inside of the poly~ object on or off.
- We send a 1 or 2 to the selector~ object to choose the straight or processed input to the selector~ and output it.
startwindow message to ezdac~ (or ezadc~) starts audio processing in the Patcher window that contains the dac~, and in any of that window's subpatches, but turns audio off in all other patches. An
enable 0 message to a pcontrol object connected to an inlet of a subpatch can be used to disable all MSP objects in that subpatch. (This disables all MIDI objects in the subpatch, too.)
You can use selector~ object to choose one of several signals to be passed on out the outlet, or to close off the flow of all the signals it receives.
You can use the poly~ object in conjunction sending the
mute 0 0/1 message to a thispatcher object in the
MSP patcher hosted by the poly~ object as a way of enabling or disabling all MSP objects in that subpatch. (This disables all MIDI objects in the subpatch, too.)
Any of these methods is an effective way to play selectively a subset of all the MSP objects in a given signal network (or to select one of several different networks). You can have many signal networks loaded, but only enable one at a time; in this way, you can switch quickly from one sound to another, but the computer only does processing that affects the sound you hear.
poly~ - Manage polyphony/DSP for patchers
selector~ - Assign one of several inputs to an outlet