msp programming and digital audio

msp Programming and Digital Audio

• msp is a set of digital audio extensions for Max. These extensions are a port of work done by Miller Puckette at IRCAM and UCSD to the Opcode Macintosh Max environment and have been programmed and marketed by David Zicarelli, the programmer of Opcode’s version of Max. The name msp has a couple of connotations: one is Max Signal Processing. Another is that msp are the initals of Miller Smith Puckette.

• Figure 1 shows a simple msp patch. All msp objects end with a twiddle (~) after the name of the object. Because it looks vaguely like a sine wave, the twiddle indicates that information coming into and/or out of the object is happening at the audio rate. Information in Max usually is sent whenever the user does something — plays in some MIDI, clicks with the mouse, etc. The fastest rate at which events can be scheduled to occur in Max is 1000 times per second. In audio, however, samples must be produced at the sampling rate for the sound to continue, and all msp objects update their outputs at the sampling rate (typically 44100 samples per second).


Figure 1: 440-hz oscillator

• The dac~ object at the bottom of Figure 1 is the digital-to-analog conversion object of msp. The two inlets at the top correspond to the left and right outputs from the sound system attached to the computer. This can be simply the stereo output from the computer itself or the outputs of a sound card (such as Digidesign gear) installed on the machine. The patch as a whole takes the audio output of a cycle~ object (a simple table-lookup oscillator), reduces the amplitude by multiplication, and sends it to the dac. The startwindow and stop messages to the dac turn on audio (for this window’s patch only) and turn it off, respectively.

• A sine wave is an example of simple harmonic motion. The wave completes one cycle of a simple back-and-forth motion at a constant rate. Because each cycle is completed in a constant amount of time, the motion of the wave is periodic. The number of cycles completed per second is the frequency of the wave, and the inverse of the frequency is its period. A wave that completes its cycle 100 times per second, then, has a frequency of 100 cycles per second (cps), also known as hertz (hz), and a period of 1/100 second, or 10 milliseconds.
• Sampling Theorem: To represent digitally a signal containing frequency components up to X Hz, it is necessary to use a sampling rate of at least 2X samples per second. If a signal has frequency components above one-half the sampling rate, these will be misrepresented in what is termed foldover, or aliasing. The frequency that is one-half the sampling rate is called the Nyquist frequency. Each frequency “has an alias equally far from the Nyquist frequency but on the other side of it. . . For this reason the Nyquist frequency is often called the folding frequency because we can think of frequencies above Nyquist as being folded down below Nyquist” [Steiglitz p. 47]. Simplifying this a bit, we can say that when an original frequency higher than one-half the sampling rate is sampled, it will produce a new frequency that is equal to the sampling frequency minus the original frequency.

• Samples in msp are interpreted as floating point values within the range –1.0 to +1.0. Samples conforming to that range occupy the full dynamic spectrum when sent to the dac~ object. Therefore, to attenuate the volume of a signal, the signal should be made to occupy a smaller range of values. In Figure 1 and Figure 2 below, the attenuation is performed by multiplying the output of cycle~ by a fractional value. Multiplication by a fraction is equivalent to division (another way to reduce the range of a signal) but is more economical to perform. Adding two signals together is equivalent to mixing them. Whenever more than one signal is mixed (added) together, take care that the combined output of the mixed sources does not exceed the range –1.0 to +1.0, because outside of that range the signal will clip.


Figure 2: Attenuation in msp

• To operate with signals digitally, we must discretize the waveform in two dimensions: in time (sampling) and in amplitude (quantizing). There are three steps to the conversion of an analog signal into a digital signal, the process called analog-to-digital conversion (ADC):

1) FILTER: A low-pass filter removes any frequency components of the signal exceeding one-half of the sampling rate.
2) MEASURE: A measurement is taken of the instantaneous amplitude of the signal at equally spaced intervals of time.
3) QUANTIZE: A quantizer assigns a precise numeric value to the measurement made in the previous step.

• The inverse process changes a digital representation to an analog one, and is called digital-to-analog conversion (DAC). In a DAC, voltage generators proportional to 2k volts are switched on when the corresponding bit k of the incoming digital representation is on. The steps of the DAC process are as follows:

1) TO VOLTAGE: The digital signal is converted to a time-varying voltage proportional to the sequence of numbers at the input.
2) TRANSIENT REMOVAL: “Glitches” introduced by step one are eliminated by ignoring fast transients.
3) FILTER: A low-pass filter set to half the sampling rate smoothes out the resultant analog signal.


Figure 3: DSP status window

• The DSP status window shows information about the configuration of digital audio on the Mac and the load a running msp program places on the central processing unit (CPU). Figure 3 shows the DSP status window during a typical execution of the simple oscillator patch of Figure 1. Notice that just running one oscillator uses almost 9% of the processing power of a Macintosh 8500/150 Power PC. The power of current CPUs to deliver digital audio directly is revolutionary, but actually using such applications quickly places a premium on processing speed.

• The cycle~ object is a table-lookup oscillator that uses a stored table of 512 samples. You can input your own sample tables or use the default sine wave. Cycle~ continuously outputs samples from the table at a frequency that corresponds to its argument (as in Figure 1) or to a value input to the left inlet.


Figure 4: cycle~ with variable frequency

• Figure 4 shows a patch with variable control over the oscillator frequency. The object line~ works like the object line, but at audio rates. Therefore the messages coming into line~ in Figure 4 will be changed into audio rate designations of frequency for the cycle~ object. Changing the value of the interpolation time into line (100 ms. in Figure 4) will change the speed with which the patch makes a portamento from one frequency to another.

• Note that the input to line~ is an ordinary Max message box. Some msp objects (such as line~ ) can take non-signal inputs and interpret these as controls over processes at the audio rate. The msp object sig~ explicitly upsamples a max value to an audio value; the object snapshot~ downsamples audio outputs to the Max range (a maximum of 1000 values per second).

• The average amplitude of a waveform is usually measured by the root-mean-square (rms) method. This works as it sounds: instantaneous measurements of amplitude are squared, summed, and averaged. The square root of the resulting number is the rms amplitude of the waveform.

• Noise is any unwanted signal added to the desired representation. Noise generally has a constant value (think of “hum”) and can be thought of as a lower limit to the range of useful signals. A commonly used measure of the presence of noise in a system is the signal-to-noise ratio (SNR), “which is usually defined as the ratio between the amplitudes of the largest useful signal and the amplitude of the inherent noise in a system.” Both amplitudes are expressed as rms values, and the SNR in decibels.
SNR (in dB) =  

• msp is very useful for experimenting with digital audio processing because so many DSP algorithms can be implemented quite directly using the objects msp provides. Rather than simply memorizing a formula such as the one above, you can use msp patches to try it out in practice. The patch shown in Figure 5 changes an amplitude value (varying between 0.0 and 1.0) to a value in decibels, using the formula shown above.


Figure 4: Amplitude to DB conversion

• MSP Exercise: Bring a Max/MSP patch to class February 26. Use MSP to implement one of the following digital signal processing techniques:
1) a low-pass filter
2) a reverb unit
3) a mixer
4) a flanger
NB: Don’t just copy the tutorial examples: do something in your patch that differs from the manual

Roads, C.. (1996) The Computer Music Tutorial. Cambridge, MA: The MIT Press.

Steiglitz, K. (1996) A Digital Signal Processing Primer New York: Addison-Wesley Publishing Company, Inc.

Leave a Comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.