Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import arb.soundcipher.*;
- import gab.opencv.*;
- import processing.video.*;
- OpenCV opencv;
- Capture cam;
- SoundCipher sc = new SoundCipher(this);
- PImage img;
- ArrayList notes = new ArrayList();
- int index, time, playDelay;
- PVector last = new PVector(-10,0);
- color segColor;
- boolean doPlayBack = false;
- boolean sketchFullScreen(){
- return true;
- }
- void setup(){
- colorMode(HSB,100);
- background(0,0,100);
- frameRate(60);
- smooth();
- size(displayWidth,displayHeight);
- img = loadImage("testmusicimage2.png");
- img.resize(160,120);
- cam = new Capture(this,160,120);
- cam.start();
- opencv = new OpenCV(this,img);
- index = 0;
- time = millis();
- playDelay = millis();
- segColor = color(random(100),10,80);
- }
- void draw(){
- if (cam.available() == true) {
- cam.read();
- img = cam.get();
- opencv.loadImage(img);
- if(mousePressed){
- background(0,0,100);
- notes = new ArrayList();
- doPlayBack = false;
- }
- for (int i = 0; i < notes.size(); i++){
- note n = new note(0,0,0);
- n = (note)notes.get(i);
- n.drawCircle();
- }
- PVector bright = opencv.max();
- //print(brightness(opencv.getOutput().get(int(bright.x),int(bright.y))) + " "); //PRINT BRIGHTEST VALUE - UNCOMMENT IF NEED TO ADJUST
- float x = bright.x;
- float y = bright.y;
- x = img.width - x;
- x = map(x, 0, img.width, 0, width);
- y = map(y, 0, img.height, 0, height);
- if (brightness(opencv.getOutput().get(int(bright.x),int(bright.y))) > 94.7) {
- lightDraw(x,y);
- } else {
- if (doPlayBack) {
- if(millis() - playDelay > 500) {
- playBack();
- }
- }
- }
- /*pushMatrix();
- scale(-1,1);
- image(opencv.getOutput(),0-img.width,0);
- popMatrix(); */ //Display the camera output - uncomment for testing
- }
- }
- void lightDraw(float x, float y) {
- doPlayBack = true;
- float w;
- if (last.x != -10) {
- w = abs(last.x - x) + abs(last.y - y);
- } else {
- w = 20;
- }
- notes.add(new note(x,y,w));
- note tempNote = (note)notes.get(notes.size() - 1);
- tempNote.drawCircle();
- last = new PVector(x,y);
- playDelay = millis();
- }
- void playBack() {
- if (index >= notes.size()) {
- index = 0;
- last = new PVector(-10,0);
- doPlayBack = false;
- segColor = color(random(100),10,80);
- if(notes.size() > 50){
- notes = new ArrayList();
- background(0,0,100);
- }
- }
- if (notes.size() != 0){
- note temp = new note(0,0,0);
- temp = (note)notes.get(index);
- temp.play();
- timing();
- }
- }
- void timing(){
- if(millis() - time > 5){
- time = millis();
- index++;
- }
- }
- //////// THE NOTE CLASS //////////
- class note {
- /*
- FIELDS: X,Y,PITCH,AMPLITUDE,COLOUR,SIZE
- FUNCTIONS: DRAW, PLAY
- */
- float x,y,amp,size;
- color c;
- int flash;
- note (float x, float y, float z) {
- this.x = x;
- this.y = y;
- this.c = c;
- size = z;
- flash = 0;
- c = segColor;
- c = color(hue(c),map(z,0,500,0,100),brightness(c));
- amp = constrain(z, 0, 100);
- }
- void drawCircle(){
- noStroke();
- if (flash == 1){
- if(brightness(c) <= 80){
- flash = 0;
- } else {
- c = color(hue(c),saturation(c),brightness(c)-2);
- }
- }
- fill(c);
- ellipse(x,y,size,size);
- }
- void play(){
- flash();
- sc.playNote(100-map(y,0,height,0,100),amp,2.0);
- }
- void flash() {
- c = color(hue(c),saturation(c),100);
- //drawCircle();
- flash = 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement