[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[sc-dev] Another potential Buffer method *streamCollection
Okay, since the fromCollection method I added in my previous candidate won't work for sending large amounts of data to a non-local machine, I thought I'd try to come up with a method which chops it into convenient chunks:
<x-tad-smaller> *streamCollection { </x-tad-smaller><x-tad-smaller>arg</x-tad-smaller><x-tad-smaller> server, collection, wait = 0.0, action;
</x-tad-smaller><x-tad-smaller>var</x-tad-smaller><x-tad-smaller> collstream, buffer, func, collsize, bufnum, bundsize, pos;
collstream = </x-tad-smaller><x-tad-smaller>CollStream</x-tad-smaller><x-tad-smaller>.new;
collstream.collection = collection;
collsize = collection.size.postln;
server = server ? </x-tad-smaller><x-tad-smaller>Server</x-tad-smaller><x-tad-smaller>.default;
bufnum = server.bufferAllocator.alloc(1);
buffer = </x-tad-smaller><x-tad-smaller>super</x-tad-smaller><x-tad-smaller>.newCopyArgs(server, bufnum, collection.size, 1)
.addToServerArray.sampleRate_(server.sampleRate);
</x-tad-smaller><x-tad-smaller>// this will wait for synced.</x-tad-smaller><x-tad-smaller>
{
</x-tad-smaller><x-tad-smaller>// 1626 largest setn size with an alloc</x-tad-smaller><x-tad-smaller>
bundsize = min(1626, collsize - collstream.pos);
server.listSendMsg(buffer.allocMsg({[</x-tad-smaller><x-tad-smaller>"b_setn"</x-tad-smaller><x-tad-smaller>, bufnum, 0, bundsize]
++ </x-tad-smaller><x-tad-smaller>Array</x-tad-smaller><x-tad-smaller>.fill(bundsize, {collstream.next})}));
</x-tad-smaller><x-tad-smaller>// wait = 0 might not be safe</x-tad-smaller><x-tad-smaller>
</x-tad-smaller><x-tad-smaller>// maybe okay with tcp</x-tad-smaller><x-tad-smaller>
pos = collstream.pos;
while({pos < collsize}, {
wait.wait;
</x-tad-smaller><x-tad-smaller>// 1633 max size for setn under udp </x-tad-smaller><x-tad-smaller>
bundsize = min(1633, collsize - collstream.pos);
server.listSendMsg([</x-tad-smaller><x-tad-smaller>"b_setn"</x-tad-smaller><x-tad-smaller>, bufnum, pos, bundsize]
++ </x-tad-smaller><x-tad-smaller>Array</x-tad-smaller><x-tad-smaller>.fill(bundsize, {collstream.next}));
pos = collstream.pos;
});
action.value(buffer);
}.fork(</x-tad-smaller><x-tad-smaller>SystemClock</x-tad-smaller><x-tad-smaller>);
^buffer;
}
</x-tad-smaller>
Then you can go:
<x-tad-smaller>s.boot;
(
a = </x-tad-smaller><x-tad-smaller>Array</x-tad-smaller><x-tad-smaller>.fill(2000000,{ rrand(0.0,1.0) });
c = CollStream.new;
c.collection = a;
b = </x-tad-smaller><x-tad-smaller>Buffer</x-tad-smaller><x-tad-smaller>.streamCollection(s, a, 0.0, {arg buf; "finished".postln;});
)
b.get(1999999, {</x-tad-smaller><x-tad-smaller>|msg|</x-tad-smaller><x-tad-smaller> [msg , a[1999999]].postln});
b.free;
</x-tad-smaller>
I thought the wait time would be safest, but this works with a 2000000 sized Array on my machine with a wait of 0, so maybe it's less of an issue than I thought. Could maybe be more elegant.
Thoughts, comments, and criticisms appreciated.
S.