forked from: forked from: 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
simppafi: Yo makc3d! Nice task, but this demostrates how to make musicvideo types of cuts in visualizer according to leftPeak.
P. S., yeah I know I really cheaped out on the visuals.
/**
* Copyright simppafi ( http://wonderfl.net/user/simppafi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5Rz7
*/
// forked from makc3d's forked from: beat detection development
// 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
simppafi: Yo makc3d! Nice task, but this demostrates how to make musicvideo types of cuts in visualizer according to leftPeak.
P. S., yeah I know I really cheaped out on the visuals.
*/
package {
import flash.utils.getTimer;
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;
private var averageStorage:Vector.<Number>;
private const averageStorageLength:int = 10;
public var agressive:Number = 0.08; // How agressive to detect peaks?
private var changetime:int = 0;
private var backgroundColor:uint = 0x000000;
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();
averageStorage = new Vector.<Number>();
for(var i:int = 0; i < averageStorageLength; i++)
{
averageStorage[i] = 0;
}
addChild(new Bitmap(d));
changetime = getTimer();
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 time:int = getTimer();
var leftPeak:Number = ch.leftPeak;
var count:Number = leftPeak + ch.rightPeak;
d.lock();
d.scroll(-1, 0);
// This is for getting points on song where something dramatic enough happens. Not for getting constant beat or something.
// Imagine if background color change are like camera cuts in music video.
// Since it compare against latest average it's always valid no matter what happens in song or what type the song is
// agressive variable is for setting the intesity of cuts. Smaller the number more cuts.
// - Cheers simppafi
averageStorage.shift();
averageStorage.push(leftPeak);
var aveVal:Number = 0;
for(var i:int = 0; i < averageStorageLength; i++)
{
aveVal+=averageStorage[i];
}
var peakAvarage:Number = aveVal/averageStorageLength;
var overaverage:Number = leftPeak - (peakAvarage * 1.45);
if(overaverage > agressive && time > (changetime+200) )
{
changetime = time;
var value:int = int(overaverage * 512);
backgroundColor = (value << 16 | value << 8 | value);
d.fillRect(r, 0xffffff);
}
d.fillRect(r, backgroundColor);
vol = Math.max(vol * 0.97, count);
d.setPixel(255, 255 - 128*vol, 0xff0000);
d.setPixel(255, 255 - 128*count, 0xffff00);
d.unlock();
}
}
}