Skip to main content

Article

Generating music with F#

Simo Raman

March 22, 2015


For a long time I have been fascinated with the idea of generating music programmatically. I decided to give it a go using F# and this is what I came up with. I wrote a program that generates a short melody in the key of C major. Not exactly Mozart, but it’s a start.

I created a type for notes and a Map of notes to their frequencies:

A scale of C major can be modelled as a list consisting of the notes of the scale:

Creation of a melody is only picking wanted amount of notes from the scale randomly

Frequencies for the notes can be extracted and then written to wave file

I took code to create wav-files from Liam McClennan’s blog. All I did was wrap it in a module called Synth and created an public function that takes a list of frequencies. Here is an example output:

That’s it so far. Nothing fancy but hopefully something to build on in the future. The full source can be found on Github

Stay updated

Our latest takes on tech, business, design and life.

Signup to our newsletter