Monday, April 9, 2007

Sound levels on video

As you know, Flash Player 9 supports up to 32 sound channels. Flash 8 supported only 8. Great you say, until you need the channel allocated to a video playing. Well, there is no way to access it!

Here is my approach for a unified sound level reading (average between left and right speakers):

private function getSoundLevel():Number {
    var spectrum:ByteArray = new ByteArray();     SoundMixer.computeSpectrum(spectrum, true, 32);
    var total:Number = 0;
    for (var i:int = 0; i<512; i++) {
        total += spectrum.readFloat();
    }
    return total/512;
}


Drawbacks:
1. It gets the entire volume level of the application
2. It assumes some calculations while monitoring. Nothing noticeable on a 20 ms timer though.

Small observation: when setting the volume via SoundTransform, you can use values outside the 0..1 range, larger values result in volume gain. Negative values work too.

No comments: