You may take the BASS library (free for non-commercial) and use BASS_ChannelGetLevel().
Here is the code to get levels and output it to STDOUT:
#include <cstddef>
#include <stdio.h>
#include <stdlib.h>
#include "bass.h"
int main(int argc, char **argv)
{
BASS_Init(0 /* "NO SOUND" device */, 44100, 0, 0, NULL);
if ( argc == 3 )
{
int block = atoi( argv[2] ); // take levels every argv[2] ms
if ( block < 20 )
block = 20;
HSTREAM chan = BASS_StreamCreateFile(FALSE, argv[1], 0, 0, BASS_STREAM_DECODE);
if ( chan )
{
// BASS_ChannelGetLevel takes 20ms from the channel
QWORD len = BASS_ChannelSeconds2Bytes(chan, (float)block / (float)1000 - (float)0.02);
char data[len];
DWORD level, left, right;
while ( -1 != (level = BASS_ChannelGetLevel(chan) ) ) // takes 20ms
{
left=LOWORD(level); // the left level
right=HIWORD(level); // the right level
printf("%i, %i\n", left, right);
BASS_ChannelGetData(chan, data, len); // get data away from the channel
}
BASS_StreamFree( chan );
}
}
BASS_Free();
return 0;
}
Extract bass.h and libbass.so from the bass24-linux.zip archive and build the cpp file with:
g++ levels.cpp -o levels -lbass
How to execute: levels filename milliseconds(20minimum)
./levels 1.mp3 5000 >levels.txt
Here is the levels.txt with levels taken every 5 sec (left channel, right channel):
1, 2
23235, 20363
22704, 20601
27203, 22476
10384, 12082
12059, 13387
9600, 10063
14590, 12261
16428, 14745
17569, 14723
29628, 27913
20799, 23554
24056, 20564
20344, 21242
21318, 22888
25389, 29050
27185, 23924
25469, 22540
28453, 29037
19669, 19797
16497, 16086
12081, 11843
20030, 20050
20512, 19537
19347, 14610
27673, 26563
26414, 24696
19775, 22869
24137, 25127
22093, 23184
26563, 24422
27718, 23791
24456, 26598
29353, 22647
562, 508
The level ranges linearly from 0 (silent) to 32768 (max).
Time for 6mb mp3 file, 100ms period:
# time ./levels 1.mp3 100 >levels.txt
real 0m0.981s
user 0m0.972s
sys 0m0.008s
About a second to produce 22Kb levels.txt file with 100ms period.