Go Down

Topic: processing beads library (Read 2907 times) previous topic - next topic



I have a program I'm working on that uses the Beads library to sample songs and change the rate at which they're played (this is done using a potentiometer--I'm sending data in from an Arduino to control the rate).

In this program, the sound clip is established in the void setup() function.

However, I would like to be able to change the song sample dynamically, in void draw(), depending on what data the program is receiving. I have five different songs, and I want the Processing program to play a different song, depending on whether the values it's receiving are between 1-100, 101-200, 201-300, 301-400, or 401-500.

Here is my code so far:

Code: [Select]

import processing.serial.*;

Serial port;

float derp = 0;

// Sampling_03.pde

// this is a more complex sampler
// clicking somewhere on the window initiates sample playback
// moving the mouse controls the playback rate

import beads.*;

AudioContext ac;

SamplePlayer sp1;

// we can run both SamplePlayers through the same Gain
Gain sampleGain;
Glide gainValue;

Glide rateValue;

void setup()
  size(800, 600);

  port = new Serial(this, "/dev/tty.usbmodem411", 9600);

  ac = new AudioContext(); // create our AudioContext

    // whenever we load a file, we need to enclose the code in a Try/Catch block
  // Try/Catch blocks will inform us if the file can't be found
  try { 
    // initialize the SamplePlayer
    sp1 = new SamplePlayer(ac, new Sample(sketchPath("") + "prelude_no_8.mp3"));
  catch(Exception e)
    // if there is an error, show an error message (at the bottom of the processing window)
    println("Exception while attempting to load sample!");
    e.printStackTrace(); // then print a technical description of the error
    exit(); // and exit the program

  // note that we want to play the sample multiple times

  rateValue = new Glide(ac, 1, 30); // initialize our rateValue Glide object
  sp1.setRate(rateValue); // connect it to the SamplePlayer

  // as usual, we create a gain that will control the volume of our sample player
  gainValue = new Glide(ac, 0.0, 30);
  sampleGain = new Gain(ac, 1, gainValue);

  ac.out.addInput(sampleGain); // connect the Gain to the AudioContext

  ac.start(); // begin audio processing

  background(0); // set the background to black
  line(width/2, 0, width/2, height); // draw a line in the middle
  text("Click to begin playback.", 100, 100); // tell the user what to do!
  text("Move the mouse to control playback speed.", 100, 120); // tell the user what to do!

// although we're not drawing to the screen, we need to have a draw function
// in order to wait for mousePressed events
void draw()
  float halfWidth = width / 2.0;

  gainValue.setValue((float)mouseY / (float)height); // set the gain based on mouse position along the Y-axis
  rateValue.setValue(((float)derp - halfWidth)/halfWidth); // set the rate based on mouse position along the X-axis

  println((float)mouseY / (float)height);
  println(((float)derp - halfWidth)/halfWidth);

// this routine is called whenever a mouse button is pressed on the Processing sketch
void mousePressed()
  // if the left mouse button is clicked, then play the sound
  if( mouseX > width / 2.0 )
    sp1.setPosition(000); // set the start position to the beginning
    sp1.start(); // play the audio file
  // if the right mouse button is clicked, then play the bass drum sample backwards
    sp1.setToEnd(); // set the start position to the end of the file
    sp1.start(); // play the file in reverse (rate set in the draw routine)

void serialEvent (Serial port)
  derp = float(port.readStringUntil('\n'));

Any help would be largely appreciated! Thank you!

Coding Badly

Is that a Processing program?


Yes. Sorry for not being clear!

Coding Badly

There are a few folks here who work with Processing.  But only a tiny few.  If you don't get help in a few days I suggest trying a Processing related forum.

Go Up