12 | Sound |
In the Wolfram Language, sound works a lot like graphics, except that instead of having things like circles, one has sound notes. Press the play button to actually play sounds. If you don’t say otherwise, the Wolfram Language will make the notes sound as if they were played on a piano.
In[1]:= |
Out[1]= |
You can specify a sequence of notes by giving them in a list.
Play three notes in sequence:
In[2]:= |
Out[2]= |
Instead of giving names of notes, you can give a number to specify their pitch. Middle C is 0. Each semitone above middle C goes up by 1. Middle G is 7 semitones above middle C, so it’s specified by the number 7. (An octave is 12 semitones.)
Specify the notes by numbers:
In[3]:= |
Out[3]= |
Use Table to generate a sequence of 5 notes:
In[4]:= |
Out[4]= |
If you don’t say otherwise, each note lasts 1 second. Use SoundNote[pitch,length] to get a different length.
Play each note for 0.1 seconds:
In[5]:= |
Out[5]= |
In addition to piano, SoundNote can handle a long list of possible instruments. The name of each instrument is a string.
In[6]:= |
Out[6]= |
Play a sequence of 20 notes with random pitches:
In[7]:= |
Out[7]= |
Sound[{...}] | create a sound from notes | |
SoundNote["C"] | a named note | |
SoundNote[5] | a note with a numbered pitch | |
SoundNote[5,0.1] | a note played for a specified time | |
SoundNote[5,0.1,"Guitar"] | a note played on a certain instrument |
12.1Generate the sequence of notes with pitches 0, 4 and 7. »
12.2Generate 2 seconds of playing middle A on a cello. »
12.3Generate a “riff” of notes from pitch 0 to pitch 48 in steps of 1, with each note lasting 0.05 seconds. »
12.4Generate a sequence of notes going from pitch 12 down to 0 in steps of 1. »
12.5Generate a sequence of 5 notes starting with middle C, then successively going up by an octave at a time. »
12.6Generate a sequence of 10 notes on a trumpet with random pitches from 0 to 12 and duration 0.2 seconds. »
12.7Generate a sequence of 10 notes with random pitches up to 12 and random durations up to 10 tenths of a second. »
12.8Generate 0.1-second notes with pitches given by the digits of 2^31. »
12.9Create a sound from the letters in CABBAGE, each playing for 0.3 seconds sounding like a guitar. »
12.10Generate 0.1-second notes with pitches given by the letter numbers of the characters in “wolfram”. »
+12.1Generate a sequence of three notes of 1 second of playing middle D on the cello, piano and guitar. »
+12.2Generate a sequence of notes from pitch 0 to pitch 12, going up in steps of 3. »
+12.3Generate a sequence of 5 notes starting with middle C, then successively going up by a perfect fifth (7 semitones) at a time. »
+12.4Generate 0.02-second notes with pitches given by the lengths of the names of integers from 1 to 200. »
How do I know which instruments are available?
Look at the list under “Details and Options” in the SoundNote reference page, or just start typing and see the completions you’re offered. You can also use instrument numbers, from 1 to 128. All the standard MIDI instruments are there, including percussion.
How do I play notes below middle C?
Just use negative numbers, like SoundNote[-10].
What are sharp and flat notes called?
E# (E sharp), Ab (A flat), etc. They also have numbers (e.g. E# is 5). The # and b can be typed as ordinary keyboard characters (though special ♯ and ♭ characters are available too).
Put note names in a list, as in SoundNote[{"C", "G"}].
For a 0.2-second rest, use SoundNote[None, 0.2].
Why do I need quotes in the name of a note like “C”?
Because the name is a Wolfram Language string. If you typed just C, it would be interpreted as a function named C, which isn’t what you want.