In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: beat detection development

spectrum-free test
/**
 * Copyright makc3d ( http://wonderfl.net/user/makc3d )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/tUY6
 */

// forked from wh0's beat detection development
// forked from wh0's dripping spectrum
/*
multipart project 2
part 3.5 of, I don't know, like 5
I knew something like this would come up.

Background:
Beat detection is an important part of music visualization.
We'd better do a little of this while we're at it then.
FYI, it's been done (e.g. http://wonderfl.net/c/k6FT), but
that implementation is kinda plain.

Task:
- find a good song to experiment with (probably electronic)
- detect beats
- try to get good accuracy, duh

P. S., yeah I know I really cheaped out on the visuals.
*/

package {
    import net.hires.debug.Stats;
    import flash.display.Sprite;
    import flash.system.Security;
    import flash.media.*;
    import flash.net.URLRequest;
    import flash.media.SoundLoaderContext;
    import flash.display.Bitmap;
    import flash.events.Event;
    import flash.utils.ByteArray;
    import flash.geom.Rectangle;
    import flash.display.BitmapData;
    public class Spectrum extends Sprite {
        
        private const s:ByteArray = new ByteArray();
        private const r:Rectangle = new Rectangle(255, 0, 1, 256);
        private var d:BitmapData = new BitmapData(256, 256, false, 0x000000);
        private var vol:Number = 1, ch:SoundChannel;
        
        public function Spectrum() {
            var s:Sound = new Sound(new URLRequest('http://www.apmmusic.com/audio/DED/DED_DED_0120/DED_DED_0120_07701.mp3'), new SoundLoaderContext(1000, true));
            ch = s.play();
            
            addChild(new Bitmap(d));
            
            addEventListener(Event.ENTER_FRAME, comp);
            
            addChild(new Stats()).x = 256;
        }
        
        private function comp(e:Event):void {
            // the question is, do we really need computeSpectrum()?
            // let us find out...
            var count:Number = ch.leftPeak + ch.rightPeak;
            
            d.lock();
            d.scroll(-1, 0);
            if (count > 1.2 * vol)//1.9)
                d.fillRect(r, 0xffffff);
            else
                d.fillRect(r, 0x000000);
            vol = Math.max(vol * 0.97, count);
            d.setPixel(255, 255 - 128*vol, 0xff0000);
            d.setPixel(255, 255 - 128*count, 0xffff00);
            d.unlock();
        }
        
    }
}