A newer version of Max is available. Click here to access the latest documentation.
Tutorial 5: Turning Signals On and Off
Open the tutorial.
Turning audio on and off selectively
So far we have seen two ways that audio processing can be turned on and off:
• 1. Send a start or stop message to a dac~, adc~, ezdac~, or ezadc~ object.
• 2, Click on a ezdac~ or ezadc~ object.
There are a couple of other ways we have not yet mentioned:
• 3. Send an int to a dac~, adc~, ezdac~, or ezadc~ object. 0 is the same as stop, and a non-zero number is the same as start.
• 4. Double-click on a dac~ or adc~ object to open the DSP Status window, then use its Audio on/ off pop-up menu. You can also choose DSP Status... from the Options menu to open the DSP Status window.
Any of those methods of starting MSP will turn the audio on in all open Patcher windows and their subpatches. There is also a way to turn audio processing on and off in a single Patcher.
Sending the message startwindow -- instead of start -- to a dac~, adc~, ezdac~, or ezadc~ 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 startwindow is more appropriate than start
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.
The number in the left inlet determines which other inlet is open
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 startwindow message. Use the pop-up menu to listen to the four different waves. Click on the stop message to turn audio off.

Technical detail: A sawtooth wave contains all harmonic partials, with the amplitude of each partial proportional to the inverse of the harmonic number. If the fundamental (first harmonic) has amplitude A, the second harmonic has amplitude A/2, the third harmonic has amplitude A/3, etc. A square wave contains only odd numbered harmonics of a sawtooth spectrum. A triangle wave contains only odd harmonics of the fundamental, with the amplitude of each partial proportional to the square of the inverse of the harmonic number. If the fundamental has amplitude A, the third harmonic has amplitude A/9, the fifth harmonic has amplitude A/25, etc.
Note that the waveforms in this patch are ideal shapes, not band-limited versions. That is to say, there is nothing limiting the high frequency content of the tones. For the richer tones such as the sawtooth and pulse waves, the upper partials can easily exceed the Nyquist rate and be folded back into the audible range. The partials that are folded over will not belong to the intended spectrum, and the result will be an inharmonic spectrum. As a case in point, if we play an ideal square wave at 2,500 Hz, only the first four partials can be accurately represented with a sampling rate of 44.1 kHz. The frequencies of the other partials exceed the Nyquist rate of 22,050 Hz, and they will be folded over back into the audible range at frequencies that are not harmonically related to the fundamental. For example, the eighth partial (the 15th harmonic) has a frequency of 37,500 Hz, and will be folded over and heard as 6,600 Hz, a frequency that is not a harmonic of 2,500. (And its amplitude is only about 24 dB less than that of the fundamental.) Other partials of the square wave will be similarly folded over.
Partials that exceed the Nyquist frequency are folded over
• Choose the square wave from the pop-up menu, and set the frequency to 2500 Hz. Turn audio on. Notice that some of the partials you hear are not harmonically related to the fundamental. If you move the frequency up further, the folded-over partials will go down by the same amount. Turn audio off.
Turning off part of a signal network: begin~
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. This is rather wasteful, computationally, and 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.
If the begin~ object is placed at the beginning of a portion of a signal network, and that portion goes to the inlet of a selector~ or gate~ object, all calculations for that portion of the network will be completely shut down when the selector~ or gate~ is ignoring that signal. This is illustrated by comparing the sinusoid and sawtooth signals in the tutorial patch.
When the sinusoid is chosen, processing for the sawtooth is turned off entirely
When the first signal inlet of selector~ is chosen, as in the example shown above, the other signal inlets are ignored. Calculations cease for all the objects between begin~ and selector~ — in this case, the sig~, *~, and phasor~ objects. The line~ object, because it is not in the chain of objects that starts with begin~, continues to run even while those other objects are stopped.
• • Choose ‘Sawtooth’ from the pop-up menu, set the frequency back to 440 Hz, and turn audio on. Click on the message box above the line~ object. The line~ makes a glissando up an octave and back down over the course of ten seconds. Now click on it again, let the glissando get underway for two seconds, then use the pop-up menu to switch the selector~ off. Wait five seconds, then switch back to the sawtooth. The glissando is on its way back down, indicating that the line~ object continued to work even though the sig~, *~, and phasor~ objects were shut down. When the glissando has finished, turn audio off.
The combination of begin~ and selector~ (or gate~) can work perfectly well from one subpatch to another, as well.
• Double-click on the patcher triangle object to view its contents.
Contents of the patcher triangle object
Here the begin~ object is inside a subpatch, and the selector~ is in the main patch, but the combination still works to stop audio processing in the objects that are between them. There is no MSP object for making a triangle wave, so cycle~ reads a single cycle of a triangle wave from an AIFF file loaded into a buffer~.
begin~ is really just an indicator of a portion of the signal network that will be disabled when selector~ turns it off. What actually comes out of begin~ is a constant signal of 0, so begin~ can be used at any point in the signal network where a 0 signal is appropriate. It can either be added with some other signal in a signal inlet (in which case it adds nothing to that signal), or it can be connected to an object that accepts but ignores signal input, such as sig~ or noise~.
Disabling audio in a Patcher: mute~ and pcontrol
You have seen that the startwindow message to dac~ turns audio on in a single Patcher and its subpatches, and turns audio off in all other patches. There are also a couple of ways to turn audio off in a specific subpatch, while leaving audio on elsewhere.

One way is to connect a mute~ object to the inlet of the subpatch you want to control.
Stopping audio processing in a specific subpatch
To mute a subpatch, connect a mute~ object to the inlet of the subpatch, as shown. When mute~ receives a non-zero int in its inlet, it stops audio processing for all MSP objects in the subpatch. Sending 0 to mute~ object's inlet unmutes the subpatch.
• Choose ‘Square’ from the pop-up menu, and turn audio on to hear the square wave. Click on the toggle above the mute~ object to disable the patcher pulsewave subpatch. Click on the same toggle again to unmute the subpatch.
This is similar to using the begin~ and selector~ objects, but the mute~ object disables the entire subpatch. (Also, the syntax is a little different. Because of the verb ‘mute’, a non-zero int to mute~ has the effect of turning audio off, and 0 turns audio on.)
In the tutorial example, it really is overkill to have the output of patcher pulsewave go to selector~ and to have a mute~ object to mute the subpatch. However, it's done here to show a distinction. The selector~ can cut off the flow of signal from the patcher pulsewave subpatch, but the MSP objects in the subpatch continue to run (because there is no begin~ object at its beginning). The mute~ object allows one to actually stop the processing in the subpatch, without using begin~ and selector~.
• Double-click on the patcher pulsewave object to see its contents.
Output is 1 for half the cycle, and 0 for half the cycle
To make a square wave oscillator, we simply send the output of phasor~ -- which goes from 0 to 1 -- into the inlet of <~ 0.5 (<~ is the MSP equivalent of the Max object <). For the first half of each wave cycle, the output of phasor~ is less than 0.5, so the <~ object sends out 1. For the second half of the cycle, the output of phasor~ is greater than 0.5, so the <~ object sends out 0.
The pass~ object between the <~ object and the outlet is necessary to avoid unwelcome noise when the subpatcher is muted. It merely passes its input to its output unless the subpatcher is muted, when it outputs a zero signal. pass~ objects are needed above any outlet of a patcher that might be muted.
Another way to disable the MSP objects in a subpatch is with the pcontrol object. Sending the message enable 0 to a pcontrol object connected to a subpatch disables all MSP objects -- and all MIDI objects! -- in that subpatch. The message enable 1 re-enables MIDI and audio objects in the subpatch.
pcontrol can disable and re-enable all MIDI and audio objects in a subpatch
The patcher harmonics subpatch contains a complete signal network that's essentially independent of the main patch. We used pcontrol to disable that subpatch initially, so that it won't conflict with the sound coming from the signal network in the main patch. (Notice that loadbang causes an enable 0 message to be sent to pcontrol when the main patch is loaded, disabling the MSP objects in the subpatch.)
• Turn audio off, click on the toggle above the patcher harmonics object to enable it, then double- click on the patcher harmonics object to see its contents.

This subpatch combines 8 harmonically related sinusoids to create a complex tone in which the amplitude of each harmonic (harmonic number n) is proportional to 1/2n. Because the tones are harmonically related, their sum is a periodic wave at the fundamental frequency.
Wave produced by the patcher harmonics subpatch
The eight frequencies fuse together psychoacoustically and are heard as a single complex tone at the fundamental frequency. It is interesting to note that even when the fundamental tone is removed, the sum of the other seven harmonics still implies that fundamental, and we perceive only a loudness change and a timbral change but no change in pitch.
The same tone, minus its first harmonic, still has the same period
• Click on the startwindow message to start audio in the subpatch. Try removing and replacing the fundamental frequency by sending 0 and 1 to the selector~. Click on stop to turn audio off.
The startwindow message to dac~ (or adc~) 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. The mute~ object, connected to an inlet of a subpatch, can be used to disable all MSP objects in that subpatch. An enable 0 message to a pcontrol object connected to an inlet of a subpatch can also be used to disable all MSP objects in that subpatch. (This disables all MIDI objects in the subpatch, too.) The pass~ object silences the output of a subpatcher when it is muted.
You can use a 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. All MSP objects that are connected in a signal flow between the outlet of a begin~ object and an inlet of a selector~ object (or a gate~ object) get completely disconnected from the signal network when that inlet is closed.
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.

See Also

Name Description
begin~ Define a switchable part of a signal network
mute~ Disable signal processing in a subpatch
pass~ Eliminate noise in a muted subpatcher
pcontrol Open and close subwindows within a patcher
selector~ Assign one of several inputs to an outlet