Algorithmic Musician

Senior project for the procedural generation of music

View project on GitHub

Initial Song Generation

24 Mar 2016

This marking period, I focused on reaching my the basic objective of my senior project: to generate a song. I wrote a new script file, main.py, which takes the information generated by the other scripts and uses it to generate music.

Let’s examine this new file, main.py:

When the program is run, a new MusicGen() object is created. Then, for each song the user wants to be analized (a list of filenames passed in using sys.argv), the program calls MusicGen.add_template(). This method parses the input songs into an array of the following dictionary:

{
    "name":string, #This is the note's name, ex: C4 or E#2
    "frequency":float,
    "durations":[float],
    "nexts":[int], # Array containing indices of possible next notes
    "next2":[int], # Array containing indices of possible 2nd next notes
    "chords":[int] # Array containing indices of notes that can be played with this one
}

Next, the method MusicGen.generate() is called. This method starts with a note representing a rest (aka: no audio). Then, the method picks a random note out of the “nexts” array and adds it to the song. The method then tries to add more notes played at the same time from the “chords” array. Once the first chord of the song is chosen, the method moves on to chose the next chord. It once again chooses from “nexts,” as well as “next2.” This process repeats until an entire song has been created.

I still have to improve and refine the algorithm, but it is working. To demonstrate this, I tested the program on a wav file I have stored on my computer called, Epilogue.wav. This file is a complex piano piece composed by Heller and played by myself. To test my program, I ran the following command in the root directory of this repository:

python main.py epilogue.wav

The output looked like this:

Opening wavefile: epilogue.wav
Done
Setting channel count
Making samples
Done

========= PERFORMING STFT ========================
..................................................
========= DONE ===================================

The program finished running, and created this lovely piece of music:

As you can hear, the program is still rough around the edges, but it does work. It is not Beethoven, but it is music!