Sound Spectrum:サウンドスペクトラム
2008年01月15日 火曜日
この連休にこんなものを作った。
http://dskymd.com/as3/sound.html
AS3.0から音の波形を取得できるようになった。
Pointクラス、ByteArrayクラス、SoundMixerクラス。
こういうの作るのは楽しいな。
以下、ソースコード。時間があるときに解説を入れていきます。。
package {
import flash.events.Event;
import flash.geom.Point;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.filters.BitmapFilter;
import flash.filters.BitmapFilterQuality;
import flash.filters.GlowFilter;
import flash.media.Sound;
import flash.media.SoundMixer;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public class SoundPolar extends MovieClip{
var snd:Sound;
var _data:ByteArray;
var pt0,pt1,pt2:Point;
var circle:Sprite;
var ctn:MovieClip;
var offset:int;
var clr:int;
var glow:GlowFilter;
var glowColor:int;
function SoundPolar(container:MovieClip,file:String,X,Y){
ctn = container;
pt0 = new Point(X,Y);
snd = new Sound();
var req:URLRequest = new URLRequest(file);
snd.load(req);
snd.addEventListener(Event.COMPLETE,playSound);
_data = new ByteArray();
offset = randRange(70,ctn.sw/4);
clr = 0xFFFFFF;
}
function playSound(event:Event=null){
snd.play(0);//randRange(0,snd.length));
circle = new Sprite();
circle.x = pt0.x;
circle.y = pt0.y;
ctn.addChild(circle);
ctn.addEventListener(Event.ENTER_FRAME,drawLine);
}
function drawLine(event:Event=null){
SoundMixer.computeSpectrum(_data, false, 0);
var gx:Graphics = circle.graphics;
gx.clear();
var radian:Number = 360/511*Math.PI/180;
var angle:Number = 0;
var fat:int = 2;
while(angle< =2*Math.PI){
var len:int = Math.abs(offset*_data.readFloat());
var pt1:Point = Point.polar(offset-len/randRange(3,14),angle);
var pt2:Point = Point.polar(offset+len+1,angle);
gx.lineStyle(fat,clr);
gx.moveTo(pt1.x,pt1.y);
gx.lineTo(pt2.x,pt2.y);
angle += radian;
}
glowColor = randRange(0xCCCCCC,0xFFFFFF);
glow = new GlowFilter();
glow.color = glowColor;
glow.alpha = 1;
glow.blurX = 20;
glow.blurY = 20;
glow.quality = BitmapFilterQuality.LOW;
circle.filters = [glow];
circle.rotation = -90;
}
function randRange(min:Number, max:Number):Number {
var randomNum:int = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
}
}
で、実行はこんな感じ。
var SP:SoundPolar = new SoundPolar(コンテナ,mp3ファイルURL,X軸中心点,Y軸中心点);
投稿者:daisuke カテゴリー:Flash,ActionScript


Hi again Daisuke
I’m actually trying to follow your sound spectrum in flash, nevertheless honestly I’m quite novice and can’t figure out what’s going on with your script above. It may not working only with that, maybe require more technique which I haven’t learnt yet
I’d like to ask, if you don’t mind, could you please provide me ‘easier’ version of tutorial/script or at least ‘fla’ file so that I could lookin?
Sorry to bothering you but please allow me to learn your amazing intelligence and technics
Thank you!
sorry, i lost fla file..