Consider loading an mp3 audio file


Now we get an object that looks like

enter image description here

One can see a picture of the sound fluctuations, which means that Mathematica knows how to access the raw values of the wave. Now, I would like to take small bits of the waveform at certain times (let's say, from second 40.0 to second 40.3), sum up its absolute value squared and divide by the time length to get the power of the signal at this time. Can I do this in Mathematica?


In version 11.1, you can use AudioLoudness. It can use several loudness definitions. I am not familiar with these, but you can look them up.

au = ExampleData[{"Audio", "Clapping"}]

AudioLoudness[au] // ListLinePlot

Mathematica graphics

To compute precisely what you are describing in version 11.0 or later, use AudioBlockMap.

AudioBlockMap[Norm, au, Quantity[0.1, "Seconds"]] // ListLinePlot

Mathematica graphics

To get the plot you showed in your screenshot, use AudioPlot[au].

| improve this answer | |
  • $\begingroup$ This looks good, but what if I want to end up with a table of loudness? Each entry in the table should correspond to the average loudness within 0.3 consecutive seconds in the mp3 audio. $\endgroup$ – Kagaratsch Mar 25 '17 at 19:54
  • 1
    $\begingroup$ @Kagaratsch You get a TimeSeries from these functions, you can convert it with Normal. Normal@AudioBlockMap[Norm, au, Quantity[0.3, "Seconds"]]. $\endgroup$ – Szabolcs Mar 25 '17 at 19:55
  • $\begingroup$ The waveform is stored in the second argument of a RawData object, which is in the first argument of the Audio object we get upon Import of the file. I wanted to access these by taking parts of the Audio object, or isolate the waveform by substitution rules, but nothing seems to work. It is as if the head Audio is protected against any manipulations. Just out of curiosity, is there a way to extract the List with raw waveform information from the Audio object somehow? $\endgroup$ – Kagaratsch Mar 25 '17 at 20:00
  • 1
    $\begingroup$ @Kagaratsch Audio objects are atomic and their internal structure shouldn't be messed with. You can get the data with AudioData. It's an analogue of ImageData. Many other audio functions are named similarly to image processing ones. You can also do direct arithmetic on Audio objects without extracting their contents, like Total[#^2] & /@ AudioPartition[au, 0.3] $\endgroup$ – Szabolcs Mar 25 '17 at 20:06
  • 1
    $\begingroup$ @Kagaratsch, then that would just be RootMeanSquare[#]^2 &. $\endgroup$ – J. M.'s discontentment Mar 26 '17 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.