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.
Generate a middle C note:
Sound[SoundNote["C"]]
You can specify a sequence of notes by giving them in a list.
Play three notes in sequence:
Sound[{SoundNote["C"], SoundNote["C"], SoundNote["G"]}]
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:
Sound[{SoundNote[0], SoundNote[0], SoundNote[7]}]
Use Table to generate a sequence of 5 notes:
Sound[Table[SoundNote[n], {n, 5}]]
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:
Sound[Table[SoundNote[n, 0.1], {n, 5}]]
In addition to piano, SoundNote can handle a long list of possible instruments. The name of each instrument is a string.
Play notes on a simulated violin:
Sound[Table[SoundNote[n, 0.1, "Violin"], {n, 5}]]
It’s easy to make “random music”—different every time you generate it.
Play a sequence of 20 notes with random pitches:
Sound[Table[SoundNote[RandomInteger[12], 0.1, "Violin"], 20]]
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. »
What are the pictures that the Wolfram Language displays for Sound?
They’re like musical scores, where the pitch of each note is represented by its height. The duration of the note is represented by its horizontal length in piano roll style, rather than with ♩, ♪, etc.
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).
How do I make a chord?
Put note names in a list, as in SoundNote[{"C", "G"}].
How do I make a rest?
For a 0.2-second rest, use SoundNote[None, 0.2].
How do I get a sound to play immediately, without having to press the play button?
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.
Can I record audio and manipulate it?
Yes. Use AudioCapture, then use functions like AudioPlot, Spectrogram, AudioPitchShift, etc., as in Section 46.