Using Beads Library in Processing - Using peakdetector on multiple objects | Sample visualization

edited June 2015 in Library Questions

Okeydoke, I'm working on a sketch using the beads and SMT libraries. I have a few things that I'm struggling with, if you can help, that'd be awesome.

The Questions:

• Trying to make each object respond by lighting up as it is played (controlled by audioPlay), currently all do at the same time, any ideas on separating the functions?

• I can't work out why the variable brightTime, although it occurs for each function (myobject1/2/3) - seems to have an effect on the counter globally for some reason. Any ideas?

• Any help on the timing function audioDraw would be appreciated - I'm not sure I've got it worked out very well as I want to make sure that if one period elapses before another beat the color jumps to full 255 alpha again.

Any other fixes or re modelling suggestions are more than welcome.

Thanks guys.

import vialab.SMT.*;
import beads.*;

//Objects
object myobject1;
object myobject2;
object myobject3;

//Sketch Variables
float midX, midY, length1;
//Zone Variables
float cX, cY;
//Time Variables
float m, n, t, dim;
//Beat Variables
float beat1, beat2, beat3, beat4, brightTime;
float bpm;
boolean play;


AudioContext ac;

void setup() {
  midX = width/2;
  midY = height/2;
  length1 = 100;

  //size(displayWidth, displayHeight, SMT.RENDERER);
  size(1000, 700, SMT.RENDERER);
  myobject1 = new object(0, 0, length1, length1, 0, 20, 100, 200);
  myobject2 = new object(0, 0, length1, length1, 0, 10, 200, 180);
  myobject3 = new object(0, 0, length1, length1, 0, 0, 0, 150);

  SMT.init( this, TouchSource.AUTOMATIC);

  Zone zone = new Zone("Object", 480, 200, 100, 100);
  Zone zone2 = new Zone("Object2", 480, 330, 100, 100);
  Zone zone3 = new Zone("Object3", 480, 460, 100, 100);

  SMT.add( zone);
  SMT.add( zone2);
  SMT.add( zone3);

  m = millis()/10;
  t = 0;

if (brightTime <= 0) {
        brightTime = t;
        dim = t;
        play = false;
      }

  ac = new AudioContext(); 

  myobject1.audio();
  myobject2.audio();
  myobject3.audio();
  play = false;
}

void draw() {

  background(0);
  noStroke();
  frameRate(100);
  m++;
  dim++;

  println( m );

  myobject1.texts();

  myobject1.timer();
//  myobject2.timer();
//  myobject3.timer();
}

void drawObject(Zone zone) {
  cX = zone.getX();
  cY = zone.getY();
  text("1", 0, 0);
  myobject1.audioIn();
  myobject1.display();
}
void touchObject(Zone zone) {
  zone.drag();
}

void drawObject2(Zone zone2) {
  cX = zone2.getX();
  cY = zone2.getY();
  text("2", 0, 0);
  myobject2.audioIn();
  myobject2.display();
}
void touchObject2(Zone zone2) {
  zone2.drag();
}

void drawObject3(Zone zone3) {
  cX = zone3.getX();
  cY = zone3.getY();
  text("3", 0, 0);
  myobject3.audioIn();
  myobject3.display();
  //  myobject3.audioDraw();
}
void touchObject3(Zone zone3) {
  zone3.drag();
}

class object {
  float xpos, ypos, wid, hei, vol, room, pit, bright, brightTime, dim, rate, Xrate;
  color c, b, a, d;
  float textY;
  int dt;

  String sourceFile; // this will hold the path to our audio file
  SamplePlayer sp; // the SamplePlayer class will be used to play the audio file
  PowerSpectrum ps;
  PeakDetector od;
  ShortFrameSegmenter sfs;

  //Gain
  Gain g;
  Glide gainValue;
  //Reverb
  Reverb r; // our Reverberation unit generator
  float brightness;
  int time; // tracks the time 

    object(float tempXpos, float tempYpos, float tempWid, float tempHei, float tempRoom, color tempA, color tempB, color tempC) {
    xpos = tempXpos;
    ypos = tempYpos;
    wid = tempWid;
    hei = tempHei;
    room = tempRoom;
    a = tempA;
    b = tempB;
    c = tempC;
  }

  void audio() {

    sourceFile = dataPath("sounds.wav");
    try {  
      sp = new SamplePlayer(ac, new Sample(sourceFile));
    }
    catch(Exception e)
    {
      println("Exception while attempting to load sample!");
      e.printStackTrace(); 
      exit();
    }
    sp.setKillOnEnd(false);

    gainValue = new Glide(ac, 0.0, 20);
    g = new Gain(ac, 1, gainValue);
    g.addInput(sp); // connect the filter to the gain
    r = new Reverb(ac, 1); // create a new reverb with a single output channel
    r.setSize(0.5); // set the room size (between 0 and 1) 0.5 is the default
    r.setDamping(room); // set the damping (between 0 and 1) - the higher the dampening, the fewer resonant high frequencies
    r.addInput(g); // connect the gain to the reverb
    ac.out.addInput(r); // connect the Reverb to the AudioContext
  }

  void texts() {
    fill(255);
    textY = 100;

//    text(+myobject1.pit, 100, textY);
//    text(+myobject1.room, 100, textY+20);
//    text(+myobject2.pit, 100, textY+40);
//    text(+myobject2.room, 100, textY+60);
//    text(+myobject3.pit, 100, textY+80);
//    text(+myobject3.room, 100, textY+100);
    text("time "+m, 100, midY);
    text("beat1 "+beat1, 100, midY+20);
    text("brightTime "+brightTime, 100, midY+40);
    text("dim "+dim, 100, midY+60);

    text("brightTime1 "+myobject1.brightTime, 100, midY+300);
    text("bright1 "+myobject1.bright, 100, midY+320);
    text("rate "+myobject1.rate, 100, midY+340);
    text("brightTime2 "+myobject2.brightTime, 300, midY+300);
    text("bright2 "+myobject2.bright, 300, midY+320);
    text("rate2 "+myobject2.rate, 300, midY+340);
    text("brightTime3 "+myobject3.brightTime, 500, midY+300);
    text("bright2 "+myobject3.bright, 500, midY+320);
    text("rate3 "+myobject3.rate, 500, midY+340);
  }

  void audioPlay() {
    gainValue.setValue(4);
    r.setSize(room);
    sp.setPitch(new Glide(ac, pit));
    ac.start(); // begin audio processing
    sp.setToLoopStart(); // move the playback pointer to the first loop point (0.0)
    sp.start(); // play the audio file
  }

  void timer() {
    beat1 = (100*(60/bpm));
    beat2 = (200*(60/bpm));
    beat3 = (300*(60/bpm));
    beat4 = (400*(60/bpm));

    if ( key== 'q') {
      bpm = 60;
    } else if (key== 'w') {
      bpm = 120;
    } else if (key== 'e') {
      bpm = 240;
    } else {
      bpm = 30;
    }

    if (m == beat1) {
      myobject1.audioPlay( );
      play = true;
    }  
    if (m == beat2) {
      //      myobject2.audioPlay();
    }
    if (m == beat3) {
      myobject2.audioPlay();
      play = true;
    }
    if (m == beat4) {
      //      myobject1.audioPlay();
    }
    if (m == beat4) {
      m = t;
    }
  }

  void display() {
    noStroke();
    colorMode(RGB);
    fill(a, b, c);    
    rectMode(CORNER);
    rect(xpos, ypos, wid, hei);
    stroke(255);
    line((xpos+(wid/2)), (ypos+(hei/2)), (xpos+(wid/2)), ypos);
    audioDraw();
  }

  void audioIn() {
    room = map(cX, 0, width, 0, 0.9);
    pit = map(cY, 0, height, 2, 0);
    rate = map(cY, 0, height, 1, 2);
    Xrate = map(cX, 0, width, 1, 1.5);
  }

  void audioDraw() {
    if (play == true) {
      fill(255, 0, 0, brightTime);
      rectMode(CORNER);
      rect(xpos, ypos, wid, hei);
      dim++;
      bright = 150;
      brightTime = bright - dim/(rate*Xrate);
    }
    if (brightTime <= 0) {
      brightTime = t;
      dim = t;
      play = false;
    }
  }
}

Answers

Sign In or Register to comment.