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

# Random Melodies

Play random melodies with various scales and instruments.

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

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 sequence of notes. Try other sequences:

To play a sequence of notes, put the notes in a list (indicated by the curly braces):

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

This is how the note numbers correspond to piano keys:

 Out[1]=

Lower notes have negative numbers. Note numbers can range from -60 to 67:

 In[2]:= XEmitSound[Sound[{SoundNote[-60], SoundNote[67]}]]

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

Play the notes faster. Try other durations:

Unless you indicate otherwise, each note is played for 1 second:

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

To play notes faster or slower, specify the length of the entire sequence in seconds:

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

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

Play a random 8-note melody. Try other lengths of melodies:

Hint: run the code again to get a different melody.

Get a random number between 1 and 12. Each time you run the code, a different number is chosen:

 In[1]:= XRandomInteger[12]
 Out[1]=

Play a random note. Each time you run the code, a different note is played:

 In[2]:= XEmitSound[Sound[SoundNote[RandomInteger[12]]]]

Make a list of 8 random notes:

 In[3]:= XTable[SoundNote[RandomInteger[12]], {8}]
 Out[3]=

Play 8 random notes for a duration of 2 seconds:

 In[4]:= XEmitSound[Sound[Table[SoundNote[RandomInteger[12]], {8}], 2]]

 In[1]:= XEmitSound[Sound[Table[SoundNote[RandomInteger[12]], {8}], 2]]

Play a random melody from a scale. Try different scales:

The black notes on the piano form a pentatonic scale:

 Out[1]=

This chooses a random note from a pentatonic scale:

 In[1]:= XRandomChoice[{1, 3, 6, 8, 10}]
 Out[1]=

Play 8 random pentatonic notes:

 In[2]:= XEmitSound[ Sound[Table[SoundNote[RandomChoice[{1, 3, 6, 8, 10}]], {8}], 2]]

You can also use note names to specify the notes:

 In[3]:= XEmitSound[ Sound[Table[ SoundNote[RandomChoice[{"c#", "d#", "f#", "g#", "a#"}]], {8}], 2]]

Play 8 random notes from a C-major chord. The C above middle C is called c5. Another name for c is c4:

 In[4]:= XEmitSound[ Sound[Table[SoundNote[RandomChoice[{"c", "e", "g", "c5"}]], {8}], 2]]

 In[1]:= XEmitSound[ Sound[Table[SoundNote[RandomChoice[{1, 3, 6, 8, 10}]], {8}], 2]]

Play a random melody on random instruments. Try other instruments, like Oboe, Glockenspiel, or HonkyTonkPiano:

Play a note with a xylophone sound (the second value, 1, is the notes duration):

 In[1]:= XEmitSound[Sound[SoundNote[0, 1, "Xylophone"]]]

Play a note with a clarinet sound:

 In[2]:= XEmitSound[Sound[SoundNote[0, 1, "Clarinet"]]]

Play a note with a xylophone, clarinet, or violin sound chosen at random:

 In[3]:= XEmitSound[ Sound[SoundNote[0, 1, RandomChoice[{"Xylophone", "Clarinet", "Violin"}]]]]

Play 8 random notes with random instruments:

 In[4]:= XEmitSound[Sound[Table[ SoundNote[RandomChoice[{1, 3, 6, 8, 10}], 1, RandomChoice[{"Xylophone", "Violin", "Clarinet"}]], {8}], 2]]

Heres a list of the available instruments:

 Out[4]=

 In[1]:= XEmitSound[Sound[Table[ SoundNote[RandomChoice[{1, 3, 6, 8, 10}], 1, RandomChoice[{"Xylophone", "Violin", "Clarinet"}]], {8}], 2]]