Playing Sine Waves

Another standard application studied in trigonometry is sound waves of the form    f ( t )  =  A sin [ B t  ] ,  where  A  is the amplitude of the sound (how loud or soft) and  B  is the frequency of the sound (the pitch -> how high or low the note is).  The frequency of a musical note is measured in Hertz (or cycles per second).  Mathematica can Play notes of the form  Sin[ B*2π t] .  (I haven't yet figured out how to get Mathematica to deal with loud or soft.)

by David R. Wood
Augustana College
March, 2004

Pure Tones

Mathematica uses its Play command to play sounds.  Here's what the Help Browser says about Play.
◼ Play[f, {t, tmin, tmax}] plays a sound whose amplitude is given by f as a function of time t in seconds between tmin and tmax. 
The following command instructs Mathematica to play a pure tone with a frequency of 263 Hertz for 0.2 seconds. The human ear can hear sounds with frequencies ranging from about: 20 Hertz to about 22,000 Hertz.  Dogs can hear sounds with much higher frequencies (higher pitches). 
There's another new feature in the command below.  The part that looks like this -> (* Middle C *) is called a comment.  Mathematica ignores comments.  We put comments into input cells to provide explanations for those who read our Mathematica documents.  
The comment below informs us that the note we hear is A below middle C.

Play[Sin[220 2  π t], {t, 0,  .2}]

The command Play is analogous to the command Plot.  It takes two arguments.  The first is a function defining the sound to play, and the second is an interval of time telling Mathematica how long to play it.  Notice that Play produces two outputs just like Plot does.  We can suppress the   ⁃Sound⁃   tag by putting a semicolon at the end of the command.  Here's a really weird sound without the   ⁃Sound⁃    tag.

Play[Sin[700 t  +  25 t Sin[350  t]], {t, 0, 4}];

Those who study music tell us that a musical octave spans a factor of 2 in frequency and that there are twelve notes per octave.  The notes are separated by the factors of (2^(1/(12 ))) (or approximately 1.05946 ) .  If the frequency of  A  below middle  C  is 220 Hertz, then the frequency of  A#  (the next note in the octave) is 220 * (2^(1/(12 ))) (or about 233.08 Hertz.  The command below defines a very simple, eight-note (diatonic) C major scale for Mathematica. (The word scale comes from the Italian word scala meaning ladder.)  Notice that we use DisplayFunction -> Identity to suppress the sound and the fancy box.  These notes all have names that are similar.  If we don't turn off the Mathematica feature that checks for misspellings, we would get many, many error messages.  That's what the first command does -> suppresses spelling errors.
Enter this command.

Off[General::spell1];
c4 = 
        Play[Sin[2  Pi 261.64 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
d4 = 
        Play[Sin[2  Pi 293.68  t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
e4 = 
        Play[Sin[2  Pi 329.64  t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
f4 = 
        Play[Sin[2  Pi 349.24 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
g4 = 
        Play[Sin[2  Pi 392.00 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
a4 = 
        Play[Sin[2  Pi 440.00  t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
b4 = 
        Play[Sin[2  Pi 493.92  t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
cAbove4 = 
        Play[Sin[2  Pi 523.28 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];

No sound!  Now let's define a simple tune for Mathematica to play.  Enter this command.

OdeToJoy = {e4, e4, f4, g4, g4, f4, e4, d4, c4, c4, d4, e4, e4, d4, 
          d4, e4, e4, f4, g4, g4, f4, e4, d4, c4, c4, d4, e4, d4, c4, 
          c4};

Again, no sound.  Now enter the command below.  Notice that we execute a series a previously produced but suppressed sounds using the Show command.

Show[OdeToJoy];

I admit that I'm not much of a musician, but I had fun defining all 12 notes in the chromatic scale at each of four note lengths (plus a few extra) and then teaching Mathematica to play a couple of simple tunes.  Enter the following functions.

Off[General::spell1];
bBelow8 = 
        Play[Sin[2  Pi 246.96 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
bBelow4 = 
        Play[Sin[2  Pi 246.96 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
bBelow2 = 
        Play[Sin[2  Pi 246.96 t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
bBelow1 = 
        Play[Sin[2  Pi 246.96 t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
c8 = 
        Play[Sin[2  Pi 261.64 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
c4 = 
        Play[Sin[2  Pi 261.64 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
c2 = 
      Play[Sin[2  Pi 261.64 t], {t, 0, 0.8}, 
        DisplayFunction -> 
          Identity];   
c1 = 
      Play[Sin[2  Pi 261.64 t], {t, 0, 1.6}, 
        DisplayFunction -> Identity];
d8 = 
        Play[Sin[2  Pi 293.68  t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
d4 = 
        Play[Sin[2  Pi 293.68  t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
d2 = 
        Play[Sin[2  Pi 293.68  t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
d1 = 
        Play[Sin[2  Pi 293.68  t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
e8 = 
        Play[Sin[2  Pi 329.64  t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
e4 = 
        Play[Sin[2  Pi 329.64  t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
e2 = 
        Play[Sin[2  Pi 329.64  t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
e2dot = 
        Play[Sin[2  Pi 329.64  t], {t, 0, 1.2}, 
          DisplayFunction -> Identity];
e1 = 
        Play[Sin[2  Pi 329.64  t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
f8 = 
        Play[Sin[2  Pi 349.24 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
f4 = 
        Play[Sin[2  Pi 349.24 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
f2 = 
        Play[Sin[2  Pi 349.24 t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
f1 = 
        Play[Sin[2  Pi 349.24 t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
g8 = 
        Play[Sin[2  Pi 392.00 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
g4 = 
        Play[Sin[2  Pi 392.00 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
g2 = 
        Play[Sin[2  Pi 392.00 t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
g1 = 
        Play[Sin[2  Pi 392.00 t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
a8 = 
        Play[Sin[2  Pi 440.00  t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
a4 = 
        Play[Sin[2  Pi 440.00  t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
a2 = 
        Play[Sin[2  Pi 440.00  t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
a1 = 
        Play[Sin[2  Pi 440.00  t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
b8 = 
        Play[Sin[2  Pi 493.92  t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
b4 = 
        Play[Sin[2  Pi 493.92  t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
b2 = 
        Play[Sin[2  Pi 493.92  t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
b1 = 
        Play[Sin[2  Pi 493.92  t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
cAbove8 = 
        Play[Sin[2  Pi 523.28 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
cAbove4 = 
        Play[Sin[2  Pi 523.28 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
cAbove4dot = 
        Play[Sin[2  Pi 523.28 t], {t, 0, 0.6}, 
          DisplayFunction -> Identity];
cAbove2 = 
        Play[Sin[2  Pi 523.28 t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
cAbove1 = 
        Play[Sin[2  Pi 523.28 t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
dSharpAbove8 = 
        Play[Sin[2  Pi 622.24 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
dAbove8 = 
        Play[Sin[2  Pi 587.36 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
dAbove4 = 
        Play[Sin[2  Pi 587.36 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
dAbove2 = 
        Play[Sin[2  Pi 587.36 t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
dAbove1 = 
        Play[Sin[2  Pi 587.36 t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
eAbove8 = 
        Play[Sin[2  Pi 659.28 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
eFlatAbove8 = 
        Play[Sin[2  Pi 622.24 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
eAbove4 = 
        Play[Sin[2  Pi 659.28 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
eAbove4dot = 
        Play[Sin[2  Pi 659.28 t], {t, 0, 0.6}, 
          DisplayFunction -> Identity];
eAbove2 = 
        Play[Sin[2  Pi 659.28 t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
eAbove1 = 
        Play[Sin[2  Pi 659.28 t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
fAbove8 = 
        Play[Sin[2  Pi 698.48 t], {t, 0, 0.2}, 
          DisplayFunction -> Identity];
fAbove4 = 
        Play[Sin[2  Pi 698.48 t], {t, 0, 0.4}, 
          DisplayFunction -> Identity];
fAbove2 = 
        Play[Sin[2  Pi 698.48 t], {t, 0, 0.8}, 
          DisplayFunction -> Identity];
fAbove1 = 
        Play[Sin[2  Pi 698.48 t], {t, 0, 1.6}, 
          DisplayFunction -> Identity];
rest8 = 
        Play[0, {t, 0, 0.2}, 
          DisplayFunction -> Identity];
rest4 = 
        Play[0, {t, 0, 0.4}, 
          DisplayFunction -> Identity];
rest2 = 
        Play[0, {t, 0, 0.8}, 
          DisplayFunction -> Identity];
rest1 = 
        Play[0, {t, 0, 1.6}, DisplayFunction -> Identity];

PollyWollyDoodle = {c8, d8, 
e4, e4, c4, c8, 
          d8, 
e4, e4, c4, c4, 
e8, e8, 
          e8, e8, f8, f8, e8, e8, 
d2, rest4, bBelow8, 
          c8, 
d4, d4, bBelow4, 
          bBelow4, 
d4, d4, bBelow4, 
          d4, 
g8, g8, g8, g8, f8, f8, d8, 
          d8, 
c2, rest4, c8, d8, 
e2dot, 
          c8, d8, 
e2dot, c8, d8, 
e4, 
          e4, f4, e4, 
d2, rest4, bBelow8, 
          c8, 
d8, d8, d8, d8, bBelow8, bBelow8, bBelow8, 
          c8, 
d8, d8, d8, d8, bBelow8, bBelow8, 
          d4, 
g8, g8, g8, g8, f8, f8, 
          d4, 
c2, rest4};

RockIslandLine = {g8, a8, 
cAbove8, cAbove8, a8, 
          cAbove4, cAbove8, cAbove8, cAbove8, 
eFlatAbove8, 
          eAbove8, cAbove8, eAbove4dot, g8, a8, 
cAbove8, 
          cAbove8, a8, cAbove4,  cAbove8, cAbove8, 
          cAbove8, 
dAbove4, b4, g4, g8, 
          a8, 
cAbove8, cAbove8, a8, cAbove4, cAbove8, 
          cAbove8, cAbove8, 
eFlatAbove8, eAbove8, cAbove8, 
          eAbove8, eAbove4dot, cAbove8, a8, 
cAbove4dot, 
          a8, cAbove8, dAbove8, cAbove8, a8, 
cAbove8, 
          dAbove8, cAbove8, a8, cAbove8, dAbove8, cAbove8, 
          a8, 
cAbove8, dAbove8, cAbove8, a8, cAbove8, 
          dAbove8, cAbove8, a8, 
g8, a8, b8, cAbove4dot, 
          rest4
};

Show[PollyWollyDoodle];

Show[RockIslandLine];

Chords

We can also get Mathematica to play more than one note at a time.

cChord  = 
        Play[{Sin[2  Pi 261.64 t]   ,   Sin[
              2  Pi 329.64  t]   , 
            Sin[2  Pi 392.00 t]   }, {t, 0, 2}];

gChord  = 
        Play[{Sin[2  Pi 392.00 t]  ,   Sin[
              2  Pi 493.92  t]   , 
            Sin[2  Pi 587.36 t]   }, {t, 0, 2}];

The musicians among us are invited to make some music.  The rest of you can make up your own sounds.