From 59d4bcb20da9675fcb2591aca39703a5b8a43fc6 Mon Sep 17 00:00:00 2001 From: Pierre Alexandre Tremblay Date: Wed, 24 Mar 2021 15:51:33 +0000 Subject: [PATCH] dataset to/from buffer + getIds --- release-packaging/Classes/FluidDataSet.sc | 30 +++++ .../HelpSource/Classes/FluidDataSet.schelp | 105 +++++++++++++++--- 2 files changed, 119 insertions(+), 16 deletions(-) diff --git a/release-packaging/Classes/FluidDataSet.sc b/release-packaging/Classes/FluidDataSet.sc index 9cad494..5349e92 100644 --- a/release-packaging/Classes/FluidDataSet.sc +++ b/release-packaging/Classes/FluidDataSet.sc @@ -72,4 +72,34 @@ FluidDataSet : FluidDataObject actions[\print] = [string(FluidMessageResponse,_,_),action]; this.prSendMsg(this.printMsg); } + + toBufferMsg{|buffer, transpose = 0, labelSet| + buffer = this.prEncodeBuffer(buffer); + ^this.prMakeMsg(\toBuffer, id, buffer, transpose, labelSet.asUGenInput); + } + + toBuffer{|buffer, transpose = 0, labelSet, action| + actions[\toBuffer] = [nil,action]; + this.prSendMsg(this.toBufferMsg(buffer, transpose, labelSet)); + } + + fromBufferMsg{|buffer, transpose = 0, labelSet| + buffer = this.prEncodeBuffer(buffer); + ^this.prMakeMsg(\fromBuffer, id, buffer, transpose, labelSet.asUGenInput); + } + + fromBuffer{|buffer, transpose = 0, labelSet, action| + actions[\fromBuffer] = [nil,action]; + this.prSendMsg(this.fromBufferMsg(buffer, transpose, labelSet)); + } + + getIdsMsg{|labelSet| + ^this.prMakeMsg(\getIds, id, labelSet.asUGenInput); + } + + getIds{|labelSet, action| + actions[\getIds] = [nil,action]; + this.prSendMsg(this.getIdsMsg(labelSet)); + } } + diff --git a/release-packaging/HelpSource/Classes/FluidDataSet.schelp b/release-packaging/HelpSource/Classes/FluidDataSet.schelp index eea1ed8..71cb782 100644 --- a/release-packaging/HelpSource/Classes/FluidDataSet.schelp +++ b/release-packaging/HelpSource/Classes/FluidDataSet.schelp @@ -47,6 +47,36 @@ Set the point: if the label exists, this method behaves like updatePoint; if the ​​ METHOD:: clear Empty the data set. + +METHOD:: toBuffer +Dump the content of the dataset to a link::Classes/Buffer::, with optional transposition, and a map of channel/frames to the original IDs as link::Classes/FluidLabelSet::. + +ARGUMENT:: buffer +The buffer to write to. It will be resized. + +ARGUMENT:: transpose +If 0, each dataset point becomes a channel, and each dataset dimension becomes a frame. If 1, points become frames, and dimensions become channels. + +ARGUMENT:: labelSet +The labelset in which to dump the point's IDs associated with their reference channel number (or frame number if transposed). + +ARGUMENT:: action +A function to run when the dump is done. + +METHOD:: fromBuffer +Import to the dataset the content of a link::Classes/Buffer::, with optional transposition, and a map of channel/frames to the original IDs as link::Classes/FluidLabelSet::. + +ARGUMENT:: buffer +The buffer to read from. The dataset will be resized. + +ARGUMENT:: transpose +If 0, each channel becomes a dataset point, and each frame becomes a dataset dimension. If 1, frames become points, and channels become dimensions. + +ARGUMENT:: labelSet +The labelset from which to retrieve the point's IDs associated with their reference channel number (or frame number if transposed). + +ARGUMENT:: action +A function to run when the import is done. ​ METHOD:: merge Merge sourceDataSet in the current DataSet. It will update the value of points with the same label if overwrite is set to 1. To add columns instead, see the 'transformJoin' method of link::Classes/FluidDataSetQuery:: @@ -76,13 +106,13 @@ fork{ ~point = Buffer.alloc(s,1,1); s.sync; 10.do{|i| - ~point.set(0,i); - ~ds.addPoint(i.asString,~point,{("addPoint"+i).postln}); - s.sync; - }; - ~ds.dump; - s.sync; - ~ds.free; + ~point.set(0,i); + ~ds.addPoint(i.asString,~point,{("addPoint"+i).postln}); + s.sync; + }; + ~ds.dump; + s.sync; + ~ds.free; }; ) @@ -92,9 +122,9 @@ d = Dictionary.new; d.add(\cols -> 1); d.add(\data -> Dictionary.newFrom(10.collect{|i|[i.asString, [i.asFloat]]}.flatten)); fork{ - ~ds = FluidDataSet.new(s); - ~ds.load(d); s.sync; - ~ds.dump; s.sync; ~ds.free; + ~ds = FluidDataSet.new(s); + ~ds.load(d); s.sync; + ~ds.dump; s.sync; ~ds.free; } ) @@ -102,16 +132,59 @@ fork{ ( ~ds = FluidDataSet.new(s); { - var trig = Impulse.kr(20); - var count = PulseCount.kr(trig) - 1; - var buf = LocalBuf(1); - BufWr.kr(count, buf); - FluidDataSetWr.kr(~ds.asUGenInput, buf: buf, trig: trig); - FreeSelf.kr(count - 8); + var trig = Impulse.kr(20); + var count = PulseCount.kr(trig) - 1; + var buf = LocalBuf(1); + BufWr.kr(count, buf); + FluidDataSetWr.kr(~ds.asUGenInput, buf: buf, trig: trig); + FreeSelf.kr(count - 8); }.play.onFree{~ds.dump{|o| o.postln;~ds.free}} ) :: +STRONG:: Buffer Interface:: +As the content of the dataset has a similar structure to buffers, namely arrays of floats in parallel, it is possible to transfer the content between the two. Careful consideration of the rotation of the buffer, as well as the relation of points to channel numbers, are needed. + +code:: +( +//Make a dummy data set +d = FluidDataSet(s); +~data = Dictionary.with(*Array.iota(20).reshape(4,5).collect{|a,i| ("row"++i)->a}); +~dsdata = Dictionary.newFrom([\cols,5,\data,~data]); +d.load(~dsdata); +d.print; +) + +//convert to separate buffer and labelset +b = Buffer(s); +l = FluidLabelSet(s); +d.toBuffer(b,0,l); + +//check the result: by default, dataset points become channels, with their associated data as sample frames +b.query +b.getn(0,20,{|x|x.postln}) +l.print + +//you can also transpose your query, where dataset points are each a sample frame, and each data column becomes a channel +d.toBuffer(b,1,l); +b.query +b.getn(0,20,{|x|x.postln}) +//note that the IDs are still one per item, as columns are unamed in datasets +l.print + +//Convert back to DS again +e = FluidDataSet(s); + +//Let's use the transposed data we just got +e.print; +e.fromBuffer(b,1,l); +e.print; +//if we didn't transpose, we would get an error as the labelset is mismatched with the number of items +e.clear +e.print +e.fromBuffer(b,0,l) +:: + STRONG:: Merging Datasets:: code:: //this is how to add items between 2 datasets.