forked from: Chapter 31 Example 5
forked from Chapter 31 Example 5 (diff: 2)
ActionScript3 source code
/**
* Copyright Aksor.Al ( http://wonderfl.net/user/Aksor.Al )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/dpoY
*/
// forked from actionscriptbible's Chapter 31 Example 5
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.media.*;
import flash.net.URLRequest;
import flash.utils.ByteArray;
[SWF(frameRate="60",backgroundColor="#808080")]
public class ch31ex5 extends Sprite {
protected var sound:Sound;
protected var waveform:ByteArray;
public function ch31ex5() {
var songurl:String = "http://scfire-dtc-aa03.stream.aol.com:80/stream/1035";
sound = new Sound(new URLRequest(songurl),
new SoundLoaderContext(1000, true));
sound.play();
waveform = new ByteArray();
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
protected function onEnterFrame(event:Event):void {
graphics.clear();
SoundMixer.computeSpectrum(waveform, false, 2);
//draw the left channel in blue
graphics.lineStyle(0, 0x0000ff);
drawChannel();
//and the right in red
graphics.lineStyle(0, 0xff0000);
drawChannel();
}
protected function drawChannel():void {
var W:Number = stage.stageWidth;
var H:Number = stage.stageHeight;
var SAMPLES:Number = 256;
var xstep:Number = W/SAMPLES;
graphics.moveTo(0, H/2);
//assumes that the ByteArray is already at the correct location
for (var i:int = 0, x:Number = 0; i < SAMPLES; i++, x+=xstep) {
var amplitude:Number = waveform.readFloat();
var y:Number = H/2 + (amplitude * H/2); //amplitude is from -1 to 1
graphics.lineTo(x, y);
}
}
}
}
