jeonghopark
6/18/2015 - 6:55 AM

PixelNote_01.pde

import ddf.minim.*;
import ddf.minim.ugens.*;

Minim       minim;
AudioOutput out;

PImage pixelBild;

int noteIndex;
int imageSize = 4;


void setup() {
  minim = new Minim(this);
  out = minim.getLineOut();

  pixelBild = loadImage("quadrat8x32.jpg");
  size( pixelBild.width * imageSize, pixelBild.height * imageSize);
  pixelBild.loadPixels();
}


void draw() {

  image(pixelBild, 0, 0, pixelBild.width * imageSize, pixelBild.height * imageSize);

  if (frameCount%10==0) {
    noteIndex = noteIndex + 1;
    if (noteIndex>pixelBild.pixels.length) noteIndex = 0;
    float r = red(pixelBild.pixels[noteIndex]);
    float g = green(pixelBild.pixels[noteIndex]);
    float b = blue(pixelBild.pixels[noteIndex]);

    println("r: "+r);
    println("g: "+g);
    println("b: "+b);
    println("PixelNr.: "+noteIndex);

    if (r > 120) {         
      out.playNote( 0.0, 0.1, new SineInstrument( r * 300 ) );
    } else if (g > 40) {      
      out.playNote( 0.0, 0.1, new SineInstrument( g * 150 ) );
    } else if (b > 120) {  
      out.playNote( 0.0, 0.1, new SineInstrument( b * 75 ) );
    }
  }
}


class SineInstrument implements Instrument{
  Oscil wave;
  Line  ampEnv;

  SineInstrument( float frequency ){
    wave   = new Oscil( frequency, 0, Waves.SINE );
    ampEnv = new Line();
    ampEnv.patch( wave.amplitude );
  }

  void noteOn( float duration ){
    ampEnv.activate( duration, 0.5f, 0 );
    wave.patch( out );
  }

  void noteOff(){
    wave.unpatch( out );
  }
}