Buffer client-side representation of a buffer on a server


superclass: Object


Buffer encapsulates a number of common tasks, OSC messages, and capabilities related to server-side buffers, which are globally available arrays of floats. These are commonly used to hold sampled audio, such as a soundfile loaded into memory, but can be used to hold other types of data as well. They can be freed or altered even while being accessed.  Buffers are commonly used with PlayBuf, RecordBuf, DiskIn, DiskOut, BufWr, BufRd, and other UGens. (See their individual help files for more examples.) See Server-Architecture for more details.


Buffer Numbers and Allocation


Although the number of buffers on a server is set at the time it is booted, memory must still be allocated within the server app before they can hold values. (At boot time all buffers have a size of 0.) 


Server-side buffers are identified by number, starting from 0. When using Buffer objects, buffer numbers are automatically allocated from the Server's bufferAllocator, unless you explicitly supply one. When you call .free on a Buffer object it will release the buffer's memory on the server, and free the buffer number for future reallocation. See ServerOptions for details on setting the number of available buffers.


Normally you should not need to supply a buffer number. You should only do so if you are sure you know what you are doing.


You can control which allocator determines the buffer index numbers by setting the server options blockAllocClass variable prior to booting the server. Two allocators are available to support different kinds of applications. See the ServerOptions help file for details.


Multichannel Buffers


Multichannel buffers interleave their data. Thus the actual number of available values when requesting or setting values by index using methods such as set, setn, get, getn, etc., is equal to numFrames * numChannels. Indices start at 0 and go up to (numFrames * numChannels) - 1. In a two channel buffer for instance, index 0 will be the first value of the first channel, index 1 will be the first value of the second channel, index 2 will be the second value of the first channel, and so on.


In some cases it is simpler to use multiple single channel buffers instead of a single multichannel one.


Completion Messages and Action Functions


Many buffer operations (such as reading and writing files) are asynchronous, meaning that they will take an arbitrary amount of time to complete. Asynchronous commands are passed to a background thread on the server so as not to steal CPU time from the audio synthesis thread. Since they can last an aribitrary amount of time it is convenient to be able to specify something else that can be done immediately on completion. The ability to do this is implemented in two ways in Buffer's various methods: completion messages and action functions. 


A completion message is a second OSC command which is included in the message which is sent to the server. (See NodeMessaging for a discussion of OSC messages.) The server will execute this immediately upon completing the first command. An action function is a Function which will be evaluated when the client receives the appropriate reply from the server, indicating that the previous command is done. Action functions are therefore inherently more flexible than completion messages, but slightly less efficient due to the small amount of added latency involved in message traffic. Action functions are passed the Buffer object as an argument when they are evaluated.


With Buffer methods that take a completion message, it is also possible to pass in a function that returns an OSC message. As in action functions this will be passed the Buffer as an argument. It is important to understand however that this function will be evaluated after the Buffer object has been created (so that its bufnum and other details are accessible), but before the corresponding message is sent to the server.


Bundling


Many of the methods below have two versions: a regular one which sends its corresponding message to the server immediately, and one which returns the message in an Array so that it can be added to a bundle. It is also possible to capture the messages generated by the regular methods using Server's automated bundling capabilities. See Server and bundledCommands for more details.


Accessing Instance Variables


The following variables have getter methods.


server - Returns the Buffer's Server object.

bufnum - Returns the buffer number of the corresponding server-side buffer.


numFrames - Returns the number of sample frames in the corresponding server-side buffer. Note that multichannel buffers interleave their samples, so when dealing with indices in methods like get and getn, the actual number of available values is numFrames * numChannels.

numChannels - Returns the number of channels in the corresponding server-side buffer. 

sampleRate - Returns the sample rate of the corresponding server-side buffer.

path - Returns a string containing the path of a soundfile that has been loaded into the corresponding server-side buffer.

s.boot;

b = Buffer.alloc(s,44100 * 8.0,2);

b.bufnum.postln;

b.free;

Creation with Immediate Memory Allocation


*alloc(server, numFrames, numChannels, completionMessage, bufnum)

Create and return a Buffer and immediately allocate the required memory on the server. The buffer's values will be initialised to 0.0.

server  - The server on which to allocate the buffer. The default is the default Server.

numFrames - The number of frames to allocate. Actual memory use will correspond to numFrames * numChannels.

numChannels - The number of channels for the Buffer. The default is 1.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.

bufnum - An explicitly specified buffer number. Generally this is not needed.

// Allocate 8 second stereo buffer

s.boot;

b = Buffer.alloc(s, s.sampleRate * 8.0, 2);

b.free;

*allocConsecutive(numBufs, server, numFrames, numChannels, completionMessage, bufnum) 


Allocates a range of consecutively-numbered buffers, for use with UGens like VOsc and VOsc3 that require a contiguous block of buffers, and returns an array of corresponding Buffer objects.

numBufs - The number of consecutively indexed buffers to allocate.

server  - The server on which to allocate the buffers. The default is the default Server.

numFrames - The number of frames to allocate in each buffer. Actual memory use will correspond to numFrames * numChannels.

numChannels - The number of channels for each buffer. The default is 1.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed each Buffer and its index in the array as arguments when evaluated.

bufnum - An explicitly specified buffer number for the initial buffer. Generally this is not needed.


N.B. You must treat the array of Buffers as a group. Freeing them individually or resuing them can result in allocation errors. You should free all Buffers in the array at the same time by iterating over it with do.


s.boot;

// allocate an array of Buffers and fill them with different harmonics

(

b = Buffer.allocConsecutive(8, s, 4096, 1, { |buf, i|

buf.sine1Msg((1..((i+1)*6)).reciprocal) // completion Messages

});

)

a = { VOsc.ar(SinOsc.kr(0.5, 0).range(b.first.bufnum + 0.1, b.last.bufnum - 0.1)

[440, 441], 0, 0.2) }.play;

a.free; 

// iterate over the array and free it

b.do(_.free);


*read(server, path, startFrame, numFrames, action, bufnum)


Allocate a buffer and immediately read a soundfile into it. This method sends a query message as a completion message so that the Buffer's instance variables will be updated automatically. N.B. You cannot rely on the buffer's instance variables being instantly updated, as there is a small amount of latency involved. action will be evaluated upon receipt of the reply to the query, so use this in cases where access to instance variables is needed.

server  - The server on which to allocate the buffer.

path - A String representing the path of the soundfile to be read.

startFrame - The first frame of the soundfile to read. The default is 0, which is the beginning of the file.

numFrames - The number of frames to read. The default is -1, which will read the whole file.

action - A Function to be evaluated once the file has been read and this Buffer's instance variables have been updated. The function will be passed this Buffer as an argument.

bufnum - An explicitly specified buffer number. Generally this is not needed.

// read a soundfile

s.boot;

b = Buffer.read(s, "sounds/a11wlk01.wav");

// now play it

(

x = SynthDef("help-Buffer",{ arg out = 0, bufnum;

Out.ar( out,

PlayBuf.ar(1, bufnum, BufRateScale.kr(bufnum))

)

}).play(s,[\bufnum, b.bufnum ]);

)

x.free; b.free;

// with an action function

// note that the vars are not immediately up-to-date

(

b = Buffer.read(s, "sounds/a11wlk01.wav", action: { arg buffer; 

("After update:" + buffer.numFrames).postln;

x = { PlayBuf.ar(1, buffer.bufnum, BufRateScale.kr(buffer.bufnum)) }.play;

});

("Before update:" + b.numFrames).postln;

)

x.free; b.free;


*readChannel(server, path, startFrame, numFrames, channels, action, bufnum)


As *read above, but takes an Array of channel indices to read in, allowing one to read only the selected channels.

server  - The server on which to allocate the buffer.

path - A String representing the path of the soundfile to be read.

startFrame - The first frame of the soundfile to read. The default is 0, which is the beginning of the file.

numFrames - The number of frames to read. The default is -1, which will read the whole file.

channels - An Array of channels to be read from the soundfile. Indices start from zero. These will be read in the order provided.

action - A Function to be evaluated once the file has been read and this Buffer's instance variables have been updated. The function will be passed this Buffer as an argument.

bufnum - An explicitly specified buffer number. Generally this is not needed.

s.boot;

// first a standard read so we can see what's in the file

b = Buffer.read(s, "sounds/SinedPink.aiff");

// "sounds/SinedPink.aiff" contains SinOsc on left, PinkNoise on right

b.plot;

b.free;

// Now just the sine

b = Buffer.readChannel(s, "sounds/SinedPink.aiff", channels: [0]);

b.plot;

b.free;

// Now just the pink noise

b = Buffer.readChannel(s, "sounds/SinedPink.aiff", channels: [1]);

b.plot;

b.free;

// Now reverse channel order

b = Buffer.readChannel(s, "sounds/SinedPink.aiff", channels: [1, 0]);

b.plot;

b.free;


*readNoUpdate(server, path, startFrame, numFrames, completionMessage, bufnum)


As *read above, but without the automatic update of instance variables. Call updateInfo (see below) to update the vars.

server  - The server on which to allocate the buffer.

path - A String representing the path of the soundfile to be read.

startFrame - The first frame of the soundfile to read. The default is 0, which is the beginning of the file.

numFrames - The number of frames to read. The default is -1, which will read the whole file.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.

bufnum - An explicitly specified buffer number. Generally this is not needed.

// with a completion message

s.boot;

(

SynthDef("help-Buffer",{ arg out=0,bufnum;

Out.ar( out,

PlayBuf.ar(1,bufnum,BufRateScale.kr(bufnum))

)

}).send(s);

y = Synth.basicNew("help-Buffer"); // not sent yet

b = Buffer.readNoUpdate(s,"sounds/a11wlk01.wav", 

completionMessage: { arg buffer;

// synth add its s_new msg to follow 

// after the buffer read completes

y.newMsg(s,[\bufnum,buffer.bufnum],\addToTail)

});

)

// note vars not accurate

b.numFrames; // nil

b.updateInfo;

b.numFrames; // 26977

// when done...

y.free;

b.free;

*cueSoundFile(server, path, startFrame, numChannels, bufferSize, completionMessage)

Allocate a buffer and preload a soundfile for streaming in using DiskIn.

server  - The server on which to allocate the buffer.

path - A String representing the path of the soundfile to be read.

startFrame - The frame of the soundfile that DiskIn will start playing at.

numChannels - The number of channels in the soundfile.

bufferSize - This must be a multiple of  (2 * the server's block size). 32768 is the default and is suitable for most cases.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.

s.boot;

(

SynthDef("help-Buffer-cue",{ arg out=0,bufnum;

Out.ar(out,

DiskIn.ar( 1, bufnum )

)

}).send(s);

)

(

s.makeBundle(nil, {

b = Buffer.cueSoundFile(s,"sounds/a11wlk01-44_1.aiff", 0, 1);

y = Synth.new("help-Buffer-cue", [\bufnum,b.bufnum], s);

});

)

b.free; y.free;


*loadCollection(server, collection, numChannels, action)


Load a large collection into a buffer on the server. Returns a Buffer object. This is accomplished through writing the collection to a SoundFile and loading it from there. For this reason this method will only work with a server on your local machine. For a remote server use *sendCollection, below. The file is automatically deleted after loading. This allows for larger collections than setn, below, and is in general the safest way to get a large collection into a buffer. The sample rate of the buffer will be the sample rate of the server on which it is created.

server  - The server on which to create the buffer.

collection - A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.

numChannels - The number of channels that the buffer should have. Note that buffers interleave multichannel data. You are responsible for providing an interleaved collection if needed. Multi-dimensional arrays will not work.

action - A Function to be evaluated once the file has been read and this Buffer's instance variables have been updated. The function will be passed this Buffer as an argument.

s.boot;

(

a = FloatArray.fill(44100 * 5.0, {1.0.rand2}); // 5 seconds of noise

b = Buffer.loadCollection(s, a);

)

// test it

b.get(20000,{|msg| (msg == a[20000]).postln});

// play it

x = { PlayBuf.ar(1, b.bufnum, BufRateScale.kr(b.bufnum), loop: 0) * 0.5 }.play;

b.free; x.free;

// interleave a multi-dimensional array

(

l = Signal.sineFill(16384, Array.fill(200, {0}).add(1));

r = Array.fill(16384, {1.0.rand2});

m = [Array.newFrom(l), r]; // a multi-dimensional array

m = m.lace(32768); // interleave the two collections 

b = Buffer.loadCollection(s, m, 2, {|buf|

x = { PlayBuf.ar(2, buf.bufnum, BufRateScale.kr(buf.bufnum), loop: 1) * 0.5 }.play;

});

)

b.plot;

x.free; b.free;

*sendCollection(server, collection, numChannels, wait, action)


Stream a large collection into a buffer on the server using multiple setn messages. Returns a Buffer object. This allows for larger collections than setn, below. This is not as safe as *loadCollection, above, but will work with servers on remote machines. The sample rate of the buffer will be the sample rate of the server on which it is created.

server  - The server on which to create the buffer.

collection - A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.

numChannels - The number of channels that the buffer should have. Note that buffers interleave multichannel data. You are responsible for providing an interleaved collection if needed. Multi-dimensional arrays will not work. See the example in *loadCollection, above, to see how to do this.

wait - An optional wait time between sending setn messages. In a high traffic situation it may be safer to set this to something above zero, which is the default.

action - A Function to be evaluated once the file has been read and this Buffer's instance variables have been updated. The function will be passed this Buffer as an argument.

s.boot;

(

a = Array.fill(2000000,{ rrand(0.0,1.0) }); // a LARGE collection

b = Buffer.sendCollection(s, a, 1, 0, {arg buf; "finished".postln;});

)

b.get(1999999, {|msg| (msg == a[1999999]).postln});

b.free;

*loadDialog(server, path, startFrame, numFrames, bufnum)


As *read above, but gives you a load dialog window to browse for a file. OSX only.

server  - The server on which to allocate the buffer.

startFrame - The first frame of the soundfile to read. The default is 0, which is the beginning of the file.

numFrames - The number of frames to read. The default is -1, which will read the whole file.

action - A Function to be evaluated once the file has been read and this Buffer's instance variables have been updated. The function will be passed this Buffer as an argument.

bufnum - An explicitly specified buffer number. Generally this is not needed.

s.boot;

(

b = Buffer.loadDialog(s, action: { arg buffer; 

x = { PlayBuf.ar(b.numChannels, buffer.bufnum, BufRateScale.kr(buffer.bufnum)) }.play;

});

)

x.free; b.free;

Creation without Immediate Memory Allocation


*new(server, numFrames, numChannels, bufnum)

Create and return a new Buffer object, without immediately allocating the corresponding memory on the server. This combined with 'message' methods can be flexible with bundles.

server  - The server on which to allocate the buffer. The default is the default Server.

numFrames - The number of frames to allocate. Actual memory use will correspond to numFrames * numChannels.

numChannels - The number of channels for the Buffer. The default is 1.

bufnum - An explicitly specified buffer number. Generally this is not needed.

s.boot;

b = Buffer.new(s, 44100 * 8.0, 2);

c = Buffer.new(s, 44100 * 4.0, 2);

b.query; // numFrames = 0

s.sendBundle(nil, b.allocMsg, c.allocMsg); // sent both at the same time

b.query; // now it's right

c.query;

b.free; c.free;

alloc(completionMessage)

allocMsg(completionMessage)

Allocate the necessary memory on the server for a Buffer previously created with *new, above.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.

s.boot;

b = Buffer.new(s, 44100 * 8.0, 2);

b.query; // numFrames = 0

b.alloc;

b.query; // numFrames = 352800

b.free;


allocRead(argpath, startFrame, numFrames, completionMessage)

allocReadMsg(argpath, startFrame, numFrames, completionMessage)

Read a soundfile into a buffer on the server for a Buffer previously created with *new, above. Note that this will not autoupdate instance variables. Call updateInfo in order to do this.


argpath - A String representing the path of the soundfile to be read.

startFrame - The first frame of the soundfile to read. The default is 0, which is the beginning of the file.

numFrames - The number of frames to read. The default is -1, which will read the whole file.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.

s.boot;

b = Buffer.new(s);

b.allocRead("sounds/a11wlk01.wav");

x = { PlayBuf.ar(1, b.bufnum, BufRateScale.kr(b.bufnum), loop: 1) * 0.5 }.play;

x.free; b.free;


allocReadChannel(argpath, startFrame, numFrames, channels, completionMessage)

allocReadChannelMsg(argpath, startFrame, numFrames, channels, completionMessage)

As allocRead above, but allows you to specify which channels to read.


argpath - A String representing the path of the soundfile to be read.

startFrame - The first frame of the soundfile to read. The default is 0, which is the beginning of the file.

numFrames - The number of frames to read. The default is -1, which will read the whole file.

channels - An Array of channels to be read from the soundfile. Indices start from zero. These will be read in the order provided.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.

s.boot;

b = Buffer.new(s);

// read only the first channel (a Sine wave) of a stereo file

b.allocReadChannel("sounds/SinedPink.aiff", channels: [0]);

x = { PlayBuf.ar(1, b.bufnum, BufRateScale.kr(b.bufnum), loop: 1) * 0.5 }.play;

x.free; b.free;

Instance Methods

read(path, fileStartFrame, numFrames, bufStartFrame, leaveOpen, action);

readMsg(path, fileStartFrame, numFrames, bufStartFrame, leaveOpen, completionMessage);


Read a soundfile into an already allocated buffer. Note that if the number of frames in the file is greater than the number of frames in the buffer, it will be truncated. Note that readMsg will not auto-update instance variables. Call updateInfo in order to do this.

path - A String representing the path of the soundfile to be read.

fileStartFrame - The first frame of the soundfile to read. The default is 0, which is the beginning of the file.

numFrames - The number of frames to read. The default is -1, which will read the whole file.

bufStartFrame - The index of the frame in the buffer at which to start reading. The default is 0, which is the beginning of the buffer.

leaveOpen - A boolean indicating whether or not the Buffer should be left 'open'. For use with DiskIn you will want this to be true, as the buffer will be used for streaming the soundfile in from disk. (For this the buffer must have been allocated with a multiple of (2 * synth block size).  A common number is 32768 frames. cueSoundFile below, provides a simpler way of doing this.) The default is false which is the correct value for all other cases.

action - A Function to be evaluated once the file has been read and this Buffer's instance variables have been updated. The function will be passed this Buffer as an argument.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.

readChannel(path, fileStartFrame, numFrames, bufStartFrame, leaveOpen, channels, action);

readChannelMsg(path, fileStartFrame, numFrames, bufStartFrame, leaveOpen, channels, completionMessage);


As read above, but allows you to specify which channels to read.

path - A String representing the path of the soundfile to be read.

fileStartFrame - The first frame of the soundfile to read. The default is 0, which is the beginning of the file.

numFrames - The number of frames to read. The default is -1, which will read the whole file.

bufStartFrame - The index of the frame in the buffer at which to start reading. The default is 0, which is the beginning of the buffer.

leaveOpen - A boolean indicating whether or not the Buffer should be left 'open'. For use with DiskIn you will want this to be true, as the buffer will be used for streaming the soundfile in from disk. (For this the buffer must have been allocated with a multiple of (2 * synth block size).  A common number is 32768 frames. cueSoundFile below, provides a simpler way of doing this.) The default is false which is the correct value for all other cases.

channels - An Array of channels to be read from the soundfile. Indices start from zero. These will be read in the order provided. The number of channels requested must match this Buffer's numChannels.

action - A Function to be evaluated once the file has been read and this Buffer's instance variables have been updated. The function will be passed this Buffer as an argument.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.


cueSoundFile(path, startFrame, completionMessage)

cueSoundFileMsg(path, startFrame, completionMessage)


A convenience method to cue a soundfile into the buffer for use with a DiskIn. The buffer must have been allocated with a multiple of (2 * the server's block size) frames.  A common size is 32768 frames.

path - A String representing the path of the soundfile to be read.

startFrame - The first frame of the soundfile to read. The default is 0, which is the beginning of the file.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.

s.boot;

//create with cueSoundFile class method

b = Buffer.cueSoundFile(s, "sounds/a11wlk01-44_1.aiff", 0, 1);

x = { DiskIn.ar(1, b.bufnum) }.play;

b.close; // must call close in between cueing

// now use like named instance method, but different arguments

b.cueSoundFile("sounds/a11wlk01-44_1.aiff");

// have to do all this to clean up properly!

x.free; b.close; b.free;

write(path, headerFormat, sampleFormat, numFrames, startFrame, leaveOpen, completionMessage)

writeMsg(path, headerFormat, sampleFormat, numFrames, startFrame, leaveOpen, completionMessage)


Write the contents of the buffer to a file. See SoundFile for information on valid values for headerFormat and sampleFormat.

path - A String representing the path of the soundfile to be written.

numFrames - The number of frames to write. The default is -1, which will write the whole buffer.

startFrame - The index of the frame in the buffer from which to start writing. The default is 0, which is the beginning of the buffer.

leaveOpen - A boolean indicating whether or not the Buffer should be left 'open'. For use with DiskOut you will want this to be true. The default is false which is the correct value for all other cases.

completionMessage - A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.


free(completionMessage)

freeMsg(completionMessage)

Release the buffer's memory on the server and return the bufferID back to the server's buffer num Berlin: clubs bars cafes nightlife going out