# teaching machines

## Music Mouse, Part V

This post is part of a series of notes and exercises for a summer camp on making musical instruments with Arduino and Pure Data.

In this exercise, we prepare our Music Mouse to send messages to a Pure Data patch. When the musician goes up or down in the Music Mouse interface, we issue a message containing a single note to be played. This will be the melody. When the joystick goes left or right, however, we issue a message containing several notes to be played. This will be the harmony.

In this exercise, we design a protocol, a language for describing how two systems communicate with each other.

### Melody Message

We need to distinguish between melody and harmony messages in Pure Data. Let’s support this on the Arduino side by sending a message type as the first byte in each message we send. We’ll use a 1 to denote a melody message. The melody, which is controlled by vertical motion, plays a single note at a time, so we technically need only send one MIDI number across the serial port. However, we will need four numbers in our harmony messages.

To make our work in Pure Data easier, let’s make both messages take up four bytes. Melody messages will have this form:

1 y 0 0

You should already have code that prints y. Precede it with code to print 1. Print two meaningless zeros after y.

### Harmony Message

We’ll use 0 to denote a harmony message. When the horizontal position changes, we want to print more than x. We also want to print the partner notes of the chord. A harmony message will have this form:

0 x (x + offset to middle) (x + offset to middle + offset to final)

Let’s figure out what these partner notes are using Tero Parviainen’s Music Mouse implementation.

Move the mouse so that the leftmost note of the chord is on a C, which is any white key immediately to the left of a pair of black keys. Observe the other two keys that are played above or to the right of the C. Count the number of keys or halfsteps it takes to jump up to these higher notes.

• How many halfsteps above the C is the middle note of the chord?
7
• How many halfsteps above the middle note is the final note of the chord starting on C?
9. All told, the chord includes C, C + 7, and C + 7 + 9.

Do all the chords have the same offsets? Let’s check. Move the mouse so that the bottom note of the chord is on a D, which is any white key between a pair of black keys.

• How many halfsteps above the D is the middle note of the chord?
7
• How many halfsteps above the middle note is the third note of the chord starting on D?
8. Ugh. The chords do not all have the same offsets. Let’s create two arrays to record the offsets:
int middle_offsets[] = {7, 7, ?, ?, ?, ?, ?};
int final_offsets[] = {9, 8, ?, ?, ?, ?, ?};


Use Music Mouse and determine the rest of the offsets.

What did you come up with?
int middle_offsets[] = {7, 7, 7, 7, 7, 7, 6};
int final_offsets[] = {9, 8, 8, 9, 9, 8, 9};


Now we are ready to send a complete message for the harmony. You should already be printing x. Precede it with a 0. After printing x, print the MIDI numbers of the middle and final notes. Which offset we use depends on our current position in the scale. What do we have that can tell us our current position in the scale?

Upload your code. You should see both message types. When you see the messages following the protocol, change all your println calls to write calls. We’re heading to Pure Data next.