var s:Sound = new Sound();
var sc:SoundChannel;
var ba:ByteArray = new ByteArray();
var array:Array;
s.load(new URLRequest("sound.mp3"));
sc = s.play(0,1);
this.addEventListener(Event.ENTER_FRAME, spectrum);
function spectrum(event:Event) {
var a:Number = 0;
SoundMixer.computeSpectrum(ba,true,0);
for (var i=0; i < 50; i++) {
a = ba.readFloat();
var num:Number = a*400;
trace(num);
}
}
기본적으로 as3에서 스펙트럼을 구현할 때 위와 같은 형태로 만들 수 있다. 크로스 도메인 정책 파일을 서버에 구현해 놓지 않으면 로드된 사운드의 데이터에 액세스할 수 없다. 이는 BitmapData의 경우도 같다.
아래는 샘플로 작업한 위 결과물의 코드다.
package{
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundMixer;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public class SoundSpectrumTest extends Sprite {
private var s:Sound;
private var ba:ByteArray;
private var mcAry:Array;
private var main1:Sprite;
private var sc:SoundChannel;
private var recTotalNum:uint;
public function SoundSpectrumTest() {
s = new Sound();
ba = new ByteArray();
mcAry = new Array();
main1 = new Sprite();
sc = new SoundChannel();
recTotalNum =300;
addEventListener(Event.ADDED_TO_STAGE, initialize);
}
private function initialize(evt:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, initialize);
loadSound();
}
private function setButtonEvent():void{
play_mc.addEventListener(MouseEvent.CLICK, playHandler);
stop_mc.addEventListener(MouseEvent.CLICK, stopHandler);
}
private function playHandler(evt:Event):void{
sc.stop();
createEnterFrame();
sc = s.play(0, 10);
}
private function stopHandler(evt:Event):void{
removeEnterFrame();
sc.stop();
}
private function startSpectrumDisplay():void{
var gap:uint = 1;
for(var i:uint=0;i<recTotalNum;i++){
var mc:Rec = new Rec();
mc.x = i*(mc.width+gap);
mc.y = 230;
main1.addChild(mc);
mcAry.push(mc);
}
addChild(main1);
main1.x = uint(stage.stageWidth/2 - main1.width/2);
main1.y = uint(stage.stageHeight/2 - main1.height/2);
createEnterFrame();
}
private function createEnterFrame():void{
addEventListener(Event.ENTER_FRAME, leftSpectrum);
addEventListener(Event.ENTER_FRAME, rightSpectrum);
}
private function removeEnterFrame():void{
removeEventListener(Event.ENTER_FRAME, leftSpectrum);
removeEventListener(Event.ENTER_FRAME, rightSpectrum);
}
private function loadSound():void {
s.load(new URLRequest("sound.mp3"));
s.addEventListener(Event.COMPLETE, loadedSound);
}
private function loadedSound(evt:Event):void{
startSpectrumDisplay();
setButtonEvent();
sc = s.play(0, 1000);
}
private function leftSpectrum(event:Event):void {
var left:Number;
var leftNum:Number = 0;
var leftTotalNum:Number = recTotalNum/2;
SoundMixer.computeSpectrum(ba,true,0);
while(leftTotalNum>=0){
left = ba.readFloat();
leftNum = left*400*sc.leftPeak;
mcAry[leftTotalNum].height = leftNum;
leftTotalNum--;
}
}
private function rightSpectrum(event:Event):void {
var right:Number;
var rightNum:Number = 0;
var rightTotalNum:Number = recTotalNum/2;
SoundMixer.computeSpectrum(ba,true,0);
while(rightTotalNum<recTotalNum){
right = ba.readFloat();
rightNum = right*400*sc.rightPeak;
mcAry[rightTotalNum].height = rightNum;
rightTotalNum++;
}
}
}
}