teaching machines

Arpeggiator, Part II

June 16, 2019 by . Filed under electronics, music, public.

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

Our first draft of the arpeggiator will only use two of the potentiometers. One will decide the root note, and the other will decide which of many possible sequences to walk through.

Sequences

We could make our arpeggiator just play the same sequence over and over again. Imagine listening to the C major scale go up and down for several hours on end. We don’t want that. You would probably keep hearing it long after it stopped.

Instead, let’s program in a palette of sequences. The arpeggiator will play one of them at a time and switch to another when the musician so chooses. We’ll accomplish this using a 2D array.

Create a new Arduino sketch and add in this code at the top of the file:

const int nsteps = 6;
const int nsequences = 2;

int sequences[nsequences][nsteps] = {
  {0, 4, 7, 12, 7, 4}, // major
  {0, 3, 7, 12, 7, 3}  // minor
};

Those first two variables specify the dimensions of the array. It’s nsequences tall and nsteps wide.

Setup

In your setup function, prepare pins A0 and A1 for INPUT. We won’t need A2 yet. Also, initialize the serial port.

Note

Remember how we made note and note2 abstractions in Pure Data? We can make abstractions in C++ as well. In this case, we want an abstraction that writes a note and then delays a bit, allowing the note to finish. Add a new function named note_wait that has this shape:

void note_wait(int id) {
  // play the note
  // wait
}

Play the note by writing its ID to the serial port with Serial.println. Wait using the delay function. You pick the number of milliseconds. Remember the number you use; you’ll need it later.

Looping Sequence 0

Most of the work will happen in loop. Follow these steps to get just sequence 0 arpeggiating:

Upload and test your instrument. You should see little arpeggiating mountains going up and down, and turning the potentiometer should alter their root.

Looping Sequence i

Now we’ll add in our second potentiometer to switch to a different sequence. Follow these steps to get all the sequences arpeggiating:

Upload and test. You should be able to switch to the different sequences!

When you are confident that the code is working, switch your Serial.println to Serial.write.

Pure Data

Let’s write a Pure Data patch to make these arpeggio mountains audible. Follow these steps:

Leave edit mode and open the serial connection. You should now be able to hear your arpeggiator!

Challenges

After you get your arpeggiator working, answer the following questions on a piece of scratch paper.