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 = Help.dir +/+ "sounds/a11wlk01.wav";
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, Help.dir +/+ "sounds/SinedPink.aiff");
173 // Help.dir +/+ "sounds/SinedPink.aiff" contains SinOsc on left, PinkNoise on right
178 b = Buffer.readChannel(s, Help.dir +/+ "sounds/SinedPink.aiff", channels: [0]);
182 // Now just the pink noise
183 b = Buffer.readChannel(s, Help.dir +/+ "sounds/SinedPink.aiff", channels: [1]);
187 // Now reverse channel order
188 b = Buffer.readChannel(s, Help.dir +/+ "sounds/SinedPink.aiff", channels: [1, 0]);
194 Read example sound file to buffer via code::read::.
196 method:: readNoUpdate
197 As link::#*read:: above, but without the automatic update of instance variables. Call code::updateInfo:: (see below) to update the vars.
199 The server on which to allocate the buffer.
201 A String representing the path of the soundfile to be read.
202 argument:: startFrame
203 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
205 The number of frames to read. The default is -1, which will read the whole file.
206 argument:: completionMessage
207 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
209 An explicitly specified buffer number. Generally this is not needed.
212 // with a completion message
215 SynthDef(\help_Buffer,{ arg out=0, bufnum;
217 PlayBuf.ar(1,bufnum,BufRateScale.kr(bufnum))
221 y = Synth.basicNew(\help_Buffer); // not sent yet
222 b = Buffer.readNoUpdate(s, Help.dir +/+ "sounds/a11wlk01.wav",
223 completionMessage: { arg buffer;
224 // synth add its s_new msg to follow
225 // after the buffer read completes
226 y.newMsg(s,[\bufnum, buffer],\addToTail)
229 // note vars not accurate
232 b.numFrames; // 188893
238 method:: cueSoundFile
239 Allocate a buffer and preload a soundfile for streaming in using link::Classes/DiskIn::.
241 The server on which to allocate the buffer.
243 A String representing the path of the soundfile to be read.
244 argument:: startFrame
245 The frame of the soundfile that DiskIn will start playing at.
246 argument:: numChannels
247 The number of channels in the soundfile.
248 argument:: bufferSize
249 This must be a multiple of (2 * the server's block size). 32768 is the default and is suitable for most cases.
250 argument:: completionMessage
251 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
256 SynthDef(\help_Buffer_cue,{ arg out=0,bufnum;
258 DiskIn.ar( 1, bufnum )
265 b = Buffer.cueSoundFile(s, Help.dir +/+ "sounds/a11wlk01.wav", 0, 1);
266 y = Synth(\help_Buffer_cue, [\bufnum, b], s);
272 method:: loadCollection
273 Load a large collection into a buffer on the server. Returns a Buffer object.
275 The server on which to create the buffer.
276 argument:: collection
277 A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.
278 argument:: numChannels
279 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.
281 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.
284 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.
288 a = FloatArray.fill(44100 * 5.0, {1.0.rand2}); // 5 seconds of noise
289 b = Buffer.loadCollection(s, a);
293 b.get(20000,{|msg| (msg == a[20000]).postln});
295 x = { PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 0) * 0.5 }.play;
298 // interleave a multi-dimensional array
300 l = Signal.sineFill(16384, Array.fill(200, {0}).add(1));
301 r = Array.fill(16384, {1.0.rand2});
302 m = [Array.newFrom(l), r]; // a multi-dimensional array
303 m = m.lace(32768); // interleave the two collections
304 b = Buffer.loadCollection(s, m, 2, {|buf|
305 x = { PlayBuf.ar(2, buf, BufRateScale.kr(buf), loop: 1) * 0.5 }.play;
312 method:: sendCollection
313 Stream a large collection into a buffer on the server using multiple setn messages. Returns a Buffer object.
315 The server on which to create the buffer.
316 argument:: collection
317 A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.
318 argument:: numChannels
319 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.
321 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.
323 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.
325 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.
329 a = Array.fill(2000000,{ rrand(0.0,1.0) }); // a LARGE collection
330 b = Buffer.sendCollection(s, a, 1, 0, {arg buf; "finished".postln;});
332 b.get(1999999, {|msg| (msg == a[1999999]).postln});
337 As link::#*read:: above, but gives you a load dialog window to browse for a file. Cocoa and SwingOSC compatible.
339 The server on which to allocate the buffer.
340 argument:: startFrame
341 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
343 The number of frames to read. The default is -1, which will read the whole file.
345 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.
347 An explicitly specified buffer number. Generally this is not needed.
352 b = Buffer.loadDialog(s, action: { arg buffer;
353 x = { PlayBuf.ar(buffer.numChannels, buffer, BufRateScale.kr(buffer)) }.play;
359 subsection:: Creation without Immediate Memory Allocation
361 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.
363 The server on which to allocate the buffer. The default is the default Server.
365 The number of frames to allocate. Actual memory use will correspond to numFrames * numChannels.
366 argument:: numChannels
367 The number of channels for the Buffer. The default is 1.
369 An explicitly specified buffer number. Generally this is not needed.
373 b = Buffer.new(s, 44100 * 8.0, 2);
374 c = Buffer.new(s, 44100 * 4.0, 2);
375 b.query; // numFrames = 0
376 s.sendBundle(nil, b.allocMsg, c.allocMsg); // sent both at the same time
377 b.query; // now it's right
382 subsection:: Cached Buffers
384 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.
385 Freeing a buffer removes it from the cache; quitting the server clears all the cached buffers. (This also occurs if the server crashes unexpectedly.)
387 You may access cached buffers using the following methods.
389 It may be simpler to access them through the server object:
391 myServer.cachedBufferAt(bufnum)
392 myServer.cachedBuffersDo(func)
394 b = Buffer.alloc(s, 2048, 1);
395 Buffer.cachedBufferAt(s, 0); // assuming b has bufnum 0
396 s.cachedBufferAt(0); // same result
397 s.cachedBuffersDo({ |buf| buf.postln });
400 method:: cachedBufferAt
401 Access a buffer by its number.
403 method:: cachedBuffersDo
404 Iterate over all cached buffers. The iteration is not in any order, but will touch all buffers.
409 subsection:: Variables
411 The following variables have getter methods.
414 Returns the Buffer's Server object.
417 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.
421 b = Buffer.alloc(s,44100 * 8.0,2);
427 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.
430 Returns the number of channels in the corresponding server-side buffer.
433 Returns the sample rate of the corresponding server-side buffer.
436 Returns a string containing the path of a soundfile that has been loaded into the corresponding server-side buffer.
439 subsection:: Explicit allocation
441 These methods allocate the necessary memory on the server for a Buffer previously created with link::#*new::.
443 method:: alloc, allocMsg
444 argument:: completionMessage
445 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
449 b = Buffer.new(s, 44100 * 8.0, 2);
450 b.query; // numFrames = 0
452 b.query; // numFrames = 352800
456 method:: allocRead, allocReadMsg
457 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.
459 A String representing the path of the soundfile to be read.
460 argument:: startFrame
461 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
463 The number of frames to read. The default is -1, which will read the whole file.
464 argument:: completionMessage
465 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
470 b.allocRead(Help.dir +/+ "sounds/a11wlk01.wav");
471 x = { PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1) * 0.5 }.play;
475 method:: allocReadChannel, allocReadChannelMsg
476 As link::#-allocRead:: above, but allows you to specify which channels to read.
478 A String representing the path of the soundfile to be read.
479 argument:: startFrame
480 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
482 The number of frames to read. The default is -1, which will read the whole file.
484 An Array of channels to be read from the soundfile. Indices start from zero. These will be read in the order provided.
485 argument:: completionMessage
486 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
491 // read only the first channel (a Sine wave) of a stereo file
492 b.allocReadChannel(Help.dir +/+ "sounds/SinedPink.aiff", channels: [0]);
493 x = { PlayBuf.ar(1, b, BufRateScale.kr(b), loop: 1) * 0.5 }.play;
497 subsection:: Other methods
499 method:: read, readMsg
500 Read a soundfile into an already allocated buffer.
502 A String representing the path of the soundfile to be read.
503 argument:: fileStartFrame
504 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
506 The number of frames to read. The default is -1, which will read the whole file.
507 argument:: bufStartFrame
508 The index of the frame in the buffer at which to start reading. The default is 0, which is the beginning of the buffer.
510 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).
511 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.
513 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.
514 argument:: completionMessage
515 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
518 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.
520 method:: readChannel, readChannelMsg
521 As link::#-read:: above, but allows you to specify which channels to read.
523 A String representing the path of the soundfile to be read.
524 argument:: fileStartFrame
525 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
527 The number of frames to read. The default is -1, which will read the whole file.
528 argument:: bufStartFrame
529 The index of the frame in the buffer at which to start reading. The default is 0, which is the beginning of the buffer.
531 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).
532 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.
534 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.
536 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.
537 argument:: completionMessage
538 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
540 method:: cueSoundFile, cueSoundFileMsg
541 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.
543 A String representing the path of the soundfile to be read.
544 argument:: startFrame
545 The first frame of the soundfile to read. The default is 0, which is the beginning of the file.
546 argument:: completionMessage
547 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
551 //create with cueSoundFile class method
552 b = Buffer.cueSoundFile(s, Help.dir +/+ "sounds/a11wlk01-44_1.aiff", 0, 1);
553 x = { DiskIn.ar(1, b) }.play;
554 b.close; // must call close in between cueing
555 // now use like named instance method, but different arguments
556 b.cueSoundFile(Help.dir +/+ "sounds/a11wlk01-44_1.aiff");
557 // have to do all this to clean up properly!
558 x.free; b.close; b.free;
561 method:: write, writeMsg
562 Write the contents of the buffer to a file. See link::Classes/SoundFile:: for information on valid values for headerFormat and sampleFormat.
564 A String representing the path of the soundfile to be written.
565 If no path is given, Buffer writes into the default recording directory with a generic name.
567 The number of frames to write. The default is -1, which will write the whole buffer.
568 argument:: startFrame
569 The index of the frame in the buffer from which to start writing. The default is 0, which is the beginning of the buffer.
571 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.
572 argument:: completionMessage
573 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
575 method:: free, freeMsg
576 Release the buffer's memory on the server and return the bufferID back to the server's buffer number allocator for future reuse.
577 argument:: completionMessage
578 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
580 method:: zero, zeroMsg
581 Sets all values in this buffer to 0.0.
582 argument:: completionMessage
583 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
586 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.
588 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.
591 b = Buffer.alloc(s, 4, 2);
592 b.set(0, 0.2, 1, 0.3, 7, 0.4); // set the values at indices 0, 1, and 7.
593 b.getn(0, 8, {|msg| msg.postln});
597 method:: setn, setnMsg
598 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.
600 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.
602 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.
605 b = Buffer.alloc(s,16);
606 b.setn(0, Array.fill(16, { rrand(0,1) }));
607 b.getn(0, b.numFrames, {|msg| msg.postln});
608 b.setn(0, [1, 2, 3], 4, [1, 2, 3]);
609 b.getn(0, b.numFrames, {|msg| msg.postln});
613 method:: loadCollection
614 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.
615 argument:: collection
616 A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.
617 argument:: startFrame
618 The index of the frame at which to start loading the collection. The default is 0, which is the start of the buffer.
620 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.
623 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.
624 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.
628 v = Signal.sineFill(128, 1.0/[1,2,3,4,5,6]);
629 b = Buffer.alloc(s, 128);
632 b.loadCollection(v, action: {|buf|
633 x = { PlayBuf.ar(buf.numChannels, buf, BufRateScale.kr(buf), loop: 1)
639 // interleave a multi-dimensional array
641 l = Signal.sineFill(16384, Array.fill(200, {0}).add(1));
642 r = Array.fill(16384, {1.0.rand2});
643 m = [Array.newFrom(l), r]; // a multi-dimensional array
644 m = m.lace(32768); // interleave the two collections
645 b = Buffer.alloc(s, 16384, 2);
648 b.loadCollection(m, 0, {|buf|
649 x = { PlayBuf.ar(2, buf, BufRateScale.kr(buf), loop: 1) * 0.5 }.play;
656 method:: sendCollection
657 Stream a large collection into this buffer using multiple setn messages.
658 argument:: collection
659 A subclass of Collection (i.e. an Array) containing only floats and integers. Multi-dimensional arrays will not work.
660 argument:: startFrame
661 The index of the frame at which to start streaming in the collection. The default is 0, which is the start of the buffer.
663 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.
665 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.
667 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.
671 a = Array.fill(2000000,{ rrand(0.0,1.0) });
672 b = Buffer.alloc(s, 2000000);
674 b = b.sendCollection(a, action: {arg buf; "finished".postln;});
675 b.get(1999999, {|msg| (msg == a[1999999]).postln});
680 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.
684 b = Buffer.alloc(s,16);
685 b.setn(0, Array.fill(16, { rrand(0.0, 1.0) }));
686 b.get(0, {|msg| msg.postln});
690 method:: getn, getMsg
691 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.
693 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.
695 method:: loadToFloatArray
696 Write the buffer to a file and then load it into a FloatArray.
698 The index in the buffer to begin writing from. The default is 0.
700 The number of values to write. The default is -1, which writes from index until the end of the buffer.
702 A Function which will be passed the resulting FloatArray as an argument and evaluated when loading is finished.
704 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.
707 b = Buffer.read(s, Help.dir +/+ "sounds/a11wlk01.wav");
708 // same as Buffer.plot
709 b.loadToFloatArray(action: { arg array; a = array; {a.plot;}.defer; "done".postln;});
713 method:: getToFloatArray
714 Stream the buffer to the client using a series of getn messages and put the results into a FloatArray.
716 The index in the buffer to begin writing from. The default is 0.
718 The number of values to write. The default is -1, which writes from index until the end of the buffer.
720 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.
722 The amount of time in seconds after which to post a warning if all replies have not yet been received. the default is 3.
724 A Function which will be passed the resulting FloatArray as an argument and evaluated when all replies have been received.
726 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.
729 b = Buffer.read(s, Help.dir +/+ "sounds/a11wlk01.wav");
731 b.getToFloatArray(wait:0.01,action:{arg array; a = array; {a.plot;}.defer;"done".postln;});
735 method:: normalize, normalizeMsg
736 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.
738 method:: fill, fillMsg
739 Starting at the index startAt, set the next numFrames to value. Additional ranges may be included in the same message.
741 method:: copyData, copyMsg
742 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.
744 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.
748 SynthDef(\help_Buffer_copy, { arg out=0, buf;
749 Line.ar(0, 0, dur: BufDur.kr(buf), doneAction: 2); // frees itself
750 Out.ar(out, PlayBuf.ar(1, buf));
755 b = Buffer.read(s, Help.dir +/+ "sounds/a11wlk01.wav");
756 c = Buffer.alloc(s, 120000);
759 Synth(\help_Buffer_copy, [\buf, b]);
761 // copy the whole buffer
763 Synth(\help_Buffer_copy, [\buf, c]);
767 b.copyData(c, numSamples: 4410);
768 Synth(\help_Buffer_copy, [\buf, c]);
770 // buffer "compositing"
772 b.copyData(c, numSamples: 4410);
773 b.copyData(c, dstStartAt: 4410, numSamples: 15500);
774 Synth(\help_Buffer_copy, [\buf, c]);
780 method:: close, closeMsg
781 After using a Buffer with a DiskOut or DiskIn, it is necessary to close the soundfile. Failure to do so can cause problems.
782 argument:: completionMessage
783 A valid OSC message or a Function which will return one. A Function will be passed this Buffer as an argument when evaluated.
786 Plot the contents of the Buffer in a GUI window.
788 The name of the resulting window.
790 An instance of Rect determining the size of the resulting view.
792 the minimum value in the plot
794 the maximum value in the plot
796 a window to place the plot in. If nil, one will be created for you
800 b = Buffer.read(s, Help.dir +/+ "sounds/a11wlk01.wav");
806 Plays the contents of the buffer on the server and returns a corresponding Synth.
808 A Boolean indicating whether or not to loop playback. If false the synth will automatically be freed when done. The default is false.
810 A value by which to scale the output. The default is 1.
814 b = Buffer.read(s, Help.dir +/+ "sounds/a11wlk01.wav");
815 b.play; // frees itself
821 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.
824 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.
826 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.
830 b = Buffer.readNoUpdate(s, Help.dir +/+ "sounds/a11wlk01.wav");
831 b.numFrames; // incorrect, shows nil
832 b.updateInfo({|buf| buf.numFrames.postln; }); // now it's right
836 subsection:: Buffer Fill Commands
837 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.
840 This is a generalized version of the commands below.
841 argument:: genCommand
842 A String indicating the name of the command to use. See Server-Command-Reference for a list of valid command names.
844 An Array containing the corresponding arguments to the command.
846 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
847 argument:: asWavetable
848 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.
849 argument:: clearFirst
850 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
852 method:: sine1, sine1Msg
853 Fill this buffer with a series of sine wave harmonics using specified amplitudes.
855 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.
857 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
858 argument:: asWavetable
859 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.
860 argument:: clearFirst
861 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
866 b = Buffer.alloc(s, 512, 1);
867 b.sine1(1.0 / [1, 2, 3, 4], true, true, true);
868 x = { Osc.ar(b, 200, 0, 0.5) }.play;
873 method:: sine2, sine2Msg
874 Fill this buffer with a series of sine wave partials using specified frequencies and amplitudes.
876 An Array containing frequencies (in cycles per buffer) for the partials.
878 An Array containing amplitudes for the partials. This should contain the same number of items as freqs.
880 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
881 argument:: asWavetable
882 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.
883 argument:: clearFirst
884 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
889 b = Buffer.alloc(s, 512, 1);
890 b.sine2([1.0, 3], [1, 0.5]);
891 x = { Osc.ar(b, 200, 0, 0.5) }.play;
896 method:: sine3, sine3Msg
897 Fill this buffer with a series of sine wave partials using specified frequencies, amplitudes, and initial phases.
899 An Array containing frequencies (in cycles per buffer) for the partials.
901 An Array containing amplitudes for the partials. This should contain the same number of items as freqs.
903 An Array containing initial phase for the partials (in radians). This should contain the same number of items as freqs.
905 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
906 argument:: asWavetable
907 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.
908 argument:: clearFirst
909 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
911 method:: cheby, chebyMsg
912 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.
913 argument:: amplitudes
914 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.
916 A Boolean indicating whether or not to normalize the buffer to a peak value of 1.0. The default is true.
917 argument:: asWavetable
918 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.
919 argument:: clearFirst
920 A Boolean indicating whether or not to clear the buffer before writing. The default is true.
924 b = Buffer.alloc(s, 512, 1, {arg buf; buf.chebyMsg([1,0,1,1,0,1])});
929 SinOsc.ar(300, 0, Line.kr(0,1,6)),