Speed up the process of exporting audio to mp3 format?

I was exporting a 60 second audio clip last night, and after waiting half an hour I went to bed. (The export had completed by the time I woke up this morning.)

Why does Mathematica require over 30 minutes for a job like this when any half decent free audio app can do it in seconds?

I cannot reproduce your timings in Mathematica 11.3:

In[10]:= a = AudioGenerator["Sin", 60];

In[11]:= AbsoluteTiming[Export["out.mp3", a]]

Out[11]= {0.29224, "out.mp3"}

I had used Play to generate audio from a wave function. The I tried to export by

Evidently this is the wrong way to do it, but 40 minutes and still counting?

I cannot reproduce:

In[4]:= sound = Play[Sin[440 2 Pi t], {t, 0, 60}, SampleRate -> 44100];

In[5]:= AbsoluteTiming[Export["out.mp3", sound]]

Out[5]= {1.31196, "out.mp3"}

Here’s what I get, and the audio is only 8 seconds.

I’m on Linux 16.04 LTS 64 bit with Mathematica 11.3.0.0.

Could it be a problem when copying and pasting the output? Or when the audio is more complex than a pure sine wave?

@Andrew Dabrowski , please add the code for generating that Sound object.
[at0]: http://community.wolfram.com/web/dabrowsa

Before I do that, do you really think the complexity of the sound could slow down the mpg3 encoding algorithm? I don’t know much about it. Would that apply to a wav export also?

To reproduce the audio: Load the two attached .m files, then run the following.

cis5 = 440.0*2^(1/3);
clA3 = clarinet[220.0];
clCis5 = clarinet[cis5];
start = Join @@@ Transpose[{clA3, clCis5}];
time = 8;
wf = evolve[#1, #2, time, 0.0002, {200, 1400}] & @@ start;
wfun = interpolateToWF @@ wf;
play[wfun[t], time]

fm.m (8.64 KB)
sines.m (72.7 KB)

I could not run attached .m files, because they have hardcoded file paths, but I know where the problem is. Your Sound object is of the form

Sound[SampledSoundFunction[(*long and complicated function*)]],

and it has to be converted to sampled audio before it can be exported to any audio file format. You should check how long it will take to evaluate

Audio[(*your Sound object*)]]

OK, thanks, I had assumed that once the audio was generated it was copied and pasted along with the input form, but I shouldn’t have.

The first note in the Details and Options section of [Sound documentation] (Sound—Wolfram Documentation) lists which primitives can be used in the Sound object. One of these primitives, SampledSoundList, stores sampled audio data and can be exported directly to any audio file format. This means the following will be fast:

Export[anyAudioFileFormat, Sound[SampledSoundList[..]]] 

and

Export[anyAudioFileFormat, Audio[..]]

If a Sound object contains any other primitives (SampledSoundFunction or SoundNote), then it has to be “rasterized” to contain only SampledSoundList primitives. This process may take a long time if SampledSoundFunction is complicated.

Wait, I’m uncertain how to handle this. Once I’ve generated the Sound object I can play it immediately and repeatedly with no delay. But if I save it to a variable, I get the same problem:

In[107]:= AbsoluteTiming@Export["test.mp3", sound]

Out[107]= {742.197, "test.mp3"}

(That was 16 second audio.) Do I really have to generate the audio all over again with Audio in order to export it?

The Audio constructor is called by Export if the expression you are trying to export is not already an Audio object. You can use AudioQ to test if your expression is an Audio object.

So maybe the solution is to avoid Play and use SampledSoundList directly?

It seems strange that Mathematica generates the audio for playback but must regenerate it for export - presumably that’s because settings are redone for the mp3 format?

Yes, if you don’t have SampledSoundFunction and SoundNote primitives in your Sound object, then Export should be fast.

Btw, another serious problem I’m having is that every time I try to play an Audio object the kernel crashes. I assume this is because of incompatibilities with the linux sound system.

Does that mean on linux there is no way to avoid generating the audio twice - once for playing, once for saving?

Never mind, I see I can use PlayList.

OK, now I’m running into a problem with SampledSoundList. Are there strict requirements on soundlists that can be exported to various formats?

In[112]:= AbsoluteTiming@Export["test.mp3", sound]

During evaluation of In[112]:= Export::nodta: Sound[SampledSoundList[{0.00914949,0.0645038,0.10364,0.119694,0.118204,0.11123,0.108125,<<37>>,-0.0343303,-0.0115127,0.0102955,0.0199559,0.0169125,0.00949124,<<176351>>},<<1>>]] contains no data that can be exported to the MP3 format.

Out[112]= {0.413006, $Failed}

You cannot export to mp3 sounds that are shorter than 0.1 seconds. Other file formats don’t have such restriction. Please attach a file with the InputForm of your Sound object.

The audio was several seconds long. Could it be because it was mono rather than stereo?

sound.m (1.97 MB)

The second argument of SampledSoundList in your example is equal to {44100,16}. Please change it to 44100 and Export will work.

I had set the sample depth to 16, isn’t that the standard for mp3? If I leave the sample depth unset, ListPlay defaults it to 8. Does that mean the mp3 file written by Mathematica will have sample depth 8?