ADDITIONAL ELEMENTS OF PROGRAMMING WITH MAX/MSP
IB.1 INTRODUCTION TO MIDI
PREREQUISITES FOR THE CHAPTER
MIDI is a protocol for communicating between electronic and/or digital musical instruments and computers. Using a physical MIDI cable, it is possible to connect a synthesizer to a computer, enabling the computer to “play” synthesizer, for example. Using the MIDI protocol, the computer sends notes to the synthesizer along with the intensity with which they should be played, their durations, and other information. MIDI instruments do not need to exist in physical form: they can also run as “virtual” instruments, which are computer applications that simulate the behavior of real instruments and produce sound through audio interfaces. Such digital instruments can communicate via MIDI, just as real-world instruments do. Programs like Max/MSP can send MIDI commands directly to a receiving program such as a virtual instrument. Indeed, Max has many objects that exploit the MIDI protocol, as we will learn in more detail in Chapter 9, but for the moment, we will stick to a basic subset that is used to manage MIDI messages. Open the file IB_01_MIDI_note.maxpat; Figure IB.1 shows the upper part of the Patcher Window.
Fig. IB.1 The upper part of the patch contained in IB_01_MIDI_note.maxpat
We have connected the kslider object (the musical keyboard) to some number boxes, which then connect to a noteout object. As we have already learned, clicking on one of the kslider< keys will generate the MIDI note value for the selected key on the left outlet. (We first used this object in Section 1.4.) Pressing a key also generates a velocity value on the right outlet that represents an intensity for the note; clicking on the upper part of the key will produce a higher velocity value, while clicking on the lower part will produce a lower value. (On physical keyboards, velocity actually reflects the velocity with which the key is pressed, hence the origin of the term.) Values for velocity can vary between 1 and 127 in MIDI.
Note and velocity values are sent to the left and center inlets of a noteout object, which then sends the appropriate command to any MIDI instruments (real or virtual) that are connected to it. In the MIDI protocol, this message is defined to be the “note-on” command. When you click close to the top of a kslider key, generating a high enough velocity value (let’s say above 90), you should hear the sound of a piano. This sound is not coming from Max/MSP, but instead, it is generated by a virtual instrument that is part of the operating system of your computer, which, by default, plays MIDI notes using the sound of a piano. If you try playing more notes, you will realize that noteout plays a sound for every new note that it receives, without interrupting or stopping the notes previously played. There is a slight problem: using noteout we told the virtual instrument when to begin playing a note, but we didn’t tell it when to stop! To interrupt a note and solve this problem, we will need to send another MIDI command: a matching MIDI note with a velocity equal to 0, which is called the “note-off” command. (A kind of “remove finger from key” command.)
One way to properly “extinguish” a MIDI note generated by kslider is to change the way that the object manages MIDI notes. In edit mode, call up the Inspector for the upper kslider, select the Value tab, and change the Display Mode parameter value from “Monophonic” to “Polyphonic”. Then return to performance mode. The first time that you click on a kslider key, the note will sound at the velocity chosen, and the second time that you click on the same key, the note will be triggered again, but this time with a velocity of 0, which will make the sound stop: try this. Besides adding the handy note-stopping feature, this also now enables you to activate more than one note at the same time: the performance capability has become polyphonic. Now turn your attention to the lower half of the patch in IB_01_MIDI_note.maxpat.
Fig. IB.2 The lower part of the patch in IB_01_MIDI_note.maxpat
We have connected the kslider object (in monophonic mode) to the makenote object, which generates a MIDI note-on/note-off command pair. Every time this object receives a note-on, it generates a note-off after a configurable length of time has elapsed. The object has three inlets, one for MIDI note number, one for velocity, and one for the duration in milliseconds (which is, of course, the amount of time to pass before the note-off command is sent). It has two outlets, one for MIDI note number and the other for velocity. There are two arguments associated with makenote, a velocity and a duration in milliseconds. In the patch, we have set velocity equal to 1 (a placeholder, as we will see), and duration equal to 500 milliseconds (or half a second). When the object receives a note-on, it will send the pitch and velocity of the note-on directly to its outlets, where, after the prescribed duration (500 milliseconds in this example) a note-off is also sent. Note that the velocity sent by the kslider (which in the example shown in the figure is a value of 103) overrides the value of 1 that had been provided as an argument. This first argument is only a placeholder that allows us to write a second argument, the note duration, after it. The second argument can also be modified by sending a value to the right inlet, which will replace the value originally specified in the second argument. Try playing some notes and changing the duration: observe how velocity values first reflect the value generated by kslider, and then, after the time specified by the duration parameter, they change to 0.
Now add an addition object to the lower part of the patch as shown in Figure IB.3:
Fig. IB.3 Transposing MIDI notes
This patch is similar to the one in the file IA_01_transposition.maxpat, which we examined in the first section of Interlude A. In this case, every key pressed on the keyboard generates two notes simultaneously, separated by a distance of 7 semitones: the interval of a fifth. Every time that a kslider key is pressed, in fact, the value of the MIDI note number is sent to the makenote object and also to the addition object that adds 7 to the first note number and then sends its output to makenote. To create these note pairs, we didn’t need to repeat velocity and duration values, since these values are sent to “cold” inlets of the makenote object, which are stored as internal variables (the contents of internal variables are re-used every time that a new value arrives at the “hot” inlet). In the figure, for example, both notes (the middle C and the G above it) will have a velocity of 112 and a duration of 500 milliseconds. From this example, you can see that Max’s rule about “hot” inlets being on the left is complementary with right-to-left execution, as discussed in Section 1.7. The first messages to be processed are those on the right, and processing these “cold” inlets initializes the internal variables in the object (such as the value for velocity used by makenote); the last message to be processed is to the “hot” inlet on the left, which causes output to occur only after internal variables have been set up.
 The right inlet of the noteout object is used to set the MIDI channel, which we don’t need at the moment. The details of this will be forthcoming in Chapter 9. top
Constructing an arpeggiator
IB.3 ROUTING SIGNALS AND MESSAGES
IB.4 THE RELATIONAL OPERATORS AND THE SELECT OBJECT
The select object
A probabilistic metronome
IB.5 REDUCING A LIST TO ITS PARTS: THE ITER OBJECT
IB.6 ITERATIVE STRUCTURES
IB.7 GENERATING RANDOM LISTS
IB.8 CALCULATIONS AND CONVERSIONS IN MAX
The expr object
Converting intervals and signals
IB.9 USING ARRAYS AS ENVELOPES: SHEPARD TONES
Repeating array-based envelopes
from “Electronic Music and Sound Design” Vol. 1 by Alessandro Cipriani and Maurizio Giri