# Wolfram Language™

## Shepard's Tone: An Auditory Illusion

Create an auditory illusion.

In:= ```range = 12; duration = 10; lfoFreq = .05; basePitch = 30; numOsc = 8; midiToFreq[m_] := 2^((m - 69)/12)*440.```

Generate ramps to control frequencies and amplitudes of the oscillators.

In:= ```phasors = Table[AudioGenerator[{"Sawtooth", lfoFreq, 2. Pi/numOsc (i - 1.)}, duration, SampleRate -> 500]/2. + .5, {i, numOsc}]; AudioPlot[phasors, PlotLayout -> "Overlaid", PlotRange -> All]```
Out= Create frequencies that will control the oscillators. The frequencies are exactly one octave apart from each other and increase exponentially, so that pitch increases linearly.

In:= ```freqs = Table[ midiToFreq[(phasors[[i]])*12*numOsc + basePitch], {i, numOsc}]; AudioPlot[freqs, PlotLayout -> "Overlaid", PlotRange -> All]```
Out= Create amplitudes that will control the oscillators. Amplitudes go to 0 when the frequencies drop to the minimum value.

In:= ```amps = Cos[#*Pi - Pi/2.] & /@ phasors; AudioPlot[amps, PlotLayout -> "Overlaid", PlotRange -> All]```
Out= Show the relation between frequency and amplitude of one oscillator.

In:= `AudioPlot[AudioNormalize /@ {freqs[], amps[]}, PlotRange -> All]`
Out= Combine a bank of oscillators using the created frequencies and amplitudes.

In:= ```res = Mean[ Table[amps[[i]] AudioGenerator[{"Sin", freqs[[i]]}], {i, numOsc}]]```
In:= ```Spectrogram[res, 8192, 4096, HannWindow, PlotRange -> {All, {0, 10000}}, ImageSize -> Medium]```
Out= 