Calculating Spectrum Data : MP3 « Development « Flash / Flex / ActionScript

Home
Flash / Flex / ActionScript
1.Animation
2.Array
3.Class
4.Data Type
5.Development
6.Function
7.Graphics
8.Language
9.Network
10.Regular Expressions
11.Statement
12.String
13.TextField
14.XML
Flash / Flex / ActionScript » Development » MP3 




Calculating Spectrum Data
 
package {
    import flash.utils.ByteArray;
    import flash.events.*;
    import flash.net.*;
    import flash.display.*;
    import flash.media.*;
    import flash.geom.*;

    public class Main extends Sprite {
        private const SPECTRUM_WIDTH:int 256;

        private const BMP_HEIGHT:int 200;
        private const BMP_WIDTH:int 256;

        private var sound:Sound = new Sound(new URLRequest("http://www.java2s.com/y.mp3"));

        private var soundData:ByteArray;

        private var bitmapData:BitmapData;
        private var bitmapDisplay:Bitmap;

        public function Main () {
            sound.play();

            bitmapData = new BitmapData(BMP_WIDTH, BMP_HEIGHT, true, 0x00000000);
            bitmapDisplay = new Bitmap(bitmapData);
            addChild(bitmapDisplay);

            this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        public function onEnterFrame (event:Event):void {
            soundData = new ByteArray();
            SoundMixer.computeSpectrum(soundData);

            bitmapData.fillRect(bitmapData.rect, 0xFF000000);

            for (var i:int=0; i < SPECTRUM_WIDTH; i++) {
                var amplitude:Number = soundData.readFloat();

                var ampHeight:Number = BMP_HEIGHT/(amplitude + 1);

                var rect:Rectangle = new Rectangle(i, BMP_HEIGHT -ampHeight, 1, ampHeight);
                bitmapData.fillRect(rect, 0xffffffff);
            }
        }
    }
}

        














Related examples in the same category
1.Load mp3 sound file
2.Load mp3 file from a URL
3.Find Out When a Sound Finishes Playing
4.Tracking the Progress of a Playing Sound
5.Reading the Level of a Sound
6.ID3 Reader
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.