In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Dynamic sound generation test ※音が出ます

Get Adobe Flash player
by terra1119 06 Mar 2010
/**
 * Copyright terra1119 ( http://wonderfl.net/user/terra1119 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/k8pt
 */

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.events.SampleDataEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.display.DisplayObjectContainer;
	import flash.text.TextField;
	import flash.filters.BlurFilter;
	[SWF(width="550", height="400", frameRate="30", backgroundColor="#000000")]

	public class TestSynthe extends Sprite {
		private var phase:Number=0;
		private var freq:Number=440;
		private const PI2:Number=Math.PI*2;
		private var soundChannelObject:SoundChannel= new SoundChannel();
		private var soundTr:SoundTransform=new SoundTransform();
		private var text_field:TextField = new TextField();
		private var isPlaying:Boolean=true;
		private var ball:Sprite;
		private var blurObj:BlurFilter;
		
		public function TestSynthe() {
			var soundObj:Sound = new Sound();
			soundObj.addEventListener(SampleDataEvent.SAMPLE_DATA,onSampleData);
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			stage.addEventListener(MouseEvent.CLICK,clickHandler);
			soundChannelObject=soundObj.play();
			addChild(text_field);
			text_field.border=true;
			text_field.background=true;
			text_field.backgroundColor=0xFFFFFF;
			text_field.width=120;
			text_field.height=20;
			text_field.x=stage.stageWidth-text_field.width-10;
			text_field.y=stage.stageHeight-text_field.height-10;
			textSet();
		}
		private function mouseMoveHandler(e:MouseEvent):void {
			freq=mouseY*2+55;
			textSet();
			if (isPlaying) {
				createBall();
			}
		}
		private function clickHandler(e:MouseEvent):void {
                        
			isPlaying=! isPlaying;
			if (! isPlaying) {
				soundTr.volume=0;
				soundChannelObject.soundTransform=soundTr;
			} else {
				soundTr.volume=1;
				soundChannelObject.soundTransform=soundTr;
			}
		}
		private function onSampleData(e:SampleDataEvent):void {
			var smpl:Number;
			var w:Number=PI2*freq/44100;
			var latency:Number=e.position/44.1-soundChannelObject.position;
			for (var i:int = 0; i < 4096; ++i) {
				phase+=w;
				if (phase>PI2) {
					phase-=PI2;
				}
				smpl=Math.sin(phase);
				e.data.writeFloat(smpl);
				e.data.writeFloat(smpl);
			}
		}
		private function textSet():void {
			var str:String=String(freq);
			text_field.text="Frequency="+str+"Hz";
		}
		private function createBall():void {
			blurObj=new BlurFilter();
			blurObj.blurX=2;
			blurObj.blurY=2;
			ball=new Sprite();
			addChild(ball);
			ball.graphics.beginFill(Math.random()*0xFFFFFF);
			ball.graphics.drawCircle(mouseX,mouseY,freq/10);
			ball.graphics.endFill();
			ball.blendMode="add";
			ball.filters=[blurObj];
			ball.addEventListener(Event.ENTER_FRAME,efHandler);
                        //ball.addEventListener(MouseEvent.CLICK,clickHandler)
		}
		private function efHandler(e:Event):void {
			var targetSprite:Sprite=e.target as Sprite;
			targetSprite.alpha-=0.15;
			blurObj.blurX+=5;
			blurObj.blurY+=5;
			if (targetSprite.alpha<0) {
				targetSprite.removeEventListener(Event.ENTER_FRAME,efHandler);
				removeChild(targetSprite);
                                targetSprite=null;
                                
			}
			targetSprite.filters=[blurObj];
		}
	}
}