Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-includes/pomo/plural-forms.php on line 210

Warning: Cannot modify header information - headers already sent by (output started at /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-includes/pomo/plural-forms.php:210) in /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-content/plugins/bad-behavior/bad-behavior/screener.inc.php on line 9

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 77

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /nfs/c03/h07/mnt/48256/domains/blog.sokay.net/html/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 87
Robot Music - blog.sokay.net

Series: Robot Music I, Robot Music II: Modes, Robot Music III: The Circle of Fifths, Robot Music IV: Scales of the World

(This post features the hip guitar stylings of none other than ME! I’ll play some real life, randomly generated, Robot Music! The link is at the end.)

Close Up DieI’ve wanted to see more procedural music in games for a long time, but the most we see are pretty sorry attempts. I’m not just talking about “shifting volume on pre-recorded riffs” procedural, I mean “the game is writing its own live soundtrack” procedural.

“But Chris, that doesn’t even happen on consoles! We can’t do it in flash, no way!” Sounds difficult or even impossible, but it isn’t. The only problem is it takes an understanding of tricky programming concepts and tricky musical concepts. Without programmers that also study music theory, we just don’t see procedurally written music.

Well, I’d like to help change that. I know a thing or two about music and a thing or two about programming so in this article I’m going to do my best to tip off any programmers interested in putting together a simple music generator. Today’s generator won’t be truly procedural, but it’ll start things off in that direction.

And if you play a little music, but don’t know how to write a song, maybe this article can help you out too.

For the sake of this exercise, let’s limit our music to looping riffs. Let’s also limit ourselves to a 4/4 time (4 quarter notes per measure, 1, 2, 3, 4 – 1, 2, 3, 4 – 1, 2, 3, 4). We’ll be using random note selection, so there won’t be any regard for tone, but don’t worry, it’ll all work out. There will still be a tone, we just won’t be manipulating it in this experiment. Later we can get into controlling tone, but not all in one post.

Guitar Frets

Any common instrument uses a grand total of 12 notes: C, C#, D, D#, E, F, F#, G, G#, A, A#, B. You can play those notes randomly and no one can tell you you’re not playing music, but most people won’t want to listen to it. If you want to make something a little prettier, I suggest you limit yourself to a Pentatonic scale (5 note scale). C Major Pentatonic looks like this: C, D, E, G, A. In my opinion, any of those notes can be played in sequence or together to produce what anyone can call a pleasing sound. We can limit ourselves further by only playing in a single octave so that our root note, C, is the lowest note we play.

Piano Keys

Now let’s generate a riff.

In 4/4 we need to generate a minimum of 4 notes for our riff. We’ll make our first note in the riff equal to the key we’re playing in (in this case, C). That leaves 3 more notes to generate. So we take 3 random numbers from 1 to 5 where each number equals a note; 1 = C, 2 = D, 3 = E, 4=G, and 5 = A.

I just rolled a die and wrote down the first set of numbers that fulfilled our requirements. I got 1, 5, 5 as our sequence. We add 1 to beginning to get 1, 1, 5, 5. Those numbers correspond with the notes C, C, G, G.

I’ve gone ahead and generated a second riff so we can switch back and forth between the two for some variety, to sound a little more “song-like.” The second one came out to 1, 3, 2, 5 or C, E, D, G. Now we try playing them. We can play the first riff 4 times, then play the second one 4 times and start over (4 measures each).

I’m playing along on my guitar;

C, C, G, G – C, C, G, G – C, C, G, G – C, C, G, G

C, E, D, G – C, E, D, G – C, E, D, G – C, E, D, G

This is an interesting pair of riffs because the first one has a really simple rock feel, but the second one bounces around kinda funny. It’s a unique little tune, especially for one written by a die (or was it? Must discuss later . . .).

Let’s go one more step to make it a full song. I’ll generate another set of notes: 1, 5, 3, 1 or C, G, E, C

We’re going to play this last one slower to break up the other two. We play each note 4 times before moving to the next one:

C, C, C, C – G, G, G, G – E, E, E, E – C, C, C, C

Now we have 3 pieces to our song that we can arrange however we want. Let’s stick to simplicity. We play the first riff 4 times, the second riff 4 times, do the first and second 4 times again, then play the third riff 2 times and start over (Our song can loop. Why not?).

This is the final product:

C, C, G, G – C, C, G, G – C, C, G, G – C, C, G, G

C, E, D, G – C, E, D, G – C, E, D, G – C, E, D, G

C, C, G, G – C, C, G, G – C, C, G, G – C, C, G, G

C, E, D, G – C, E, D, G – C, E, D, G – C, E, D, G

C, C, C, C – G, G, G, G – E, E, E, E – C, C, C, C

C, C, C, C – G, G, G, G – E, E, E, E – C, C, C, C

And start over. But those letters don’t mean much, so you should probably take a listen: Robot Music – Song 1

Pretty simple right?

1. Pentatonic Scale (C, D, E, G, A) in 4/4.

2. The root note is the first note of the riff.

3. The last 3 notes are randomly generated, where numbers correspond with notes (1 = C, 2 = D, 3 = E, 4=G, and 5 = A)

4. Arrange multiple riffs for some variety.

5. End on the same note that you started with (C).

Of course, you can make your own modifications to this logic. For example, I find that it’s usually a bad idea to make the end of a riff blur too much with the beginning of the next riff. This is a small problem when we play the slow part of our song and all the C notes just mash together into one long sequence. You can fix your algorithm so that the last note will always be different from the first one, or so that no note will ever be played right after itself.

If you’re using this method to help you write your own music, you can modify the notes yourself. I think the slow part of our song sounds a lot better as C, G, E, D instead of C, G, E, C, but you can switch any of them out however you want. And don’t be afraid to ignore the rules we set up today.

You can also add in chords, like exchanging each single note for a power chord (C+G, D+A, E+A#, etc). Many other chords can be used, but they may not be so easy to just slap in (and still sound nice all the time).

The point is, you’ll have to experiment. Try a lot of stuff. There are no real rules to music and if anyone tells you you’re doing it wrong, slap them in the face and tell them they don’t know a damn thing about it. Music is whatever you say it is and if they don’t like it, then tough!

Next time, we’ll modify the logic to recognize different modes of the pentatonic scale, so that we have better control over our song’s tone, but this will do for now.

Hope you enjoyed the subject.

-Christopher J. Rock

About the author:
Christopher J. Rock (http://)
Film student at California State, Long Beach. I want to make the gaming world a better place.