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

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
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
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
w___w__w___.j__av__a__2s___._c_om | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.