The Representation of Sound

"Sound" describes how you can take functions and lists of data and produce sounds from them. This tutorial 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[{s1,s2,...}]a sound object containing a list of sound primitives

The structure of a sound object.

The functions Play and ListPlay discussed in "Sound" return Sound objects.

Play returns a Sound object. On appropriate computer systems, it also produces sound.
In[1]:=
Click for copyable input
Out[1]=
The Sound object contains a SampledSoundFunction primitive which uses a compiled function to generate amplitude samples for the sound.
In[2]:=
Click for copyable input
Out[2]//Short=
SampledSoundList[{a1,a2,...},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
SoundNote[n,t,"style"]a note-like sound with note n, time specification t, with the specified style

Mathematica sound primitives.

At the lowest level, all sounds in Mathematica are represented as a sequence of amplitude samples, or as a sequence of MIDI events. 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 . In SoundNote, a note-like sound is represented as a sequence of MIDI events that represent the frequency, duration, amplitude, and styling of the note.

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 sampled 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 and . 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. The low-level representation of note-based sound is as a time-quantized byte stream of MIDI events that specify various parameters about the note objects. The quantization of time is determined automatically at playback.

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

New to Mathematica? Find your learning path »
Have a question? Ask support »