Procedural Music Generation

Music Generation Introductory

by Christer Kaitila

“TL;DR: Think of music as a dungeon.”

Music has a profound effect on your players. It’s that extra bit of polish that sticks in people’s memories, fires up imaginations, and sets the mood. In this article, we are going to explore useful patterns and rules that will help you create your own music generator.

Even if you know nothing about music theory and can’t tell a major scale from a minor scale, the good news is that many of the same rules you would use for dungeon generation can also apply to music. Once we apply a few patterns and impose some limits, your music generation system will produce surprisingly good songs.

Please Break these Rules

The following “rules” are filled with opinionated, subjective advice, and tend to promote generic, traditional, familiar melodies. If you are a music expert you will see that I am oversimplifying and missing out in tons of juicy advanced stuff. Consider these tips as mere starting points for your inspiration. As with any artistic advice, once you know the rules it is a good idea to break them.

Musical Phrases are like Dungeon Rooms

While coding, think of your music as a game level. Consider what makes a good level generator. The aesthetics of music are much the same. To begin, to move beyond mere random noise, we might decide to “clump” things together, much like terrain biomes or dungeon rooms.

Give a dungeon generator a few simple rules (rooms connected by hallways, a viable path from beginning to end, a few rare special things like treasure chests), and your dungeons, now no longer totally random, take shape and become fun to play. They have pacing. Areas of exitement and areas of rest. The same should happen in your music generator.

Think of each section in your song as a “room” in a dungeon. One room might be an intro, the next a chorus, another, a solo. They are conceptual chunks connected by simple passages.

Just as a dungeon should have an easy entrance, some highs and lows leading up to a bass battle, and then an exit, a song should have a simple intro, build complexity and use dynamics for lows and highs and a climax, and then recapitulate the central theme, turnaround, or fade out.

Playing a Note

It will be left as an exercise for the reader to dig into your game engine of choice and figure out how to play sounds at different pitches. This is often a case of setting the sample pitch or playback frequency (speeding the sound waves up or down, which stretches the sound wave to make low bass notes or speeds it up for high mickey mouse squeaks). To help you on your way, you could start from this example or make your own, in whatever engine you like. In Unity, adjust the pitch of an audiosource. If you must go low level (for example, you are generating tones using sine waves), here is how to determine the frequency of a musical note in Hz.

Rhythm is Everything

More important than the notes you choose, a beat is what makes sound become music. When you begin coding your music generator, first just ensure you can play a sound four times per second forever. A metronome; a repeating heartbeat. This will require you to measure elapsed time and play sounds at specific times, and is left as an exercise to the reader.



The most important note in every group is the very first one. The start. It should be loudest. The next more important one is half way through a phrase a bar. The down beat.


In a 4 beat bar, hit hard on beat 1 and 3. Choose those notes to fill with sound more often, and allow beats 3 and 4 to more often be left empty.


The most important part of a group of notes that makes it seem like a song is repetition. This adds rhythm, and a conversational, poetic, call-and-response feeling. Again, each rule we add reduces pure random noise and chaos and adds order and familiarity.

Almost all popular music repeats some part of the previous 4 or 3 bars, normally 4 to 8 times in a row. Therefore, instead of generating a long string of notes, generate a few chunks of 4 to 16 and then loop that “phrase” four times before moving to another section in your song. “Tile” your sprites, and add a pattern.

Patterns and Repetition

Repetition is a key component to music; no much and it gets boring, too little and the music will sound meaningless and exhausting. New things add tension and interest, but like with detail in visual art, and all architecture both real and virtual, rhythm and repetition relax things and give comfort. Repetition allows the listener to feel familiar with the song, they can expect what will come next, and it becomes catchy. You wouldn’t want your dungeon to only use each wall sprite once - a long wall should use the same sprite perhaps broken up by the occasional more complex “detail”.

Reducing detail is also key to good sounding music: interesting sections that punctuate emotion can have a lot of detail and complexity, but you shouldn’t keep that level going the whole time or it would be like a song that was all guitar solo. You need to build up to it, and let it end before it gets tiresome.

Reduced Palette

Dungeons that use every colour of the rainbow or every shape end up looking garish and tacky; useful for that one disco scene you wanted, but the rest of the time, an artist would recommend sticking to only a few colours that complement each other.


The same holds true in music; unless you are writing dissonant avant-garde jazz, you can select a “palette” of musical notes to use in a song much like a few well-chosen colours would serve a pixel artist well. Get them to coordinate, to blend nicely together, and suddenly your art (pixels or notes) takes on a more mature, more cohesive, less garish tone. Limits are the secret to creativity.


Therefore, instead of randomly choosing any note on the piano keyboard, what if your music only chose notes from a particular scale or chord? Immediately the music will start to make sense.

Note Intervals

The distance between two notes is called the interval. For example, the distance between C and C# on a piano keyboard is one semitone. To play a C major scale, which sounds happy, you wouldn’t play every key, just the white ones starting from C. You don’t want to play every single tone in the musical scale. Instead, only select notes that sound nice together. Try choosing 5 to 8 colors from a rainbow of 12.

Chords and arpeggios are built from scales using the rules of harmony. You can learn a lot about chords here.

Armed with these arrays, basically lookup tables, you can start generating music instead of random notes.

Small Intervals (walk, don’t jump)

One common pattern you see in almost all popular music is that the melody doesn’t jump around randomly - it normally walks up or down from no more than one or two notes away from the previous note. Large intervals add tension and excitement, while small steps (such as simply playing the same note again or moving only one note downward in the array of allowable notes in your chosen scale).



The rooms, like themes or sections in the music, represent important areas that tell a story or have a reason to exist; plot beats or increasing difficulty. Between them, as a way to give your game “pacing” (and better performance) are less detailed hallways, mere transitions between major rooms that provide a breather, some rest, and drive the player forward.

In music, we can do the same thing. Present important themes, like a new room, that allow for the rise and fall of dynamics (tension and release, loudness and softness, fast and slow, complex and simple). Transitions between major events, like connecting hallways, give the composition forward and give the listeners a breather.

A dungeon filled with treasure or monsters on every single tile is bad in the same way as a piece of music where every single moment is filled with sound; too busy. Just as a dungeon could put a maximum number of items or mobs per room, a music generator can impose maximums and minimums for number of notes per section, per bar, or per beat, thus avoiding getting muddy by being too busy.


To begin, a random dungeon is not fun if it is “truly random” - throw tiles everywhere with no connectivity or logic or auto-edging and the map doesn’t feel like a world, if feels like junk, noise. The same holds true for music: choosing random notes will result in 1970’s movie-computer beeps and boops that have no emotion, no tension or release, and no harmony. Pure random notes sound like garbage, just as pure random terrain looks like garbage.

To upgrade our generators for both physical and sonic landscapes, we might consider implementing something with smoother transitions, such as perlin noise. Each value is very close to that of its neighbor; things change from white to black gradually. Every game engine already has example code for it, and it generates non-random “smoothed” values that are not pure white noise. It instead allows “the next note” to be generally “near” the previous, just like a perlin terrain has smooth slopes; neighboring heightmap pixels are always close in value - it isn’t random noise.

Suddenly instead of static, your map looks like terrain, with mountains and lakes. This analogy is perfect for music too; smooth out the transitions and it starts to sound like something real. Don’t jump around randomly, make neighbor notes be close in value.

Empty Space is Important

Add another rule to your generator that doesn’t allow more than roughly have of the song to be filled with a note playing. Leave empty space, especially at the end of a phrase. Much like in human speech, we need room to breath. The more complex one section is, the more empty the next should be. Lower the density of notes in your song to 25% to 50% of the available space.

Randomly select notes from a scale. Create a few groups of eight notes that are nearby to each other. Then repeat each one four times. Poof: you have a song. It will sound shockingly close to real music.


Go one step further and limit the distance you travel from one note to the next. The tones will flow like words from a human speaking - rising and falling in pitch but rarely jumping suddenly a huge distance. The gradual evolution from low to high or loud to soft is preferable to sudden changes.

Harmonies Add Beauty

Playing more than one note at a time can sound lovely, when they are in harmony, and very ugly, when they are dissonant. The secret is choosing intervals that synch up nicely with the root note.


For example, a pleasant harmony is to layer a note that is scale[offset +2] so that you harmonize a C with an F. This is called 3rds. Another one that works in almost every situation is the 5th, so shift the note you want to complement 4 more positions in the scale array to get a new note that sounds nice on top of it.

It's all About the Bass

Alongside melody and harmony, you may want a bass line to glue everything together. Create a rule that moset often chooses the very first note of the bar (and downbeat) to play. Make another that selects the first note (the root) of the scale you are choosing notes from. These two rules are enough to fill the role bass often plays in popular music.


Now have fun and extend these concepts, be sure to break the rules of music theory and pop music conventions,and add all sorts of interesting sounds to your music, like drum beats or ambient textures.

You could even fade in different songs that share the same tempo, simple peaceful ones like mostly-empty major scale harmonies with melodies that use small intervals between notes for exploration sections.

For the boss battle, generate a very busy song with a melody that uses larger intervals and notes that came from one of the minor scales. People won’t believe that the music is procedurally generated. It will sound happy and scary at the appropriate moments.

Occasionally you will find “that perfect random seed” or perlin noise location and size that produces a song that sounds “real”.


That’s it! With this handful of rules, you have the conceptual underpinnings to a proper music generator. Now let’s put these concepts into practice in a way that is directly of use for you today as you pe in to procedural content generation.

Good luck on this year’s PROCJAM. If you do make some awesome procedural music, I’d love to hear it. Get in touch on twitter: @mcfunkypants. Here are a few dozen random songs that you can use for your games if you don't end up making a music generator. I have placed them into the public domain as a little gift to the community. Enjoy!

Christer "McFunkypants" Kaitila