Project-ColorMusic


这是16年5月帮助一个小朋友做的关于颜色和声音的项目。通过识别到试管内变化的颜色,演奏出音乐。



原理其实很简单,识别到capture视频流中的特定像素点的颜色,根据RGB颜色值播放相应的声音媒体

代码:

import processing.video.*;
import ddf.minim.*;
//import ddf.minim.effects.*;
Capture cam;
color findColor1;
color findColor2;
color findColor3;
color findColor4;
color findColor5;
color findColor6;
color findColor7;
Minim minim;
AudioPlayer s1;
AudioPlayer s2;
AudioPlayer s3;
AudioPlayer s4;
AudioPlayer s5;
AudioPlayer s6;
AudioPlayer s7;
float dc1;
float dc2;
float dc3;
float dc4;
float dc5;
float dc6;
float dc7;
float difference1 = 300;
float difference2 = 300;
float difference3 = 300;
float difference4 = 300;
float difference5 = 300;
float difference6 = 300;
float difference7 = 300;
boolean isPlaying = false;
boolean otherisPlaying = false;
int delay = 30;
void setup() {
  size(640, 480);
  frameRate(30);
  String[] cameras = Capture.list();
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }
    cam = new Capture(this, cameras[0]);
    cam.start();
  }
  findColor1 = color(64, 16, 18);
  findColor2 = color(30, 63, 56);
  findColor3 = color(65, 105, 128);
  findColor4 = color(150, 143, 45);
  smooth();
  minim = new Minim(this);
  s1 = minim.loadFile("a1.mp3");
  s2 = minim.loadFile("a2.mp3");
  s3 = minim.loadFile("a3.mp3");
}
void draw() {
  if (cam.available() == true) {
    cam.read();
  }
  cam.loadPixels();
  image(cam, 0, 0);
  int miniX = 0;
  int miniY = 0;
  for (int x = 0; x < cam.width; x ++ ) {
    for (int y = 0; y < cam.height; y ++ ) {
      int loc = x + y*cam.width;
      color videoColor = cam.pixels[loc];
      float r0 = red(videoColor);
      float g0 = green(videoColor);
      float b0 = blue(videoColor);
      float r1 = red(findColor1);
      float g1 = green(findColor1);
      float b1 = blue(findColor1);
      float r2 = red(findColor2);
      float g2 = green(findColor2);
      float b2 = blue(findColor2);
      float r3 = red(findColor3);
      float g3 = green(findColor3);
      float b3 = blue(findColor3);
      dc1 = dist(r0, g0, b0, r1, g1, b1);
      if (dc1 < difference1) {
        difference1 = dc1;
        miniX = x;
        miniY = y;
      }
      dc2 = dist(r0, g0, b0, r2, g2, b2);
      if (dc2 < difference2) {
        difference2 = dc2;
        miniX = x;
        miniY = y;
      }
      dc3 = dist(r0, g0, b0, r3, g3, b3);
      if (dc3 < difference3) {
        difference3 = dc3;
        miniX = x;
        miniY = y;
      }
    }
  }
//  println(difference1);
//  println(difference2);
//  println(difference3);
  if (difference1 < 5 && isPlaying == false) {
    //    fill(findColor);
    //    strokeWeight(2.0);
    //    stroke(255);
    //    rectMode(CENTER);
    //    rect(miniX, miniY, 60, 60);
    isPlaying = true;
    println("s1 is playing");
    s1.play();
    delay --;
    if (delay == 0)
    {
      s1.rewind();
      delay = 30;
      difference1 = 300;
    }
  } else {
    s1.pause();
    isPlaying = false;
  }
  if (difference2 < 5 && isPlaying == false) {
    //    fill(findColor);
    //    strokeWeight(2.0);
    //    stroke(255);
    //    rectMode(CENTER);
    //    rect(miniX, miniY, 60, 60);
    isPlaying = true;
    println("s2 is playing");
    s2.play();
    delay --;
    if (delay == 0)
    {
      s2.rewind();
      delay = 30;
      difference2 = 300;
    }
  } else {
    isPlaying = false;
    s2.pause();
  }
  if (difference3 < 5 && isPlaying == false ) {
    //    fill(findColor);
    //    strokeWeight(2.0);
    //    stroke(255);
    //    rectMode(CENTER);
    //    rect(miniX, miniY, 60, 60);
    isPlaying = true;
    println("s3 is playing");
    s3.play();
    delay --;
    if (delay == 0)
    {
      s3.rewind();
      delay = 30;
      difference3 = 300;
    }
  } else {
    isPlaying = false;
    s3.pause();
  }
  //updatePixels();
}
//void mousePressed() {
//  if (mouseButton == LEFT)
//  {
//    int loc = mouseX + mouseY*cam.width;
//    findColor1 = cam.pixels[loc];
//  }
//  if (mouseButton == RIGHT)
//  {
//    int loc = mouseX + mouseY*cam.width;
//    findColor2 = cam.pixels[loc];
//  }
//}

 
 


发表评论

您的电子邮箱地址不会被公开。