Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to display image and single pixel

$
0
0

Hello! I am working on a sketch that I have found here, I have modified it a bit. Basically, at the moment, there are two modes of displaying the image: one is only showing the color of the single pixel, and the other is showing the image when the key is pressed. What I 'd like to do is showing both. Having both the image and the color of the pixel being picked up in the same sketch. Is it possible? how can I modify the code to obtain that?

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


PImage img;
PImage [] imgArray = new PImage [1];
int direction = 1;
float signal = 0;

int savedTime;
int totalTime = 2000; // load new image after 5 seconds

Minim minim;
AudioOutput out;
SineWave sine;

boolean show_color = true;
color c;





void setup(){
 // fullScreen();
 size(800,800);
 // frameRate(random (5,10));
  savedTime = millis();

  img = loadImage("imageName0"+".jpg");


  imgArray[0] = img;



  img.resize(0,800);

  minim = new Minim(this);
  // get a line out from Minim, default bufferSize = 1024, default sample rate = 44100, bit depth = 16
  out = minim.getLineOut(Minim.STEREO);
  // create a sine wave Oscillator, set to 440 Hz, at 0.5 amplitude, sample rate from line out
  sine = new SineWave(440, 0.5, out.sampleRate());
  // set the portamento speed on the oscillator to 200 milliseconds
  sine.portamento(100);
  // add the oscillator to the line out
  out.addSignal(sine);


  noFill();
  noStroke();
  image(imgArray[int (random(imgArray.length))],0,0);

}




void draw(){
   int passedTime = millis () - savedTime;
   play();
    if (passedTime > totalTime){
    thread ("setup");

    savedTime =millis ();
    }
 }
 void play () {
  if (signal > img.width*img.height || signal < 0);

  else
  //signal += 0.33*direction;
   signal += random(0, 10)*direction;

//int sX = int (random (0,800));
// int sY = int(random (0,800));

  int sX = int(signal) % width;
  int sY = int(signal) / height;


  c = img.get(sX, sY);

  set(0, 0, img);
  stroke(255);
  strokeWeight(2);
  rect(sX -8, sY -8 , 16,16);
  point(sX, sY);
  noStroke();
  frameRate (random (5,15));

  if(show_color)  draw_color();



  float freq = map(red(img.get(sX, sY)), 0, 255, 250, 900);
  float amplitude = map(green(img.get(sX, sY)), 0, 255, 0, 1);
   println (amplitude);
  sine.setFreq(freq);
  out.playNote( 0.0, 0.5, new SineInstrument( freq, amplitude ) );
  out.resumeNotes();
  float pan = map(blue(img.get(sX, sY)), 0, 255, -1, 1);
  sine.setPan(pan);

  println( " R = " + red(img.get(sX, sY)) + ", G = " + green(img.get(sX, sY)) + ", B = " + blue(img.get(sX, sY)));
  //println (signal);
    sine.setAmp(0);


    }



void keyPressed()
{
  show_color = ! show_color ? true : false;
}


void draw_color()
{
  background(c);
}


class SineInstrument implements Instrument
{
  Oscil wave;
  Line  ampEnv;

  SineInstrument( float frequency, float amplitude )
  {
    // make a sine wave oscillator
    // the amplitude is zero because
    // we are going to patch a Line to it anyway
    wave   = new Oscil( frequency, amplitude, Waves.SINE );
    ampEnv = new Line();
    ampEnv.patch( wave.amplitude );
  }

  // this is called by the sequencer when this instrument
  // should start making sound. the duration is expressed in seconds.
  void noteOn( float duration )
  {
    // start the amplitude envelope
    ampEnv.activate( duration, 0.5f, 0 );
    // attach the oscil to the output so it makes sound
    wave.patch( out );
  }

  // this is called by the sequencer when the instrument should
  // stop making sound
  void noteOff()
  {
    wave.unpatch( out );
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles