Wolfram Archive
Wolfram Programming Lab is a legacy product.
All the same functionality and features, including access to Programming Lab Explorations, are available with Wolfram|One.
Start programming now. »
Try it now »
(no sign-in required)

# Random Melodies

Play random melodies with various scales and instruments.

Run the code to create a musical note, then click the Play button to hear it. Try note numbers other than 0:

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

SoundNote[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):

Sound[SoundNote[0]]

Play the note immediately with EmitSound:

EmitSound[Sound[SoundNote[0]]]

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):

Sound[{SoundNote[0], SoundNote[2], SoundNote[4]}]

This is how the note numbers correspond to piano keys:

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

Sound[{SoundNote[-60], SoundNote[67]}]

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:

Sound[{SoundNote[0], SoundNote[2], SoundNote[4]}]

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

Sound[{SoundNote[0], SoundNote[2], SoundNote[4]}, 1]

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:

RandomInteger[12]

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

Sound[SoundNote[RandomInteger[12]]]

Make a list of 8 random notes:

Table[SoundNote[RandomInteger[12]], {8}]

Play 8 random notes for a duration of 2 seconds:

Sound[Table[SoundNote[RandomInteger[12]], {8}], 2]

Sound[Table[SoundNote[RandomInteger[12]], {8}], 2]

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

The black keys on the piano form a pentatonic scale:

This chooses a random note from a pentatonic scale:

RandomChoice[{1, 3, 6, 8, 10}]

Play 8 random pentatonic notes:

Sound[Table[SoundNote[RandomChoice[{1, 3, 6, 8, 10}]], {8}], 2]

You can also use note names to specify the notes:

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:

Sound[Table[SoundNote[RandomChoice[{"c", "e", "g", "c5"}]], {8}], 2]

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":

Hint: you can also specify instruments with numbers from 1 to 128.

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

Sound[SoundNote[0, 1, "Xylophone"]]

Play a note with a clarinet sound:

Sound[SoundNote[0, 1, "Clarinet"]]

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

Sound[SoundNote[0, 1, RandomChoice[{"Xylophone", "Clarinet", "Violin"}]]]

Play 8 random notes with random instruments:

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

Heres a list of the available instruments:

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