-
Notifications
You must be signed in to change notification settings - Fork 0
/
RawJitterMatrixNodes.cs
408 lines (340 loc) · 11 KB
/
RawJitterMatrixNodes.cs
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
#region licence/info
//////project name
// Jitter Matrix Encoder/Decoder
//////description
// Decodes and Encodes matrix data for Max/MSP/Jitter
// Can receive data sent via jit.net.send and decode it
// Can encode data to be recieved in Max with jit.net.recv
// For details how a Jitter matrix is constructed see
// http://cycling74.com/sdk/MaxSDK-6.0.4/html/chapter_jit_networking.html
////// version history
// 130308: v0.2
// . now supports decoding of arbitrary plane count
// 130301: v0.1
// . first alpha release
// . basic functionality for encoding and decoding of 1 or 2 dimensional matrices of type 0 (char) implemented
// . encodes from and decodes to a color spread
// . heavy testing needed
////// TODO
// Decoder:
// . reply with latency chunk (see Max API docs)
// . high dimension sizes (> 40x40) result in memory problems
// . ...
// Encoder:
// . Encode time right
// . handle more than 2 dimensions?
// . ...
//////licence
//GNU Lesser General Public License (LGPL)
//english: http://www.gnu.org/licenses/lgpl.html
//german: http://www.gnu.de/lgpl-ger.html
//////initial author
// motzi (Matthias Husinsky -> mhusinsky[at]fhstp.ac.at)
// Institute for Creative\Media/Technologies
// University of Applied Sciences St.Pölten, Austria
// -> http://icmt.fhstp.ac.at
#endregion licence/info
#region usings
using System;
using System.IO;
using System.ComponentModel.Composition;
using System.Collections.Generic;
using System.Text;
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;
using VVVV.Utils.VColor;
using VVVV.Utils.VMath;
using VVVV.Utils.Streams;
//using VVVV.Core.Logging;
#endregion usings
namespace VVVV.Nodes
{
#region PluginInfo
[PluginInfo(
Name = "Jitter Matrix Decoder",
Category = "Raw", Help = "Decodes matrices sent from Max via jit.net.send",
Tags = "Max, Jitter, Network",
Author = "motzi",
AutoEvaluate = false
)]
#endregion PluginInfo
public class RawJitterMatrixDecoderNode : IPluginEvaluate
{
#region fields & pins
[Input("Input", IsSingle=true)]
IDiffSpread<Stream> FStreamIn;
[Output("ID", Visibility = PinVisibility.OnlyInspector)]
ISpread<string> FId;
[Output("Chunk Size", Visibility = PinVisibility.OnlyInspector)]
ISpread<int> FChunkSize;
[Output("Plane Count")]
ISpread<int> FPlaneCount;
[Output("Type", Visibility = PinVisibility.OnlyInspector)]
ISpread<int> FType;
[Output("Dim Count")]
ISpread<int> FDimCount;
[Output("Dim")]
ISpread<int> FDim;
[Output("Dimstride", Visibility = PinVisibility.OnlyInspector)]
ISpread<int> FDimstride;
[Output("Datasize", Visibility = PinVisibility.OnlyInspector)]
ISpread<int> FDataSize;
[Output("Time", Visibility = PinVisibility.OnlyInspector)]
ISpread<double> FTime;
//[Output("Debug")]
//ISpread<int> FDebug;
[Output("Data Value")]
ISpread<int> FData;
[Output("Data Color")]
ISpread<RGBAColor> FDataCol;
ByteOrder FBo = ByteOrder.BigEndian;
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
#endregion fields & pins
//called when data for any output pin is requested
public void Evaluate(int spreadMax)
{
if(!FStreamIn.IsChanged)
return;
FId.SliceCount = spreadMax;
FChunkSize.SliceCount = spreadMax;
//FDim.SliceCount = spreadMax;
//FDimstride.SliceCount = spreadMax;
for (int i = 0; i < spreadMax; i++)
{
//get the input stream
var inputStream = FStreamIn[i];
byte[] chunkId1 = new byte[4];
int chunkSize1 = 0;
byte[] chunkId2 = new byte[4];
int chunkSize2 = 0;
int planeCount = 0;
int type = 0;
int dimcount = 0;
int[] dim = new int[32];
int[] dimstride = new int[32];
int datasize = 0;
double time = 0;
byte[] data;
try{
using (var r = new BinaryReader(FStreamIn[i]))
{
// First Chunk: int32 id (4 bytes containing chars), int32 size
chunkId1 = r.ReadBytes(4);
chunkSize1 = r.ReadInt32();
chunkId2 = r.ReadBytes(4);
chunkSize2 = r.ReadInt32(FBo);
planeCount = r.ReadInt32(FBo);
type = r.ReadInt32(FBo);
dimcount = r.ReadInt32(FBo);
r.Read(dim, 0, 32, FBo);
r.Read(dimstride, 0, 32, FBo);
datasize = r.ReadInt32(FBo);
time = r.ReadDouble(FBo);
FId[i] = asciiEncoding.GetString(chunkId2);
FChunkSize[i] = chunkSize2;
FPlaneCount[i] = planeCount;
FType[i] = type;
FDimCount[i] = dimcount;
FDim.SliceCount = dimcount;
FDimstride.SliceCount = dimcount;
for(int j=0; j<dimcount; j++)
{
FDim[j] = dim[j];
}
for(int j=0; j<dimcount; j++)
{
FDimstride[j] = dimstride[j];
}
FDataSize[i] = datasize;
FTime[i] = time;
// process data for output
data = new byte[datasize];
data = r.ReadBytes(datasize);
int pixelCount = dim[0] * dim[1];
int dataCount = (int)(datasize / planeCount);
FData.SliceCount = pixelCount * planeCount;
FDataCol.SliceCount = pixelCount;
// how many bytes are used to fill up the block?
int padding = dimstride[1]-dimstride[0]*dim[0];
int xBlockSize = dim[0]*planeCount + padding;
// decode to values
int count = 0;
for(int j=0; j<datasize; j++)
{
// only consider data that is not padding
if(j % xBlockSize < dim[0]*planeCount)
{
FData[count] = data[j];
count++;
}
}
// decode to color
if(planeCount<=4)
{
for(int j=0; j<dataCount;j++)
{
RGBAColor col = new RGBAColor();
// cases emulate behaviour in max
switch(planeCount)
{
case 1:
col.R = FData[j*planeCount] /255.0;
col.G = FData[j*planeCount] /255.0;
col.B = FData[j*planeCount] /255.0;
col.A = 1;
break;
case 2:
col.G = FData[j*planeCount] / 255.0;
col.R = FData[j*planeCount+1] / 255.0;
col.B = FData[j*planeCount+1] / 255.0;
col.A = 1;
break;
case 3:
col.B = FData[j*planeCount] / 255.0;
col.R = FData[j*planeCount+1] / 255.0;
col.G = FData[j*planeCount+2] / 255.0;
col.A = 1;
break;
case 4:
col.A = FData[j*planeCount] / 255.0;
col.R = FData[j*planeCount+1] / 255.0;
col.G = FData[j*planeCount+2] / 255.0;
col.B = FData[j*planeCount+3] / 255.0;
break;
default:
break;
}
FDataCol[j] = col;
}
}
}
}
catch (Exception e)
{}
}
}
}
#region PluginInfo
[PluginInfo(
Name = "Jitter Matrix Encoder",
Category = "Raw",
Help = "Encodes matrices to be recieved in Max via jit.net.recieve",
Tags = "Max, Jitter, Network",
Author = "motzi",
AutoEvaluate = false
)]
#endregion PluginInfo
public class RawJitterMatrixEncoderNode : IPluginEvaluate, IPartImportsSatisfiedNotification
{
#region fields & pins
[Input("Input", DefaultColor = new double[] { 1.0, 1.0, 1.0, 1.0 })]
ISpread<RGBAColor> FColor;
[Input("Dimension X", MinValue = 1, DefaultValue = 1)]
ISpread<int> FDimX;
[Input("Dimension Y", MinValue = 1, DefaultValue = 1)]
ISpread<int> FDimY;
[Input("Plane Count", MinValue = 1, DefaultValue=4)]
ISpread<int> FPlaneCount;
//[Output("Debug")]
//ISpread<int> FDebug;
[Output("Output")]
ISpread<Stream> FStreamOut;
#endregion fields & pins
//called when all inputs and outputs defined above are assigned from the host
public void OnImportsSatisfied()
{
//start with an empty stream output
FStreamOut.SliceCount = 0;
}
//called when data for any output pin is requested
public void Evaluate(int spreadMax)
{
spreadMax = Math.Max(FDimX.SliceCount, FDimY.SliceCount);
spreadMax = Math.Max(spreadMax, FPlaneCount.SliceCount);
//ResizeAndDispose will adjust the spread length and thereby call
//the given constructor function for new slices and Dispose on old
//slices.
FStreamOut.ResizeAndDispose(spreadMax, () => new MemoryStream());
for (int i = 0; i < spreadMax; i++)
{
//get the output stream (this works because of ResizeAndDispose above)
var outputStream = FStreamOut[i];
outputStream.Position = 0;
// actually an int32 according to Max6 API docs
byte[] chunkId1 = new byte[4]{ 0x4A, 0x4D, 0x54, 0x58}; // JMTX
int chunkSize1 = 288;
// from here on everything is meant to be encoded big endian (see stream writing later)
byte[] chunkId2 = new byte[4] { 0x4A, 0x4D, 0x54, 0x58};
int chunkSize2 = 288;
int planeCount = FPlaneCount[i];
int type = 0; // 0 for char, 1 for long, 2 for float32, 3 for float64
int dimcount = 2;
int[] dim = new int[32]
{
FDimX[i],
FDimY[i],
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
};
// details for calculation: http://cycling74.com/forums/topic.php?id=6677
// padding for dimstride[2] apparently not nescessary for sending, warumauchimmer
// int padding = 0 - (FPlaneCount[i]*FDimX[i] % 16);
int[] dimstride = new int[32]
{
FPlaneCount[i], // should actually be sizeof(type)*planecount
FPlaneCount[i]*FDimX[i],
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
int datasize = FDimX[i]*FDimY[i]*planeCount;
double time = 10765666.2262;
outputStream.SetLength(296 + datasize);
outputStream.Write(chunkId1, 0, 4);
outputStream.Write(BitConverter.GetBytes(chunkSize1), 0, 4);
outputStream.Write(ConvertEndian(chunkId2),0,4);
outputStream.Write(ConvertEndian(chunkSize2),0,4);
outputStream.Write(ConvertEndian(planeCount),0,4);
outputStream.Write(ConvertEndian(type),0,4);
outputStream.Write(ConvertEndian(dimcount),0,4);
for (int j=0; j<dim.Length; j++)
outputStream.Write(ConvertEndian(dim[j]),0,4);
for (int j=0; j<dimstride.Length; j++)
outputStream.Write(ConvertEndian(dimstride[j]),0,4);
outputStream.Write(ConvertEndian(datasize),0,4);
outputStream.Write(ConvertEndian(time),0,8);
for (int j=0; j<FDimX[i]*FDimY[i]; j++)
{
byte[] ARGB = new byte[4];
ARGB[0] = (byte)Math.Round(FColor[j].A * 255);
ARGB[1] = (byte)Math.Round(FColor[j].R * 255);
ARGB[2] = (byte)Math.Round(FColor[j].G * 255);
ARGB[3] = (byte)Math.Round(FColor[j].B * 255);
outputStream.Write(ARGB,0,4);
}
}
//this will force the changed flag of the output pin to be set
FStreamOut.Flush(true);
}
public byte[] ConvertEndian(byte[] buffer)
{
Array.Reverse(buffer);
return buffer;
}
public byte[] ConvertEndian(int num)
{
byte[] buffer = BitConverter.GetBytes(num);
Array.Reverse(buffer);
return buffer;
}
public byte[] ConvertEndian(long num)
{
byte[] buffer = BitConverter.GetBytes(num);
Array.Reverse(buffer);
return buffer;
}
public byte[] ConvertEndian(double num)
{
byte[] buffer = BitConverter.GetBytes(num);
Array.Reverse(buffer);
return buffer;
}
}
}