// SpectrumAnalyzer.as
package {
import flash.display.*;
import flash.media.*;
import flash.net.*;
import flash.utils.ByteArray;
import flash.events.*;
class SpectrumAnalyzer extends Sprite {
// Settings
private var lineThickness:Number = 2;
private var lineColor:Number = 0x993300;
private var circleSize:Number = 75;
private var scaleOnBeat:Number = 1.1; // 120%
private var reactToBeat:Number = 30;
//
private var music:Sound = new Sound;
private var ba:ByteArray = new ByteArray();
private var __width:uint;
private var __height:uint;
function SpectrumAnalyzer(mp3:String, _width:uint, _height:uint) {
__width = _width;
__height = _height;
x = __width/2;
y = __height/2
music.load(new URLRequest(mp3));
music.play(0, 999);
addEventListener(Event.ENTER_FRAME, processSound);
}
private function processSound(ev:Event):void {
SoundMixer.computeSpectrum(ba, true, 0);
graphics.clear();
graphics.moveTo(0, -circleSize);
graphics.lineStyle(lineThickness, lineColor);
var vol:Number = 0;
for (var i:uint = 0; i <512; i++) {
var lev:Number = ba.readFloat();
vol += lev;
var a:uint = i;
var j:Number = i;
if (i <256) a += 256;
if (i == 256) graphics.moveTo(0, -circleSize);
graphics.lineTo(-Math.sin(i/256*Math.PI)*circleSize*(lev+1), Math.cos(a/256*Math.PI)*circleSize*(lev+1));
}
if (vol> reactToBeat) {
scaleX = scaleY = scaleOnBeat;
}
else {
scaleX = scaleY = 1;
}
}
}
}
I am thinking that
private var reactToBeat:Number = 30;
is completely dependant on song.mp3. If the song was processed at a different volume level, then you may need to lower the value. (e.g. 30 would be lowered to 19)
I ultimately want to have this work with any song, because I will be making a playlist and have song.mp3 replaced with the var.
To start, however, I would at least like to get this spectrum to only respond to low frequencies.
I think for start, I would need to change:
SoundMixer.computeSpectrum(ba, true, 0);
to
SoundMixer.computeSpectrum(ba, false, 0);
so that it reads it as a frenquency spectrum rather than a complete wav file (according to the as3 ref doc)
With that, the first 256 values should be low freqs and 257 to 512 would be high freqs.
How can I utilize that to only scaleOnBeat to the low frequencies?
Also, I didn’t know if it was best to use a select case for a certain range of values.
Thanks