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

beat detection development

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.
/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/fmCA
 */

// 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.Sound;
    import flash.net.URLRequest;
    import flash.media.SoundLoaderContext;
    import flash.display.Bitmap;
    import flash.events.Event;
    import flash.utils.ByteArray;
    import flash.media.SoundMixer;
    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 = 256;
        
        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));
            s.play();
            
            addChild(new Bitmap(d));
            
            addEventListener(Event.ENTER_FRAME, comp);
            
            addChild(new Stats()).x = 256;
        }
        
        private function comp(e:Event):void {
            var count:Number = 0;
            SoundMixer.computeSpectrum(s, true);
            s.position = 0;
            
            for (var f:int = 0; f < 256; f++) {
                var v:Number = s.readFloat();
                count += v * v * f / 64;
            }
            
            d.lock();
            d.scroll(-1, 0);
            if (count > vol * 1.5 && count > 16)
                d.fillRect(r, 0xffffff);
            else
                d.fillRect(r, 0x000000);
            vol = Math.max(vol * 0.97, count);
            d.setPixel(255, 255 - vol, 0xff0000);
            d.setPixel(255, 255 - count, 0xffff00);
            d.unlock();
        }
        
    }
}