# 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 the Wolfram Language.

The graphics output of the Wolfram Language'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 |

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

Standard waveform definitions.

In[1]:= |

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.

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. The Wolfram Language's SetOptions function may be used to make Fourier summation the default.

### 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 Null has relative amplitude Null |

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.

In[7]:= |

In[8]:= |

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

In[10]:= |

### 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 amplitude-modulated sinusoid, having carrier and modulator frequencies Null and Null measured in hertz, a modulation index of mi, and a duration of dur seconds |

The expression used by AmplitudeModulation is given by .

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.

AmplitudeModulation[f_{c},f_{m},mi,dur,RingModulation->True] | |

create a Sound object that is a ring-modulated sinusoid |

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 .

### 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[f_{c},{f_{m},pd},dur] | |

create a Sound object that is a frequency-modulated sinusoid, having carrier and modulating frequencies Null and Null hertz, peak deviation pd hertz, and a duration of dur seconds |

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.

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[f_{c},{{f_{1},pd_{1}},{f_{2},pd_{2}},…},dur,ModulationType->Cascade] | |

create a Sound object that is a cascade frequency-modulated sinusoid, where Null is the peak deviation associated with modulating frequency Null, 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.

FrequencyModulation[f_{c},{{f_{1},pd_{1}},{f_{2},pd_{2}},…},dur,ModulationType->Parallel] | |

create a Sound object that is a parallel frequency-modulated sinusoid, where Null is the peak deviation associated with modulating frequency Null, 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 .

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

All the sound‐generating functions in the Audio package use the Wolfram Language 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 DisplayFunction->Identity, which causes Sound objects to be returned without being played. You can reset these options by using the Wolfram Language'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 the Wolfram Language, 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.

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, Null 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.

The Wolfram Language 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.

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.

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 {-1,1}. PlayRange in Play behaves like PlotRange in Plot.

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 Audio` return a Sound object, the returned values can be played in sequence with the Show function.

In[29]:= |