Project: p5_sc / Examples: Buffer.write

Examples: Buffer.write

When the mouse button is pressed, transfers the contents of a buffer to Processing via the minim library, and plays back the sound (inside Processing itself) with a waveform visualisation.

Processing Code

import supercollider.*;
import java.util.ArrayList;
import ddf.minim.*;

Buffer buffer;
AudioPlayer song;

void setup ()
{
  buffer = new Buffer(2);
  buffer.read("/Users/daniel/audio/samples/acoustic/piano-chord.aif",
         this, "done");
}

void done (Buffer buffer)
{
  println("Buffer loaded.");
  println("Channels:    " + buffer.channels);
  println("Frames:      " + buffer.frames);
  println("Sample Rate: " + buffer.sampleRate);  
}

void mousePressed()
{
  buffer.write("/tmp/test.aif", "aiff", "int16", this, "written");
}

void written (Buffer buffer)
{
  println("Buffer written to disk");
  Minim.start(this);
  song = Minim.loadFile("/tmp/test.aif");
  song.play();
}

void draw()
{
  background(0);
  stroke(255);

  // shamelessly taken from an example found here:
  // http://code.compartmental.net/tools/minim/manual-audiosource/
  if (song != null)
  {
    for (int i = 0; i < song.bufferSize() - 1; i++ )
    {
      float x = height/4 - song.left.get(i)*50;
      float y = height/4 - song.left.get(i+1)*50;
      line(i, x, i+1, y);
      x = 3*height/4 - song.right.get(i)*50;
      y = 3*height/4 - song.right.get(i+1)*50;
      line(i, x, i+1, y);
    }
  }
}

Creative Commons License

Valid XHTML 1.0 Strict Valid CSS 2