This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
Wolfram Research, Inc.

Advanced Topic: Color OutputExporting Graphics and Sounds

2.9.18 The Representation of Sound

Section 1.9.13 described how you can take functions and lists of data and produce sounds from them. This subsection discusses how sounds are represented in Mathematica.

Mathematica treats sounds much like graphics. In fact, Mathematica allows you to combine graphics with sound to create pictures with "sound tracks".

In analogy with graphics, sounds in Mathematica are represented by symbolic sound objects. The sound objects have head Sound, and contain a list of sound primitives, which represent sounds to be played in sequence.

The structure of a sound object.

The functions Play and ListPlay discussed in Section 1.9.13 return Sound objects.

Play returns a Sound object. On appropriate computer systems, it also produces sound.

In[1]:= Play[Sin[300 t + 2 Sin[400 t]], {t, 0, 2}]

Out[1]= -Sound-

The Sound object contains a SampledSoundFunction primitive which uses a compiled function to generate amplitude samples for the sound.

In[2]:= Short[ InputForm[%] ]


Mathematica sound primitives.

At the lowest level, all sounds in Mathematica are represented as a sequence of amplitude samples. In SampledSoundList, these amplitude samples are given explicitly in a list. In SampledSoundFunction, however, they are generated when the sound is output, by applying the specified function to a sequence of integer arguments. In both cases, all amplitude values obtained must be between and 1.

ListPlay generates SampledSoundList primitives, while Play generates SampledSoundFunction primitives. With the default option setting Compiled -> True, Play will produce a SampledSoundFunction object containing a CompiledFunction.

Once you have generated a Sound object containing various sound primitives, you must then output it as a sound. Much as with graphics, the basic scheme is to take the Mathematica representation of the sound, and convert it to a lower-level form that can be handled by an external program, such as a Mathematica front end.

The low-level representation of sound used by Mathematica consists of a sequence of hexadecimal numbers specifying amplitude levels. Within Mathematica, amplitude levels are given as approximate real numbers between 1 and 1. In producing the low-level form, the amplitude levels are "quantized". You can use the option SampleDepth to specify how many bits should be used for each sample. The default is SampleDepth -> 8, which yields 256 possible amplitude levels, sufficient for most purposes.

You can use the option SampleDepth in any of the functions Play, ListPlay and PlaySound. In sound primitives, you can specify the sample depth by replacing the sample rate argument by the list rate, depth.

Since graphics and sound can be combined in Mathematica, their low-level representations must not conflict. As discussed in Section 2.9.14, all graphics in Mathematica are generated in the PostScript language. Sounds are also generated as a special PostScript function, which can be ignored by PostScript interpreters on devices which do not support sound output.

Sending sound to a stream.

Mathematica uses the same function Display to output sound, graphics, and combinations of the two.

In Play, ListPlay and Sound, the option DisplayFunction specifies how the sound should ultimately be output. The default for this option is the global variable $SoundDisplayFunction. Typically, this is set to an appropriate call to Display.

Advanced Topic: Color OutputExporting Graphics and Sounds