0

I am confused by how pydub computes rms.

In [187]: audio = AudioSegment.from_mp3("sample-mp3")
In [188]: audio.rms
Out[188]: 1041

In [189]: audio.dBFS
Out[189]: -29.959984108983633

However using sox:

$ sox sample.mp3 -n stat
Samples read:         130231296
Length (seconds):   1476.545306
Scaled by:         2147483647.0
Maximum amplitude:     1.000000
Minimum amplitude:    -1.000000
Midline amplitude:    -0.000000
Mean    norm:          0.017384
Mean    amplitude:    -0.000023
**RMS     amplitude:     0.031763**
Maximum delta:         1.308396
Minimum delta:         0.000000
Mean    delta:         0.015841
RMS     delta:         0.028429
Rough   frequency:         6282
Volume adjustment:        1.000

Can anyone enlighten me please on how these rms values are computed?? Thx.

Blue482
  • 2,926
  • 5
  • 29
  • 40

1 Answers1

1

They represent the same value, just on different scales. pydub appears to work with signed 16-bit values (maybe because of the 16-bit depth of the mp3 file?), while SoX by default scales the internal 32-bit signed values to [-1,1]. You can bring the two outputs in to congruency by scaling by 2^15, or by telling SoX to use a signed 16-bit scale by using the -s argument. As 2^31/2^15 is 2^16, that should be -s 65536.

AkselA
  • 8,153
  • 2
  • 21
  • 34
  • Thanks @ AkselA! The `-s` argument doesn't seem to work. `sox sample_audio.mp3 -n stats -s 65536` `sox FAIL stats: parameter 'p->scale' must be between -99 and 99 sox FAIL stats: usage: [-b bits|-x bits|-s scale] [-w window-time]` – Blue482 Oct 13 '19 at 16:14
  • Also, from the example in the question. 1041/65536=0.015884 which is not 0.031763 given by sox. The scaling here is a bit confusing. If I do 1041/(2**32/2**16)=0.031768 which is close to 0.031763.. but why. – Blue482 Oct 13 '19 at 16:20
  • 1
    @Blue482: You need to use `-s` with `stat`, not `stats`. Divide by 2^16 when going from signed 32 to signed 16 bit, but by 2^15 to go from signed 16 bit to [-1,1] (2^1). Remember how multiplication with exponents work. 16-1 = 15. – AkselA Oct 13 '19 at 19:02