Created
December 17, 2019 09:33
-
-
Save SonnyYan/df8aa28e0f300ed8d4c0e66fdd2fb7d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.video.*; | |
import processing.serial.*; | |
import processing.sound.*; | |
Serial myPort; | |
Capture cam; | |
PImage prev; | |
//PImage prev2; | |
boolean p[]; | |
int circleSize = 10; | |
SoundFile a4; | |
SoundFile b4; | |
SoundFile c4; | |
SoundFile c5; | |
SoundFile cello_slide; | |
SoundFile d4; | |
SoundFile e4; | |
SoundFile f4; | |
SoundFile g4; | |
SoundFile violin_slide; | |
SoundFile guitar; | |
void setup() { | |
size(800,600); | |
cam = new Capture(this, 800,600); | |
cam.start(); | |
prev = cam.get(); | |
//prev2 = cam.get(); | |
p = new boolean[width * height]; | |
myPort= new Serial(this, Serial.list()[ 2 ], 9600); | |
a4 = new SoundFile(this, "a4.wav"); | |
b4 = new SoundFile(this, "b4.wav"); | |
c4 = new SoundFile(this, "c4.wav"); | |
c5 = new SoundFile(this, "c5.wav"); | |
//cello_slide = new SoundFile(this, "cello_slide.wav"); | |
d4 = new SoundFile(this, "d4.wav"); | |
e4 = new SoundFile(this, "e4.wav"); | |
f4 = new SoundFile(this, "f4.wav"); | |
g4 = new SoundFile(this, "g4.wav"); | |
guitar = new SoundFile(this, "guitar.wav"); | |
//violin_slide = new SoundFile(this, "violin_slide.wav"); | |
} | |
void draw() { | |
//background(0); | |
if (cam.available()) { | |
cam.read(); | |
cam.loadPixels(); | |
} | |
translate(cam.width,0); | |
scale(-1,1); | |
image(cam,0,0); | |
int w = cam.width; | |
int h = cam.height; | |
for (int y = 0; y < h; y+=circleSize){ | |
for (int x = 0; x < w; x+=circleSize) { | |
int i = x + y*w; | |
//fill( 0 ); | |
if(cam.pixels[i] != prev.pixels[i]){ | |
//fill(cam.pixels[i]); | |
p[i] = true; | |
} | |
else{ | |
p[i] = false; | |
} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
for (int y = circleSize; y < h- circleSize; y+=circleSize){ | |
for (int x = circleSize; x < w- circleSize; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
fill(cam.pixels[i]); | |
//p[i] = true; | |
} | |
else{ | |
//p[i] = false; | |
fill( 0 ); | |
} | |
rect(x,y,circleSize,circleSize); | |
} | |
} | |
int countD = 0; | |
for (int y = circleSize; y < 150; y+=circleSize){ | |
for (int x = circleSize; x < 200; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countD++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countD); | |
if (countD > 100){ | |
myPort.write('D'); | |
if(!guitar.isPlaying()) { | |
guitar.play(); | |
} | |
} | |
else { | |
myPort.write('d'); | |
} | |
//else { | |
// fill(0); | |
// rect(circleSize,circleSize, 200,150); | |
//} | |
int countC = 0; | |
for (int y = circleSize; y < 150; y+=circleSize){ | |
for (int x = 200+circleSize; x < 400; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countC++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countC); | |
if (countC > 100){ | |
myPort.write('C'); | |
if(!g4.isPlaying()) { | |
g4.play(); | |
} | |
} | |
else { | |
myPort.write('c'); | |
} | |
int countB = 0; | |
for (int y = circleSize; y < 150; y+=circleSize){ | |
for (int x = 400+circleSize; x < 600; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countB++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println("B: " + countB); | |
if (countB > 100){ | |
myPort.write('B'); | |
if(!f4.isPlaying()) { | |
f4.play(); | |
} | |
} | |
else { | |
myPort.write('b'); | |
} | |
int countA = 0; | |
for (int y = circleSize; y < 150; y+=circleSize){ | |
for (int x = 600+circleSize; x < 800; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countA++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countA); | |
if (countA > 100){ | |
myPort.write('A'); | |
if(!guitar.isPlaying()) { | |
guitar.play(); | |
} | |
} | |
else { | |
myPort.write('a'); | |
} | |
int countH = 0; | |
for (int y = 150+circleSize; y < 300; y+=circleSize){ | |
for (int x = circleSize; x < 200; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countH++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countH); | |
if (countH > 100){ | |
myPort.write('H'); | |
if(!a4.isPlaying()) { | |
a4.play(); | |
} | |
} | |
else { | |
myPort.write('h'); | |
} | |
int countG = 0; | |
for (int y = 150+circleSize; y < 300; y+=circleSize){ | |
for (int x = 200+circleSize; x < 400; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countG++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countG); | |
if (countG > 100){ | |
myPort.write('G'); | |
} | |
else { | |
myPort.write('g'); | |
} | |
int countF = 0; | |
for (int y = 150+circleSize; y < 300; y+=circleSize){ | |
for (int x = 400+circleSize; x < 600; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countF++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countF); | |
if (countF > 100){ | |
myPort.write('F'); | |
} else { | |
myPort.write('f'); | |
} | |
int countE = 0; | |
for (int y = 150+circleSize; y < 300; y+=circleSize){ | |
for (int x = 600+circleSize; x < 800; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countE++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countE); | |
if (countE > 100){ | |
myPort.write('E'); | |
if(!e4.isPlaying()) { | |
e4.play(); | |
} | |
} | |
else { | |
myPort.write('e'); | |
} | |
int countL = 0; | |
for (int y = 300+circleSize; y < 450; y+=circleSize){ | |
for (int x = circleSize; x < 200; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countL++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countL); | |
if (countL > 100){ | |
myPort.write('L'); | |
if(!b4.isPlaying()) { | |
b4.play(); | |
} | |
} | |
else { | |
myPort.write('l'); | |
} | |
int countK = 0; | |
for (int y = 300+circleSize; y < 450; y+=circleSize){ | |
for (int x = 200+circleSize; x < 400; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countK++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countK); | |
if (countK > 100){ | |
myPort.write('K'); | |
} | |
else { | |
myPort.write('k'); | |
} | |
int countJ = 0; | |
for (int y = 300+circleSize; y < 450; y+=circleSize){ | |
for (int x = 400+circleSize; x < 600; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countJ++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countJ); | |
if (countJ > 100){ | |
myPort.write('J'); | |
} | |
else { | |
myPort.write('j'); | |
} | |
int countI = 0; | |
for (int y = 300+circleSize; y < 450; y+=circleSize){ | |
for (int x = 600+circleSize; x < 800; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
int downLt = (x - circleSize) + (y+ circleSize) * w; | |
int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[down] && p[left] && p[right] && p[upRt] && p[upLt] && p[downRt] && p[downLt]){ | |
//fill(cam.pixels[i]); | |
countI++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countI); | |
if (countI > 100){ | |
myPort.write('I'); | |
if(!d4.isPlaying()) { | |
d4.play(); | |
} | |
} | |
else { | |
myPort.write('i'); | |
} | |
int countP = 0; | |
for (int y = 450+circleSize; y < 600; y+=circleSize){ | |
for (int x = circleSize; x < 200; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
//int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
//int downLt = (x - circleSize) + (y+ circleSize) * w; | |
//int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[left] && p[right] && p[upRt] && p[upLt]){ | |
//fill(cam.pixels[i]); | |
countP++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countP); | |
if (countP > 100){ | |
myPort.write('P'); | |
if(!c5.isPlaying()) { | |
c5.play(); | |
} | |
} | |
else { | |
myPort.write('p'); | |
} | |
int countO = 0; | |
for (int y = 450+circleSize; y < 600; y+=circleSize){ | |
for (int x = 200+circleSize; x < 400; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
//int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
//int downLt = (x - circleSize) + (y+ circleSize) * w; | |
//int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[left] && p[right] && p[upRt] && p[upLt]){ | |
//fill(cam.pixels[i]); | |
countO++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countO); | |
if (countO > 100){ | |
myPort.write('O'); | |
} | |
else { | |
myPort.write('o'); | |
} | |
int countN = 0; | |
for (int y = 450+circleSize; y < 600; y+=circleSize){ | |
for (int x = 400+circleSize; x < 600; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
//int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
//int downLt = (x - circleSize) + (y+ circleSize) * w; | |
//int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[left] && p[right] && p[upRt] && p[upLt]){ | |
//fill(cam.pixels[i]); | |
countN++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countN); | |
if (countN > 100){ | |
myPort.write('N'); | |
} | |
else { | |
myPort.write('n'); | |
} | |
int countM = 0; | |
for (int y = 450+circleSize; y < 600; y+=circleSize){ | |
for (int x = 600+circleSize; x < 800; x+=circleSize) { | |
int i = x + y*w; | |
int up = x + (y- circleSize) * w; | |
//int down = x + (y+ circleSize) * w; | |
int left = (x - circleSize) + y*w; | |
int right = (x + circleSize) + y*w; | |
int upLt = (x - circleSize) + (y- circleSize) * w; | |
int upRt = (x + circleSize) + (y- circleSize) * w; | |
//int downLt = (x - circleSize) + (y+ circleSize) * w; | |
//int downRt = (x + circleSize) + (y+ circleSize) * w; | |
//fill( 0 ); | |
if(p[i] && p[up] && p[left] && p[right] && p[upRt] && p[upLt]){ | |
//fill(cam.pixels[i]); | |
countM++; | |
//p[i] = true; | |
} | |
//else{ | |
// //p[i] = false; | |
// fill( 0 ); | |
//} | |
//rect(x,y,circleSize,circleSize); | |
} | |
} | |
println(countM); | |
if (countM > 100){ | |
myPort.write('M'); | |
if(!c4.isPlaying()) { | |
c4.play(); | |
} | |
} | |
else { | |
myPort.write('m'); | |
} | |
//prev2 = prev.get(); | |
prev = cam.get(); | |
cam.updatePixels(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment