teaching machines

Arpeggiator, Part II

July 14, 2018 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. Not only is it repetitive, it doesn’t really need a musician.

Instead, let’s program in a palette of sequences. The arpeggiator will play one of them at a time, and then maybe switch to another if 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 = 12;
const int nsequences = 3;

int sequences[nsequences][nsteps] = {
  {0, 2, 4, 5, 7,  9, 11,  9, 7, 5, 4, 2}, // major scale
  {0, 2, 3, 5, 7,  8, 10,  8, 7, 5, 3, 2}, // minor scale
  {0, 3, 5, 6, 7, 10, 12, 10, 7, 6, 5, 3}, // blues scale
};

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 notewait that has this shape:

void notewait(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. 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

Watching the arpeggio mountains is much less fun than listening to them. Let’s write a quick Pure Data patch. 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.