User:Tolga Ozuygur/prototyping1: Difference between revisions
No edit summary |
mNo edit summary |
||
Line 11: | Line 11: | ||
---- | ---- | ||
<source lang="actionscript3" size= | <source lang="actionscript3" size=14> | ||
//Import Libraries | //Import Libraries | ||
import flash.media.Sound; | import flash.media.Sound; |
Revision as of 21:00, 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 lang="actionscript3" size=14> //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="NO WEBCAM"; addChild(label); } else { video=new Video(cam.width,cam.height); video.attachCamera(cam); //addChild(video);
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 previous = addChild(new Bitmap(prev)); //previous.x = video.width;
//var diffBitmap = addChild(new Bitmap(diff)); //diffBitmap.y = 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>