1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
|
Buffer {
classvar serverCaches;
// don't use the setter methods for the vars below
// they're private and have no effect on the server
var <server, <bufnum, <>numFrames, <>numChannels, <>sampleRate;
var <>path, >doOnInfo;
*initClass { serverCaches = IdentityDictionary.new }
// doesn't send
*new { arg server, numFrames, numChannels, bufnum;
server = server ? Server.default;
bufnum ?? { bufnum = server.bufferAllocator.alloc(1) };
if(bufnum.isNil) {
Error("No more buffer numbers -- free some buffers before allocating more.").throw
};
^super.newCopyArgs(server,
bufnum,
numFrames,
numChannels).sampleRate_(server.sampleRate).cache;
}
*alloc { arg server, numFrames, numChannels = 1, completionMessage, bufnum;
server = server ? Server.default;
bufnum ?? { bufnum = server.bufferAllocator.alloc(1) };
if(bufnum.isNil) {
Error("No more buffer numbers -- free some buffers before allocating more.").throw
};
^super.newCopyArgs(server,
bufnum,
numFrames,
numChannels)
.alloc(completionMessage).sampleRate_(server.sampleRate).cache;
}
*allocConsecutive { |numBufs = 1, server, numFrames, numChannels = 1, completionMessage,
bufnum|
var bufBase, newBuf;
bufBase = bufnum ?? { server.bufferAllocator.alloc(numBufs) };
if(bufBase.isNil) {
Error("No block of % consecutive buffer numbers is available.".format(numBufs)).throw
};
^Array.fill(numBufs, { |i|
newBuf = Buffer.new(server, numFrames, numChannels, i + bufBase);
server.sendMsg(\b_alloc, i + bufBase, numFrames, numChannels,
completionMessage.value(newBuf, i));
newBuf.cache
});
}
alloc { arg completionMessage;
server.listSendMsg( this.allocMsg(completionMessage) )
}
allocRead { arg argpath,startFrame = 0,numFrames = -1, completionMessage;
path = argpath;
server.listSendMsg(this.allocReadMsg( argpath,startFrame,numFrames, completionMessage));
}
allocReadChannel { arg argpath,startFrame,numFrames = 0, channels = -1, completionMessage;
path = argpath;
server.listSendMsg(this.allocReadChannelMsg( argpath,startFrame,numFrames, channels,
completionMessage));
}
allocMsg { arg completionMessage;
this.cache;
^["/b_alloc", bufnum, numFrames.asInt, numChannels, completionMessage.value(this)]
}
allocReadMsg { arg argpath,startFrame = 0,numFrames = -1, completionMessage;
this.cache;
path = argpath;
^["/b_allocRead",bufnum, path,startFrame,(numFrames ? -1).asInt, completionMessage.value(this)]
}
allocReadChannelMsg { arg argpath,startFrame = 0,numFrames = -1, channels, completionMessage;
this.cache;
path = argpath;
^["/b_allocReadChannel",bufnum, path,startFrame, (numFrames ? -1).asInt] ++ channels ++ [completionMessage.value(this)]
}
// read whole file into memory for PlayBuf etc.
// adds a query as a completion message
*read { arg server,path,startFrame = 0,numFrames = -1, action, bufnum;
server = server ? Server.default;
bufnum ?? { bufnum = server.bufferAllocator.alloc(1) };
if(bufnum.isNil) {
Error("No more buffer numbers -- free some buffers before allocating more.").throw
};
^super.newCopyArgs(server, bufnum)
.doOnInfo_(action).cache
.allocRead(path,startFrame,numFrames,{|buf|["/b_query",buf.bufnum]});
}
read { arg argpath, fileStartFrame = 0, numFrames = -1,
bufStartFrame = 0, leaveOpen = false, action;
this.cache;
doOnInfo = action;
server.listSendMsg(
this.readMsg(argpath,fileStartFrame,numFrames,bufStartFrame,
leaveOpen,{|buf|["/b_query",buf.bufnum]} )
);
}
*readChannel { arg server,path,startFrame = 0,numFrames = -1, channels, action, bufnum;
server = server ? Server.default;
bufnum ?? { bufnum = server.bufferAllocator.alloc(1) };
if(bufnum.isNil) {
Error("No more buffer numbers -- free some buffers before allocating more.").throw
};
^super.newCopyArgs(server, bufnum)
.doOnInfo_(action).cache
.allocReadChannel(path,startFrame,numFrames,channels,
{|buf|["/b_query",buf.bufnum]});
}
readChannel { arg argpath, fileStartFrame = 0, numFrames = -1,
bufStartFrame = 0, leaveOpen = false, channels, action;
this.cache;
doOnInfo = action;
server.listSendMsg(
this.readChannelMsg(argpath,fileStartFrame,numFrames,bufStartFrame,
leaveOpen,channels,{|buf|["/b_query",buf.bufnum]} )
);
}
*readNoUpdate { arg server,path,startFrame = 0,numFrames = -1, bufnum, completionMessage;
server = server ? Server.default;
bufnum ?? { bufnum = server.bufferAllocator.alloc(1) };
if(bufnum.isNil) {
Error("No more buffer numbers -- free some buffers before allocating more.").throw
};
^super.newCopyArgs(server, bufnum)
.allocRead(path,startFrame,numFrames, completionMessage);
}
readNoUpdate { arg argpath, fileStartFrame = 0, numFrames = -1,
bufStartFrame = 0, leaveOpen = false, completionMessage;
server.listSendMsg(
this.readMsg(argpath,fileStartFrame,numFrames,bufStartFrame,
leaveOpen, completionMessage)
);
}
readMsg { arg argpath, fileStartFrame = 0, numFrames = -1,
bufStartFrame = 0, leaveOpen = false, completionMessage;
path = argpath;
^["/b_read", bufnum, path, fileStartFrame, (numFrames ? -1).asInt,
bufStartFrame, leaveOpen.binaryValue, completionMessage.value(this)]
// doesn't set my numChannels etc.
}
readChannelMsg { arg argpath, fileStartFrame = 0, numFrames = -1,
bufStartFrame = 0, leaveOpen = false, channels, completionMessage;
path = argpath;
^["/b_readChannel", bufnum, path, fileStartFrame, (numFrames ? -1).asInt,
bufStartFrame, leaveOpen.binaryValue] ++ channels ++ [completionMessage.value(this)]
// doesn't set my numChannels etc.
}
// preload a buffer for use with DiskIn
*cueSoundFile { arg server,path,startFrame = 0,numChannels= 2,
bufferSize=32768,completionMessage;
^this.alloc(server,bufferSize,numChannels,{ arg buffer;
buffer.readMsg(path,startFrame,bufferSize,0,true,completionMessage)
}).cache;
}
cueSoundFile { arg path,startFrame,completionMessage;
this.path_(path);
server.listSendMsg(
this.cueSoundFileMsg(path,startFrame,completionMessage)
)
}
cueSoundFileMsg { arg path,startFrame = 0,completionMessage;
^["/b_read", bufnum,path,startFrame,numFrames.asInt,0,1,completionMessage.value(this) ]
}
// transfer a collection of numbers to a buffer through a file
*loadCollection { arg server, collection, numChannels = 1, action;
var data, sndfile, path, bufnum, buffer;
server = server ? Server.default;
bufnum = server.bufferAllocator.alloc(1);
if(bufnum.isNil) {
Error("No more buffer numbers -- free some buffers before allocating more.").throw
};
server.isLocal.if({
if(collection.isKindOf(RawArray).not) { collection = collection.as(FloatArray) };
sndfile = SoundFile.new;
sndfile.sampleRate = server.sampleRate;
sndfile.numChannels = numChannels;
path = PathName.tmp ++ sndfile.hash.asString;
if(sndfile.openWrite(path),
{
sndfile.writeData(collection);
sndfile.close;
^super.newCopyArgs(server, bufnum)
.cache.doOnInfo_({ |buf|
if(File.delete(path), { buf.path = nil},
{("Could not delete data file:" + path).warn;});
action.value(buf);
}).allocRead(path, 0, -1,{|buf|["/b_query",buf.bufnum]});
}, {"Failed to write data".warn; ^nil}
);
}, {"cannot use loadCollection with a non-local Server".warn; ^nil});
}
loadCollection { arg collection, startFrame = 0, action;
var data, sndfile, path;
server.isLocal.if({
if(collection.isKindOf(RawArray).not,
{data = collection.collectAs({|item| item}, FloatArray)}, {data = collection;}
);
if ( collection.size > ((numFrames - startFrame) * numChannels),
{ "Collection larger than available number of Frames".warn });
sndfile = SoundFile.new;
sndfile.sampleRate = server.sampleRate;
sndfile.numChannels = numChannels;
path = PathName.tmp ++ sndfile.hash.asString;
if(sndfile.openWrite(path),
{
sndfile.writeData(data);
sndfile.close;
this.read(path, bufStartFrame: startFrame, action: { |buf|
if(File.delete(path), { buf.path = nil },
{("Could not delete data file:" + path).warn;});
action.value(buf);
});
}, {"Failed to write data".warn;}
);
}, {"cannot do fromCollection with a non-local Server".warn;});
}
// send a Collection to a buffer one UDP sized packet at a time
*sendCollection { arg server, collection, numChannels = 1, wait = -1, action;
var buffer = this.new(server, ceil(collection.size / numChannels), numChannels);
forkIfNeeded {
buffer.alloc;
server.sync;
buffer.sendCollection(collection, 0, wait, action);
}
^buffer;
}
sendCollection { arg collection, startFrame = 0, wait = -1, action;
var collstream, collsize, bundsize;
if(collection.isSequenceableCollection
and: { startFrame.isNumber and: { wait.isNumber } }) {
collstream = CollStream.new;
collstream.collection = collection;
collsize = collection.size;
if ( collsize > ((numFrames - startFrame) * numChannels),
{ "Collection larger than available number of Frames".warn });
this.streamCollection(collstream, collsize, startFrame * numChannels, wait, action);
} {
MethodError("Invalid arguments to Buffer:sendCollection", this).throw;
};
}
// called internally
streamCollection { arg collstream, collsize, startFrame = 0, wait = -1, action;
var bundsize, pos;
{
// wait = -1 allows an OSC roundtrip between packets
// wait = 0 might not be safe in a high traffic situation
// maybe okay with tcp
pos = collstream.pos;
while { pos < collsize } {
// 1626 max size for setn under udp
bundsize = min(1626, collsize - pos);
server.listSendMsg(['/b_setn', bufnum, pos + startFrame, bundsize]
++ Array.fill(bundsize, { collstream.next }));
pos = collstream.pos;
if(wait >= 0) { wait.wait } { server.sync };
};
action.value(this);
}.forkIfNeeded;
}
// these next two get the data and put it in a float array which is passed to action
loadToFloatArray { arg index = 0, count = -1, action;
var msg, cond, path, file, array;
{
path = PathName.tmp ++ this.hash.asString;
msg = this.write(path, "aiff", "float", count, index);
server.sync;
file = SoundFile.new;
protect {
file.openRead(path);
array = FloatArray.newClear(file.numFrames * file.numChannels);
file.readData(array);
} {
file.close;
if(File.delete(path).not) { ("Could not delete data file:" + path).warn };
};
action.value(array, this);
}.forkIfNeeded;
}
// risky without wait
getToFloatArray { arg index = 0, count, wait = 0.01, timeout = 3, action;
var refcount, array, pos, getsize, resp, done = false;
pos = index = index.asInteger;
count = (count ? (numFrames * numChannels)).asInteger;
array = FloatArray.newClear(count);
refcount = (count / 1633).roundUp;
count = count + pos;
//("refcount" + refcount).postln;
resp = OSCFunc({ arg msg;
if(msg[1] == bufnum, {
//("received" + msg).postln;
array = array.overWrite(FloatArray.newFrom(msg.copyToEnd(4)), msg[2] - index);
refcount = refcount - 1;
//("countDown" + refcount).postln;
if(refcount <= 0, {done = true; resp.clear; action.value(array, this); });
});
}, '/b_setn', server.addr);
{
while({pos < count}, {
// 1633 max size for getn under udp
getsize = min(1633, count - pos);
//("sending from" + pos).postln;
server.listSendMsg(this.getnMsg(pos, getsize));
pos = pos + getsize;
if(wait >= 0) { wait.wait } { server.sync };
});
}.forkIfNeeded;
// lose the responder if the network choked
SystemClock.sched(timeout,
{ done.not.if({ resp.free; "Buffer-streamToFloatArray failed!".warn;
"Try increasing wait time".postln;});
});
}
write { arg path, headerFormat = "aiff", sampleFormat = "int24", numFrames = -1,
startFrame = 0,leaveOpen = false, completionMessage;
path = path ?? { thisProcess.platform.recordingsDir +/+ "SC_" ++ Date.localtime.stamp ++ "." ++ headerFormat };
server.listSendMsg(
this.writeMsg(path, headerFormat, sampleFormat, numFrames, startFrame,
leaveOpen, completionMessage)
);
}
writeMsg { arg path,headerFormat="aiff",sampleFormat="int24",numFrames = -1,
startFrame = 0,leaveOpen = false, completionMessage;
// doesn't change my path
^["/b_write", bufnum, path,
headerFormat,sampleFormat, numFrames, startFrame,
leaveOpen.binaryValue, completionMessage.value(this)];
// writeEnabled = true;
}
free { arg completionMessage;
server.listSendMsg( this.freeMsg(completionMessage) );
}
freeMsg { arg completionMessage;
var msg;
this.uncache;
server.bufferAllocator.free(bufnum);
msg = ["/b_free", bufnum, completionMessage.value(this)];
bufnum = numFrames = numChannels = sampleRate = path = nil;
^msg
}
*freeAll { arg server;
var b;
server = server ? Server.default;
server.bufferAllocator.blocks.do({ arg block;
(block.address .. block.address + block.size - 1).do({ |i|
b = b.add( ["/b_free", i] );
});
server.bufferAllocator.free(block.address);
});
server.sendBundle(nil, *b);
this.clearServerCaches(server);
}
zero { arg completionMessage;
server.listSendMsg(this.zeroMsg(completionMessage));
}
zeroMsg { arg completionMessage;
^["/b_zero", bufnum , completionMessage.value(this) ]
}
set { arg index,float ... morePairs;
server.listSendMsg(["/b_set",bufnum,index,float] ++ morePairs);
}
setMsg { arg index,float ... morePairs;
^["/b_set",bufnum,index,float] ++ morePairs;
}
setn { arg ... args;
server.sendMsg(*this.setnMsg(*args));
}
setnMsgArgs{arg ... args;
var nargs;
nargs = List.new;
args.pairsDo{ arg control, moreVals;
if(moreVals.isArray,{
nargs.addAll([control, moreVals.size]++ moreVals)
},{
nargs.addAll([control, 1, moreVals]);
});
};
^nargs;
}
setnMsg { arg ... args;
^["/b_setn",bufnum] ++ this.setnMsgArgs(*args);
}
get { arg index, action;
OSCpathResponder(server.addr,['/b_set',bufnum,index],{ arg time, r, msg;
action.value(msg.at(3)); r.remove }).add;
server.listSendMsg(["/b_get",bufnum,index]);
}
getMsg { arg index;
^["/b_get",bufnum,index];
}
getn { arg index, count, action;
OSCpathResponder(server.addr,['/b_setn',bufnum,index],{arg time, r, msg;
action.value(msg.copyToEnd(4)); r.remove } ).add;
server.listSendMsg(["/b_getn",bufnum,index, count]);
}
getnMsg { arg index, count;
^["/b_getn",bufnum,index, count];
}
fill { arg startAt,numFrames,value ... more;
server.listSendMsg(["/b_fill",bufnum,startAt,numFrames.asInt,value]
++ more);
}
fillMsg { arg startAt,numFrames,value ... more;
^["/b_fill",bufnum,startAt,numFrames.asInt,value]
++ more;
}
normalize { arg newmax=1, asWavetable=false;
server.listSendMsg(["/b_gen",bufnum,if(asWavetable, "wnormalize", "normalize"),newmax]);
}
normalizeMsg { arg newmax=1, asWavetable=false;
^["/b_gen",bufnum,if(asWavetable, "wnormalize", "normalize"),newmax];
}
gen { arg genCommand, genArgs, normalize=true,asWavetable=true,clearFirst=true;
server.listSendMsg(["/b_gen",bufnum,genCommand,
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ genArgs)
}
genMsg { arg genCommand, genArgs, normalize=true,asWavetable=true,clearFirst=true;
^["/b_gen",bufnum,genCommand,
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ genArgs;
}
sine1 { arg amps,normalize=true,asWavetable=true,clearFirst=true;
server.listSendMsg(["/b_gen",bufnum,"sine1",
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ amps)
}
sine2 { arg freqs, amps,normalize=true,asWavetable=true,clearFirst=true;
server.listSendMsg(["/b_gen",bufnum,"sine2",
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ [freqs, amps].lace(freqs.size * 2))
}
sine3 { arg freqs, amps, phases,normalize=true,asWavetable=true,clearFirst=true;
server.listSendMsg(["/b_gen",bufnum,"sine3",
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ [freqs, amps, phases].lace(freqs.size * 3))
}
cheby { arg amplitudes,normalize=true,asWavetable=true,clearFirst=true;
server.listSendMsg(["/b_gen",bufnum,"cheby",
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ amplitudes)
}
sine1Msg { arg amps,normalize=true,asWavetable=true,clearFirst=true;
^["/b_gen",bufnum,"sine1",
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ amps
}
sine2Msg { arg freqs, amps,normalize=true,asWavetable=true,clearFirst=true;
^["/b_gen",bufnum,"sine2",
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ [freqs, amps].lace(freqs.size * 2)
}
sine3Msg { arg freqs, amps, phases,normalize=true,asWavetable=true,clearFirst=true;
^["/b_gen",bufnum,"sine3",
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ [freqs, amps, phases].lace(freqs.size * 3)
}
chebyMsg { arg amplitudes,normalize=true,asWavetable=true,clearFirst=true;
^["/b_gen",bufnum,"cheby",
normalize.binaryValue
+ (asWavetable.binaryValue * 2)
+ (clearFirst.binaryValue * 4)]
++ amplitudes
}
copyData { arg buf, dstStartAt = 0, srcStartAt = 0, numSamples = -1;
server.listSendMsg(
this.copyMsg(buf, dstStartAt, srcStartAt, numSamples)
)
}
copyMsg { arg buf, dstStartAt = 0, srcStartAt = 0, numSamples = -1;
^["/b_gen", buf.bufnum, "copy", dstStartAt, bufnum, srcStartAt, numSamples]
}
// close a file, write header, after DiskOut usage
close { arg completionMessage;
server.listSendMsg( this.closeMsg(completionMessage) );
}
closeMsg { arg completionMessage;
^["/b_close", bufnum, completionMessage.value(this) ];
}
query {
OSCFunc({ arg msg;
Post << "bufnum : " << msg[1] << Char.nl
<< "numFrames : " << msg[2] << Char.nl
<< "numChannels : " << msg[3] << Char.nl
<< "sampleRate : " << msg[4] << Char.nl << Char.nl;
}, '/b_info', server.addr).oneShot;
server.sendMsg("/b_query",bufnum)
}
updateInfo { |action|
// add to the array here. That way, update will be accurate even if this buf
// has been freed
this.cache;
doOnInfo = action;
server.sendMsg("/b_query", bufnum);
}
// cache Buffers for easy info updating
cache {
Buffer.initServerCache(server);
serverCaches[server][bufnum] = this;
}
uncache {
if(serverCaches[server].notNil,{
serverCaches[server].removeAt(bufnum);
});
if(serverCaches[server].size == 1) {
// the 1 item would be the responder
// if there is more than 1 item then the rest are cached buffers
// else we can remove.
// cx: tho i don't see why its important. it will just have to be added
// back when the next buffer is added and the responder is removed when
// the server reboots
Buffer.clearServerCaches(server);
}
}
*initServerCache { |server|
serverCaches[server] ?? {
serverCaches[server] = IdentityDictionary.new;
serverCaches[server][\responder] = OSCFunc({ |m|
var buffer = serverCaches[server][m[1]];
if(buffer.notNil) {
buffer.numFrames = m[2];
buffer.numChannels = m[3];
buffer.sampleRate = m[4];
buffer.queryDone;
};
}, '/b_info', server.addr).fix;
NotificationCenter.register(server,\newAllocators,this,{
this.clearServerCaches(server);
});
}
}
*clearServerCaches { |server|
if(serverCaches[server].notNil) {
serverCaches[server][\responder].free;
serverCaches.removeAt(server);
}
}
*cachedBuffersDo { |server, func|
var i = 0;
serverCaches[server] !? {
serverCaches[server].keysValuesDo({ |key, value|
if(key.isNumber) { func.value(value, i); i = i + 1 };
});
}
}
*cachedBufferAt { |server, bufnum|
^serverCaches[server].tryPerform(\at, bufnum)
}
// called from Server when b_info is received
queryDone {
doOnInfo.value(this);
doOnInfo = nil;
}
printOn { arg stream;
stream << this.class.name << "(" <<* [bufnum,numFrames,numChannels,sampleRate,path] <<")";
}
*loadDialog { arg server,startFrame = 0,numFrames, action, bufnum;
var buffer;
server = server ? Server.default;
bufnum ?? { bufnum = server.bufferAllocator.alloc(1) };
if(bufnum.isNil) {
Error("No more buffer numbers -- free some buffers before allocating more.").throw
};
buffer = super.newCopyArgs(server, bufnum).cache;
File.openDialog("Select a file...",{ arg path;
buffer.doOnInfo_(action)
.allocRead(path,startFrame,numFrames, {["/b_query",buffer.bufnum]})
});
^buffer;
}
play { arg loop = false, mul = 1;
^{ var player;
player = PlayBuf.ar(numChannels,bufnum,BufRateScale.kr(bufnum),
loop: loop.binaryValue);
loop.not.if(FreeSelfWhenDone.kr(player));
player * mul;
}.play(server);
}
duration { ^numFrames / sampleRate }
asUGenInput { ^this.bufnum }
asControlInput { ^this.bufnum }
asBufWithValues {
^this
}
}
|