flash on 2009-6-1
/**
* Copyright spikything ( http://wonderfl.net/user/spikything )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/sdrT
*/
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageQuality;
import flash.events.Event;
[SWF(width=500, height=400, backgroundColor=0x000000, frameRate=30)]
public class Main extends Sprite {
private var mic:Mic;
private var view:SmokeTrailEffect;
private var scratchPad:Shape;
private var jitter:Number = 250;
private var visualGain:Number = .4;
private var colour:int = 0x9933CC;
private var _width:uint = 500;
private var _height:uint = 400;
public function Main ():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init (e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.quality = StageQuality.LOW;
mic = new Mic();
scratchPad = new Shape();
view = new SmokeTrailEffect(scratchPad, _width, _height);
addChild(view);
addEventListener(Event.ENTER_FRAME, update);
}
private function update (e:Event):void {
var level:Number = mic.activityLevel;
if (level > 40 || Math.random() < .01) scratchPad.graphics.clear();
if (level > 90) view.setupPerlinNoise();
scratchPad.graphics.beginFill(level * colour);
scratchPad.graphics.drawCircle(_width/2 + Math.random()*jitter - jitter/2, _height/2 + Math.random()*jitter - jitter/2, level * visualGain);
scratchPad.graphics.endFill();
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.IBitmapDrawable;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BitmapFilter;
import flash.filters.BlurFilter;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;
import flash.geom.Point;
import flash.geom.Rectangle;
class SmokeTrailEffect extends Sprite {
private var _width:uint;
private var _height:uint;
private var bitmap:Bitmap;
private var displacementMap:BitmapData;
private var displacementMapFilter:BitmapFilter;
private var blurFilter:BitmapFilter;
private var source:IBitmapDrawable;
public var shimmer:Boolean;
public function SmokeTrailEffect (source:IBitmapDrawable, width:uint, height:uint) {
this.source = source;
_width = width;
_height = height;
bitmap = new Bitmap(new BitmapData(_width, _height, false, 0xffff00ff));
addChild(bitmap);
displacementMap = new BitmapData(_width, _height, false);
setupPerlinNoise();
displacementMapFilter = getDisplacementMapFilter(displacementMap, 20);
blurFilter = new BlurFilter(2, 2);
addEventListener(Event.ENTER_FRAME, update);
}
public function setupPerlinNoise():void {
addPerlinNoise(displacementMap);
}
private function addPerlinNoise (bitmapData:BitmapData):void{
bitmapData.perlinNoise(100, 100, 2, Math.floor(Math.random() * int.MAX_VALUE), false, true, 3, false);
}
private function update (e:Event):void {
if (shimmer) setupPerlinNoise();
bitmap.bitmapData.draw(source);
bitmap.bitmapData.applyFilter(bitmap.bitmapData, bitmap.getRect(this), new Point(), displacementMapFilter);
bitmap.bitmapData.applyFilter(bitmap.bitmapData, bitmap.getRect(this), new Point(), blurFilter);
}
private function getDisplacementMapFilter (mapBitmap:BitmapData, amount:Number):BitmapFilter {
return new DisplacementMapFilter(mapBitmap, new Point(), 1, 2, amount, amount, DisplacementMapFilterMode.CLAMP);
}
}
import flash.media.Microphone;
import flash.media.SoundTransform;
import flash.system.Security;
class Mic {
public var mic:Microphone;
public function Mic() {
mic = Microphone.getMicrophone();
mic.gain = 50;
mic.rate = 11;
mic.setLoopBack(true);
mic.setUseEchoSuppression(true);
mic.soundTransform = new SoundTransform(0);
}
public function get activityLevel ():Number { return mic.activityLevel };
}