Audio Package
This package provides functions for the generation of standard waveforms and waveforms with userspecified 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. 
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. 
Here Table is used to create a sequence of sawtooth waveform sounds running from ten overtones down to only two.
Out[14]=  

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[{{n_{1},a_{1}},{n_{2},a_{2}},...},freq,dur] 
 create a Sound object with a fundamental frequency of freq lasting for dur seconds, with a spectrum in which relative frequency n_{i} has relative amplitude a_{i} 
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 halfsteps, 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[18]=  

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). 
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[f_{c},f_{m},mi,dur]  create a Sound object that is an amplitudemodulated sinusoid, having carrier and modulator frequencies f_{c} and f_{m} measured in hertz, a modulation index of mi, and a duration of dur seconds 
Amplitude modulation.
The expression used by
AmplitudeModulation is given by
(1+ mi cos (2f_{m}t)) cos (2f_{c}t).
When the modulating frequency is subaudio (below 20 hertz), the result of amplitude modulation is what musicians call tremolo. 
The depth or intensity of the modulation is controlled by the modulation index
mi. The carrier is said to be overmodulated when
mi>1. The best way to understand the effect of
mi is to try it out with different values, usually between 0.1 and 2.0.
Ring modulation.
If you set the option
RingModulation>True, 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
mi cos (2f_{m}t) cos (2f_{c}t).
In this example, the output sound contains the frequencies 700 and 300 hertz, the sum and difference of the carrier and modulating frequencies. 
Frequency Modulation
Frequency modulation (FM) is a technique used at major universities on workstations and in suburban homes on synthesizers. The ubiquitousness 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[f_{c},{f_{m},pd},dur] 
 create a Sound object that is a frequencymodulated sinusoid, having carrier and modulating frequencies f_{c} and f_{m} 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. 
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 lowfrequency components to the sound.
This is an example of frequency modulation with a modulation index of 5500/550. 
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. One speaks of a harmonic relationship when the overtones and sidebands have frequencies that coincide with the harmonics of some fundamental, not necessarily the carrier.
FrequencyModulation[f_{c},{{f_{1},pd_{1}},{f_{2},pd_{2}},...},dur,ModulationType>Cascade] 
 create a Sound object that is a cascade frequencymodulated sinusoid, where pd_{i} is the peak deviation associated with modulating frequency f_{i}, both values measured in hertz 
Cascade frequency modulation.
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. 
FrequencyModulation[f_{c},{{f_{1},pd_{1}},{f_{2},pd_{2}},...},dur,ModulationType>Parallel] 
 create a Sound object that is a parallel frequencymodulated sinusoid, where pd_{i} is the peak deviation associated with modulating frequency f_{i}, 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. 
Setting Options DisplayFunction, SampleRate, SampleDepth, and PlayRange
All the soundgenerating functions in the
Audio package use the
Mathematica Play function to do the lowlevel 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
DisplayFunction>Identity, 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.
By setting the option
DisplayFunction>$SoundDisplayFunction, 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.
This assigns a frequency modulated sound to casfm2 and plays the sound. 
You can hear the same function with slightly different values of the modulating frequencies. 
You will be better able to use the soundgenerating 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,
n samples per second are required to accurately represent a waveform having a maximum frequency content of
n/2 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 2020,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 digitaltoanalog 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. 
This plays the same sound with SampleRate>1234. The additional noise is due to the fact that the digitaltoanalog converter's sampling rate is not a multiple of 1234 hertz. 
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
2^{8} different amplitude values, and
SampleDepth>16 gives you
2^{16}. 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. 
On some computers, this SampleDepth produces no sound at all. You might get a sound that is simply noisier. 
PlayRange is an option for
Play and related functions which 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
{1, 1}.
PlayRange in
Play behaves like
PlotRange in
Plot.
Here is a simple plot of a sine waveform.
Out[27]=  

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 soundgenerating functions defined in
Audio` return a
Sound object, the returned values can be played in sequence with the
Show function.
First create three sounds. 