Professional Documents
Culture Documents
All rights reserved. No part of this book may be reproduced in any form by any electronic
or mechanical means (including photocopying, recording, or information storage and
retrieval) without permission in writing from the publisher.
This book was set in Stone Serif and Stone Sans by Graphic Composition, Inc. and was
printed and bound in the United States of America.
Material
3 Graphic Programming with Max
Copyrighted Material
42 Programming Foundation
Copyrighted Material
43 Graphic Programming with Max
Object-Oriented Programming
Figure 3.1
Modules and submodules
Copyrighted Material
45 Graphic Programming with Max
Copyrighted Material
46 Programming Foundation
[User lnterface[
- [Controllers] -k
Figure 3.2
Encapsulation: radio "object"
!t4aterial
47 Graphic Programming with Max
Copyrighted Material
48 Programming Foundation
Graphical Programming
Copyrighted Material
49 Graphic Programming with Max
Introduction to Max
Objects
Copyrighted Material
50 Programming Foundation
-
noteout
Noteout plays two pitches, a mïnor
third and a perfect fifth above the
original, at a quieter dynamic level.
Figure 3.3
Anatomy of a simple Max program
Copyrighted Material
Si Graphic Programming with Max
Figure 3.4
An embedded object, patcher MinorChord
Copyrighted Material
52 Programming Foundation
ILFÏ Untitled
=1
:1 2
timer delay
>Th
Figure 3.5
The patcher window
A descriptive name typed into the object box describes its function,
such as timer or delay. The user doesn't need to know exactly how
delay works, only that it will delay a specific type of data entering it
by a specified amount of time. An object box can also contain symbols
for simple math or comparisons, such as + lo (add 10), * 5 (times 5),
or > 70 (greater than 70). An object may be thought of as a "black
box," with data entry, processing, and output occurring without any
of the "insides" known to the user. All that needs to be known is what
kind of action takes place inside each box, and what type of informa-
tion is expected. This simplifies matters tremendously, allowing musi-
cians to deal with higher levels of programming more closely related
to musical concepts.
Once an object receives data at its inputs, all the parameters and
variables that define the process are put into action. After the process
is completed and a result has been computed the data will be sent to
any object that it is connected to one of its outlets. If there are no more
objects connected, then the program will search for the next process
to begin.
Another group of objects, called user interface objects (fig. 3.6), use
graphic icons to represent their function. Their appearance suggests
Copyrighted Material
53 Graphic Programming with Max
-T-
45 87
Start Stop
Im a message
r
print Message
Figure 3.6
Examples of user interface objects
the way they operate, as well as the results they will produce. These are
the objects used to create the user interface, and they all operate in real
time. For example, various types of sliders (analogous to mixing-board
faders) increase or decrease values as they are moved up and down.
Toggle switches may be used to switch between one of two inputs, or
to start and stop processes, sending out a one for on, and a zero for off.
A special interface object, the bang button, outputs a bang message,
which means "do it." The bang button can be thought of as an "on"
switch, starting the process of whatever object it is connected to. The
bang button and the toggle switch are available as the fourth and
fifth items in the palette (fig. 3.5). The eighth item is the slider.
Copyrighted Material
54 Programming Foundation
10
<- slider object
seq
midiout
Figure 3.7
How objects pass messages
Passing Messages
The double black bars above and below the object name have points
where lines connect the output of one object to the input of another.
These lines represent the connections that enable messages to pass be-
tween objects. Object outlets (processed data) are always located at the
bottom bars; object inlets (data to be processed) are always at the top
bars. Objects have varying numbers of inlets and outlets depending on
their function. In figure 3.7, a data message (the number 15) is passed
from a number box to the + object (configured to add 10). The results
of the calculation are then passed to a second number box for display.
In another example, a control message (the word start) is passed to the
seq object, which starts a sequencer. [Control messages and parameter
names are shown in italics throughout this book. Object names appear
in bold.1 Since control messages may send commands to an object,
they are sometimes referred to as "command" messages.
Each inlet and outlet has a specific purpose, as specified in the Max
manual. With the Assistance Option selected in the options menu, a
brief description of an inlet or outlet will appear at the bottom of the
patcher window by pointing to it with the mouse. Extensive on-line
Copyrighted Material
SS Graphic Programming with Max
random
Generate a random number
Click to get
a random Set range via
number. right inlet.
N13
random 40 Optional argument initializes range (default 1).
Figure 3.8
Help file: random.help
help files are available explaining each Max object (fig. 3.8). New help
files can be created to describe custom objects. Simply holding down
the Option key and clicking on an object will bring up an operating
example with text explaining inlets, outlets, arguments, and other as-
pects of the object's operation.
Each object has a limited number of parameters, which are set to deter-
mine its behavior. Parameters are the elements in each object that de-
fine its function. Most often, they are variables, changing during a
performance according to a score or improvisation. For example, the
delay object has one parameter: delay time (fig. 3.10). The number
2000, representing 2000 milliseconds (two seconds), sent directly to
the delay object's right inlet, will set the object so that a bang message
entering the left inlet will be output two seconds later.
Other objects have more controllable parameters. The noteout ob-
ject creates a single MIDI note with the parameters of pitch, velocity,
and MIDI channel. Velocity and pitch messages must be sent to note-
out before it will play a note on a synthesizer. Some parameters will
use a logical default value if a message is not received in the right inlets.
The parameter for a MIDI channel number, for instance, if not specified
will always default to MIDI channel 1. In figure 3.9, a MIDI instrument
Copyrighted Material
56 Programming Foundation
60 3O
oteout
Figitre 3.9
Object parameters
These two examples are identical, delaying the bang message by two seconds.
bang button
means 'do it'
delay delay 200
The number 2000 entering the right inlet, The argument, 2000, sets the value
sets the delay time parameter to 2000 for the delay time parameter without
milliseconds (two seconds). using the inlet. Any number received
by the right inlet will override the
typed iii argument.
Figure 3.10
Changing object parameters with object inlets and arguments
Copyrighted Material
57 Graphic Programming with Max
In lets
Arguments
60
60 pitch 75 vel dur
makenote 75 1000
tL_
makenote
noteout
noteout List
Values for parameters are typed in as
Values for parameters (pitch, arguments. In the case of makenote,
velocity, and duration) are sent 75 1000 the first argument always represents
into makenote throught inlets. velocity, and the second argument
makenote
always represents duration.
noteout
Values for parameters are passed
to makenote as a list. The first
item in the list represents pitch,
the second represents velocity,
and the third represents duration.
Figure 3.11
Three ways to set parameters
3.11 shows three different ways to set parameters for makenote; the
results for all three are identical. Each example passes the message 60
75 100 to a makenote object, creating a single note (60 = C) at a me-
dium dynamic level (velocity 75) lasting one second (1000 millisec-
onds). The first example uses individual values sent to three inlets, the
second uses a list of value (lists are always sent to the left inlet), and
the third uses two arguments for velocity and duration with just the
note value entering the left inlet.
Message Types
Copyrighted Material
58 Programming Foundation
stop
delay 2OOO
Figure 3.12
Symbols
Copyrighted Material
59 Graphic Programming with Max
t."
¡ ¡ -
Electrical Processed
message electrical
MIDI message
message
Figure 3.13
Objects and messages: MIDI studio
Copyrighted Material
60 Programming Foundation
Figure 314
Interface objects and message equivalence
Cet Info
Copyrighted Material
61 Graphic Programming with Max
metro 500
>25 Uil
25
+ 50
*50
1>75 1>7
Figure 315
Multiple ways to send the same message
Order of execution
Even though Max is fast enough to give the appearance that many
things happen at once, in reality, a Max patch executes only one step
at a time. Max schedules the order of events from right-to-left: objects
appearing to the right of other objects will be executed first (fig. 3.17).
Max objects with more than one inlet also expect to receive data in
Copyrighted Material
62 Programming Foundation
L. Minimum
Draw Triangle
L Send on Mouse-Up Can't Change
n
C4
DispIaj:
0K
Note Names C4
i Cancel J
r-35
Slider Range 1 28
Offset 50
Multiplier 20
OK [Cancel :
Figure 3.16
Get Info
criaI
63 Graphic Programming with Max
otein
J
pitch velocity channel
60
op
E elay 2000
oteo ut
i
Channel and velocity set the
parameters:
pitch triggers a noteout.
Figure 3.17
Right-to-left order
Copyrighted Material
64 Programming Foundation
NoteOn! 1 60 = middle C 64 = mp
Figure 3.18
Anatomy of a MIDI message
The most common message, NoteOn, sends three bytes: a status byte
(type of data and channel), a note number byte (pitch), and a velocity
byte (loudness). Figure 3.18 shows the three-byte MIDI message re-
ceived by Max when middle C is played on a keyboard: [1001000
00111100 010000001. Fortunately, Max displays this as three integers:
i for MIDI channel, 60 for pitch, and 64 for velocity. The process of
transposing that note by a major second would involve simply adding
the number 2 to the pitch, to make D (62). Imagine how time consum-
ing it would be to have to program in binary by taking 00111100
(decimal 60) and adding 00000010 (decimal 2) to create 00111110
(decimal 62)!
Max translates all the essential data from complex MIDI messages
and presents it to the user as simple integers that use specialized MIDI
objects. These objects handle input and output from Max and separate
specific types of data from the entire MIDI stream, such as note data or
pitch bend data. From there, it is quite easy to manipulate and process
the numbers, using algorithms to create music. After the processing is
completed, Max repackages the final output in the form of complete
MIDI messages before sending the data out to a MIDI device which
automates the confusing task of interpreting and assembling binary
messages. In between input and output, Max is busy processing the
data received.
Most MIDI devices receive data on at least sixteen channels, while
they usually send data on only one channel. Each MIDI channel can
have a different sound assigned to it. MIDI objects in Max can specify
on what channels they will send and receive data. In figure 3.19, the
Copyrighted Material
6S Graphic Programming with Max
notein
pe 40
L.
pe 400 pe 80 pe 80 pe 120g pipe 1200
Figure 3.19
MIDI channels
Many Max programs use notein for input, and noteout for output (fig.
3.20). Notein does not take note messages through its inlet, since it
receives messages directly from the computer's serial port, which is
connected to a synthesizer via the MIDI interface. (The inlet on notein
may be used to specify the input device.) Likewise, noteout does not
have an outlet at all, since it never passes data to other objects within
Max. Notein separates a MIDI Note-On message, sent via a MIDI con-
troller, into its three parts: pitch, velocity, and MIDI channel. Note
number 60, middle C, may be used as a reference to calculate the note
names and the register for other note numbers, or a number box may
be set to display note numbers as more familiar note names, such as
D3 or B5, using GET INFO.
When a keyboard note is released, a Note-Off message is sent, begin-
ning with a Note-Off status byte (1000), and otherwise identical to its
corresponding Note-On message. Max interprets this as a second event,
receiving the same note number and MIDI channel, with velocity 0.
Since a single keystroke is usually thought of as a single event, a special
Copyrighted Material
66 Programming Foundation
Notein receives MIDI data Noteout sends MIDI note data to a MIDI device.
from any MIDI device,
representing pitch, velocity, pitch 60, velocity 100
and MIDI channel.
1o loo
oo pitch 60, velocity zero
(Note-Off)
note in
oteout
-1
N60 N loo Ni
pitch velocity MIDI channel
Figure 3.20
notein and noteout
Copyrighted Material
67 Graphic Programming with Max
controller value
MIDI channel
75 controller number,
N
Figure 3.21
MIDI controllers
Max also handles all types of MIDI data not directly related to notes.
Continuous controllers (fig. 3.21) are MIDI controller devices that include
the modulation wheels, foot pedals, data sliders, and aftertouch that
are present on many MIDI keyboard instruments. Each controller is
identified by a corresponding controller number, and outputs values
between O and 127. These data can be interpreted by the computer in
a variety of ways. The modulation wheel, for example, is continuous
controller number 1. It often controls vibrato and timbre changes on
a synthesizer, but it can be used to control computer processes, such as
changing tempo or delay time. Ctlin and ctlout handle continuous
controller input and output from Max. The object's parameters, from
left to right, correspond to controller value (data), controller number
(identifies type of controller), and MIDI channel. Many control num-
bers are reserved for common functions found on synthesizers, such as
controller number 7, which is used to control the overall volume of
each MIDI channel. This is similar to moving a volume slider, and is
different from a velocity value, which represents the dynamic level of
a single note.
Copyrighted Material
68 Programming Foundatìon
bendin u Chin]
-I. -
75 75
aftertouch value
IPitchbend value
1111
1111
T T
75 75
[kendou u ch o u
Figure 3.22
Pitchbend and aftertouch
al
69 Graphic Programming with Max
Figure 3.23
Program change messages
Max also has provisions for sending and receiving the entire stream of
MIDI data, which could include messages for notes, continuous con-
trollers, MIDI channels, and program changesin other words, every-
thing sent to the serial port via MIDI. Midiin accepts the entire MIDI
stream, and is often paired with midiparse, which separates all the
data into its various types (fig. 3.24). If only pitch and velocity are
required for input, notein is a much more efficient choice than midiin.
However, it is sometimes useful to have the entire MIDI stream avail-
able. On the output side, midiformat packs the various types of data
into a message that is sent out as a MIDI stream by midiout. Midiin
and midiout are often used in conjunction with the seq object, which
can record and play back a performance (sequence), and which writes
and reads standard MIDI files saved to disk. Seq can also play standard
MIDI files created with another software application, such as a se-
quencer or a notation program. A MIDI overview and specification can
be found in the Max manual.
Copyrighted Material
70 Programming Foundation
Midiin is an object that receives all MIDI data being sent to Max.
mid un
midiparse
list of pitch list of list of program aftertouch pitchbend MIDI
and velocity polyphonic controller change channel
pressure key number and
and value value
midiformat
midiout
Figure 3.24
midiin and midiout
Chapter Summary
Copyrighted Material