Pixel brightness value (from camera) to sound: coding help

Hi, I'm pretty sure I'm almost there with this script. The sketch runs without any errors and I've got the camera to run, a box to scan through the pixels of the image and a tone to play; but the tone doesn't change in relation to the pixels being scanned which is what i wanted to happen. This code is pretty messy and I'm fairly new to processing so apologies in advance.

Any help would be great, Thanks!

import processing.video.*;
import ddf.minim.*;
import ddf.minim.signals.*;

Minim minim;
AudioOutput out;
SineWave sine;
Capture video;
int location = 0;
int fullSize;
int[] aPixels;
int direction = 1;
boolean onetime= true;
float signal;


void setup() {

  String[] cameras = Capture.list();

  video = new Capture(this, cameras[0]);
  video.start();
  fullSize = video.height * video.width;
  size(500, 500);
  aPixels = new int[width*height];
  noFill();
  stroke(255);
  frameRate(50);
  loadPixels();
  arrayCopy(pixels, aPixels);

  minim = new Minim(this);
  out = minim.getLineOut(Minim.STEREO);
  sine = new SineWave(440, 0.5, out.sampleRate());
  sine.portamento(10);
  out.addSignal(sine);
}

void draw() {

  {
    if (signal > width*height-1 || 
      signal < 0) { 
      direction = direction * -1;
    }

    if (mousePressed) {
      if (mouseY > height-1) { 
        mouseY = height-1;
      }
      if (mouseY < 0) { 
        mouseY = 0;
      }
      signal = mouseY*width+mouseX;
    } 
    else {
      signal += (0.33*direction);
    }


    background(255, 255, 255);
    if (video.available() == true) {
      video.read();
    }
    image(video, 0, 0);

    if (location == fullSize) {
      location = 0;
    } 
    else {
      location++;
    }


    loadPixels();
    fill(pixels[location]);
    int row = location / width;
    int pos = location - (row * width);
    updatePixels();
    rect(signal%width-5, int(signal/width)-5, 10, 10);
    point(signal%width, int(signal/width));

    for (int i=0; i<width*height; i++) {
      pixels[i] = aPixels[i];
    }

    float freq = map(brightness(aPixels[int(signal)]), 0, 255, 987.77, 261.63);
    sine.setFreq(freq);
    float pan = map(pixels[int(signal)], 0, width, -1, 1);
    sine.setPan(pan);
  }
}

Answers

  • Answer ✓

    I got your PM and have had a look at your sketch.

    It is not possible to modify your code easily because apart from the untidiness, there are no comments, and much of the code doesn't make sense or appears redundant.

    So I have taken your code, chopped out the redundant bits and got it to change frequency based on the underlining brightness of the pixel. It might not do exactly what you want but should form a good starting point.

    import processing.video.*;
    import ddf.minim.*;
    import ddf.minim.signals.*;
    
    Minim minim;
    AudioOutput out;
    SineWave sine;
    Capture video;
    
    // Current position in video image being sampled
    int scanX = 0, scanY = 0;
    
    void setup() {
      size(500, 500);
      // Initialise and start camera
      String[] cameras = Capture.list();
      video = new Capture(this, cameras[0]);
      video.start();
    
      frameRate(50);
      // Initialise Minim
      minim = new Minim(this);
      out = minim.getLineOut(Minim.STEREO);
      sine = new SineWave(440, 0.5, out.sampleRate());
      sine.portamento(10);
      out.addSignal(sine);
    }
    
    void mouseClicked() {
      // See if the scan position is to change
      if (mouseX < video.width) 
        scanX = mouseX;
      if (mouseY < video.height) 
        scanY = mouseY;
    }
    
    void draw() {
      // update scan position - constraining it to the smaller of the screen or video scan
      scanX++;
      if (scanX >= video.width || scanX >= width) {
        scanX = 0;
        scanY++;
        if (scanY >= video.height || scanX >= height)
          scanY = 0;
      }  
      // Clear the screen
      background(255, 255, 255);
      // Get video image and display it
      if (video.available() == true) {
        video.read();
      }
      image(video, 0, 0);
      // Get colour and brightness at scan position
      int scanCol = get(scanX, scanY);
      float scanBright = brightness(scanCol);
      // Get highlight box details
      int highlight = color(255, 255, 255, scanBright);
      fill(highlight);
      stroke(255);
      strokeWeight(1.5);
      rect(scanX-5, scanY-5, 10, 10);
      // Caluclate the frequency
      float freq = map(scanBright, 0, 255, 987.77, 261.63);
      sine.setFreq(freq);
      float pan = map(scanX, 0, width, -1, 1);
      sine.setPan(pan);
    }
    
  • Thanks so much man, i really appreciate your help :)

Sign In or Register to comment.