Message Boards Message Boards

0
|
11784 Views
|
27 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Speed up the process of exporting audio to mp3 format?

Posted 5 years ago

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?

POSTED BY: Andrew Dabrowski
27 Replies

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"}
POSTED BY: Piotr Wendykier

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

enter image description here

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

POSTED BY: Andrew Dabrowski

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"}
POSTED BY: Piotr Wendykier

Here's what I get, and the audio is only 8 seconds. enter image description here

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?

POSTED BY: Andrew Dabrowski

@Andrew Dabrowski , please add the code for generating that Sound object.

POSTED BY: Piotr Wendykier

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?

enter image description here

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]
Attachments:
POSTED BY: Andrew Dabrowski

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*)]]
POSTED BY: Piotr Wendykier

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.

POSTED BY: Andrew Dabrowski

The first note in the Details and Options section of Sound 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.

POSTED BY: Piotr Wendykier

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?

POSTED BY: Andrew Dabrowski

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.

POSTED BY: Piotr Wendykier

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?

POSTED BY: Andrew Dabrowski

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

POSTED BY: Piotr Wendykier

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.

POSTED BY: Andrew Dabrowski

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}
POSTED BY: Andrew Dabrowski

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.

POSTED BY: Piotr Wendykier

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

Attachments:
POSTED BY: Andrew Dabrowski

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

POSTED BY: Piotr Wendykier

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?

POSTED BY: Andrew Dabrowski

The following is a bug that we will fix:

In[3]:= Audio[ListPlay[RandomReal[1, {5}], SampleDepth -> 16]] // AudioQ

During evaluation of In[3]:= Audio::audioin: The specified argument  should be an array of rank 1 or 2 with machine-sized numbers, a string representing local or remote audio resource, a Sound or an Audio object.

Out[3]= False

If you want to control the sample depth, then you should use the "AudioEncoding" option of Export to AIFF, WAV, FLAC or SND:

In[2]:= a = AudioGenerator["Sin"];

In[3]:= Export["out.flac", a, "AudioEncoding" -> "Integer24"]

Out[3]= "out.flac"

In[4]:= Import["out.flac", "AudioEncoding"]

Out[4]= "Integer24"
POSTED BY: Piotr Wendykier

OK, thank you for your help.

POSTED BY: Andrew Dabrowski

Andrew,

I just tested on a fresh installed ubuntu 16.04 and Audio object plays fine for me. From a terminal, can you do "aplay -l" and "aplay -L" and send me the output to look at.

POSTED BY: Chien-Yu Chen

Here it is:

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC3234 Analog [ALC3234 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 9: HDMI 3 [HDMI 3]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 10: HDMI 4 [HDMI 4]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

$ aplay -L
default
    Playback/recording through the PulseAudio sound server
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
sysdefault:CARD=PCH
    HDA Intel PCH, ALC3234 Analog
    Default Audio Device
front:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    Front speakers
surround21:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    4.0 Surround output to Front and Rear speakers
surround41:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
hdmi:CARD=PCH,DEV=0
    HDA Intel PCH, HDMI 0
    HDMI Audio Output
hdmi:CARD=PCH,DEV=1
    HDA Intel PCH, HDMI 1
    HDMI Audio Output
hdmi:CARD=PCH,DEV=2
    HDA Intel PCH, HDMI 2
    HDMI Audio Output
hdmi:CARD=PCH,DEV=3
    HDA Intel PCH, HDMI 3
    HDMI Audio Output
hdmi:CARD=PCH,DEV=4
    HDA Intel PCH, HDMI 4
    HDMI Audio Output
dmix:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    Direct sample mixing device
dmix:CARD=PCH,DEV=3
    HDA Intel PCH, HDMI 0
    Direct sample mixing device
dmix:CARD=PCH,DEV=7
    HDA Intel PCH, HDMI 1
    Direct sample mixing device
dmix:CARD=PCH,DEV=8
    HDA Intel PCH, HDMI 2
    Direct sample mixing device
dmix:CARD=PCH,DEV=9
    HDA Intel PCH, HDMI 3
    Direct sample mixing device
dmix:CARD=PCH,DEV=10
    HDA Intel PCH, HDMI 4
    Direct sample mixing device
dsnoop:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    Direct sample snooping device
dsnoop:CARD=PCH,DEV=3
    HDA Intel PCH, HDMI 0
    Direct sample snooping device
dsnoop:CARD=PCH,DEV=7
    HDA Intel PCH, HDMI 1
    Direct sample snooping device
dsnoop:CARD=PCH,DEV=8
    HDA Intel PCH, HDMI 2
    Direct sample snooping device
dsnoop:CARD=PCH,DEV=9
    HDA Intel PCH, HDMI 3
    Direct sample snooping device
dsnoop:CARD=PCH,DEV=10
    HDA Intel PCH, HDMI 4
    Direct sample snooping device
hw:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    Direct hardware device without any conversions
hw:CARD=PCH,DEV=3
    HDA Intel PCH, HDMI 0
    Direct hardware device without any conversions
hw:CARD=PCH,DEV=7
    HDA Intel PCH, HDMI 1
    Direct hardware device without any conversions
hw:CARD=PCH,DEV=8
    HDA Intel PCH, HDMI 2
    Direct hardware device without any conversions
hw:CARD=PCH,DEV=9
    HDA Intel PCH, HDMI 3
    Direct hardware device without any conversions
hw:CARD=PCH,DEV=10
    HDA Intel PCH, HDMI 4
    Direct hardware device without any conversions
plughw:CARD=PCH,DEV=0
    HDA Intel PCH, ALC3234 Analog
    Hardware device with all software conversions
plughw:CARD=PCH,DEV=3
    HDA Intel PCH, HDMI 0
    Hardware device with all software conversions
plughw:CARD=PCH,DEV=7
    HDA Intel PCH, HDMI 1
    Hardware device with all software conversions
plughw:CARD=PCH,DEV=8
    HDA Intel PCH, HDMI 2
    Hardware device with all software conversions
plughw:CARD=PCH,DEV=9
    HDA Intel PCH, HDMI 3
    Hardware device with all software conversions
plughw:CARD=PCH,DEV=10
    HDA Intel PCH, HDMI 4
    Hardware device with all software conversions
POSTED BY: Andrew Dabrowski

Can you find a wave file, or Export["/tmp/test.wav", Audio["ExampleData/car.mp3"]] and inside a terminal, do aplay -D default /tmp/test.wav and see what happen?

POSTED BY: Chien-Yu Chen

The command line aplay works fine, audio plays and there are no errors.

aplay -D default test.wav
POSTED BY: Andrew Dabrowski

Andrew, Can you generate a core dump for me to debug? Here is the step. 1. Start WL 2. $ProcessID 3. open a terminal and type "gdb" 4. inside the gdb command, type "attach pid" where pid is what you get from step 2 5. Audio["ExampleData/car.mp3"] 6. in gdb command, type "continue" 7. play the Audio object to create the crash. 8. inside the gdb command, type "generate-core-file" 9. quit from gdb The core file should be at the directory where you start gdb. Shoot me a note and I'll give you a place to send the core dump.

POSTED BY: Chien-Yu Chen

Well, what do you know - this time when I tried it the kernel didn't crash. Next time I find it crashing I'll try to get a core for you.

POSTED BY: Andrew Dabrowski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract