Wolfram Research, Inc.

1.9.13 Sound

On most computer systems, Mathematica can produce not only graphics but also sound. Mathematica treats graphics and sound in a closely analogous way.

For example, just as you can use Plot[f, x, xmin, xmax] to plot a function, so also you can use Play[f, t, 0, tmax] to "play" a function. Play takes the function to define the waveform for a sound: the values of the function give the amplitude of the sound as a function of time.

Playing a function.

On a suitable computer system, this plays a pure tone with a frequency of 440 hertz for one second.

In[1]:= Play[Sin[2Pi 440 t], {t, 0, 1}]

Out[1]= -Sound-

Sounds produced by Play can have any waveform. They do not, for example, have to consist of a collection of harmonic pieces. In general, the amplitude function you give to Play specifies the instantaneous signal associated with the sound. This signal is typically converted to a voltage, and ultimately to a displacement. Note that amplitude is sometimes defined to be the peak signal associated with a sound; in Mathematica, it is always the instantaneous signal as a function of time.

This plays a more complex sound.

In[2]:= Play[ Sin[700 t + 25 t Sin[350 t]], {t, 0, 4} ]

Out[2]= -Sound-

Play is set up so that the time variable that appears in it is always measured in absolute seconds. When a sound is actually played, its amplitude is sampled a certain number of times every second. You can specify the sample rate by setting the option SampleRate.

Specifying the sample rate for a sound.

In general, the higher the sample rate, the better high-frequency components in the sound will be rendered. A sample rate of typically allows frequencies up to hertz. The human auditory system can typically perceive sounds in the frequency range 20 to 22000 hertz (depending somewhat on age and sex). The fundamental frequencies for the 88 notes on a piano range from 27.5 to 4096 hertz.

The standard sample rate used for compact disc players is 44100. The effective sample rate in a typical telephone system is around 8000. On most computer systems, the default sample rate used by Mathematica is around 8000.

You can use Play[, , ... ] to produce stereo sound. In general, Mathematica supports any number of sound channels.

Playing sampled sounds.

The function ListPlay allows you simply to give a list of values which are taken to be sound amplitudes sampled at a certain rate.

When sounds are actually rendered by Mathematica, only a certain range of amplitudes is allowed. The option PlayRange in Play and ListPlay specifies how the amplitudes you give should be scaled to fit in the allowed range. The settings for this option are analogous to those for the PlotRange graphics option discussed in Section 1.9.3.

Specifying the scaling of sound amplitudes.

While it is often convenient to use the default setting PlayRange -> Automatic, you should realize that Play may run significantly faster if you give an explicit PlayRange specification, so it does not have to derive one.

Replaying a sound object.

Both Play and ListPlay return Sound objects which contain procedures for synthesizing sounds. You can replay a particular Sound object using the function Show that is also used for redisplaying graphics.

The internal structure of Sound objects is discussed in Section 2.9.18.