Difference between revisions of "Team:NYU Shanghai/Code"
Line 448: | Line 448: | ||
<br>} | <br>} | ||
− | int instrument(int n){ | + | <br><br>int instrument(int n){ |
− | int h=int(hue(color(colors[n][0],colors[n][1], | + | <br>int h=int(hue(color(colors[n][0],colors[n][1], |
− | colors[n][2]))%6); | + | <br><br>colors[n][2]))%6); |
− | if (h==0){return 38;} | + | <br>if (h==0){return 38;} |
− | + | <br>else if (h==1){return 103;} | |
− | else if (h==2){return 112;} | + | <br>else if (h==2){return 112;} |
− | else if (h==3){return 113;} | + | <br>else if (h==3){return 113;} |
− | else if (h==4){return 117;} | + | <br>else if (h==4){return 117;} |
− | else {return 124;} | + | <br>else {return 124;} |
− | } | + | <br>} |
− | int noteVal(int n){ | + | <br><br>int noteVal(int n){ |
− | int h=int(hue(color(colors[n][0],colors[n][1], | + | <br>int h=int(hue(color(colors[n][0],colors[n][1], |
− | colors[n][2]))%6); | + | <br>colors[n][2]))%6); |
− | if (h==0){return 14;} | + | <br>if (h==0){return 14;} |
− | else if (h==1){return 20;} | + | <br>else if (h==1){return 20;} |
− | else if (h==2){return 24;} | + | <br>else if (h==2){return 24;} |
− | else if (h==3){return 60;} | + | <br>else if (h==3){return 60;} |
− | else if (h==4){return 96;} | + | <br>else if (h==4){return 96;} |
− | else {return 60;} | + | <br>else {return 60;} |
− | } | + | <br>} |
− | void volume_up() { | + | <br><br>void volume_up() { |
− | volume=constrain(volume+5, 0, 127); | + | <br>volume=constrain(volume+5, 0, 127); |
− | } | + | <br>} |
− | void volume_down() { | + | <br><br>void volume_down() { |
− | volume=constrain(volume-5, 0, 127); | + | <br>volume=constrain(volume-5, 0, 127); |
− | } | + | <br>} |
− | } | + | <br>} |
− | //step sequencer | + | <br><br>//step sequencer |
− | class Sequencer { | + | <br>class Sequencer { |
− | boolean[] empty; | + | <br><br>boolean[] empty; |
− | float[] rgb_list; | + | <br>float[] rgb_list; |
− | int grid_num; | + | <br>int grid_num; |
− | int grid_size; | + | <br>int grid_size; |
− | int interval; | + | <br>int interval; |
− | SoundCipher sc; | + | <br><br>SoundCipher sc; |
− | boolean select=false; | + | <br>boolean select=false; |
− | int a, b; | + | <br>int a, b; |
− | int volume=0; | + | <br>int volume=0; |
− | boolean on=true; | + | <br>boolean on=true; |
− | float red=0; | + | <br>float red=0; |
− | float green=0; | + | <br>float green=0; |
− | float blue=0; | + | <br>float blue=0; |
− | int[] notes = { | + | <br>int[] notes = { |
− | 96, 93, 91, 89, 86, 84, 81, 79, 77, 74, 72, 69, 67, 65, 62, 60 | + | <br>96, 93, 91, 89, 86, 84, 81, 79, 77, 74, 72, 69, 67, 65, 62, 60 |
− | }; | + | <br>}; |
− | boolean[] sequence; | + | <br>boolean[] sequence; |
− | Sequencer(SoundCipher _sc, | + | <br><br>Sequencer(SoundCipher _sc, |
− | int _grid_num, int _grid_size, int _interval, int _a, int _b) { | + | <br>int _grid_num, int _grid_size, int _interval, int _a, int _b) { |
− | sc=_sc; | + | <br>sc=_sc; |
− | + | <br>grid_num=_grid_num; | |
− | grid_size=_grid_size; | + | <br>grid_size=_grid_size; |
− | interval=_interval; | + | <br>interval=_interval; |
− | a=_a; | + | <br><br>a=_a; |
− | b=_b; | + | <br>b=_b; |
− | sequence=new boolean[grid_num*grid_num]; | + | <br><br>sequence=new boolean[grid_num*grid_num]; |
− | for (int i=0; i<sequence.length; i++) { | + | <br>for (int i=0; i<sequence.length; i++) { |
− | sequence[i]=false; | + | <br>sequence[i]=false; |
− | } | + | <br>} |
− | empty=new boolean[grid_num*grid_num]; | + | <br><br>empty=new boolean[grid_num*grid_num]; |
− | for (int i=0; i<sequence.length; i++) { | + | <br>for (int i=0; i<sequence.length; i++) { |
− | empty[i]=false; | + | <br>empty[i]=false; |
− | } | + | <br>} |
− | } | + | <br>} |
− | void get_sequence(boolean[] _sequence) { | + | <br><br>void get_sequence(boolean[] _sequence) { |
− | sequence=_sequence; | + | <br>sequence=_sequence; |
− | } | + | <br>} |
− | void get_color(float[] _rgb_list) { | + | <br><br>void get_color(float[] _rgb_list) { |
− | rgb_list=_rgb_list; | + | <br>rgb_list=_rgb_list; |
− | red=rgb_list[0]; | + | <br>red=rgb_list[0]; |
− | green=rgb_list[1]; | + | <br>green=rgb_list[1]; |
− | blue=rgb_list[2]; | + | <br>blue=rgb_list[2]; |
− | } | + | <br>} |
− | void draw_grid() { | + | <br><br>void draw_grid() { |
− | pushMatrix(); | + | <br>pushMatrix(); |
− | translate(a, b); | + | <br>translate(a, b); |
− | rectMode(CORNER); | + | <br>rectMode(CORNER); |
− | int x=0; | + | <br>int x=0; |
− | int y=0; | + | <br>int y=0; |
− | + | <br><br>//black background | |
− | noStroke(); | + | <br>noStroke(); |
− | fill(0); | + | <br>fill(0); |
− | rect(-interval, -interval, (grid_size+interval)*grid_num+interval*2, | + | <br>rect(-interval, -interval, (grid_size+interval)*grid_num+interval*2, |
− | (grid_size+interval)*grid_num+interval*2); | + | <br>(grid_size+interval)*grid_num+interval*2); |
− | for (int i=0; i<sequence.length; i++) { | + | <br><br>for (int i=0; i<sequence.length; i++) { |
− | x=(grid_size+interval)*(i%grid_num); | + | <br>x=(grid_size+interval)*(i%grid_num); |
− | y=(grid_size+interval)*(i/grid_num); | + | <br>y=(grid_size+interval)*(i/grid_num); |
− | if (sequence[i]) { | + | <br>if (sequence[i]) { |
− | fill(red, green, blue); | + | <br>fill(red, green, blue); |
− | } else { | + | <br>} else { |
− | if (select){fill(100);} | + | <br>if (select){fill(100);} |
− | else {fill(70);} | + | <br>else {fill(70);} |
− | } | + | <br>} |
− | rect(x, y, grid_size, grid_size); | + | <br>rect(x, y, grid_size, grid_size); |
− | } | + | <br>} |
− | popMatrix(); | + | <br><br>popMatrix(); |
− | } | + | <br>} |
− | void glow(int n) { | + | <br><br>void glow(int n) { |
− | pushMatrix(); | + | <br>pushMatrix(); |
− | translate(a, b); | + | <br>translate(a, b); |
− | rectMode(CORNER); | + | <br><br>rectMode(CORNER); |
− | for (int m=0; m<grid_num; m++) { | + | <br>for (int m=0; m<grid_num; m++) { |
− | if (sequence[m*grid_num+n]) { | + | <br>if (sequence[m*grid_num+n]) { |
− | for (int p=-1; p<2; p++) { | + | <br>for (int p=-1; p<2; p++) { |
− | for (int q=-1; q<2; q++) { | + | <br>for (int q=-1; q<2; q++) { |
− | if (n+p>=0&&n+p<grid_num&&m+q>=0&& | + | <br>if (n+p>=0&&n+p<grid_num&&m+q>=0&& |
− | m+q<grid_num) { | + | <br>m+q<grid_num) { |
− | noStroke(); | + | <br>noStroke(); |
− | fill(255, 255, 255, 20); | + | <br>fill(255, 255, 255, 20); |
− | + | <br>rect((n+p)*(interval+grid_size), (m+q)*(interval+grid_size), | |
− | grid_size, grid_size); | + | <br>grid_size, grid_size); |
− | } | + | <br>} |
− | if (p==0&&q==0){ | + | <br>if (p==0&&q==0){ |
− | fill(red, green, blue); | + | <br>fill(red, green, blue); |
− | rect(n*(interval+grid_size)-interval/2, m*(interval+grid_size)-interval/2, | + | <br>rect(n*(interval+grid_size)-interval/2, m*(interval+grid_size)-interval/2, |
− | grid_size+interval, grid_size+interval); | + | <br>grid_size+interval, grid_size+interval); |
− | fill(255, 255, 255, 30); | + | <br>fill(255, 255, 255, 30); |
− | rect(n*(interval+grid_size)-interval/2, m*(interval+grid_size)-interval/2, | + | <br>rect(n*(interval+grid_size)-interval/2, m*(interval+grid_size)-interval/2, |
− | grid_size+interval, grid_size+interval); | + | <br>grid_size+interval, grid_size+interval); |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | popMatrix(); | + | <br>popMatrix(); |
− | } | + | <br>} |
− | void play_sounds(int n) { | + | <br><br>void play_sounds(int n) { |
− | float noteVal; | + | <br>float noteVal; |
− | float[] noteVals=new float[0]; | + | <br>float[] noteVals=new float[0]; |
− | for (int i=0; i<grid_num; i++) { | + | <br><br>for (int i=0; i<grid_num; i++) { |
− | // if the data text file has a "1" play note | + | <br><br>// if the data text file has a "1" play note |
− | if (sequence[i*grid_num+n]) { | + | <br>if (sequence[i*grid_num+n]) { |
− | noteVal = float(notes[i]); | + | <br>noteVal = float(notes[i]); |
− | noteVals=append(noteVals, noteVal); | + | <br>noteVals=append(noteVals, noteVal); |
− | } | + | <br>} |
− | } | + | <br><br>} |
− | sc.instrument(instrument()); | + | <br>sc.instrument(instrument()); |
− | sc.playChord(noteVals, volume, 0.25); | + | <br>sc.playChord(noteVals, volume, 0.25); |
− | } | + | <br>} |
− | int instrument(){ | + | <br><br>int instrument(){ |
− | int h=int(hue(color(red,green,blue))%8); | + | <br>int h=int(hue(color(red,green,blue))%8); |
− | if (h==0){return 2;} | + | <br>if (h==0){return 2;} |
− | else if (h==1){return 3;} | + | <br>else if (h==1){return 3;} |
− | else if (h==2){return 38;} | + | <br>else if (h==2){return 38;} |
− | else if (h==3){return 46;} | + | <br>else if (h==3){return 46;} |
− | else if (h==4){return 47;} | + | <br>else if (h==4){return 47;} |
− | else if (h==5){return 55;} | + | <br>else if (h==5){return 55;} |
− | else if (h==6){return 116;} | + | <br>else if (h==6){return 116;} |
− | else {return 120;} | + | <br>else {return 120;} |
− | } | + | <br>} |
− | void select(boolean _select) { | + | <br><br>void select(boolean _select) { |
− | select=_select; | + | <br>select=_select; |
− | } | + | <br>} |
− | void set_volume(int _volume) { | + | <br><br>void set_volume(int _volume) { |
− | volume=constrain(_volume, 0, 127); | + | <br>volume=constrain(_volume, 0, 127); |
− | } | + | <br>} |
− | void delete() { | + | <br><br>void delete() { |
− | sequence=empty; | + | <br>sequence=empty; |
− | } | + | <br>} |
− | } | + | <br>} |
− | //step sequencer video | + | <br>//step sequencer video |
− | class Sequencer_video { | + | <br>class Sequencer_video { |
− | Capture video; | + | <br>Capture video; |
− | SoundCipher sc; | + | <br>SoundCipher sc; |
− | BlobDetection theBlobDetection; | + | <br>BlobDetection theBlobDetection; |
− | boolean newFrame=false; | + | <br>boolean newFrame=false; |
− | PImage img; | + | <br>PImage img; |
− | int a, b; | + | <br>int a, b; |
− | int grid_size; | + | <br>int grid_size; |
− | int grid_num; | + | <br>int grid_num; |
− | int interval; | + | <br>int interval; |
− | int block_size; | + | <br>int block_size; |
− | int volume=0; | + | <br>int volume=0; |
− | float[][] blob_color; | + | <br>float[][] blob_color; |
− | float[] avg_color=new float[3]; | + | <br>float[] avg_color=new float[3]; |
− | float[][] blobs; | + | <br>float[][] blobs; |
− | float[][][] raw_color; | + | <br>float[][][] raw_color; |
− | float threshold=0.5; | + | <br>float threshold=0.5; |
− | boolean draw_grid=false; | + | <br>boolean draw_grid=false; |
− | boolean[] sequence; | + | <br>boolean[] sequence; |
− | int[] notes = { | + | <br>int[] notes = { |
− | + | <br>, 93, 91, 89, 86, 84, 81, 79, 77, 74, 72, 69, 67, 65, 62, 60 | |
− | }; | + | <br>}; |
− | int radius=180; | + | <br>int radius=180; |
− | Sequencer_video(Capture _video, SoundCipher _sc, int _grid_num, | + | <br><br>Sequencer_video(Capture _video, SoundCipher _sc, int _grid_num, |
− | int _grid_size,int _interval) { | + | <br>int _grid_size,int _interval) { |
− | colorMode(RGB, 255, 255, 255, 100); | + | <br>colorMode(RGB, 255, 255, 255, 100); |
− | video=_video; | + | <br>video=_video; |
− | sc=_sc; | + | <br>sc=_sc; |
− | grid_num=_grid_num; | + | <br>grid_num=_grid_num; |
− | grid_size=_grid_size; | + | <br>grid_size=_grid_size; |
− | interval=_interval; | + | <br>interval=_interval; |
− | block_size=(interval+grid_size)*grid_num; | + | <br>block_size=(interval+grid_size)*grid_num; |
− | sequence=new boolean[grid_num*grid_num]; | + | <br>sequence=new boolean[grid_num*grid_num]; |
− | img = new PImage(grid_num*(grid_size+interval)-interval, | + | <br>img = new PImage(grid_num*(grid_size+interval)-interval, |
− | grid_num*(grid_size+interval)-interval); | + | <br>grid_num*(grid_size+interval)-interval); |
− | theBlobDetection = new BlobDetection(img.width, img.height); | + | <br>theBlobDetection = new BlobDetection(img.width, img.height); |
− | theBlobDetection.setPosDiscrimination(true); | + | <br>theBlobDetection.setPosDiscrimination(true); |
− | } | + | <br>} |
− | void threshold_up() { | + | <br><br>void threshold_up() { |
− | threshold=constrain(threshold+0.02, 0, 1); | + | <br>threshold=constrain(threshold+0.02, 0, 1); |
− | println(threshold); | + | <br>println(threshold); |
− | } | + | <br>} |
− | void threshold_down() { | + | <br><br>void threshold_down() { |
− | threshold=constrain(threshold-0.02, 0, 1); | + | <br>threshold=constrain(threshold-0.02, 0, 1); |
− | println(threshold); | + | <br>println(threshold); |
− | } | + | <br>} |
− | void position(int _a, int _b) { | + | <br><br>void position(int _a, int _b) { |
− | a=_a; | + | <br>a=_a; |
− | b=_b; | + | <br>b=_b; |
− | } | + | <br>} |
− | void grid_on() { | + | <br><br>void grid_on() { |
− | draw_grid=true; | + | <br>draw_grid=true; |
− | } | + | <br>} |
− | void grid_off(){ | + | <br><br>void grid_off(){ |
− | draw_grid=false; | + | <br>draw_grid=false; |
− | } | + | <br>} |
− | void bd() { | + | <br><br>void bd() { |
− | if (video.available()) { | + | <br>if (video.available()) { |
− | theBlobDetection.setThreshold(threshold); | + | <br>theBlobDetection.setThreshold(threshold); |
− | video.read(); | + | <br>video.read(); |
− | img.copy(video, (video.width-video.height)/2, 0, video.height, | + | <br>img.copy(video, (video.width-video.height)/2, 0, video.height, |
− | + | <br>video.height, 0, 0, img.width, img.height); | |
− | image(img, a, b, img.width+interval, img.height+interval); | + | <br>image(img, a, b, img.width+interval, img.height+interval); |
− | fastblur(img, 2); | + | <br>fastblur(img, 2); |
− | theBlobDetection.computeBlobs(img.pixels); | + | <br>theBlobDetection.computeBlobs(img.pixels); |
− | save_blob(); | + | <br>save_blob(); |
− | blob_color(); | + | <br>blob_color(); |
− | convert(); | + | <br>convert(); |
− | } | + | <br>} |
− | } | + | <br>} |
− | void bd_draw(int n) { | + | <br>void bd_draw(int n) { |
− | if (draw_grid) { | + | <br>if (draw_grid) { |
− | draw_grid(); | + | <br>draw_grid(); |
− | glow(n); | + | <br>glow(n); |
− | play_sounds(n); | + | <br>play_sounds(n); |
− | } else { | + | <br>} else { |
− | drawBlobsAndEdges(true, false); | + | <br>drawBlobsAndEdges(true, false); |
− | } | + | <br>} |
− | } | + | <br>} |
− | void blob_color() { | + | <br><br>void blob_color() { |
− | float red=0; | + | <br>float red=0; |
− | float blue=0; | + | <br>float blue=0; |
− | float green=0; | + | <br>float green=0; |
− | int total=theBlobDetection.getBlobNb (); | + | <br>int total=theBlobDetection.getBlobNb (); |
− | for (int n=0; n<total; n++) { | + | <br>for (int n=0; n<total; n++) { |
− | //get the color | + | <br><br>//get the color |
− | blob_color=new float[total][3]; | + | <br><br><br>blob_color=new float[total][3]; |
− | int centerX = int(blobs[n][0]+0.5*blobs[n][2]); | + | <br><br><br><br>int centerX = int(blobs[n][0]+0.5*blobs[n][2]); |
− | int centerY = int(blobs[n][1]+0.5*blobs[n][3]); | + | <br>int centerY = int(blobs[n][1]+0.5*blobs[n][3]); |
− | color c = get(centerX+a, centerY+b); | + | <br><br>color c = get(centerX+a, centerY+b); |
− | blob_color[n][0] = red(c); | + | <br>blob_color[n][0] = red(c); |
− | blob_color[n][1] = green(c); | + | <br><br>blob_color[n][1] = green(c); |
− | blob_color[n][2] = blue(c); | + | <br>blob_color[n][2] = blue(c); |
− | red+=blob_color[n][0]; | + | <br><br>red+=blob_color[n][0]; |
− | green+=blob_color[n][1]; | + | <br>green+=blob_color[n][1]; |
− | blue+=blob_color[n][2]; | + | <br>blue+=blob_color[n][2]; |
− | } | + | <br>} |
− | avg_color[0]=constrain(red/total*1.2,0,255); | + | <br>avg_color[0]=constrain(red/total*1.2,0,255); |
− | avg_color[1]=constrain(green/total*1.2,0,255); | + | <br>avg_color[1]=constrain(green/total*1.2,0,255); |
− | avg_color[2]=constrain(blue/total*1.2,0,255); | + | <br>avg_color[2]=constrain(blue/total*1.2,0,255); |
− | } | + | <br>} |
− | void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges) { | + | <br><br>void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges) { |
− | pushMatrix(); | + | <br>pushMatrix(); |
− | translate(a, b); | + | <br><br>translate(a, b); |
− | noFill(); | + | <br><br><br>noFill(); |
− | rectMode(CORNER); | + | <br><br><br><br>rectMode(CORNER); |
− | Blob b; | + | <br>Blob b; |
− | EdgeVertex eA, eB; | + | <br>EdgeVertex eA, eB; |
− | for (int n=0; n<theBlobDetection.getBlobNb (); n++) | + | <br><br>for (int n=0; n<theBlobDetection.getBlobNb (); n++) |
− | { | + | <br>{ |
− | b=theBlobDetection.getBlob(n); | + | <br>b=theBlobDetection.getBlob(n); |
− | if (b!=null) | + | <br>if (b!=null) |
− | { | + | <br>{ |
− | // Edges | + | <br>// Edges |
− | if (drawEdges) | + | <br>if (drawEdges) |
− | { | + | <br>{ |
− | strokeWeight(3); | + | <br>strokeWeight(3); |
− | stroke(0, 255, 0); | + | <br><br>stroke(0, 255, 0); |
− | for (int m=0; m<b.getEdgeNb (); m++) | + | <br>for (int m=0; m<b.getEdgeNb (); m++) |
− | { | + | <br>{ |
− | eA = b.getEdgeVertexA(m); | + | <br>eA = b.getEdgeVertexA(m); |
− | eB = b.getEdgeVertexB(m); | + | <br>eB = b.getEdgeVertexB(m); |
− | if (eA !=null && eB !=null) | + | <br>if (eA !=null && eB !=null) |
− | line( | + | <br>line( |
− | eA.x*block_size, eA.y*block_size, | + | <br><br>eA.x*block_size, eA.y*block_size, |
− | eB.x*block_size, eB.y*block_size | + | <br><br><br>eB.x*block_size, eB.y*block_size |
− | ); | + | <br>); |
− | } | + | <br>} |
− | } | + | <br>} |
− | // Blobs | + | <br><br>// Blobs |
− | if (dist(block_size/2,block_size/2,b.xMin*block_size, | + | <br>if (dist(block_size/2,block_size/2,b.xMin*block_size, |
− | b.yMin*block_size)<radius | + | <br>b.yMin*block_size)<radius |
− | &&drawBlobs&&b.w*block_size>5&&b.w*block_size<25&& | + | <br>&&drawBlobs&&b.w*block_size>5&&b.w*block_size<25&& |
− | b.h*block_size>5&&b.h*block_size<25) | + | <br>b.h*block_size>5&&b.h*block_size<25) |
− | { | + | <br>{ |
− | strokeWeight(3); | + | <br>strokeWeight(3); |
− | stroke(255, 0, 0); | + | <br>stroke(255, 0, 0); |
− | rect( | + | <br><br>rect( |
− | b.xMin*block_size, b.yMin*block_size, | + | <br><br><br>b.xMin*block_size, b.yMin*block_size, |
− | b.w*block_size, b.h*block_size | + | <br><br><br><br>b.w*block_size, b.h*block_size |
− | ); | + | <br>); |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | + | <br> popMatrix(); | |
− | } | + | <br><br>} |
− | void fastblur(PImage img, int radius) | + | <br><br>void fastblur(PImage img, int radius) |
− | { | + | <br>{ |
− | if (radius<1) { | + | <br>if (radius<1) { |
− | return; | + | <br>return; |
− | } | + | <br>} |
− | int w=img.width; | + | <br>int w=img.width; |
− | int h=img.height; | + | <br>int h=img.height; |
− | int wm=w-1; | + | <br>int wm=w-1; |
− | int hm=h-1; | + | <br>int hm=h-1; |
− | int wh=w*h; | + | <br>int wh=w*h; |
− | int div=radius+radius+1; | + | <br>int div=radius+radius+1; |
− | int r[]=new int[wh]; | + | <br>int r[]=new int[wh]; |
− | int g[]=new int[wh]; | + | <br>int g[]=new int[wh]; |
− | int b[]=new int[wh]; | + | <br>int b[]=new int[wh]; |
− | int rsum, gsum, bsum, x, y, i, p, p1, p2, yp, yi, yw; | + | <br>int rsum, gsum, bsum, x, y, i, p, p1, p2, yp, yi, yw; |
− | int vmin[] = new int[max(w, h)]; | + | <br>int vmin[] = new int[max(w, h)]; |
− | int vmax[] = new int[max(w, h)]; | + | <br>int vmax[] = new int[max(w, h)]; |
− | int[] pix=img.pixels; | + | <br>int[] pix=img.pixels; |
− | int dv[]=new int[256*div]; | + | <br>int dv[]=new int[256*div]; |
− | for (i=0; i<256*div; i++) { | + | <br>for (i=0; i<256*div; i++) { |
− | dv[i]=(i/div); | + | <br>dv[i]=(i/div); |
− | } | + | <br>} |
− | yw=yi=0; | + | <br><br>yw=yi=0; |
− | for (y=0; y<h; y++) { | + | <br><br>for (y=0; y<h; y++) { |
− | rsum=gsum=bsum=0; | + | <br>rsum=gsum=bsum=0; |
− | for (i=-radius; i<=radius; i++) { | + | <br>for (i=-radius; i<=radius; i++) { |
− | p=pix[yi+min(wm, max(i, 0))]; | + | <br>p=pix[yi+min(wm, max(i, 0))]; |
− | rsum+=(p & 0xff0000)>>16; | + | <br>rsum+=(p & 0xff0000)>>16; |
− | gsum+=(p & 0x00ff00)>>8; | + | <br>gsum+=(p & 0x00ff00)>>8; |
− | bsum+= p & 0x0000ff; | + | <br>bsum+= p & 0x0000ff; |
− | } | + | <br>} |
− | for (x=0; x<w; x++) { | + | <br>for (x=0; x<w; x++) { |
− | r[yi]=dv[rsum]; | + | <br><br>r[yi]=dv[rsum]; |
− | g[yi]=dv[gsum]; | + | <br>g[yi]=dv[gsum]; |
− | b[yi]=dv[bsum]; | + | <br>b[yi]=dv[bsum]; |
− | + | <br><br>if (y==0) { | |
− | vmin[x]=min(x+radius+1, wm); | + | <br>vmin[x]=min(x+radius+1, wm); |
− | + | <br>vmax[x]=max(x-radius, 0); | |
− | } | + | <br>} |
− | p1=pix[yw+vmin[x]]; | + | <br>p1=pix[yw+vmin[x]]; |
− | p2=pix[yw+vmax[x]]; | + | <br><br>p2=pix[yw+vmax[x]]; |
− | rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16; | + | <br><br>rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16; |
− | gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8; | + | <br>gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8; |
− | bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff); | + | <br><br><br>bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff); |
− | yi++; | + | <br>yi++; |
− | } | + | <br>} |
− | yw+=w; | + | <br>yw+=w; |
− | + | <br> } | |
− | for (x=0; x<w; x++) { | + | <br><br>for (x=0; x<w; x++) { |
− | rsum=gsum=bsum=0; | + | <br>rsum=gsum=bsum=0; |
− | yp=-radius*w; | + | <br>yp=-radius*w; |
− | for (i=-radius; i<=radius; i++) { | + | <br>for (i=-radius; i<=radius; i++) { |
− | yi=max(0, yp)+x; | + | <br>yi=max(0, yp)+x; |
− | rsum+=r[yi]; | + | <br>rsum+=r[yi]; |
− | gsum+=g[yi]; | + | <br>gsum+=g[yi]; |
− | bsum+=b[yi]; | + | <br>bsum+=b[yi]; |
− | yp+=w; | + | <br>yp+=w; |
− | } | + | <br>} |
− | yi=x; | + | <br>yi=x; |
− | for (y=0; y<h; y++) { | + | <br>for (y=0; y<h; y++) { |
− | pix[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum]; | + | <br>pix[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum]; |
− | if (x==0) { | + | <br>if (x==0) { |
− | vmin[y]=min(y+radius+1, hm)*w; | + | <br>vmin[y]=min(y+radius+1, hm)*w; |
− | vmax[y]=max(y-radius, 0)*w; | + | <br>vmax[y]=max(y-radius, 0)*w; |
− | } | + | <br>} |
− | p1=x+vmin[y]; | + | <br>p1=x+vmin[y]; |
− | p2=x+vmax[y]; | + | <br>p2=x+vmax[y]; |
− | rsum+=r[p1]-r[p2]; | + | <br><br>rsum+=r[p1]-r[p2]; |
− | gsum+=g[p1]-g[p2]; | + | <br>gsum+=g[p1]-g[p2]; |
− | bsum+=b[p1]-b[p2]; | + | <br>bsum+=b[p1]-b[p2]; |
− | yi+=w; | + | <br><br>yi+=w; |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | void save_blob() { | + | <br><br>void save_blob() { |
− | Blob b; | + | <br><br>Blob b; |
− | blobs=new float[theBlobDetection.getBlobNb ()][4]; | + | <br>blobs=new float[theBlobDetection.getBlobNb ()][4]; |
− | for (int n=0; n<theBlobDetection.getBlobNb (); n++) | + | <br>for (int n=0; n<theBlobDetection.getBlobNb (); n++) |
− | { | + | <br>{ |
− | b=theBlobDetection.getBlob(n); | + | <br>b=theBlobDetection.getBlob(n); |
− | if (dist(block_size/2,block_size/2,b.xMin*block_size, | + | <br>if (dist(block_size/2,block_size/2,b.xMin*block_size, |
− | b.yMin*block_size)<radius | + | <br>b.yMin*block_size)<radius |
− | &&b.w*block_size>5&&b.w*block_size<25&& | + | <br>&&b.w*block_size>5&&b.w*block_size<25&& |
− | b.h*block_size>5&&b.h*block_size<25){ | + | <br>b.h*block_size>5&&b.h*block_size<25){ |
− | blobs[n][0]=b.xMin*block_size; | + | <br>blobs[n][0]=b.xMin*block_size; |
− | blobs[n][1]=b.yMin*block_size; | + | <br>blobs[n][1]=b.yMin*block_size; |
− | blobs[n][2]=b.w*block_size; | + | <br>blobs[n][2]=b.w*block_size; |
− | blobs[n][3]=b.h*block_size; | + | <br>blobs[n][3]=b.h*block_size; |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | void convert() { | + | <br><br>void convert() { |
− | int row, col, i; | + | <br>int row, col, i; |
− | for (int n=0; n<sequence.length; n++) { | + | <br>for (int n=0; n<sequence.length; n++) { |
− | sequence[n]=false; | + | <br>sequence[n]=false; |
− | } | + | <br>} |
− | for (int n=0; n<blobs.length; n++) { | + | <br>for (int n=0; n<blobs.length; n++) { |
− | row=int(blobs[n][0]+blobs[n][2]*0.5)/grid_size; | + | <br>row=int(blobs[n][0]+blobs[n][2]*0.5)/grid_size; |
− | col=int(blobs[n][1]+blobs[n][3]*0.5)/grid_size; | + | <br>col=int(blobs[n][1]+blobs[n][3]*0.5)/grid_size; |
− | i=row+col*grid_num; | + | <br>i=row+col*grid_num; |
− | + | <br>if (i>0&&i<grid_num*grid_num) { | |
− | sequence[i]=true; | + | <br>sequence[i]=true; |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | void draw_grid() { | + | <br><br>void draw_grid() { |
− | pushMatrix(); | + | <br>pushMatrix(); |
− | translate(a, b); | + | <br>translate(a, b); |
− | rectMode(CORNER); | + | <br>rectMode(CORNER); |
− | int x=0; | + | <br>int x=0; |
− | int y=0; | + | <br>int y=0; |
− | noStroke(); | + | <br>noStroke(); |
− | fill(0); | + | <br>fill(0); |
− | rect(-interval,-interval,(grid_size+interval)*grid_num+interval*2, | + | <br>rect(-interval,-interval,(grid_size+interval)*grid_num+interval*2, |
− | (grid_size+interval)*grid_num+interval*2); | + | <br>(grid_size+interval)*grid_num+interval*2); |
− | for (int i=0; i<sequence.length; i++) { | + | <br>for (int i=0; i<sequence.length; i++) { |
− | x=(grid_size+interval)*(i%grid_num); | + | <br>x=(grid_size+interval)*(i%grid_num); |
− | y=(grid_size+interval)*(i/grid_num); | + | <br>y=(grid_size+interval)*(i/grid_num); |
− | noStroke(); | + | <br><br>noStroke(); |
− | if (sequence[i]) { | + | <br><br>if (sequence[i]) { |
− | fill(avg_color[0], avg_color[1], avg_color[2]); | + | <br>fill(avg_color[0], avg_color[1], avg_color[2]); |
− | } else { | + | <br>} else { |
− | + | <br>fill(70); | |
− | } | + | <br>} |
− | rect(x, y, grid_size, grid_size); | + | <br>rect(x, y, grid_size, grid_size); |
− | } | + | <br>} |
− | popMatrix(); | + | <br>popMatrix(); |
− | } | + | <br>} |
− | void glow(int n) { | + | <br><br>void glow(int n) { |
− | pushMatrix(); | + | <br>pushMatrix(); |
− | translate(a, b); | + | <br>translate(a, b); |
− | rectMode(CORNER); | + | <br>rectMode(CORNER); |
− | for (int m=0; m<grid_num; m++) { | + | <br>for (int m=0; m<grid_num; m++) { |
− | if (sequence[m*grid_num+n]) { | + | <br>if (sequence[m*grid_num+n]) { |
− | for (int p=-1; p<2; p++) { | + | <br>for (int p=-1; p<2; p++) { |
− | for (int q=-1; q<2; q++) { | + | <br>for (int q=-1; q<2; q++) { |
− | if (n+p>=0&&n+p<grid_num&&m+q>=0&& | + | <br>if (n+p>=0&&n+p<grid_num&&m+q>=0&& |
− | m+q<grid_num) { | + | <br>m+q<grid_num) { |
− | noStroke(); | + | <br>noStroke(); |
− | fill(255, 255, 255, 20); | + | <br><br>fill(255, 255, 255, 20); |
− | rect((n+p)*(interval+grid_size), (m+q)*(interval+grid_size), | + | <br>rect((n+p)*(interval+grid_size), (m+q)*(interval+grid_size), |
− | grid_size, grid_size); | + | <br><br>grid_size, grid_size); |
− | } | + | <br>} |
− | if (p==0&&q==0){ | + | <br>if (p==0&&q==0){ |
− | fill(255, 255, 255, 30); | + | <br>fill(255, 255, 255, 30); |
− | rect(n*(interval+grid_size), m*(interval+grid_size), | + | <br>rect(n*(interval+grid_size), m*(interval+grid_size), |
− | grid_size, grid_size); | + | <br>grid_size, grid_size); |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | } | + | <br>} |
− | popMatrix(); | + | <br>popMatrix(); |
− | } | + | <br>} |
− | + | <br> void play_sounds(int n) { | |
− | float noteVal; | + | <br>float noteVal; |
− | float[] noteVals=new float[0]; | + | <br>float[] noteVals=new float[0]; |
− | for (int i=0; i<grid_num; i++) { | + | <br>for (int i=0; i<grid_num; i++) { |
− | if (sequence[i*grid_num+n]) { | + | <br>if (sequence[i*grid_num+n]) { |
− | noteVal = float(notes[i]); | + | <br>noteVal = float(notes[i]); |
− | noteVals=append(noteVals, noteVal); | + | <br>noteVals=append(noteVals, noteVal); |
− | } | + | <br>} |
− | } | + | <br>} |
− | sc.instrument(instrument()); | + | <br>sc.instrument(instrument()); |
− | sc.playChord(noteVals, volume, 0.25); | + | <br>sc.playChord(noteVals, volume, 0.25); |
− | } | + | <br>} |
− | float[] grid_color() { | + | <br><br>float[] grid_color() { |
− | float[] color_return=new float[3]; | + | <br>float[] color_return=new float[3]; |
− | arrayCopy(avg_color, color_return); | + | <br>arrayCopy(avg_color, color_return); |
− | return color_return; | + | <br>return color_return; |
− | } | + | <br>} |
− | boolean[] sequence() { | + | <br><br>boolean[] sequence() { |
− | boolean[] to_return=new boolean[sequence.length]; | + | <br>boolean[] to_return=new boolean[sequence.length]; |
− | arrayCopy(sequence, to_return); | + | <br>arrayCopy(sequence, to_return); |
− | return to_return; | + | <br>return to_return; |
− | } | + | <br>} |
− | int instrument(){ | + | <br><br><br>int instrument(){ |
− | int h=int(hue(color(avg_color[0],avg_color[1], | + | <br>int h=int(hue(color(avg_color[0],avg_color[1], |
− | avg_color[2]))%8); | + | <br>avg_color[2]))%8); |
− | if (h==0){return 2;} | + | <br>if (h==0){return 2;} |
− | else if (h==1){return 3;} | + | <br>else if (h==1){return 3;} |
− | else if (h==2){return 38;} | + | <br>else if (h==2){return 38;} |
− | else if (h==3){return 46;} | + | <br>else if (h==3){return 46;} |
− | else if (h==4){return 47;} | + | <br>else if (h==4){return 47;} |
− | else if (h==5){return 55;} | + | <br>else if (h==5){return 55;} |
− | else if (h==6){return 116;} | + | <br>else if (h==6){return 116;} |
− | else {return 120;} | + | <br>else {return 120;} |
− | } | + | <br>} |
− | void set_volume(int _volume) { | + | <br><br>void set_volume(int _volume) { |
− | volume=constrain(_volume, 0, 127); | + | <br>volume=constrain(_volume, 0, 127); |
− | } | + | <br>} |
− | } | + | <br>} |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
<br><br>Source: blob detection | <br><br>Source: blob detection |
Revision as of 06:30, 10 September 2015