Exercise 13.1 - MIDI Input 1 2 3  

The techniques we have looked at so far allow us to describe musical pieces that the computer then performs when you start playback. Any variation in the performance has been written into the code. However, we can instead have the music and code react during playback to live input, such as from a MIDI controller (e.g. keyboard). MIDI allows us to input not only note pitch and volume (or velocity), but also numeric values (0 to 127) that can be used in our formulas and code to represent... anything.
   In this tutorial, we will use MIDI to vary the music as it plays, and show you some techniques that can be useful for creating a live performance. In computing terms, we are looking at user input and event handling. Whereas previous code examples ran autonomously and in a prescribed and predictable order, input events can be triggered at any time, in any order – at the whim of a human user. It is up to us to make sure any code that is run as a result (the "event handler") works with any ongoing musical processes and output.
   Manhattan responds to three types of MIDI event: Note On, Note Off, and Control Change. The first two occur when you hit a note and subsequently release it, respectively. The latter responds to a variety of other interactions with your MIDI device: turning a knob, moving a slider, pitch bend and mod wheels, hitting buttons or pads, etc. In each case, we simply write a block of code that knows how to handle these events and does something with the associated data – the pitch, velocity, or other parameters.

To open the MIDI Events editor, press ⌘=. This window gives you three text boxes to enter code that processes incoming Note On, Note Off, and Control ChangFe (CC) messages. Like functions and macros (in the previous tutorial), you can insert any code here and, in place of arguments, have access to parameter values supplied by the MIDI input message:

Note On / Off with pitch (p) and velocity (v) – which note was triggered (p, C-0 to B-9) and how fast/hard it was struck or released (v, 0 to 127).

MIDI CC with control (cc) and value (v) – which control was changed (cc, 0 to 127) and to what value (v, 0 to 127).

Some very basic expressions are provided for you. These simply set values in the pattern at the named locations, to show you what MIDI data is coming in. Even without playing, you should see the values change as you play notes and move sliders on your MIDI controller. (Note: ensure your MIDI device is correctly set up in the Audio Options settings. Be sure to check your input device is enabled.)

Exercise 13.2 - Handling Note Input 1 2 3

Add code to respond to note input, setting the pitch of a pattern cell to that of the MIDI note:

Unlike normal MIDI performance, triggering notes on your device does not directly and instantly sound new notes in the music, but triggers a change in the pattern being looped. While there might be a delay before your input is audible, this enables you to respond to live input in a way that makes sense musically – such that the input fits with your music.

To illustrate how MIDI can be effective at controlling processes operating in your pattern, we combine the MIDI input values with a macro.

Look at the 0xy macro provided (press Ctrl-Shift-=):

[+1].pitch = .pitch + y

[+2].pitch = .pitch + x

[+3].pitch = .pitch + y

Exit the Macro editor and enter the effect "047" in the top cell of Channel 02. Can you work out what it will do? Now, play the pattern and input more MIDI notes. You should hear a somewhat more interesting pattern that responds to your live input.

Exercise 13.3 - MIDI CC (Control Change) 1 2 3  

MIDI Control Change (CC) messages are sent whenever sliders, knobs, or other widgets are moved on the MIDI controller. There are 128 different MIDI CC controls, each with their own value.
  Separate sliders or knobs on a controller are each mapped to separate CC numbers. For example, one slider might set the value for CC#46, the next slider for CC#47.

Handle a MIDI Control Change message:

In your code, you can use different CC values for different roles. Manhattan remembers all the values for each CC#, so that you can access them later on, anywhere in your cell formulas, macros, or event handlers.

Recall the current/last value for a CC#:

When you are satisfied with how everything works, spend some time changing both the MIDI Event handling code and Macro code, to explore the various patterns and variations you can produce and control using MIDI. If you want to loop more than 4 rows, and build longer, more intricate patterns, move or remove the "C" effect.