read mp3 decimal values
I would like to read the be able to store the decibel values across intervals of a local mp3 into a text file. i think i can h开发者_Python百科andle writing to a text file once i have the values(although any help would be great) Best
i want to do this using AS3
and many thanks
First, you must load the MP3, for example, by using Sound.load()
, or by including it in your library in the Flash IDE and exporting it for ActionScript. Then you can use Sound.extract()
to grab the waveform data from an MP3. This will give you the sample data back for the interval packed in a ByteArray
, which you can read out.
The samples are in the range of [-1.0, 1.0], so one simple way to calculate an intensity level for an interval is to find the maximum absolute value among the samples. Here's some example code:
var sound:Sound = new Sound();
sound.load(new URLRequest("sound.mp3"));
sound.addEventListener(Event.COMPLETE, onSoundLoaded);
function onSoundLoaded(event:Event):void {
var byteArray:ByteArray = new ByteArray();
sound.extract(byteArray, 4096);
byteArray.position = 0;
var max:Number = 0;
while(byteArray.position != byteArray.length)
{
var sample:Number = Math.abs(byteArray.readFloat());
if(sample > max) max = sample;
}
trace(max);
}
That will output the level for the first 4096 samples. You'll have to repeat this to get more values.
精彩评论