User:Tolga Ozuygur/prototyping1: Difference between revisions
No edit summary |
No edit summary |
||
Line 14: | Line 14: | ||
import flash.media.Sound;<br> | import flash.media.Sound;<br> | ||
import flash.events.SampleDataEvent; | import flash.events.SampleDataEvent;<br> | ||
import flash.events.MouseEvent; | import flash.events.MouseEvent;<br> | ||
import flash.display.Sprite; | import flash.display.Sprite;<br> | ||
import flash.display.StageAlign; | import flash.display.StageAlign;<br> | ||
import flash.display.StageScaleMode; | import flash.display.StageScaleMode;<br> | ||
import flash.display.BitmapData; | import flash.display.BitmapData;<br> | ||
import flash.display.Bitmap; | import flash.display.Bitmap;<br> | ||
import flash.utils.setInterval; | import flash.utils.setInterval;<br> | ||
import flash.text.TextField; | import flash.text.TextField;<br> | ||
import flash.media.Camera; | import flash.media.Camera;<br> | ||
import flash.media.Video; | import flash.media.Video;<br> | ||
import flash.geom.Point; | import flash.geom.Point;<br> | ||
import flash.geom.Rectangle; | import flash.geom.Rectangle;<br> | ||
var diff_array:Array = [0xFF000000,0xFF111111,0xFF151515,0xFF202020,0xFF252525,0xFF303030,0xFF353535,0xFF444444,0xFF555555,0xFF666666,0xFF777777,0xFF888888,0xFF999999]; | var diff_array:Array = [0xFF000000,0xFF111111,0xFF151515,0xFF202020,0xFF252525,0xFF303030,0xFF353535,0xFF444444,0xFF555555,0xFF666666,0xFF777777,0xFF888888,0xFF999999];<br> | ||
var which_diff_value:Number = 7; | var which_diff_value:Number = 7;<br> | ||
var auto_calibrate_loop:Number = 0; | var auto_calibrate_loop:Number = 0;<br> | ||
var auto_calibrate_loop_max:Number = 20; | var auto_calibrate_loop_max:Number = 20;<br> | ||
var auto_calibrate_switch:Boolean = false; | var auto_calibrate_switch:Boolean = false;<br> | ||
var current_difference_amount:Number = 0; | var current_difference_amount:Number = 0;<br> | ||
var difference_calibration_array:Array = []; | var difference_calibration_array:Array = [];<br> | ||
options_menu.visible = false; | options_menu.visible = false;<br> | ||
calibration_progress.visible = false; | calibration_progress.visible = false;<br> | ||
var current_pos:int=0; | var current_pos:int=0;<br> | ||
var sound:Sound = new Sound(); | var sound:Sound = new Sound();<br> | ||
var cam:Camera; | var cam:Camera;<br> | ||
var video:Video; | var video:Video;<br> | ||
var now:BitmapData; | var now:BitmapData;<br> | ||
var out:BitmapData; | var out:BitmapData;<br> | ||
var diff:BitmapData; | var diff:BitmapData;<br> | ||
var prev:BitmapData; | var prev:BitmapData;<br> | ||
var label:TextField; | var label:TextField;<br> | ||
var myInt:Number; | var myInt:Number;<br> | ||
var camFPS:Number=15; | var camFPS:Number=15;<br> | ||
var camW:Number=250; | var camW:Number=250;<br> | ||
var camH:Number=190; | var camH:Number=190;<br> | ||
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, take_sample); | sound.addEventListener(SampleDataEvent.SAMPLE_DATA, take_sample);<br> | ||
sound.play(); | sound.play();<br> | ||
var hertz:Number = 440; | var hertz:Number = 440;<br> | ||
var amp:Number = 1; | var amp:Number = 1;<br> | ||
var volume_multip:Number = 0.25; | var volume_multip:Number = 0.25;<br> | ||
function take_sample(event:SampleDataEvent):void { | function take_sample(event:SampleDataEvent):void {<br> | ||
for (var i:int = 0; i < 2048; i++) { | for (var i:int = 0; i < 2048; i++) {<br> | ||
var phase:Number=current_pos/44100*Math.PI*2; | var phase:Number=current_pos/44100*Math.PI*2;<br> | ||
current_pos++; | current_pos++;<br> | ||
var little_sample:Number=Math.sin(phase*int(hertz)*amp); | var little_sample:Number=Math.sin(phase*int(hertz)*amp);<br> | ||
event.data.writeFloat(little_sample*volume_multip); | event.data.writeFloat(little_sample*volume_multip);<br> | ||
event.data.writeFloat(little_sample*volume_multip); | event.data.writeFloat(little_sample*volume_multip);<br> | ||
} | }<br> | ||
} | }<br> | ||
function MotionCam() { | function MotionCam() {<br> | ||
cam=Camera.getCamera();<br> | |||
cam.setMode(camW, camH, camFPS);<br> | |||
if (cam==null) {<br> | |||
label = new TextField();<br> | |||
label.text="I don't sense a webcam.";<br> | |||
addChild(label);<br> | |||
} else {<br> | |||
video=new Video(cam.width,cam.height);<br> | |||
video.attachCamera(cam);<br> | |||
now=new BitmapData(video.width,video.height);<br> | |||
out=new BitmapData(video.width,video.height);<br> | |||
diff=new BitmapData(video.width,video.height);<br> | |||
prev=new BitmapData(video.width,video.height);<br> | |||
var output=cam_holder.addChild(new Bitmap(out));<br> | |||
output.x=352;<br> | |||
output.y=0;<br> | |||
output.width=video.width*2;<br> | |||
output.height=video.height*2;<br> | |||
output.scaleX=-2;<br> | |||
myInt=setInterval(render,int(2000/camFPS));<br> | |||
}<br> | |||
}<br> | |||
cam | function render() {<br> | ||
if (! cam.currentFPS) {<br> | |||
return;<br> | |||
}<br> | |||
now.draw(video);<br> | |||
diff.draw(video);<br> | |||
diff.draw(prev,null,null,"difference");<br> | |||
out.fillRect(new Rectangle(0,0,out.width,out.height),0xFF000000);<br> | |||
current_difference_amount=Number(out.threshold(diff,new Rectangle(0,0,diff.width,diff.height),new Point(0,0),">",diff_array[which_diff_value],0xFFFFFFFF));<br> | |||
prev.draw(video);<br> | |||
//<br> | |||
scan_pixels();<br> | |||
}<br> | |||
function scan_pixels() {<br> | |||
for (var ic=1; ic <=2; ic++) {<br> | |||
var iw_shift:Number;<br> | |||
if (ic==1) {<br> | |||
iw_shift=0;<br> | |||
}<br> | |||
if (ic==2) {<br> | |||
iw_shift=135;<br> | |||
}<br> | |||
var temp_pixel_data_prev:Number=0;<br> | |||
var temp_found_it:Boolean=false;<br> | |||
var ih:Number=1;<br> | |||
while (temp_found_it == false) {<br> | |||
for (var iw=(1+Number(iw_shift)); iw <= (40+Number(iw_shift)); iw++) {<br> | |||
var temp_pixel_data:Number=Number(out.getPixel(iw,ih));<br> | |||
if ((temp_pixel_data!=0)) {<br> | |||
//FOUND IT! yay.<br> | |||
temp_found_it=true;<br> | |||
this["level_bar_"+ic].y=this["level_bar_"+ic].y+Number(((ih*2)-this["level_bar_"+ic].y)/4);<br> | |||
if (ic==1) {<br> | |||
this["level_bar_"+ic].level_bar_inner.x = this["level_bar_"+ic].level_bar_inner.x + Number(((40-(iw*2))-this["level_bar_"+ic].level_bar_inner.x)/4);<br> | |||
hertz=440+(this["level_bar_"+ic].y*2);<br> | |||
amp = (((iw/10)-amp)/2);<br> | |||
if (amp<0.5) {<br> | |||
amp=0.5;<br> | |||
}<br> | |||
}<br> | |||
if (ic==2) {<br> | |||
volume_multip=0.5-(this["level_bar_"+ic].y/400);<br> | |||
if (volume_multip<0) {<br> | |||
function | volume_multip=0;<br> | ||
}<br> | |||
}<br> | |||
}<br> | |||
temp_pixel_data_prev=temp_pixel_data;<br> | |||
}<br> | |||
ih=Number(ih)+Number(1);<br> | |||
if (ih>camH) {<br> | |||
//nothing is found, but still end the loop<br> | |||
temp_found_it=true;<br> | |||
}<br> | |||
}<br> | |||
}<br> | |||
auto_calibrate();<br> | |||
}<br> | |||
for (var ic=1; ic <=2; ic++) { | help_button.addEventListener(MouseEvent.MOUSE_UP,help_button_pressed);<br> | ||
var iw_shift:Number; | function help_button_pressed(event:MouseEvent) {<br> | ||
if (ic==1) { | options_menu.visible=true;<br> | ||
iw_shift=0; | }<br> | ||
} | options_menu.auto_calibrate_button.addEventListener(MouseEvent.MOUSE_UP,auto_calibrate_button_pressed);<br> | ||
if (ic==2) { | function auto_calibrate_button_pressed(event:MouseEvent) {<br> | ||
iw_shift=135; | auto_calibrate_switch=true;<br> | ||
} | auto_calibrate_loop=auto_calibrate_loop_max;<br> | ||
var temp_pixel_data_prev:Number=0; | which_diff_value=0;<br> | ||
var temp_found_it:Boolean=false; | options_menu.visible=false;<br> | ||
var ih:Number=1; | }<br> | ||
while (temp_found_it == false) { | options_menu.close_button.addEventListener(MouseEvent.MOUSE_UP,options_menu_close_button_pressed);<br> | ||
for (var iw=(1+Number(iw_shift)); iw <= (40+Number(iw_shift)); iw++) { | function options_menu_close_button_pressed(event:MouseEvent) {<br> | ||
var temp_pixel_data:Number=Number(out.getPixel(iw,ih)); | options_menu.visible=false;<br> | ||
if ((temp_pixel_data!=0)) { | }<br> | ||
//FOUND IT! yay. | function auto_calibrate() {<br> | ||
temp_found_it=true; | if (auto_calibrate_switch) {<br> | ||
this["level_bar_"+ic].y=this["level_bar_"+ic].y+Number(((ih*2)-this["level_bar_"+ic].y)/4); | volume_multip = 0;<br> | ||
if (ic==1) { | calibration_progress.visible=true;<br> | ||
this["level_bar_"+ic].level_bar_inner.x = this["level_bar_"+ic].level_bar_inner.x + Number(((40-(iw*2))-this["level_bar_"+ic].level_bar_inner.x)/4); | if (auto_calibrate_loop<=0) {<br> | ||
hertz=440+(this["level_bar_"+ic].y*2); | which_diff_value=Number(which_diff_value)+Number(1);<br> | ||
amp = (((iw/10)-amp)/2); | var temp_difference_amount:Number=0;<br> | ||
if (amp<0.5) { | for (var i4=0; i4 < difference_calibration_array.length; i4++) {<br> | ||
amp=0.5; | temp_difference_amount=Number(temp_difference_amount)+Number(difference_calibration_array[auto_calibrate_loop]);<br> | ||
} | |||
} | |||
if (ic==2) { | |||
volume_multip=0.5-(this["level_bar_"+ic].y/400); | |||
if (volume_multip<0) { | |||
volume_multip=0; | |||
} | |||
} | |||
} | |||
temp_pixel_data_prev=temp_pixel_data; | |||
} | |||
ih=Number(ih)+Number(1); | |||
if (ih>camH) { | |||
//nothing is found, but still end the loop | |||
temp_found_it=true; | |||
} | |||
} | |||
} | |||
auto_calibrate(); | |||
} | |||
help_button.addEventListener(MouseEvent.MOUSE_UP,help_button_pressed); | |||
function help_button_pressed(event:MouseEvent) { | |||
options_menu.visible=true; | |||
} | |||
options_menu.auto_calibrate_button.addEventListener(MouseEvent.MOUSE_UP,auto_calibrate_button_pressed); | |||
function auto_calibrate_button_pressed(event:MouseEvent) { | |||
auto_calibrate_switch=true; | |||
auto_calibrate_loop=auto_calibrate_loop_max; | |||
which_diff_value=0; | |||
options_menu.visible=false; | |||
} | |||
options_menu.close_button.addEventListener(MouseEvent.MOUSE_UP,options_menu_close_button_pressed); | |||
function options_menu_close_button_pressed(event:MouseEvent) { | |||
options_menu.visible=false; | |||
} | |||
function auto_calibrate() { | |||
if (auto_calibrate_switch) { | |||
volume_multip = 0; | |||
calibration_progress.visible=true; | |||
if (auto_calibrate_loop<=0) { | |||
which_diff_value=Number(which_diff_value)+Number(1); | |||
var temp_difference_amount:Number=0; | |||
for (var i4=0; i4 < difference_calibration_array.length; i4++) { | |||
temp_difference_amount=Number(temp_difference_amount)+Number(difference_calibration_array[auto_calibrate_loop]); | |||
} | } | ||
auto_calibrate_loop=auto_calibrate_loop_max; | temp_difference_amount=int(temp_difference_amount/auto_calibrate_loop_max);<br> | ||
} else { | trace(temp_difference_amount + " " + which_diff_value);<br> | ||
auto_calibrate_loop=auto_calibrate_loop-1; | if (current_difference_amount<3) {<br> | ||
} | //found a good calibration.<br> | ||
difference_calibration_array[auto_calibrate_loop]=current_difference_amount; | auto_calibrate_switch=false;<br> | ||
} else { | }<br> | ||
calibration_progress.visible=false; | if (which_diff_value>diff_array.length) {<br> | ||
} | auto_calibrate_switch=false;<br> | ||
} | }<br> | ||
MotionCam(); | auto_calibrate_loop=auto_calibrate_loop_max;<br> | ||
} else {<br> | |||
auto_calibrate_loop=auto_calibrate_loop-1;<br> | |||
}<br> | |||
difference_calibration_array[auto_calibrate_loop]=current_difference_amount;<br> | |||
} else {<br> | |||
calibration_progress.visible=false;<br> | |||
}<br> | |||
}<br> | |||
MotionCam();<br> |
Revision as of 20:54, 15 October 2010
The project was about transforming data into audio. I didn't want to transform a data that was not designed to produce music into audio. That would simply produce some noise. Instead, I have prepared an instrument that turns the webcam input (its still data) into audio. It will still produce meaningless noises, but this time I can blame the user. "My tool is good, you suck at playing it."
I used action script 3 instead of a pipeline setup with bash, since I usually want everyone to be able to try the stuff I code in their browsers, even in their mobile platforms (except the iphone users. I don't see them as users.) I did not record any output, since I don't have a good ear to put some nice tunes together. Lets see what you can do with it: http://www.odcaf.com/stuff/theremine/theremine.html
If you encounter any problem, try the ? button.
Source:
import flash.media.Sound;
import flash.events.SampleDataEvent;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.utils.setInterval;
import flash.text.TextField;
import flash.media.Camera;
import flash.media.Video;
import flash.geom.Point;
import flash.geom.Rectangle;
var diff_array:Array = [0xFF000000,0xFF111111,0xFF151515,0xFF202020,0xFF252525,0xFF303030,0xFF353535,0xFF444444,0xFF555555,0xFF666666,0xFF777777,0xFF888888,0xFF999999];
var which_diff_value:Number = 7;
var auto_calibrate_loop:Number = 0;
var auto_calibrate_loop_max:Number = 20;
var auto_calibrate_switch:Boolean = false;
var current_difference_amount:Number = 0;
var difference_calibration_array:Array = [];
options_menu.visible = false;
calibration_progress.visible = false;
var current_pos:int=0;
var sound:Sound = new Sound();
var cam:Camera;
var video:Video;
var now:BitmapData;
var out:BitmapData;
var diff:BitmapData;
var prev:BitmapData;
var label:TextField;
var myInt:Number;
var camFPS:Number=15;
var camW:Number=250;
var camH:Number=190;
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, take_sample);
sound.play();
var hertz:Number = 440;
var amp:Number = 1;
var volume_multip:Number = 0.25;
function take_sample(event:SampleDataEvent):void {
for (var i:int = 0; i < 2048; i++) {
var phase:Number=current_pos/44100*Math.PI*2;
current_pos++;
var little_sample:Number=Math.sin(phase*int(hertz)*amp);
event.data.writeFloat(little_sample*volume_multip);
event.data.writeFloat(little_sample*volume_multip);
}
}
function MotionCam() {
cam=Camera.getCamera();
cam.setMode(camW, camH, camFPS);
if (cam==null) {
label = new TextField();
label.text="I don't sense a webcam.";
addChild(label);
} else {
video=new Video(cam.width,cam.height);
video.attachCamera(cam);
now=new BitmapData(video.width,video.height);
out=new BitmapData(video.width,video.height);
diff=new BitmapData(video.width,video.height);
prev=new BitmapData(video.width,video.height);
var output=cam_holder.addChild(new Bitmap(out));
output.x=352;
output.y=0;
output.width=video.width*2;
output.height=video.height*2;
output.scaleX=-2;
myInt=setInterval(render,int(2000/camFPS));
}
}
function render() {
if (! cam.currentFPS) {
return;
}
now.draw(video);
diff.draw(video);
diff.draw(prev,null,null,"difference");
out.fillRect(new Rectangle(0,0,out.width,out.height),0xFF000000);
current_difference_amount=Number(out.threshold(diff,new Rectangle(0,0,diff.width,diff.height),new Point(0,0),">",diff_array[which_diff_value],0xFFFFFFFF));
prev.draw(video);
//
scan_pixels();
}
function scan_pixels() {
for (var ic=1; ic <=2; ic++) {
var iw_shift:Number;
if (ic==1) {
iw_shift=0;
}
if (ic==2) {
iw_shift=135;
}
var temp_pixel_data_prev:Number=0;
var temp_found_it:Boolean=false;
var ih:Number=1;
while (temp_found_it == false) {
for (var iw=(1+Number(iw_shift)); iw <= (40+Number(iw_shift)); iw++) {
var temp_pixel_data:Number=Number(out.getPixel(iw,ih));
if ((temp_pixel_data!=0)) {
//FOUND IT! yay.
temp_found_it=true;
this["level_bar_"+ic].y=this["level_bar_"+ic].y+Number(((ih*2)-this["level_bar_"+ic].y)/4);
if (ic==1) {
this["level_bar_"+ic].level_bar_inner.x = this["level_bar_"+ic].level_bar_inner.x + Number(((40-(iw*2))-this["level_bar_"+ic].level_bar_inner.x)/4);
hertz=440+(this["level_bar_"+ic].y*2);
amp = (((iw/10)-amp)/2);
if (amp<0.5) {
amp=0.5;
}
}
if (ic==2) {
volume_multip=0.5-(this["level_bar_"+ic].y/400);
if (volume_multip<0) {
volume_multip=0;
}
}
}
temp_pixel_data_prev=temp_pixel_data;
}
ih=Number(ih)+Number(1);
if (ih>camH) {
//nothing is found, but still end the loop
temp_found_it=true;
}
}
}
auto_calibrate();
}
help_button.addEventListener(MouseEvent.MOUSE_UP,help_button_pressed);
function help_button_pressed(event:MouseEvent) {
options_menu.visible=true;
}
options_menu.auto_calibrate_button.addEventListener(MouseEvent.MOUSE_UP,auto_calibrate_button_pressed);
function auto_calibrate_button_pressed(event:MouseEvent) {
auto_calibrate_switch=true;
auto_calibrate_loop=auto_calibrate_loop_max;
which_diff_value=0;
options_menu.visible=false;
}
options_menu.close_button.addEventListener(MouseEvent.MOUSE_UP,options_menu_close_button_pressed);
function options_menu_close_button_pressed(event:MouseEvent) {
options_menu.visible=false;
}
function auto_calibrate() {
if (auto_calibrate_switch) {
volume_multip = 0;
calibration_progress.visible=true;
if (auto_calibrate_loop<=0) {
which_diff_value=Number(which_diff_value)+Number(1);
var temp_difference_amount:Number=0;
for (var i4=0; i4 < difference_calibration_array.length; i4++) {
temp_difference_amount=Number(temp_difference_amount)+Number(difference_calibration_array[auto_calibrate_loop]);
}
temp_difference_amount=int(temp_difference_amount/auto_calibrate_loop_max);
trace(temp_difference_amount + " " + which_diff_value);
if (current_difference_amount<3) {
//found a good calibration.
auto_calibrate_switch=false;
}
if (which_diff_value>diff_array.length) {
auto_calibrate_switch=false;
}
auto_calibrate_loop=auto_calibrate_loop_max;
} else {
auto_calibrate_loop=auto_calibrate_loop-1;
}
difference_calibration_array[auto_calibrate_loop]=current_difference_amount;
} else {
calibration_progress.visible=false;
}
}
MotionCam();