WebCam映像にサウンドでエフェクト
/**
* Copyright mousepancyo ( http://wonderfl.net/user/mousepancyo )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fzaj
*/
package {
import flash.display.Sprite;
import flash.media.Video;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.utils.Timer;
import flash.geom.ColorTransform;
import flash.system.Security;
[SWF(backgroundColor="#000000", frameRate="15")]
public class Main extends Sprite{
private var _video:CameraCapture
private var _video2:CameraCapture
private var _bmd:BitmapData
private var _bmd2:BitmapData
private var _bm:Bitmap
private var _bm2:Bitmap
private var _sound:SoundPlayStop = new SoundPlayStop()
private var _waveBytes:ByteArray = new ByteArray();
private var _timer:Timer
public function Main() {
Security.loadPolicyFile("http://www.digifie.jp/crossdomain.xml");
_video = new CameraCapture(320,240)
_bmd = new BitmapData(320, 240, true, 0x000000)
_bm = new Bitmap(_bmd)
_bmd2 = new BitmapData(320, 240, true, 0x000000)
_bm2 = new Bitmap(_bmd2)
_video.alpha = 0.5
_bm2.y = 245
addChild(_video)
addChild(_bm)
addChild(_bm2)
startDelay()
_video.addEventListener(CameraCapture.ACTIVE,soundStart)
}
private function startDelay():void{
_timer = new Timer(1000,1)
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, startDelayComplete)
_timer.start()
}
private function startDelayComplete(e:TimerEvent):void{
_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, startDelayComplete)
_timer = null
addEventListener(Event.ENTER_FRAME,update)
}
private function soundStart(e:Event):void{
_video.removeEventListener(CameraCapture.ACTIVE,soundStart)
_sound.soundStart("http://www.digifie.jp/files/test.mp3", 100)
}
private function update(e:Event):void{
_bmd.draw(stage)
_bmd2.draw(_video)
setChildIndex(_video, numChildren-1)
modeChange()
}
private function modeChange():void{
var n:int
SoundMixer.computeSpectrum(_waveBytes, true, 0);
n = _waveBytes.readFloat()*100
if(n>=100){
_video.blendMode = "overlay"
}else if(n<100 && n>=70){
_video.blendMode = "screen"
}else if(n<70 && n>=60){
_video.blendMode = "lighten"
}else if(n<60 && n>=50){
_video.blendMode = "difference"
}else if(n<50 && n>=40){
_video.blendMode = "add"
}else if(n<40 && n>=30){
_video.blendMode = "hardlight"
}else if(n<30 && n>=20){
_video.blendMode = "darken"
}else if(n<20 && n>=10){
_video.blendMode = "multiply"
}else{
_video.blendMode = "normal"
}
}
}
}
//
import flash.events.ActivityEvent;
import flash.events.Event;
import flash.media.Camera;
import flash.display.Sprite;
import flash.media.Video;
class CameraCapture extends Video{
private var _cam:Camera;
private var _camW:Number
private var _camH:Number
private var _fps:Number
private var _activityThreshold:Number
private var _actLevel:int
public static const ACTIVE:String = "active"
public static const INACTIVE:String = "inactive"
public function CameraCapture(camW:Number, camH:Number, fps:Number=30, activityThreshold:Number=10) {
this.width = camW
this.height = camH
_camW = camW
_camH = camH
_fps = fps
_activityThreshold = activityThreshold
setUpCamera();
}
private function setUpCamera():void {
_cam = Camera.getCamera();
_cam.setMode(_camW, _camH, _fps);
this.attachCamera(_cam);
_cam.addEventListener(ActivityEvent.ACTIVITY, CamActivityCheck);
}
private function CamActivityCheck(e:ActivityEvent):void {
if (_cam.activityLevel >= _activityThreshold) {
dispatchEvent(new Event(CameraCapture.ACTIVE))
} else {
dispatchEvent(new Event(CameraCapture.INACTIVE))
}
_actLevel = _cam.activityLevel
}
public function get actLevel():Number{
return _actLevel
}
}
//
import flash.events.Event
import flash.events.EventDispatcher
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.media.SoundLoaderContext
import flash.net.URLRequest
import flash.system.Security;
class SoundPlayStop extends EventDispatcher{
public var my_sc:SoundChannel;
public var my_sound:Sound;
public var _flg:Boolean = true
public function SoundPlayStop(){
Security.loadPolicyFile("http://www.digifie.jp/crossdomain.xml");
}
public function soundStart(Url:String, ref:uint = 1, vol:Number = 1):void {
if(_flg){
var trans:SoundTransform = new SoundTransform(vol,0)
var context:SoundLoaderContext = new SoundLoaderContext();
context.checkPolicyFile = true
//
if (my_sc != null) {
my_sc.stop();
my_sc = null;
}
try{
my_sound = new Sound(new URLRequest(Url),context);
my_sc = my_sound.play(0,ref);
my_sc.soundTransform = trans;
}catch(e:Error){
trace(e,Url)
}
}
}
public function soundStop():void {
if (my_sc != null) {
my_sc.stop();
my_sc = null;
my_sound = null;
}
}
}