Music From Mathematical Constants

February 1st, 2008 | Categories: general math, math software, mathematica, programming | Tags:

In a recent post over at 360 the author was discussing how Joe Turner (a prof of musical composition) has formed a musical composition based on the expansion of Pi in base 12 and it sounds pretty good considering the fact that it is based on a random sequence. This was picked up by another blog I discovered recently (thanks to the carnival of mathematics) where the author asked if anyone else would mind producing similar compositions based on other mathematical constants.

On the train journey home from work I thought that I might be able to get Mathematica to produce simplified versions of these sort of compositions without too much effort. So I fired up my copy of Mathematica, plugged in some headphones (I was fairly sure that my fellow commuters would not appreciate the music of Pi as much as I) and set to work. Although I knew that Mathematica had sound capabilities I had never used them before so the help browser (as usual) was indispensable.

I am going to present the rest of this post as a tutorial, with Mathematica commands in bold followed by the output in normal tyle. This is my first attempt at a tutorial in a blog post so feedback would be welcomed.

From the help browser I discovered that you can represent a note in Mathematica using SoundNote. Middle C is represented by SoundNote[0], one semi-tone above middle C is SoundNote[1] , a whole tone above Middle C is SoundNote[2] and so on. You can also use negative numbers so that SoundNote[-1] is one semi-tone below middle C for example.

So far so good but I don’t want to just represent the notes symbolically – I want to actually play them. The way to do this is by wrapping a SoundNote expression with Sound[]. So to play a middle C just evaluate


A little interface appears that looks like the graphic above and when you click the play button (represented by a triangle) you will hear a piano sample at middle C that lasts for one second.

So far so good – I have a way of playing any integer I choose but I need to be able to play a list of notes if I am going to convert Pi to music. For those of you who don’t know much about Mathematica all you need to do to create a list of ANYTHING is separate them by commas and wrap the whole thing in curly braces {} so a list the integers 1 to 5 looks like this


A list of strings looks like this


Finally, a list of three SoundNotes looks like this:

{SoundNote[3], SoundNote[1], SoundNote[4]}

Wrap this list with Sound[] to play the three notes in sequence:

Sound[{SoundNote[3], SoundNote[1], SoundNote[4]}]

So all I need now is to get the digits of Pi (or any other number I might choose) into a list of SoundNotes. If all I wanted was the decimal expansion of Pi to any number of digits I choose then I could use the N command:



Since the original article asks for Pi in base twelve I could use BaseForm[number,base] to display Pi in base 12 as follows:



This is all well and good but what I actually want is a list of the digits of Pi in base 12 and so I am going to use the RealDigits command which has the syntax RealDigits[number,base,number_of_digits]. This returns a list of the digits of your number according to your specified base and number of digits. So to get a list of the first 20 digits of Pi in base 12 I do

RealDigits[Pi, 12, 20]

{{3, 1, 8, 4, 8, 0, 9, 4, 9, 3, 11, 9, 1, 8, 6, 6, 4, 5, 7, 3}, 1}

Now this result is not just a simple list but is list of lists instead. The first element is a list of the digits of Pi in base 12 and the second element is the number of digits to the left of the decimal point. I only want the first element of this list of lists – namely the list of digits of Pi. To extract elements from lists in Mathematica you use double square brackets [[ ]] so to get the 3rd element of the list {1,4,9,16,25} you would type



So to get that first list from the result of RealDigits[Pi, 12, 20] we do

RealDigits[Pi, 12, 20][[1]]

{3, 1, 8, 4, 8, 0, 9, 4, 9, 3, 11, 9, 1, 8, 6, 6, 4, 5, 7, 3}

So far so good but we are not done yet – somehow I need to wrap each integer in this list with SoundNote[]. One way to do this is by using Map[]. For example I can wrap each element of the list {3,1,4} with SoundNote by doing


{SoundNote[3], SoundNote[1], SoundNote[4]}

Applying this knowledge to the code we have built up so far allows us to write the following

RealDigits[Pi, 12, 20][[1]]

{SoundNote[3], SoundNote[1], SoundNote[8], SoundNote[4], SoundNote[8],SoundNote[0], SoundNote[9], SoundNote[4], SoundNote[9],SoundNote[3], SoundNote[11], SoundNote[9], SoundNote[1],SoundNote[8], SoundNote[6], SoundNote[6], SoundNote[4], SoundNote[5],SoundNote[7], SoundNote[3]}

I have started to format my Mathematica commands a bit to make them more readable by using multiple lines. This is not necessary but I find that if I don’t do this then I soon start to loose track of all those square brackets and get into a horrible mess. All that I have to do now is wrap this result with Sound[] and my job is done.

RealDigits[Pi, 12, 20][[1]]

By modifying this group of commands you can ‘play’ any expression you like to as many decimal places as you like but there is still work to be done. For example we are stuck with only using a piano and every note has a one second duration which is far from ideal but this post is getting a bit long so I will explain how to change these in a future post if anyone is interested.

By the time my train journey was over I had produced a little ‘applet’ in Mathematica using the principles described here along with the Manipulate function. It could play Pi in any number base using any one of a range of instruments with variable note speed. I fully intended on refining it a bit and submitting it to the Wolfram Demonstrations project but when I got home and regained internet access I discovered that I had been beaten to it by someone called Hector Zenil. Hector has written a very nice demonstration called Math Songs which does exactly what I had been trying to achieve and a whole lot more. Not only can you listen to Pi but you can also listen to e, Euler’s Gamma constant, the golden ratio, the Catalan constant and a few more I haven’t even heard of.

Oh well…you can’t win them all and Hector has done a fantastic job (better than I would have done to be honest). The best thing is that you don’t even need Mathematica to play with his creation. The free (but rather large) MathPlayer will do the job nicely for you.

So, have fun with Hector’s little demonstration and I hope the mini-tutorial here was useful to someone. As always feel free to leave comments.

Comments are closed.