Try now in the
Open Cloud »
(no sign-in required)

# The Sound of Pi

Play melodies based on the digits of Pi.

Run the code to play a musical note. Try other note numbers:

SoundNote denotes a musical note. Pitch 0 is middle C:

 In[1]:= XSoundNote[0]

SoundNote by itself doesnt make a sound. Put it inside Sound to make a playable note (click the Play button to play the note):

 In[2]:= XSound[SoundNote[0]]
 Out[2]=

Play the note immediately with EmitSound:

 In[3]:= XEmitSound[Sound[SoundNote[0]]]

 In[1]:= XEmitSound[Sound[SoundNote[0]]]

Play a melody. Try other sequences of notes:

Play a sequence of notes by putting them in a list (indicated by the curly braces):

 In[1]:= XEmitSound[Sound[{SoundNote[0], SoundNote[0], SoundNote[2]}]]

If you dont specify otherwise, each note is played for 1 second. To make the melody play faster, specify the duration of the entire sequence in seconds:

 In[2]:= XEmitSound[Sound[{SoundNote[0], SoundNote[0], SoundNote[2]}, 1]]

A more compact way of writing a sequence of notes uses /@ (Map):

 In[3]:= XSoundNote /@ {0, 0, 2}
 Out[3]=

Play a sequence of notes using Map:

 In[4]:= XEmitSound[Sound[SoundNote /@ {0, 0, 2}, 1]]

This is how the note numbers correspond to piano keys:

 Out[4]=

 In[1]:= XEmitSound[Sound[SoundNote /@ {0, 0, 2, 0, 5, 4}, 2]]

Get the first 12 digits of Pi. Try longer or shorter sequences:

This gives a list of the first 12 digits of π (in base 10), plus the number of digits to the left of the decimal point:

 In[1]:= XRealDigits[Pi, 10, 12]
 Out[1]=

Use First to get just the digits:

 In[2]:= XFirst[RealDigits[Pi, 10, 12]]
 Out[2]=

 In[1]:= XFirst[RealDigits[Pi, 10, 12]]
 Out[1]=

Play the first 12 digits of Pi. Try longer or shorter sequences:

This plays a sequence of 3 notes:

 In[1]:= XEmitSound[Sound[SoundNote /@ {0, 1, 2}, 2]]

Replace the note sequence with the digits of Pi to play them as notes:

 In[2]:= XEmitSound[Sound[SoundNote /@ First[RealDigits[Pi, 10, 12]], 2]]

 In[1]:= XEmitSound[Sound[SoundNote /@ First[RealDigits[Pi, 10, 12]], 2]]

Play the digits of Pi on a marimba. Try other instruments, such as Clarinet, Trumpet, or ElectricPiano:

Use Style to specify what instrument to play the notes on:

 In[1]:= XEmitSound[ Sound[Style[SoundNote /@ First[RealDigits[Pi, 10, 12]], "Marimba"], 2]]

Heres a list of the available instruments:

 Out[1]=

 In[1]:= XEmitSound[ Sound[Style[SoundNote /@ First[RealDigits[Pi, 10, 12]], "Marimba"], 2]]