Well the world got a little weirder these past few weeks. It’s a good time to play video games, at least.
As promised, this release of Sverdheim has the music in it. Rather than write the music myself, and reveal just how not musically inclined I am, I programmed the game to generate the music for me. If you know anything about modular synthesis and generative music, then you already know the techniques I’ve used.
Each class in the music chain is like a module in a modular synthesizer.
I started with a simple “Clock.” It has a parameter where you can set and change the BPM. It emits a signal each tick, the frequency defined by that BPM value. Any other component in the music chain can link to that signal and do *something* and it will always be on beat.
From there, we have a “Clock Calculator”. This object takes a signal from a master clock, and in turn triggers several output clocks based on its emitted beat signal. This way, we have many clocks, all with beats based on the master BPM. One output clock would be at double speed, one quadruple. One will be at half speed, another will be one quarter speed. This way, we can control other components at various speeds, and they all will still be on beat.
Next, I wrote a Variable Frequency Oscillator. It’s basically a sine wave generator. I tried to create other wave forms, but they sound terrible. I’ll include the others later, but to do so I will have to figure out the proper Furrier Transforms, or more likely grab an asset from Unity’s asset store that already generates them. For now though, the sine wave will be enough.
The oscillator has many settings for customizing the sound. You can set the frequency, and the length of the signal in fractions of a second. I created a standardized set of frequency presets that each match the natural musical notes from C to B at octave zero. The natural notes I know I can pretty much combine in any order to make a chord that sounds good, or a scale of notes that sound good. Therefore, I avoided any flat or sharp notes or any of the other weird stuff that people with more musical knowledge than I have tend to use. There’s an octave setting – each octave just doubles the frequency of the same note from the previous octave. This gives me a wide range of notes I can play.
The sine wave by itself sounds rather digital, but the magic of making any garbage sound good is reverb. Reverb is why you sound good singing in the shower. Luckily, Unity has a reverb filter built in that I could use, and it makes my sine notes sound like bells.
Next I created a sampler. This one’s pretty simple: I feed it wav files and a clock and it will play the sound when the clock triggers it. I created a couple of heavy, reverberated bass drums and a high clap in LMMS, and using them in a handful of samplers gives me a passable drum machine.
To run the drum machine, I implemented a software version of Music Thing Modular’s Turing Machine, another module for Eurorack synthesizers. I call my class a Randomizer. This object takes an input from the master clock, then outputs various random beats, and also a chain of random frequencies. It will then change the beats and frequencies over time at a customizable rate that I can set via an attribute.
Using the Randomizer, I create a random percussion sequence that changes over time. I also connect it to the Oscillator with a low octave for a slow, low, droning bass line. I have a faster clock controlling another Randomizer that feeds into another Oscillator, and provides a single note, continuous random melody.
Next, I created a Matrix Synthesizer. It’s like a 16×16 grid, where the y-axis is the frequency of the notes (each frequency a natural note, spanning a little over two octaves), and the x-axis is time. Each beat from the input clock advances the time one place, and we play every note on that Y-axis at once, making a progression of chords.
I fill in the matrix using Bresenham’s Line Algorithm. Usually, this algorithm is for drawing lines in graphics programs, but here we use it to draw notes on our matrix. This arranges the notes in a scale every time. A new random scale is added to the matrix each time a public method is called, overlapping them and creating random chords. We call the method after each time the 16th chord is played, causing the melody to gradually change over time. There’s a minimum and maximum density setting, which allows me to set how many notes are allowed on the matrix. Once the maximum is reached, the lines start deleting notes back to the minimum, where notes are added again.
An interesting thing about the Matrix Synthesizer was when I first wrote it, like many things we program, it didn’t work as expected. I needed a way to quickly visualize the matrix itself. I didn’t want to spend a bunch of time programming some visualizer though. That night, while asleep, I designed a simple matrix visualizer in my dreams. It’s just a unicode text box in the Unity Inspector. I use white and black boxes to represent the matrix – black boxes showing notes turned on, and white ones showing notes turned off. White and black circles replace the boxes in the currently played column. I wrote it when I woke up and it gave me exactly what I needed to debug the class. I feature this visualizer in the video above.
Finally, I added a higher level of music generation than you can get from modular synthesis: Procedural patching. All of these elements are randomly combined in different ways, according to a set of rules. I’m calling each set of rules a “song” for better want of a term. There’s a song for the main menu that’s a slow, random percussion that introduces you to the game like the beginning of The Anvil of Crom. Once you start the game, that song evolves with bass droning and matrix melody. In combat, there’s a fast percussion song that’s meant to get your heart going. When you rest, there’s a song that’s a slow, light melody that’s supposed to be more relaxing. And while just out in the world exploring, random modules are combined into various songs.
The weird thing about all this is that the music still has a cohesive sameness to it. Like, the “songs” each tend to sound the same just because they follow the same rules despite the actual progression of notes being completely random. If I’m not mistaken, we copyright music based on those concrete combinations of notes, but I’m no longer convinced that the actual notes of a song are what make it a song. I can’t put my finger on what does, though. It’s not just the sameness of the instruments, as your typical rock band will play a wide variety of songs with the same instruments. I don’t know. Music is beyond me. That’s a puzzle for someone else who’s been a bit more saturated in these disciplines.
So I’m almost at my goal! With the completion of the music, I now just have a list of bugs to work out, and my Minimum Viable Product will be complete. Just like the other aspects of the game, though the music is finished, it will likely get added to and altered and tweaked as time goes on and I get new ideas.
Nothing is ever really finished, despite the goal markers being reached.