Project: processing-sc / Class: Buffer

Class: Buffer

Represents a single audio buffer. Used to read, record and write sound data.

Examples: read write setn getn fill


Variables

  • int index - this buffer's unique index
  • int frames - number of frames (not yet updated after read() operations)
  • int channels - number of sound channels (not yet updated after read() operations)
  • int sampleRate - audio sample rate

Methods

Buffer (int channels)
Buffer (Server server, int channels)
Buffer (Server server, int frames, int channels)
Create a new buffer object, with the given number of frames and channels. Defaults to Server.local if no server is specified.
void read (String path)
void read (String path, Object target, String action)

Allocate a buffer and read the specified sound file into it.

The method named action is invoked on the object target upon completion, with this Buffer object as its single argument.

The frames, channels and sampleRate instance variables are automatically populated with the correct values after the command is completed.

void cueSoundFile (String path)
void cueSoundFile (String path, Object target, String action)

Allocate a buffer for streaming from disk with DiskIn.

The method named action is invoked on the object target upon completion, with this Buffer object as its single argument.

The frames, channels and sampleRate instance variables are automatically populated with the correct values after the command is completed.

void alloc ()

Allocates the required buffer space on the server.

The method named action is invoked on the object target upon completion, with this Buffer object as its single argument.

void write (String path, String type, String format, int frames, int start, int leave_open)
void write (String path, String type, String format)

Begins writing to disk from the buffer, using the specified type and format parameters (see SuperCollider's Buffer helpfile for details). Writes at most frames frames, from index start. If leave_open is greater than 0, leaves the buffer open for further writing (for use with DiskOut).

void close(Object target, String action)
void close()

Closes the output file previously opened with write().

The method named action is invoked on the object target upon completion, with this Buffer object as its single argument.

void free(Object target, String action)
void free()

Free the buffer object.

The method named action is invoked on the object target upon completion, with this Buffer object as its single argument.

void get(int frame, Object target, String action)

Get a single sample from a buffer object, passing the float value via the specified callback.

The method named action is invoked on the object target upon completion, which must have the below signature:

(Buffer buffer, int index, float value)

void get(int [] frames, Object target, String action)

Get multiple samples from a buffer at the given frame indices, passing an array of float values via the specified callback.

The method named action is invoked on the object target upon completion, which must have the below signature:

(Buffer buffer, int [] indices, float [] values)

void getn(int frame, int count, Object target, String action)

Get a range of samples from a buffer object, passing an array of float values.

This function cannot be used to retrieve large numbers of values; currently, it can only get a maximum of 200 samples. To retrieve a full buffer from an scsynth running locally, use Buffer.write() with a callback which reads the file data using a sound library such as Minim (example).

The method named action is invoked on the object target upon completion, which must have signature:

(Buffer buffer, int index, float [] values)

void set(int frame, float value)

Set a single sample to the specified value (-1..1).

void set(int [] frames, float [] values)

Set several discrete sample values at the given frame indices. frames and values must have the same number of elements.

void setn(int frame, int count, float[] values)

Set a range of samples to the specified array of values. The array values must have count elements.

void fill(int frame, int count, float value)

Fill a range of samples with the same value.

void zero()

Set every sample in the buffer to 0.0.


Sample Code

Usage

import supercollider.*;

Buffer buffer;

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

void draw ()
{
}

void done (Buffer buffer)
{
  println("Buffer loaded.");
  println("Channels:    " + buffer.channels);
  println("Frames:      " + buffer.frames);
  println("Sample Rate: " + buffer.sampleRate);
  
  Synth synth = new Synth("playbuf_2");
  synth.set("bufnum", buffer.index);
  synth.create();
}

void mousePressed()
{
  buffer.free(this, "freed");
}

void freed (Buffer buffer)
{
  println("Buffer freed.");
}