## 2.10.18 The Representation of Sound

Section 1.9.12 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.

 Sound[{, , ... }] a sound object containing a list of sound primitives

The structure of a sound object.

The functions Play and ListPlay discussed in Section 1.9.12 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]=
The Sound object contains a SampledSoundFunction primitive which uses a compiled function to generate amplitude samples for the sound.
 In[2]:=  Short[ InputForm[%] ]
 Out[2]//Short=

 SampledSoundList[{, , ... }, r] a sound with a sequence of amplitude levels, sampled at rate r SampledSoundFunction[f, n, r] a sound whose amplitude levels sampled at rate r are found by applying the function f to n successive integers

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.10.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.

 Display[stream, sound] output sound to a stream Display[stream, {graphics, sound}] output graphics and sound to a stream

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.

THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.