# Why does Mathematica take so long to produce this sound?

Mathematica is behaving very slow, but it is not like I could compare, as the speed has always been like this. The following ran in 58.64 seconds. Is this normal? How can I speed it up?

AbsoluteTiming[
Audio[Play[
Sum[Sin[(1 - 1/n^2)*2 Pi*t*880] + Sin[(1 - 1/n^2)*2 Pi*t*440] +
Sin[(1/4 - 1/(n + 1)^2)*2 Pi*t*440], {n, 1, 10}], {t, 0, 5},
SampleRate -> 44100]]]


P.S: If the problem is related to computation, I have a pretty powerful GPU; could I get Mathematica to use that?

Creating the audio signal as a vector first allows us to use more efficient ways to sample the function:

rate = 44100;
T = 5.;
f[t_] :=
Sum[Sin[(1 - 1/n^2) 2 Pi t 880] + Sin[(1 - 1/n^2) 2 Pi t 440] +
Sin[(1/4 - 1/(n + 1)^2) 2 Pi t 440], {n, 1, 10}];

First@AbsoluteTiming[
a = Audio[Play[f[t], {t, 0., T}, SampleRate -> rate]];
]

First@AbsoluteTiming[
t = Subdivide[0., T, T rate - 1];
v = f[t]/(4 Pi);
b = Audio[v, SampleRate -> rate];
]


28.3754

0.04235

This is more than a 600-fold speed-up. And the two results a and b sound pretty similar. Alas, AudioPlot reveals slight differences. And I did also not get why I have to divide by 4 Pi. I simply guessed this factor by trial and error. I just don't know how Play normalizes the generated signal. Must have to do with how the PlayRange is computed. Maybe anybody else can help?

• that's a remarkable boost actually!
– alex
Commented Mar 15, 2023 at 12:35
• Yeah. I think somebody was a bit careless when implementing Play. Would be great if you would send a report on this to Wolfram Support. Thanks. Commented Mar 15, 2023 at 15:51

The way you have created the function you are forcing MMA to keep evaluating your expression, which although lightweight on its own, is rather heavy when it needs to be repeated for different values of n and over 5 seconds with a 44 kHz sampling rate.

## 1. Break things down

You can see that this dramatically reduces your evaluation time.

func = Sum[
Sin[(1 - 1/n^2)*2 Pi*t*880] + Sin[(1 - 1/n^2)*2 Pi*t*440] +
Sin[(1/4 - 1/(n + 1)^2)*2 Pi*t*440], {n, 1, 10}];
AbsoluteTiming[Audio[Play[func, {t, 0, 5}, SampleRate -> 44100]]]


## 2. You do not need both Play and Audio

Play[func, {t, 0, 5}, SampleRate -> 44100] // AbsoluteTiming


You can then export your data as you see it fit.

## 3. GPU boosting

I am not certain why you would need GPU boosting, but if it is important to use said technology, then you might want to look into Compile and CUDAFunctionLoad.

My advice is that you are complicating your life unnecessarily with those two as they are significantly more advanced uses of Mathematica and you will need to understand far more technical aspects of the language.

Keep in mind that these boostings only offer you an advantage over truly large computations. If your computation time is only 20 seconds, chances are a parallelisable or GPU boosted or compiled function will take longer. That is because setting up the correct kernel distribution is now an added task. Setting up the code to a GPU-readable function will also account for resources. These are better suited for computations that take significantly more time.

Bottom line: anything less than 1 minute is probably not quite worth your while in terms of GPU/parallelisation. (feel free to explore that avenue, of course :) )

• Thank you I understand now, but a couple of things: Just using play gives me an image which I am not able to play The time for me is still slightly above yours (24 seconds for me and 15 seconds for you), would it because of hardware? And can I somehow get my GPU to produce these?
– Anm
Commented Mar 15, 2023 at 10:14
• The line which contains Play, produces a fully controllable function. You have to click on the Play button (the left facing trangle) which is at the bottom of the output. I am not sure why you want to GPU boost your work. But I've edited my post to give you some directions.
– alex
Commented Mar 15, 2023 at 12:05
• what version of mathematica are you using?
– alex
Commented Mar 15, 2023 at 12:15
• Thanks for the new edit, I will read it soon. I am using Mathematica_13.1.0 where clicking on the buttons does nothing. It just outlines the borders of the widget
– Anm
Commented Mar 15, 2023 at 12:21
• I'm using win10/MMA v.13.2 what OS are you using?
– alex
Commented Mar 15, 2023 at 12:28