A stochastic drum machine

Here’s some more motivation: I don’t really like writing songs. The idea of writing a verse-chorus pop song, or even something a little weirder, and then playing that exact same song over and over to record and then at live shows is something that just isn’t exciting to me. Plus, honestly, it feels narcissistic to assume people will want to hear the stuff I write more than once. So when I make music, I want it to be adaptive and improvisational.

Everything I’ve created for 80KV is supposed to complement the kind of music I want to make. This means I needed something that would create backing tracks on the fly, that could write the kind of drum beats I like behind my music and keep it interesting so I’m free to improvise on top of it. That’s what this ugly little box does.

It’s mostly software-based, so I’ll go through the basic hardware setup first and then get into the software and how to hook it up to talk to your computer.


At the moment, the hardware consists of an Arduino Uno, three SPST pushbutton switches, and one cheap piezo sensor, plus some LEDs for status indicators. The pin confifuration on the Arduino don’t make a whole lot of sense, which is an artifact of how I developed the thing. Here are a couple of diagrams of the hardware, using the fantastic Fritzing app:


Doesn’t really get more basic than that. Apologies if there are any mistakes here — I drew this from memory rather than busting open the project box, because it seemed easier.

Software, part 1:
The Algorithm

Source code

I’ve tried to leave useful comments, but the code is pretty complicated, so I’ve drawn a diagram. You can find it at the bottom of the section. I’ve also written a detailed description here.

You have three drums to work with, “k” for kick, “s” for snare, and “h” for hi hat, although in practice you can map them to any drum samples you want. The software selects a random drum from a “bank” of six MIDI notes for each drum; k_note starts at MIDI note 24 (aka C0) and can select any note between 24 and 29, inclusive. s_note is the same but starts on 36 (C1), h_note starts on 48 (C2).

Then it generates a pattern. Within each quarter note, each sixteenth note — “1 e & a” — has an individual probability of being a positive hit for each of those drums, as determined for k and s by these arrays:

k_probs = [90, 10, 30, 10]

s_probs = [10, 25, 75, 25]

So, the kick drum has a 90% chance of hitting on every quarter note, a 10% chance of hitting on the “e” or “a” sixteenth note, and a 30% chance of hitting on the eighth note. The hi hat is different; it generates a random number between 15 and 75%, and has that same probability for every 16th note (so the hi hat can hit very rarely, very often, or anywhere in between).

Each drum has a sixteen-position boolean (true or false) array that represents one measure of a pattern. For each of those sixteen positions, for each drum, a random number between 0 and 99 (inclusive) is generated; if it’s less than the probability of that drum at that position within the measure, we store a 1 into the array at that position. A typical result might be (quarter-notes bolded to make it easier to read):

k = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1}

s = {0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0}

h = {1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1}

Wherever there’s a 1 in that array, that drum will hit; wherever there’s a 0, it’ll be silent.

With the drums and pattern randomized, it enters the main loop, which writes MIDI notes that a DAW can read (more on that in part 3). It tracks how many measures have elapsed; for three measures it makes notes that match the stored pattern, and for every fourth measure it generates a new random pattern (using the same probabilities) just for that measure. Then it (usually) returns to the pattern.

At the end of every eight measures, any number of things can happen:

  1. If the user has pushed the “randomize pattern” button any time in the last eight measures, a new random pattern is generated
  2. If the user has pushed the “randomize drums” button any time in the last eight measures, a new set of random drums is generated from the bank of drums
  3. If the user hasn’t pushed any buttons, it still has a probability of randomizing one drum (only one at a time)
  4. If the user hasn’t pushed any buttons, it still has a probability of generating a new pattern. This probability grows with each set of 8 measures that elapses. A single pattern cannot persist for more than 32 measures.

    And, finally,
  5. If the user has pushed the “set tempo” button, the drum beat fades out for four measures (by decreasing MIDI velocity) and then note production is halted until the new tempo is established.

Setting the tempo is handled by tapping on the piezo; it accepts five inputs (the tempo cannot be faster than 250ms = 240 bpm) and then averages them to create the new tempo. It counts down for three beats and then begins the new pattern.


Software, part 2:
Hooking it up

Arduino communicates with the computer it’s plugged into using a communication protocol called “serial”, which you may note is not the same as MIDI. Most DAWs accept MIDI but don’t know what to do with serial. Fortunately, there’s a tool — Hairless MIDI <-> Serial — that can translate for you. You can download Hairless here:


You need to configure it to match your Arduino setup. This Instructables tutorial walks you through it:


That translates your serial output into MIDI, but your DAW probably won’t recognize the output directly from Hairless. You need an additional program to route the MIDI signal into your DAW. I use LoopBe1:


With those programs set up and working in tandem, you should be able to select input from LoopBe1 in your DAW and observe the MIDI notes from Rhygenerate.

Now you have your DAW receiving MIDI, you need to set it up to do something with it. The easiest way to handle this is with a multiple sampler — Ableton Live, which I primarily use, has the “drum rack”. Most DAWs seem to have a built-in one, but Reaper doesn’t, so you’ll have to figure something else out if you’re using Reaper. I have a friend who uses Native Instruments Battery 4 with it. There are some free ones, but I’ve mostly found them to be kind of brittle. If you know of one and have a recommendation, please email me and I’ll add it to the library!

Final notes

As with everything I work on, this has endless possibility for improvement and expansion, and I have some big plans for it. The next step I’m going to take is probably to add some pots that let me adjust the probability for each drum individually, and maybe an additional one that let you adjust the overall randomness.

If you’re confused about anything in the post or have more questions, feel free to reach out at eightykv@gmail.com. I can’t guarantee I’ll get back to you — I’m pretty flaky sometimes — but I’ll do my best.

Thanks for reading,


Comments are closed.

Powered by WordPress.com.

Up ↑