Forked from: wh0's beat detection development diff:16 forked from: beat detection development spectrum-free test makc3d forked:2favorite:7lines:39license : MIT License modified : 2012-02-21 19:41:12 Embed Tweet // 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(); } } } Code Fullscreen Preview Fullscreen hacker_7z56r.. 0xABCDEF Mingchoi nki2 Vladik : music aobyrne : beat-detectionbeatDetectionbeatdetectionbeat_detection hidrodixtion.. : beatdetectionsound beat beat-detection beatDetection beat_detection detection music sound setPixel fillRect ByteArray scroll Rectangle unlock lock Stats URLRequest BitmapData Math.max Event.ENTER_FRAME Event Number sort new page view favorite forked pv763 fforked from: beat detection d.. codeonwort forked:1 favorite:6lines:182 (diff:236) tag: audio pv445 forked from: forked from: beat.. simppafi forked:0 favorite:1lines:66 (diff:52)