Loading...
Logo
Processing Forum
Hello,

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:

Copy code
  1. import processing.serial.*;

  2. Serial port;

  3. float derp = 0;

  4. // Sampling_03.pde

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

  8. import beads.*;

  9. AudioContext ac;

  10. SamplePlayer sp1;

  11. // we can run both SamplePlayers through the same Gain
  12. Gain sampleGain;
  13. Glide gainValue;

  14. Glide rateValue;

  15. void setup()
  16. {
  17.   size(800, 600);

  18.   port = new Serial(this, "/dev/tty.usbmodem411", 9600);
  19.   port.bufferUntil('\n');

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

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

  34.   // note that we want to play the sample multiple times
  35.   sp1.setKillOnEnd(false);

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

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

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

  43.   ac.start(); // begin audio processing

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

  50. // although we're not drawing to the screen, we need to have a draw function
  51. // in order to wait for mousePressed events
  52. void draw()
  53. {
  54.   
  55.   float halfWidth = width / 2.0;

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

  58.   print("gain:");
  59.   println((float)mouseY / (float)height);
  60.   print("rate:");
  61.   println(((float)derp - halfWidth)/halfWidth);
  62.   print("derp:");
  63.   println(derp);
  64. }

  65. // this routine is called whenever a mouse button is pressed on the Processing sketch
  66. void mousePressed()
  67. {
  68.   // if the left mouse button is clicked, then play the sound
  69.   if( mouseX > width / 2.0 )
  70.   {
  71.     sp1.setPosition(000); // set the start position to the beginning
  72.     sp1.start(); // play the audio file
  73.   }
  74.   // if the right mouse button is clicked, then play the bass drum sample backwards
  75.   else 
  76.   {
  77.     sp1.setToEnd(); // set the start position to the end of the file
  78.     sp1.start(); // play the file in reverse (rate set in the draw routine)
  79.   }
  80. }

  81. void serialEvent (Serial port)
  82. {
  83.   derp = float(port.readStringUntil('\n'));
  84. }
Any help would be largely appreciated! Thank you!