AUDIO PACKAGE TUTORIAL

# Audio Package

This package provides functions for the generation of standard waveforms and waveforms with user-specified spectra, the synthesis of amplitude- and frequency-modulated sinusoids, and a function for reading sound files into Mathematica.

The graphics output of Mathematica's sound functions is hardware dependent, so the graphics included in this documentation may differ from those produced by your machine.

### Generating Standard Waveforms

 Waveform[type,freq,dur] create a Sound object that is a standard waveform of type type, having a fundamental frequency of freq hertz, and a duration of dur seconds

Creating a standard waveform.

Waveform uses computationally efficient algorithms to create a standard waveform with a theoretically infinite number of overtones.

Standard waveform definitions.

Here Waveform returns a Sound object corresponding to a triangle wave having a fundamental of 440 hertz and lasting 0.2 seconds.
 Out[2]=
 Waveform[type,freq,dur,Overtones->n] create a standard waveform with overtones

Creating a standard waveform with a specified number of overtones.

By setting the option Overtones, you can limit the number of overtones that Waveform includes in a sound.

You can generate a square wave that has only eight overtones, then hear the sound with the Show function.
 Out[3]=
Here Table is used to create a sequence of sawtooth waveform sounds running from 10 overtones down to only two.
 Out[4]=

When you use the option Overtones, the Waveform function uses Fourier summation to create the sound. This process is not as computationally efficient as the default algorithm, but generates a cleaner sound with only the specified number of overtones. Mathematica's SetOptions function may be used to make Fourier summation the default.

This ensures that Waveform will use Fourier summation for the creation of sounds.
 Out[5]=
This ensures the use of faster algorithms in creating sounds.
 Out[6]=

### Specifying the Spectrum for a Waveform

 ListWaveform[{{n1,a1},{n2,a2},...},freq,dur] create a Sound object with a fundamental frequency of freq lasting for dur seconds, with a spectrum in which relative frequency has relative amplitude

Creating a waveform with a specified spectrum.

ListWaveform allows you to create a waveform with a specific spectrum. The first argument is a list of pairs, in which the first member of each pair is a frequency relative to the fundamental and the second is the frequency's relative amplitude.

For example, you can define a partial list corresponding to an "instrument" in which the partials' frequencies are nonharmonically related and have complicated amplitude relationships. ListWaveform may then be used to generate these relationships at different fundamental frequencies.

This partial list will define the instrument.
This generates an ascending sequence of six chromatic half-steps, beginning at 440 hertz, all using the same partial list, and each lasting for 0.2 seconds.
Now you can hear one partial list at different fundamental frequencies.
 Out[9]=

You can create chords with ListWaveform by defining partial lists with amplitude relationships of unity, so that all component frequencies share the same amplitude.

This sequence of relative frequencies defines a major chord. Note that the amplitudes are set at unity, so that the component frequencies are of equal amplitude.
This creates a major chord based on a fundamental of 493.88 hertz (i.e., the pitch B4).
 Out[11]=

### Amplitude Modulation

Amplitude modulation is a common analog studio technique in which the created sound contains three frequencies: the carrier frequency, and the sum and difference of the carrier and modulating frequencies.

 AmplitudeModulation[fc,fm,mi,dur] create a Sound object that is an amplitude-modulated sinusoid, having carrier and modulator frequencies and measured in hertz, a modulation index of mi, and a duration of dur seconds

Amplitude modulation.

The expression used by AmplitudeModulation is given by .

When the modulating frequency is subaudio (below 20 hertz), the result of amplitude modulation is what musicians call tremolo.
 Out[12]=

The depth or intensity of the modulation is controlled by the modulation index mi. The carrier is said to be overmodulated when . The best way to understand the effect of mi is to try it out with different values, usually between 0.1 and 2.0.

 AmplitudeModulation[fc,fm,mi,dur,RingModulation->True] create a Sound object that is a ring-modulated sinusoid

Ring modulation.

If you set the option , the Sound object created by AmplitudeModulation will contain only two frequencies: the sum and the difference of the carrier and modulating frequencies. This is called ring modulation, and is given by the expression .

In this example, the output sound contains the frequencies 700 and 300 hertz, the sum and difference of the carrier and modulating frequencies.
 Out[13]=

### Frequency Modulation

Frequency modulation (FM) is a technique used at major universities on workstations and in suburban homes on synthesizers. The ubiquity of this technique stems in part from its mathematical simplicity and its ease of implementation in an electronic circuit. The expression for frequency modulation is given by .

 FrequencyModulation[fc,{fm,pd},dur] create a Sound object that is a frequency-modulated sinusoid, having carrier and modulating frequencies and hertz, peak deviation pd hertz, and a duration of dur seconds

Frequency modulation.

When the modulating frequency is subaudio (below 20 hertz), the result is what musicians call vibrato.
 Out[14]=

The modulation index is the ratio of the peak deviation to the modulating frequency. Higher values of the modulation index result in more partials and a brighter sound. Very high values will result in aliasing, and will add low-frequency components to the sound.

This is an example of frequency modulation with a modulation index of 5500/550.
 Out[15]=

The modulation ratio is the ratio of the modulating frequency to the carrier frequency. If this ratio is an integer greater than zero, the resulting overtones and sidebands will have harmonic relationships to each other, analogous to those of standard orchestral instruments. If the modulation ratio is not integer valued, the resulting overtones and sidebands will have an inharmonic relationship to each other, analogous to percussion instruments and bells. A harmonic relationship is when the overtones and sidebands have frequencies that coincide with the harmonics of some fundamental, not necessarily the carrier.

 FrequencyModulation[fc,{{f1,pd1},{f2,pd2},...},dur,ModulationType->Cascade] create a Sound object that is a cascade frequency-modulated sinusoid, where is the peak deviation associated with modulating frequency , both values measured in hertz

Cascade frequency modulation is a type of frequency modulation where the modulating frequency is itself modulated, thus cascading the modulation. The expression for cascade frequency modulation with two modulating frequencies is given by . This nesting process can be carried out for a list of modulating frequencies of any size.

In this example of cascade frequency modulation, each modulating frequency is given a distinct peak deviation.
 Out[16]=
 FrequencyModulation[fc,{{f1,pd1},{f2,pd2},...},dur,ModulationType->Parallel] create a Sound object that is a parallel frequency-modulated sinusoid, where is the peak deviation associated with modulating frequency , both values measured in hertz

Parallel frequency modulation.

In contrast to cascade frequency modulation, parallel frequency modulation is a technique where the carrier frequency is modulated by two or more modulating frequencies that do not modulate each other. The expression for parallel frequency modulation with two modulating frequencies is given by .

This uses the same values as the example for cascade FM, but generates an instance of parallel FM instead. Note the stronger presence of the higher partials in the resulting sound.
 Out[17]=

### Setting Options DisplayFunction, SampleRate, SampleDepth, and PlayRange

All the sound-generating functions in the Audio package use the Mathematica Play function to do the low-level work of generating sounds. Some options of Play, such as SampleRate, SampleDepth, and PlayRange, can significantly change the quality and constitution of the resulting sound. The option DisplayFunction affects whether the created Sound object will be played immediately after it is returned.

Loading the package will set the options SampleRate, SampleDepth, and PlayRange to their optimum values for your machine. The package also sets , which causes Sound objects to be returned without being played. You can reset these options by using Mathematica's SetOptions function, or by specifying them on the command line.

You can create a sound at a specified SampleRate.
 Out[18]=

By setting the option , you can hear a sound immediately after it is created by Mathematica, without having to use the Show function each time. This is most useful when you are experimenting with combinations of arguments and wish to hear the effect they have on the resulting sound.

Now all Sound objects returned by FrequencyModulation will be played immediately after being returned.
 Out[19]=
This assigns a frequency modulated sound to and plays the sound.
 Out[20]=
You can hear the same function with slightly different values of the modulating frequencies.
 Out[21]=
Now FrequencyModulation will no longer play every Sound object it creates.
 Out[22]=

You will be better able to use the sound-generating functions if you are aware of the effect of the various options to Play.

SampleRate is an option for sound primitives that specifies the number of samples per second to generate for sounds. According to Nyquist's sampling theorem, samples per second are required to accurately represent a waveform having a maximum frequency content of hertz. In other words, your sampling rate must be at least twice the highest frequency you would like to hear. Sampling at a lower rate will result in aliasing, in which frequency components above half the sampling rate are rendered as lower frequency components. Since people register frequencies in the range of 20-20,000 hertz as audio signals, a sampling rate of 40,000 samples per second is the minimum required for the accurate encoding of the entire audio range.

Mathematica allows you to set the SampleRate to any positive number. However, the sound that will be created depends upon the sampling rate of your digital-to-analog converter. If the converter's sampling rate is not a multiple of the chosen sampling rate, a software conversion to the converter's sampling rate will automatically take place, and there may be distortions in the resulting sound.

This is a frequency-modulated sound at the default SampleRate.
 Out[23]=
This plays the same sound with SampleRate->1234. The additional noise is due to the fact that the digital-to-analog converter's sampling rate is not a multiple of 1234 hertz.
 Out[24]=

SampleDepth (frequently called quantization) is an option for sound primitives that specifies how many bits should be used to encode sound amplitude levels. Setting SampleDepth->8 gives you different amplitude values, and SampleDepth->16 gives you . You should choose a value for SampleDepth that is the best for your hardware.

Here is the pitch A5 using the default setting of SampleDepth.
 Out[25]=
On some computers, this SampleDepth produces no sound at all. You might get a sound that is simply noisier.
 Out[26]=

PlayRange is an option for Play and related functions that specifies what range of sound amplitude levels should be included. The functions Waveform and FrequencyModulation set this value to All, and the AmplitudeModulation function sets it to . PlayRange in Play behaves like PlotRange in Plot.

Here is a simple plot of a sine waveform.
 Out[27]=
This gives the same plot with a restricted PlotRange.
 Out[28]=

Notice that in the second plot, when the value of the function exceeds the limits specified by PlotRange, the value is cut off or clipped. Similarly, if the amplitude of your sound exceeds the setting of PlayRange, what you hear will not be a complete representation of your function. This phenomenon is termed waveshaping by composers and distortion by engineers. PlayRange should be set sufficiently high so that all generated samples may be represented without distortion.

### Playing Sound Objects in Arbitrary Sequences

Since all the sound-generating functions defined in return a Sound object, the returned values can be played in sequence with the Show function.

First create three sounds.
This plays the three Sound objects in reverse sequence.
 Out[30]=
New to Mathematica? Find your learning path »
Have a question? Ask support »