       Speeding up code producing sound

• To: mathgroup at smc.vnet.net
• Subject: [mg43522] Speeding up code producing sound
• From: Erich Neuwirth <erich.neuwirth at univie.ac.at>
• Date: Fri, 19 Sep 2003 03:41:46 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```I need help.
I am creating the Shephard tone, which is the acoustic equivalent
It seems to go up continuously in pitch, but nevertheless
after some time is exactly the same tone as at the start.

I have Mathematica code, and it works,
but the code is terribly slow.
Perhaps one of the masters on the list is willing to have a look
and speed up the code.

To hear the tone, you have to do something like
Play[ShepWave[t, (60+#)&, 12 &, 60 &, 2 &], {t, 0, 12}]
But it will take quite long until one can hear something.

The problem is:
computing one second of sound takes 30 seconds computing time
on my not too fast notebook.

So here is the code.
Perhaps there are ways to improve speed easily.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

SineWave[t_] := N[Sin[2*Pi*t]]
Env[x_ /; Abs[x] <= 1] := N[(Sin[Pi*(x + 1/2)] + 1)/2]
Env[x_ /; Abs[x] > 1] := 0
Hz[Midinote_] := N[440*2^((Midinote - 69)/12)]
MidiPitch[Hz_] := N[12*Log[2, (Hz/440)] + 69]
ErichWave[t_, Skew_] := (Sqrt[1 - Skew*Skew]/Skew)*
SineWave[t]/(1/Skew - SineWave[t + 1/4])

PitchIntegrate[p_] := Function[t, Integrate[Hz[p[x]], {x, 0, t}]]
FilteredAmp[Pitch_, FilterCenter_, OvertoneSemitones_, HalfFilterWidth_] :=
Env[(Pitch - FilterCenter)/(HalfFilterWidth*OvertoneSemitones)]

ShepWave[t_, Pitch_, OvertoneSemitones_, FilterCenter_, HalfFilterWidth_] :=
Sum[FilteredAmp[Pitch[t] + i*OvertoneSemitones[t], FilterCenter[t],
OvertoneSemitones[t], HalfFilterWidth[t]]*
SineWave[PitchIntegrate[(Pitch[#] + i*OvertoneSemitones[#]) &][t]],
{i, -Floor[HalfFilterWidth[t]] +
Floor[(FilterCenter[t] - Pitch[t])/OvertoneSemitones[t]],
Floor[HalfFilterWidth[t]] +
Floor[(FilterCenter[t] - Pitch[t])/OvertoneSemitones[t]] + 1}]

-=-=-=-=

Erich Neuwith

```

• Prev by Date: Re: Convert all graphics to reduce file size?
• Next by Date: Re: Re: NSolve fails where Solve succeeds!
• Previous by thread: Re: how to simplify a complicated equation?
• Next by thread: contourplot3d again