Sound Playback

On most computer systems, the Wolfram System can produce not only graphics but also sound. The Wolfram System 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.

Play[f,{t,0,tmax}]play a sound with amplitude f as a function of time t in seconds

Playing a function.

On a suitable computer system, this plays a pure tone with a frequency of 440 hertz for one second:
Click for copyable input

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 the Wolfram System, it is always the instantaneous signal as a function of time.

This plays a more complex sound:
Click for copyable input

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.

Play[f,{t,0,tmax},SampleRate->r]play a sound, sampling it r times a second

Specifying the sample rate for a sound.

In general, the higher the sample rate, the better highfrequency components in the sound will be rendered. A sample rate of r 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 4186 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 the Wolfram System is around 8000.

You can use Play[{f1,f2,] to produce stereo sound. In general, the Wolfram System supports any number of sound channels.

ListPlay[{a1,a2,},SampleRate->r]play a sound with a sequence of amplitude levels

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.

Play noise generated from random values:
Click for copyable input

When sounds are actually rendered by the Wolfram System, 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 "Options for Graphics".

PlayRange->Automaticuse an internal procedure to scale amplitudes
PlayRange->Allscale so that all amplitudes fit in the allowed range
PlayRange->{amin,amax}make amplitudes between amin and amax fit in the allowed range, and clip others

Specifying the scaling of sound amplitudes.

While it is often convenient to use the 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.

EmitSound[snd]emit a sound when evaluated

Playing sounds programmatically.

A Sound object in output is typically formatted as a button which contains a visualization of the sound and which plays the sound when pressed. Sounds can be played without the need for user intervention or producing output by using EmitSound. In fact, the internal implementation of Sound buttons uses EmitSound when the button is pressed.

The internal structure of Sound objects is discussed in "The Representation of Sound".