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

Mosquito Noise 17k Hz

Get Adobe Flash player
by seagirl 19 May 2009
/**
 * Copyright seagirl ( http://wonderfl.net/user/seagirl )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/94k2
 */

package {
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.SampleDataEvent;
	import flash.media.Sound;

	public class MosquitoNoise extends Sprite
	{
		private static const SAMPLING_RATE:int = 44100;
		private static const SAMPLE_SIZE:int = 8192; // 2048 〜 8192
		
		public function MosquitoNoise()
		{
			addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
		}
		
		private var frequency:Number = 17000;
		private var phase:Number = 0.0;
		
		private var waveform:SineWave;
		private var sound:Sound;
		
		private function init():void
		{
			trace('------------------------------------------------------------', new Date());
			trace('Mosquito Noise by Takuho Yoshizu');
			
			waveform = new SineWave();
			
			sound = new Sound();
			sound.addEventListener(SampleDataEvent.SAMPLE_DATA, sampleDataHandler);
			sound.play();
		}
		
		private function addToStageHandler(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			
			stage.frameRate = 60;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			init();
		}
		
		private function sampleDataHandler(event:SampleDataEvent):void
		{
			var sample:Number;
			for (var i:int = 0; i < SAMPLE_SIZE; i++)
			{
				phase += frequency / SAMPLING_RATE;
				
				sample = waveform.exec(phase);
				sample *= 0.8;
							
				event.data.writeFloat(sample); // L
				event.data.writeFloat(sample); // R
			}
		}
	}
}

class SineWave
{
	public function exec(phase:Number):Number
	{
		var phaseAngle:Number = phase * Math.PI * 2;
		var sample:Number = Math.sin(phaseAngle);
			
		return sample;
	}
}