Sound Fun with SoundMixer.computeSpectrum();

computeSpectrum

This is an example of how the computeSpectrum works on Flash CS3, I’ts just great the posible things you can make with it , this is based on a great example made by Peter deHaan In my example I just made the spectrum being represented by circles and have a gradient fill across the spectrum here’s the link.

And here’s the script:

?View Code ACTIONSCRIPT3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var url:String ="songs/mysong.mp3";
var request:URLRequest = new URLRequest(url);
var s:Sound = new Sound();
s.addEventListener(Event.COMPLETE, completeHandler);
s.load(request);
var song:SoundChannel = s.play();
song.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
var ba:ByteArray = new ByteArray();
 
var gr:Sprite = new Sprite();
 
gr.x = 20;
gr.y = 200;
 
addChild(gr);
 
var time:Timer = new Timer(50);
time.addEventListener(TimerEvent.TIMER, timerHandler);
time.start();
 
function completeHandler(event:Event):void {
	event.target.play();
}
function soundCompleteHandler(event:Event):void {
	time.stop();
}
function timerHandler(event:TimerEvent):void {
	SoundMixer.computeSpectrum(ba, true);
	var i:int;
	// Gradien Fill
	var fillType:String = GradientType.LINEAR;
	var colors:Array = [0xFF0000, 0x0000FF];
	var alphas:Array = [100, 100];
	var ratios:Array = [0x00, 0xFF];
	var matr:Matrix = new Matrix();
	matr.createGradientBox(400, 20, 0, 0, 0);
	var spreadMethod:String = SpreadMethod.PAD;
	// ends fill
	gr.graphics.clear();
	gr.graphics.lineStyle(0, 0xFF0000);
	gr.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
	gr.graphics.moveTo(0, 0);
	var w:uint = 2;
	for (i=0; i<512; i+=w) {
		var t:Number = ba.readFloat();
		var n:Number = (t * 100);
 
		gr.graphics.drawCircle(i, w, -n);
 
	}
}

Author: Dave Gamez

Digital Craftsman from México and have more than 14 years of experience both in design and development in the digital field. I currently work in web, desktop, mobile and illustration projects alike and spend my time between being Technology Director at Team-O a Digital Studio I co-founded with Cecy Meade and doing consultancy work, when time allows it I like to experiment and tinker with code, animation and illustration.