User:Tolga Ozuygur/prototyping1: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Prototyping]]
[[Category:2011 P1.01]]
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."
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."


Line 11: Line 14:
----
----


'''Source:'''
<source lang="actionscript3">
//Import Libraries
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;


import flash.media.Sound;<br>
//Prepare the variables
import flash.events.SampleDataEvent;<br>
var diff_array:Array = [0xFF000000,0xFF111111,0xFF151515,0xFF202020,0xFF252525,0xFF303030,0xFF353535,0xFF444444,0xFF555555,0xFF666666,0xFF777777,0xFF888888,0xFF999999];
import flash.events.MouseEvent;<br>
var which_diff_value:Number = 7;
import flash.display.Sprite;<br>
var auto_calibrate_loop:Number = 0;
import flash.display.StageAlign;<br>
var auto_calibrate_loop_max:Number = 20;
import flash.display.StageScaleMode;<br>
var auto_calibrate_switch:Boolean = false;
import flash.display.BitmapData;<br>
var current_difference_amount:Number = 0;
import flash.display.Bitmap;<br>
var difference_calibration_array:Array = [];
import flash.utils.setInterval;<br>
options_menu.visible = false;
import flash.text.TextField;<br>
calibration_progress.visible = false;
import flash.media.Camera;<br>
var current_pos:int=0;
import flash.media.Video;<br>
var sound:Sound = new Sound();
import flash.geom.Point;<br>
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, take_sample);
import flash.geom.Rectangle;<br>
sound.play();
var hertz:Number = 440;
var amp:Number = 1;
var volume_multip:Number = 0.25;
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;


var diff_array:Array = [0xFF000000,0xFF111111,0xFF151515,0xFF202020,0xFF252525,0xFF303030,0xFF353535,0xFF444444,0xFF555555,0xFF666666,0xFF777777,0xFF888888,0xFF999999];<br>
//Functions
var which_diff_value:Number = 7;<br>
function take_sample(event:SampleDataEvent):void {
var auto_calibrate_loop:Number = 0;<br>
for (var i:int = 0; i < 2048; i++) {
var auto_calibrate_loop_max:Number = 20;<br>
var phase:Number=current_pos/44100*Math.PI*2;
var auto_calibrate_switch:Boolean = false;<br>
current_pos++;
var current_difference_amount:Number = 0;<br>
var little_sample:Number=Math.sin(phase*int(hertz)*amp);
var difference_calibration_array:Array = [];<br>
event.data.writeFloat(little_sample*volume_multip);
options_menu.visible = false;<br>
event.data.writeFloat(little_sample*volume_multip);
calibration_progress.visible = false;<br>
}
var current_pos:int=0;<br>
}
var sound:Sound = new Sound();<br>
function MotionCam() {
var cam:Camera;<br>
cam=Camera.getCamera();
var video:Video;<br>
cam.setMode(camW, camH, camFPS);
var now:BitmapData;<br>
var out:BitmapData;<br>
var diff:BitmapData;<br>
var prev:BitmapData;<br>
var label:TextField;<br>
var myInt:Number;<br>
var camFPS:Number=15;<br>
var camW:Number=250;<br>
var camH:Number=190;<br>
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, take_sample);<br>
sound.play();<br>
var hertz:Number = 440;<br>
var amp:Number = 1;<br>
var volume_multip:Number = 0.25;<br>
function take_sample(event:SampleDataEvent):void {<br>
for (var i:int = 0; i < 2048; i++) {<br>
var phase:Number=current_pos/44100*Math.PI*2;<br>
current_pos++;<br>
var little_sample:Number=Math.sin(phase*int(hertz)*amp);<br>
event.data.writeFloat(little_sample*volume_multip);<br>
event.data.writeFloat(little_sample*volume_multip);<br>
}<br>
}<br>


function MotionCam() {<br>
if (cam==null) {
cam=Camera.getCamera();<br>
label = new TextField();
cam.setMode(camW, camH, camFPS);<br>
label.text="I don't sense a webcam.";
if (cam==null) {<br>
addChild(label);
label = new TextField();<br>
} else {
label.text="I don't sense a webcam.";<br>
video=new Video(cam.width,cam.height);
addChild(label);<br>
video.attachCamera(cam);
} 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>


function render() {<br>
now=new BitmapData(video.width,video.height);
if (! cam.currentFPS) {<br>
out=new BitmapData(video.width,video.height);
return;<br>
diff=new BitmapData(video.width,video.height);
}<br>
prev=new BitmapData(video.width,video.height);
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>
var output=cam_holder.addChild(new Bitmap(out));
for (var ic=1; ic <=2; ic++) {<br>
output.x=352;
var iw_shift:Number;<br>
output.y=0;
if (ic==1) {<br>
output.width=video.width*2;
iw_shift=0;<br>
output.height=video.height*2;
}<br>
output.scaleX=-2;
if (ic==2) {<br>
myInt=setInterval(render,int(2000/camFPS));
iw_shift=135;<br>
}
}<br>
}
var temp_pixel_data_prev:Number=0;<br>
function render() {
var temp_found_it:Boolean=false;<br>
if (! cam.currentFPS) {
var ih:Number=1;<br>
return;
while (temp_found_it == false) {<br>
}
for (var iw=(1+Number(iw_shift)); iw <= (40+Number(iw_shift)); iw++) {<br>
now.draw(video);
var temp_pixel_data:Number=Number(out.getPixel(iw,ih));<br>
diff.draw(video);
if ((temp_pixel_data!=0)) {<br>
diff.draw(prev,null,null,"difference");
//FOUND IT! yay.<br>
out.fillRect(new Rectangle(0,0,out.width,out.height),0xFF000000);
temp_found_it=true;<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));
this["level_bar_"+ic].y=this["level_bar_"+ic].y+Number(((ih*2)-this["level_bar_"+ic].y)/4);<br>
prev.draw(video);
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>
scan_pixels();
hertz=440+(this["level_bar_"+ic].y*2);<br>
}
amp = (((iw/10)-amp)/2);<br>
function scan_pixels() {
if (amp<0.5) {<br>
for (var ic=1; ic <=2; ic++) {
amp=0.5;<br>
var iw_shift:Number;
}<br>
if (ic==1) {
}<br>
iw_shift=0;
if (ic==2) {<br>
}
volume_multip=0.5-(this["level_bar_"+ic].y/400);<br>
if (ic==2) {
if (volume_multip<0) {<br>
iw_shift=135;
volume_multip=0;<br>
}
}<br>
var temp_pixel_data_prev:Number=0;
}<br>
var temp_found_it:Boolean=false;
}<br>
var ih:Number=1;
temp_pixel_data_prev=temp_pixel_data;<br>
while (temp_found_it == false) {
}<br>
for (var iw=(1+Number(iw_shift)); iw <= (40+Number(iw_shift)); iw++) {
ih=Number(ih)+Number(1);<br>
var temp_pixel_data:Number=Number(out.getPixel(iw,ih));
if (ih>camH) {<br>
if ((temp_pixel_data!=0)) {
//nothing is found, but still end the loop<br>
//FOUND IT!
temp_found_it=true;<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);
auto_calibrate();<br>
hertz=440+(this["level_bar_"+ic].y*2);
}<br>
amp = (((iw/10)-amp)/2);
help_button.addEventListener(MouseEvent.MOUSE_UP,help_button_pressed);<br>
if (amp<0.5) {
function help_button_pressed(event:MouseEvent) {<br>
amp=0.5;
options_menu.visible=true;<br>
}
}<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>
volume_multip=0.5-(this["level_bar_"+ic].y/400);
auto_calibrate_switch=true;<br>
if (volume_multip<0) {
auto_calibrate_loop=auto_calibrate_loop_max;<br>
volume_multip=0;
which_diff_value=0;<br>
}
options_menu.visible=false;<br>
}
}<br>
}
options_menu.close_button.addEventListener(MouseEvent.MOUSE_UP,options_menu_close_button_pressed);<br>
temp_pixel_data_prev=temp_pixel_data;
function options_menu_close_button_pressed(event:MouseEvent) {<br>
}
options_menu.visible=false;<br>
ih=Number(ih)+Number(1);
}<br>
if (ih>camH) {
function auto_calibrate() {<br>
//nothing is found, but still end the loop
if (auto_calibrate_switch) {<br>
temp_found_it=true;
volume_multip = 0;<br>
}
calibration_progress.visible=true;<br>
}
if (auto_calibrate_loop<=0) {<br>
}
which_diff_value=Number(which_diff_value)+Number(1);<br>
auto_calibrate();
var temp_difference_amount:Number=0;<br>
}
for (var i4=0; i4 < difference_calibration_array.length; i4++) {<br>
function auto_calibrate() {
temp_difference_amount=Number(temp_difference_amount)+Number(difference_calibration_array[auto_calibrate_loop]);<br>
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;
}
}
temp_difference_amount=int(temp_difference_amount/auto_calibrate_loop_max);<br>
auto_calibrate_loop=auto_calibrate_loop_max;
trace(temp_difference_amount + " " + which_diff_value);<br>
} else {
if (current_difference_amount<3) {<br>
auto_calibrate_loop=auto_calibrate_loop-1;
//found a good calibration.<br>
}
auto_calibrate_switch=false;<br>
difference_calibration_array[auto_calibrate_loop]=current_difference_amount;
}<br>
} else {
if (which_diff_value>diff_array.length) {<br>
calibration_progress.visible=false;
auto_calibrate_switch=false;<br>
}
}<br>
}
auto_calibrate_loop=auto_calibrate_loop_max;<br>
 
} else {<br>
//Interface listeners and functions
auto_calibrate_loop=auto_calibrate_loop-1;<br>
help_button.addEventListener(MouseEvent.MOUSE_UP,help_button_pressed);
}<br>
function help_button_pressed(event:MouseEvent) {
difference_calibration_array[auto_calibrate_loop]=current_difference_amount;<br>
options_menu.visible=true;
} else {<br>
}
calibration_progress.visible=false;<br>
options_menu.auto_calibrate_button.addEventListener(MouseEvent.MOUSE_UP,auto_calibrate_button_pressed);
}<br>
function auto_calibrate_button_pressed(event:MouseEvent) {
}<br>
auto_calibrate_switch=true;
MotionCam();<br>
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;
}
 
//Finally, run it!
MotionCam();
 
<source>

Latest revision as of 10:43, 19 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 lang="actionscript3"> //Import Libraries 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;

//Prepare the variables 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(); sound.addEventListener(SampleDataEvent.SAMPLE_DATA, take_sample); sound.play(); var hertz:Number = 440; var amp:Number = 1; var volume_multip:Number = 0.25; 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;

//Functions 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! 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(); } 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; } }

//Interface listeners and functions 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; }

//Finally, run it! MotionCam();

<source>