2 summary:: Client-side representation of a buffer on a server
3 categories:: Server>Abstractions
7 A buffer is most often used to hold sampled audio, such as a soundfile loaded into memory, but can be used to hold other types of data as well. It is a globally available array of floating-point numbers on the server. The Buffer class encapsulates a number of common tasks, OSC messages, and capabilities related to server-side buffers – see the examples lower down this document for many examples of using Buffers for sound playback and recording.
9 Buffers are commonly used with link::Classes/PlayBuf::, link::Classes/RecordBuf::, link::Classes/DiskIn::, link::Classes/DiskOut::, link::Classes/BufWr::, link::Classes/BufRd::, and other UGens. (See their individual help files for more examples.) Buffers can be freed or altered even while being accessed. See link::Reference/Server-Architecture:: for some technical details.
11 Buffer objects should not be created or modified within a link::Classes/SynthDef::. If this is needed, see link::Classes/LocalBuf::.
13 subsection:: Buffer Numbers and Allocation
15 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.)
17 link::Classes/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 code::.free:: on a Buffer object it will release the buffer's memory on the server, and free the buffer number for future reallocation. See link::Classes/ServerOptions:: for details on setting the number of available buffers.
19 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. Similarly, in normal use you should not need to access the buffer number, since instances of Buffer can be used directly as link::Classes/UGen:: inputs or link::Classes/Synth:: args.
21 subsection:: Multichannel Buffers
23 Multichannel buffers interleave their data. Thus the actual number of available values when requesting or setting values by index using methods such as code::set, setn, get, getn::, etc., is equal to code::numFrames * numChannels::.
24 Indices start at 0 and go up to code::(numFrames * numChannels) - 1::.
25 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.
27 In some cases it is simpler to use multiple single channel buffers instead of a single multichannel one.
29 subsection:: Completion Messages and Action Functions
31 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.
32 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.
33 The ability to do this is implemented in two ways in Buffer's various methods: completion messages and action functions.
35 A completion message is a second OSC command which is included in the message which is sent to the server. (See link::Guides/NodeMessaging:: for a discussion of OSC messages.)
36 The server will execute this immediately upon completing the first command.
37 An action function is a link::Classes/Function:: which will be evaluated when the client receives the appropriate reply from the server, indicating that the previous command is done.
38 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.
40 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.
41 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.
45 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 link::Classes/Array:: so that it can be added to a bundle.
46 It is also possible to capture the messages generated by the regular methods using Server's automated bundling capabilities.
47 See link::Classes/Server:: and link::Guides/Bundled-Messages:: for more details.
50 private:: initClass, initServerCache, clearServerCaches
52 subsection:: Creation with Immediate Memory Allocation
55 Create and return a Buffer and immediately allocate the required memory on the server. The buffer's values will be initialised to 0.0.
57 The server on which to allocate the buffer. The default is the default Server.
59 The number of frames to allocate. Actual memory use will correspond to numFrames * numChannels.
60 argument:: numChannels
61 The number of channels for the Buffer. The default is 1.
62 argument:: completionMessage
63 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
65 An explicitly specified buffer number. Generally this is not needed.
68 // Allocate 8 second stereo buffer
70 b = Buffer.alloc(s, s.sampleRate * 8.0, 2);
74 method:: allocConsecutive
75 Allocates a range of consecutively-numbered buffers, for use with UGens like link::Classes/VOsc:: and link::Classes/VOsc3:: that require a contiguous block of buffers, and returns an array of corresponding Buffer objects.
77 The number of consecutively indexed buffers to allocate.
79 The server on which to allocate the buffers. The default is the default Server.
81 The number of frames to allocate in each buffer. Actual memory use will correspond to numFrames * numChannels.
82 argument:: numChannels
83 The number of channels for each buffer. The default is 1.
84 argument:: completionMessage
85 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.
87 An explicitly specified buffer number for the initial buffer. Generally this is not needed.
89 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.
92 // allocate an array of Buffers and fill them with different harmonics
94 b = Buffer.allocConsecutive(8, s, 4096, 1, { |buf, i|
95 buf.sine1Msg((1..((i+1)*6)).reciprocal) // completion Messages
98 a = { VOsc.ar(SinOsc.kr(0.5, 0).range(b.first.bufnum + 0.1, b.last.bufnum - 0.1),
99 [440, 441], 0, 0.2) }.play;
103 // iterate over the array and free it
108 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.
110 The server on which to allocate the buffer.
112 A String representing the path of the soundfile to be read.
113 argument:: startFrame
114 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
116 The number of frames to read. The default is -1, which will read the whole file.
118 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.
120 An explicitly specified buffer number. Generally this is not needed.
123 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.
127 p = Platform.resourceDir +/+ "sounds/a11wlk01.wav";
128 b = Buffer.read(s, p);
132 x = SynthDef(\help_Buffer, { arg out = 0, bufnum;
134 PlayBuf.ar(1, bufnum, BufRateScale.kr(bufnum))
136 }).play(s,[\bufnum, b]);
140 // with an action function
141 // note that the vars are not immediately up-to-date
143 b = Buffer.read(s, p, action: { arg buffer;
144 ("After update:" + buffer.numFrames).postln;
145 x = { PlayBuf.ar(1, buffer, BufRateScale.kr(buffer)) }.play;
147 ("Before update:" + b.numFrames).postln;
153 As link::#*read:: above, but takes an link::Classes/Array:: of channel indices to read in, allowing one to read only the selected channels.
155 The server on which to allocate the buffer.
157 A String representing the path of the soundfile to be read.
158 argument:: startFrame
159 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
161 The number of frames to read. The default is -1, which will read the whole file.
163 An Array of channels to be read from the soundfile. Indices start from zero. These will be read in the order provided.
165 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.
167 An explicitly specified buffer number. Generally this is not needed.
171 // first a standard read so we can see what's in the file
172 b = Buffer.read(s, Platform.resourceDir +/+ "sounds/SinedPink.aiff");
173 // Platform.resourceDir +/+ "sounds/SinedPink.aiff" contains SinOsc on left, PinkNoise on right
178 b = Buffer.readChannel(s, Platform.resourceDir +/+ "sounds/SinedPink.aiff", channels: [0]);
182 // Now just the pink noise
183 b = Buffer.readChannel(s, Platform.resourceDir +/+ "sounds/SinedPink.aiff", channels: [1]);
187 // Now reverse channel order
188 b = Buffer.readChannel(s, Platform.resourceDir +/+ "sounds/SinedPink.aiff", channels: [1, 0]);
193 method:: readNoUpdate
194 As link::#*read:: above, but without the automatic update of instance variables. Call code::updateInfo:: (see below) to update the vars.
196 The server on which to allocate the buffer.
198 A String representing the path of the soundfile to be read.
199 argument:: startFrame
200 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
202 The number of frames to read. The default is -1, which will read the whole file.
203 argument:: completionMessage
204 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
206 An explicitly specified buffer number. Generally this is not needed.
209 // with a completion message
212 SynthDef(\help_Buffer,{ arg out=0, bufnum;
214 PlayBuf.ar(1,bufnum,BufRateScale.kr(bufnum))
218 y = Synth.basicNew(\help_Buffer); // not sent yet
219 b = Buffer.readNoUpdate(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav",
220 completionMessage: { arg buffer;
221 // synth add its s_new msg to follow
222 // after the buffer read completes
223 y.newMsg(s,[\bufnum, buffer],\addToTail)
226 // note vars not accurate
229 b.numFrames; // 188893
235 method:: cueSoundFile
236 Allocate a buffer and preload a soundfile for streaming in using link::Classes/DiskIn::.
238 The server on which to allocate the buffer.
240 A String representing the path of the soundfile to be read.
241 argument:: startFrame
242 The frame of the soundfile that DiskIn will start playing at.
243 argument:: numChannels
244 The number of channels in the soundfile.
245 argument:: bufferSize
246 This must be a multiple of (2 * the server's block size). 32768 is the default and is suitable for most cases.
247 argument:: completionMessage
248 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
253 SynthDef(\help_Buffer_cue,{ arg out=0,bufnum;
255 DiskIn.ar( 1, bufnum )
262 b = Buffer.cueSoundFile(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav", 0, 1);
263 y = Synth(\help_Buffer_cue, [\bufnum, b], s);
269 method:: loadCollection
270 Load a large collection into a buffer on the server. Returns a Buffer object.
272 The server on which to create the buffer.
273 argument:: collection
274 A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.
275 argument:: numChannels
276 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.
278 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.
281 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 code::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.
285 a = FloatArray.fill(44100 * 5.0, {1.0.rand2}); // 5 seconds of noise
286 b = Buffer.loadCollection(s, a);
290 b.get(20000,{|msg| (msg == a[20000]).postln});
292 x = { PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 0) * 0.5 }.play;
295 // interleave a multi-dimensional array
297 l = Signal.sineFill(16384, Array.fill(200, {0}).add(1));
298 r = Array.fill(16384, {1.0.rand2});
299 m = [Array.newFrom(l), r]; // a multi-dimensional array
300 m = m.lace(32768); // interleave the two collections
301 b = Buffer.loadCollection(s, m, 2, {|buf|
302 x = { PlayBuf.ar(2, buf, BufRateScale.kr(buf), loop: 1) * 0.5 }.play;
309 method:: sendCollection
310 Stream a large collection into a buffer on the server using multiple setn messages. Returns a Buffer object.
312 The server on which to create the buffer.
313 argument:: collection
314 A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.
315 argument:: numChannels
316 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 link::#*loadCollection:: above, to see how to do this.
318 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.
320 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.
322 This allows for larger collections than setn, below. This is not as safe as link::#*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.
326 a = Array.fill(2000000,{ rrand(0.0,1.0) }); // a LARGE collection
327 b = Buffer.sendCollection(s, a, 1, 0, {arg buf; "finished".postln;});
329 b.get(1999999, {|msg| (msg == a[1999999]).postln});
334 As link::#*read:: above, but gives you a load dialog window to browse for a file. Cocoa and SwingOSC compatible.
336 The server on which to allocate the buffer.
337 argument:: startFrame
338 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
340 The number of frames to read. The default is -1, which will read the whole file.
342 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.
344 An explicitly specified buffer number. Generally this is not needed.
349 b = Buffer.loadDialog(s, action: { arg buffer;
350 x = { PlayBuf.ar(buffer.numChannels, buffer, BufRateScale.kr(buffer)) }.play;
356 subsection:: Creation without Immediate Memory Allocation
358 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.
360 The server on which to allocate the buffer. The default is the default Server.
362 The number of frames to allocate. Actual memory use will correspond to numFrames * numChannels.
363 argument:: numChannels
364 The number of channels for the Buffer. The default is 1.
366 An explicitly specified buffer number. Generally this is not needed.
370 b = Buffer.new(s, 44100 * 8.0, 2);
371 c = Buffer.new(s, 44100 * 4.0, 2);
372 b.query; // numFrames = 0
373 s.sendBundle(nil, b.allocMsg, c.allocMsg); // sent both at the same time
374 b.query; // now it's right
379 subsection:: Cached Buffers
381 To assist with automatic updates of buffer information (see code::updateInfo:: and code::read::), buffer objects are cached in a collection associated with the link::Classes/Server:: object hosting the buffers.
382 Freeing a buffer removes it from the cache; quitting the server clears all the cached buffers. (This also occurs if the server crashes unexpectedly.)
384 You may access cached buffers using the following methods.
386 It may be simpler to access them through the server object:
388 myServer.cachedBufferAt(bufnum)
389 myServer.cachedBuffersDo(func)
391 b = Buffer.alloc(s, 2048, 1);
392 Buffer.cachedBufferAt(s, 0); // assuming b has bufnum 0
393 s.cachedBufferAt(0); // same result
394 s.cachedBuffersDo({ |buf| buf.postln });
397 method:: cachedBufferAt
398 Access a buffer by its number.
400 method:: cachedBuffersDo
401 Iterate over all cached buffers. The iteration is not in any order, but will touch all buffers.
406 subsection:: Variables
408 The following variables have getter methods.
411 Returns the Buffer's Server object.
414 Returns the buffer number of the corresponding server-side buffer. In normal use you should not need to access this value, since instances of Buffer can be used directly as UGen inputs or Synth args.
418 b = Buffer.alloc(s,44100 * 8.0,2);
424 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.
427 Returns the number of channels in the corresponding server-side buffer.
430 Returns the sample rate of the corresponding server-side buffer.
433 Returns a string containing the path of a soundfile that has been loaded into the corresponding server-side buffer.
436 subsection:: Explicit allocation
438 These methods allocate the necessary memory on the server for a Buffer previously created with link::#*new::.
440 method:: alloc, allocMsg
441 argument:: completionMessage
442 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
446 b = Buffer.new(s, 44100 * 8.0, 2);
447 b.query; // numFrames = 0
449 b.query; // numFrames = 352800
453 method:: allocRead, allocReadMsg
454 Read a soundfile into a buffer on the server for a Buffer previously created with link::#*new::. Note that this will not autoupdate instance variables. Call code::updateInfo:: in order to do this.
456 A String representing the path of the soundfile to be read.
457 argument:: startFrame
458 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
460 The number of frames to read. The default is -1, which will read the whole file.
461 argument:: completionMessage
462 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
467 b.allocRead(Platform.resourceDir +/+ "sounds/a11wlk01.wav");
468 x = { PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1) * 0.5 }.play;
472 method:: allocReadChannel, allocReadChannelMsg
473 As link::#-allocRead:: above, but allows you to specify which channels to read.
475 A String representing the path of the soundfile to be read.
476 argument:: startFrame
477 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
479 The number of frames to read. The default is -1, which will read the whole file.
481 An Array of channels to be read from the soundfile. Indices start from zero. These will be read in the order provided.
482 argument:: completionMessage
483 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
488 // read only the first channel (a Sine wave) of a stereo file
489 b.allocReadChannel(Platform.resourceDir +/+ "sounds/SinedPink.aiff", channels: [0]);
490 x = { PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1) * 0.5 }.play;
494 subsection:: Other methods
496 method:: read, readMsg
497 Read a soundfile into an already allocated buffer.
499 A String representing the path of the soundfile to be read.
500 argument:: fileStartFrame
501 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
503 The number of frames to read. The default is -1, which will read the whole file.
504 argument:: bufStartFrame
505 The index of the frame in the buffer at which to start reading. The default is 0, which is the beginning of the buffer.
507 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).
508 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.
510 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.
511 argument:: completionMessage
512 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
515 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.
517 method:: readChannel, readChannelMsg
518 As link::#-read:: above, but allows you to specify which channels to read.
520 A String representing the path of the soundfile to be read.
521 argument:: fileStartFrame
522 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
524 The number of frames to read. The default is -1, which will read the whole file.
525 argument:: bufStartFrame
526 The index of the frame in the buffer at which to start reading. The default is 0, which is the beginning of the buffer.
528 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).
529 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.
531 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.
533 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.
534 argument:: completionMessage
535 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
537 method:: cueSoundFile, cueSoundFileMsg
538 A convenience method to cue a soundfile into the buffer for use with a link::Classes/DiskIn::. The buffer must have been allocated with a multiple of (2 * the server's block size) frames. A common size is 32768 frames.
540 A String representing the path of the soundfile to be read.
541 argument:: startFrame
542 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
543 argument:: completionMessage
544 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
548 //create with cueSoundFile class method
549 b = Buffer.cueSoundFile(s, Platform.resourceDir +/+ "sounds/a11wlk01-44_1.aiff", 0, 1);
550 x = { DiskIn.ar(1, b) }.play;
551 b.close; // must call close in between cueing
552 // now use like named instance method, but different arguments
553 b.cueSoundFile(Platform.resourceDir +/+ "sounds/a11wlk01-44_1.aiff");
554 // have to do all this to clean up properly!
555 x.free; b.close; b.free;
558 method:: write, writeMsg
559 Write the contents of the buffer to a file. See link::Classes/SoundFile:: for information on valid values for headerFormat and sampleFormat.
561 A String representing the path of the soundfile to be written.
562 If no path is given, Buffer writes into the default recording directory with a generic name.
564 The number of frames to write. The default is -1, which will write the whole buffer.
565 argument:: startFrame
566 The index of the frame in the buffer from which to start writing. The default is 0, which is the beginning of the buffer.
568 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.
569 argument:: completionMessage
570 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
572 method:: free, freeMsg
573 Release the buffer's memory on the server and return the bufferID back to the server's buffer number allocator for future reuse.
574 argument:: completionMessage
575 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
577 method:: zero, zeroMsg
578 Sets all values in this buffer to 0.0.
579 argument:: completionMessage
580 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
583 Set the value in the buffer at index to be equal to float. Additional pairs of indices and floats may be included in the same message.
585 Note that multichannel buffers interleave their sample data, therefore the actual number of available values is equal to code::numFrames * numChannels::. Indices start at 0.
588 b = Buffer.alloc(s, 4, 2);
589 b.set(0, 0.2, 1, 0.3, 7, 0.4); // set the values at indices 0, 1, and 7.
590 b.getn(0, 8, {|msg| msg.postln});
594 method:: setn, setnMsg
595 Set a contiguous range of values in the buffer starting at the index startAt to be equal to the Array of floats or integers, values. The number of values set corresponds to the size of values. Additional pairs of starting indices and arrays of values may be included in the same message.
597 Note that multichannel buffers interleave their sample data, therefore the actual number of available values is equal to code::numFrames * numChannels::. You are responsible for interleaving the data in values if needed. Multi-dimensional arrays will not work. Indices start at 0.
599 N.B. The maximum number of values that you can set with a single setn message is 1633 when the server is using UDP as its communication protocol. Use link::#-loadCollection:: and link::#-sendCollection:: to set larger ranges of values.
602 b = Buffer.alloc(s,16);
603 b.setn(0, Array.fill(16, { rrand(0,1) }));
604 b.getn(0, b.numFrames, {|msg| msg.postln});
605 b.setn(0, [1, 2, 3], 4, [1, 2, 3]);
606 b.getn(0, b.numFrames, {|msg| msg.postln});
610 method:: loadCollection
611 Load a large collection into this buffer. 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.
612 argument:: collection
613 A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.
614 argument:: startFrame
615 The index of the frame at which to start loading the collection. The default is 0, which is the start of the buffer.
617 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.
620 This allows for larger collections than setn, above, 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 was created.
621 The number of channels and frames will have been determined when the buffer was allocated. You are responsible for making sure that the size of collection is not greater than numFrames, and for interleaving any data if needed.
625 v = Signal.sineFill(128, 1.0/[1,2,3,4,5,6]);
626 b = Buffer.alloc(s, 128);
629 b.loadCollection(v, action: {|buf|
630 x = { PlayBuf.ar(buf.numChannels, buf, BufRateScale.kr(buf), loop: 1)
636 // interleave a multi-dimensional array
638 l = Signal.sineFill(16384, Array.fill(200, {0}).add(1));
639 r = Array.fill(16384, {1.0.rand2});
640 m = [Array.newFrom(l), r]; // a multi-dimensional array
641 m = m.lace(32768); // interleave the two collections
642 b = Buffer.alloc(s, 16384, 2);
645 b.loadCollection(m, 0, {|buf|
646 x = { PlayBuf.ar(2, buf, BufRateScale.kr(buf), loop: 1) * 0.5 }.play;
653 method:: sendCollection
654 Stream a large collection into this buffer using multiple setn messages.
655 argument:: collection
656 A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.
657 argument:: startFrame
658 The index of the frame at which to start streaming in the collection. The default is 0, which is the start of the buffer.
660 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.
662 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.
664 This allows for larger collections than setn. 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.
668 a = Array.fill(2000000,{ rrand(0.0,1.0) });
669 b = Buffer.alloc(s, 2000000);
671 b = b.sendCollection(a, action: {arg buf; "finished".postln;});
672 b.get(1999999, {|msg| (msg == a[1999999]).postln});
677 Send a message requesting the value in the buffer at index. action is a Function which will be passed the value as an argument and evaluated when a reply is received.
681 b = Buffer.alloc(s,16);
682 b.setn(0, Array.fill(16, { rrand(0.0, 1.0) }));
683 b.get(0, {|msg| msg.postln});
687 method:: getn, getMsg
688 Send a message requesting the a contiguous range of values of size count starting from index. action is a Function which will be passed the values in an Array as an argument and evaluated when a reply is received. See setn above for an example.
690 N.B. The maximum number of values that you can get with a single getn message is 1633 when the server is using UDP as its communication protocol. Use link::#-loadToFloatArray:: and link::#-getToFloatArray:: to get larger ranges of values.
692 method:: loadToFloatArray
693 Write the buffer to a file and then load it into a FloatArray.
695 The index in the buffer to begin writing from. The default is 0.
697 The number of values to write. The default is -1, which writes from index until the end of the buffer.
699 A Function which will be passed the resulting FloatArray as an argument and evaluated when loading is finished.
701 This is safer than getToFloatArray but only works with a server on your local machine. In general this is the safest way to get a large range of values from a server buffer into the client app.
704 b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
705 // same as Buffer.plot
706 b.loadToFloatArray(action: { arg array; a = array; {a.plot;}.defer; "done".postln;});
710 method:: getToFloatArray
711 Stream the buffer to the client using a series of getn messages and put the results into a FloatArray.
713 The index in the buffer to begin writing from. The default is 0.
715 The number of values to write. The default is -1, which writes from index until the end of the buffer.
717 The amount of time in seconds to wait between sending getn messages. Longer times are safer. The default is 0.01 seconds which seems reliable under normal circumstances. A setting of 0 is not recommended.
719 The amount of time in seconds after which to post a warning if all replies have not yet been received. the default is 3.
721 A Function which will be passed the resulting FloatArray as an argument and evaluated when all replies have been received.
723 This is more risky than loadToFloatArray but does works with servers on remote machines. In high traffic situations it is possible for data to be lost. If this method has not received all its replies by timeout it will post a warning saying that the method has failed. In general use loadToFloatArray instead wherever possible.
726 b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
728 b.getToFloatArray(wait:0.01,action:{arg array; a = array; {a.plot;}.defer;"done".postln;});
732 method:: normalize, normalizeMsg
733 Normalizes the buffer so that the peak absolute value is newmax (which defaults to 1). If your buffer is in Wavetable format then set the asWavetable argument to true.
735 method:: fill, fillMsg
736 Starting at the index startAt, set the next numFrames to value. Additional ranges may be included in the same message.
738 method:: copyData, copyMsg
739 Starting at the index srcSamplePos, copy numSamples samples from this to the destination buffer buf starting at dstSamplePos. If numSamples is negative, the maximum number of samples possible is copied. The default is start from 0 in the source and copy the maximum number possible starting at 0 in the destination.
741 Note: This method used to be called copy, but this conflicts with the method for object-copying. Therefore Buffer:copy is now intended to create a copy of the client-side Buffer object. Its use for copying buffer data on the server is deprecated. If you see a deprecation warning, the data will still be copied on the server and your code will still work, but you should update your code for the new method name.
745 SynthDef(\help_Buffer_copy, { arg out=0, buf;
746 Line.ar(0, 0, dur: BufDur.kr(buf), doneAction: 2); // frees itself
747 Out.ar(out, PlayBuf.ar(1, buf));
752 b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
753 c = Buffer.alloc(s, 120000);
756 Synth(\help_Buffer_copy, [\buf, b]);
758 // copy the whole buffer
760 Synth(\help_Buffer_copy, [\buf, c]);
764 b.copyData(c, numSamples: 4410);
765 Synth(\help_Buffer_copy, [\buf, c]);
767 // buffer "compositing"
769 b.copyData(c, numSamples: 4410);
770 b.copyData(c, dstStartAt: 4410, numSamples: 15500);
771 Synth(\help_Buffer_copy, [\buf, c]);
777 method:: close, closeMsg
778 After using a Buffer with a DiskOut or DiskIn, it is necessary to close the soundfile. Failure to do so can cause problems.
779 argument:: completionMessage
780 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
783 Plot the contents of the Buffer in a GUI window.
785 The name of the resulting window.
787 An instance of Rect determining the size of the resulting view.
789 the minimum value in the plot
791 the maximum value in the plot
793 a window to place the plot in. If nil, one will be created for you
797 b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
803 Plays the contents of the buffer on the server and returns a corresponding Synth.
805 A Boolean indicating whether or not to loop playback. If false the synth will automatically be freed when done. The default is false.
807 A value by which to scale the output. The default is 1.
811 b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
812 b.play; // frees itself
818 Sends a b_query message to the server, asking for a description of this buffer. The results are posted to the post window. Does not update instance vars.
821 Sends a b_query message to the server, asking for a description of this buffer. Upon reply this Buffer's instance variables are automatically updated.
823 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.
827 b = Buffer.readNoUpdate(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
828 b.numFrames; // incorrect, shows nil
829 b.updateInfo({|buf| buf.numFrames.postln; }); // now it's right
833 subsection:: Buffer Fill Commands
834 These correspond to the various b_gen OSC Commands, which fill the buffer with values for use. See link::Reference/Server-Command-Reference:: for more details.
837 This is a generalized version of the commands below.
838 argument:: genCommand
839 A String indicating the name of the command to use. See Server-Command-Reference for a list of valid command names.
841 An Array containing the corresponding arguments to the command.
843 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
844 argument:: asWavetable
845 A Boolean indicating whether or not to write to the buffer in wavetable format so that it can be read by interpolating oscillators. The default is true.
846 argument:: clearFirst
847 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
849 method:: sine1, sine1Msg
850 Fill this buffer with a series of sine wave harmonics using specified amplitudes.
852 An Array containing amplitudes for the harmonics. The first float value specifies the amplitude of the first partial, the second float value specifies the amplitude of the second partial, and so on.
854 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
855 argument:: asWavetable
856 A Boolean indicating whether or not to write to the buffer in wavetable format so that it can be read by interpolating oscillators. The default is true.
857 argument:: clearFirst
858 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
863 b = Buffer.alloc(s, 512, 1);
864 b.sine1(1.0 / [1, 2, 3, 4], true, true, true);
865 x = { Osc.ar(b, 200, 0, 0.5) }.play;
870 method:: sine2, sine2Msg
871 Fill this buffer with a series of sine wave partials using specified frequencies and amplitudes.
873 An Array containing frequencies (in cycles per buffer) for the partials.
875 An Array containing amplitudes for the partials. This should contain the same number of items as freqs.
877 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
878 argument:: asWavetable
879 A Boolean indicating whether or not to write to the buffer in wavetable format so that it can be read by interpolating oscillators. The default is true.
880 argument:: clearFirst
881 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
886 b = Buffer.alloc(s, 512, 1);
887 b.sine2([1.0, 3], [1, 0.5]);
888 x = { Osc.ar(b, 200, 0, 0.5) }.play;
893 method:: sine3, sine3Msg
894 Fill this buffer with a series of sine wave partials using specified frequencies, amplitudes, and initial phases.
896 An Array containing frequencies (in cycles per buffer) for the partials.
898 An Array containing amplitudes for the partials. This should contain the same number of items as freqs.
900 An Array containing initial phase for the partials (in radians). This should contain the same number of items as freqs.
902 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
903 argument:: asWavetable
904 A Boolean indicating whether or not to write to the buffer in wavetable format so that it can be read by interpolating oscillators. The default is true.
905 argument:: clearFirst
906 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
908 method:: cheby, chebyMsg
909 Fill this buffer with a series of chebyshev polynomials, which can be defined as: code::cheby(n) = amplitude * cos(n * acos(x))::. To eliminate a DC offset when used as a waveshaper, the wavetable is offset so that the center value is zero.
910 argument:: amplitudes
911 An Array containing amplitudes for the harmonics. The first float value specifies the amplitude for n = 1, the second float value specifies the amplitude for n = 2, and so on.
913 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
914 argument:: asWavetable
915 A Boolean indicating whether or not to write to the buffer in wavetable format so that it can be read by interpolating oscillators. The default is true.
916 argument:: clearFirst
917 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
921 b = Buffer.alloc(s, 512, 1, {arg buf; buf.chebyMsg([1,0,1,1,0,1])});
926 SinOsc.ar(300, 0, Line.kr(0,1,6)),