forked from vvvv/DelphiOSCUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOSCUtils.pas
More file actions
948 lines (816 loc) · 23.4 KB
/
OSCUtils.pas
File metadata and controls
948 lines (816 loc) · 23.4 KB
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
//////project name
//OSCUtils
//////description
//Utility library to encode/decode osc-packets
//inspired by original OSC reference implementation (OSC-Kit)
//and OSC.Net library as shipped with the TUIO-CSharp sample
//from http://reactable.iua.upf.edu/?software
//////licence
//GNU Lesser General Public License (LGPL)
//english: http://www.gnu.org/licenses/lgpl.html
//german: http://www.gnu.de/lgpl-ger.html
//////language/ide
//delphi
//////initial author
//joreg -> joreg@vvvv.org
//additions for FreePascal
//simon -> simonmoscrop@googlemail.com
//////instructions
////for use with FreePascal
//define: FPC
////encoding a single message:
//first create a message: msg := TOSCMessage.Create(address)
//then call msg.AddFloat(value)... to add any number of arguments
//with msg.ToOSCBytes you get the TBytes you can send via an indy10 TidUDPClient.SendBuffer
////encoding a bundle:
//first create a bundle: bundle := TOSCBundle.Create
//then add any number of packets (i.e. message, bundle) via bundle.Add(packet)
//with bundle.ToOSCBytes you get the TBytes you can send via an indy10 TidUDPClient.SendBuffer
////decoding a string
//use TOSCPacket.Unpack(bytes, Length(bytes)) to create
//TOSCPackets of your osc-bytes (those can be either bundles or single
//messages. if you want to decode several packets at once you can create
//a container bundle first and add the packets you create like this.
//then use msg := FPacket.MatchAddress(address) to find a message with the
//according address in your packet-structure.
//before you now can access the arguments and typetags of a message you have
//to call msg.Decode
//voila.
unit OSCUtils;
interface
{$I SBDefines.INC}
uses Classes, SysUtils, Generics.Collections;
{$IFDEF NEXTGEN}
{$ZEROBASEDSTRINGS OFF}
{$ENDIF}
type
TOSCPacket = class;
TOSCMessage = class;
TOSCPacket = class (TObject)
private
protected
FBytes: TBytes;
function MatchBrackets(pMessage, pAddress: PChar): Boolean;
function MatchList(pMessage, pAddress: PChar): Boolean;
function MatchPattern(pMessage, pAddress: PChar): Boolean;
public
constructor Create(Bytes: TBytes);
function MatchAddress(Address: String): TOSCMessage; virtual; abstract;
function ToOSCBytes: TBytes; virtual; abstract;
procedure Unmatch; virtual; abstract;
class function Unpack(Bytes: TBytes; Count: Integer): TOSCPacket; overload;
class function Unpack(Bytes: TBytes; Offset, Count: Integer; TimeTag: Extended
= 0): TOSCPacket; overload; virtual;
end;
TOSCMessage = class(TOSCPacket)
private
FAddress: string;
FArguments: TList<TBytes>;
FIsDecoded: Boolean;
FMatched: Boolean;
FTimeTag: Extended;
FTypeTagOffset: Integer;
FTypeTags: string;
function GetArgument(Index: Integer): TBytes;
function GetArgumentAsFloat(Index: Integer): Single;
function GetArgumentAsInt(Index: Integer): Integer;
function GetArgumentCount: Integer;
function GetTypeTag(Index: Integer): string;
function GetArgumentAsString(Index: Integer): string;
function GetArgumentAsDouble(Index: Integer): Double;
public
constructor Create(Address: string); overload;
constructor Create(Bytes: TBytes); overload;
destructor Destroy; override;
function AddAsBytes(const TypeTag: Char; const Value: String; const fs:
TFormatSettings): HResult;
procedure AddFloat(Value: Single);
procedure AddDouble(Value: Double);
procedure AddInteger(Value: Integer);
procedure AddString(Value: String);
procedure AddBlob(Value: TBytes);
procedure Decode;
function MatchAddress(Address: String): TOSCMessage; override;
function ToOSCBytes: TBytes; override;
procedure Unmatch; override;
class function Unpack(Bytes: TBytes; PacketOffset, Count: Integer; TimeTag:
Extended = 0): TOSCPacket; overload; override;
property Address: string read FAddress write FAddress;
property Argument[Index: Integer]: TBytes read GetArgument;
property ArgumentAsString[Index: Integer]: string read GetArgumentAsString;
property ArgumentAsFloat[Index: Integer]: Single read GetArgumentAsFloat;
property ArgumentAsDouble[Index: Integer]: Double read GetArgumentAsDouble;
property ArgumentAsInt[Index: Integer]: Integer read GetArgumentAsInt;
property ArgumentCount: Integer read GetArgumentCount;
property IsDecoded: Boolean read FIsDecoded write FIsDecoded;
property Matched: Boolean read FMatched write FMatched;
property TimeTag: Extended read FTimeTag write FTimeTag;
property TypeTag[Index: Integer]: String read GetTypeTag;
property TypeTagOffset: Integer read FTypeTagOffset write FTypeTagOffset;
end;
TOSCBundle = class(TOSCPacket)
private
FPackets: TObjectList<TOSCPacket>;
public
constructor Create(Bytes: TBytes);
destructor Destroy; override;
procedure Add(const Packet: TOSCPacket);
function MatchAddress(Address: String): TOSCMessage; override;
function ToOSCBytes: TBytes; override;
procedure Unmatch; override;
class function Unpack(Bytes: TBytes; PacketOffset, Count: Integer; TimeTag:
Extended = 0): TOSCPacket; overload; override;
end;
function MakeOSCFloat(value: Single): TBytes;
function MakeOSCInt(value: Integer): TBytes;
function MakeOSCString(value: String): TBytes;
function UnpackInt(Bytes: TBytes; var Offset: Integer): TBytes;
function UnpackString(Bytes: TBytes; var Offset: Integer): TBytes;
function UnpackFloat(Bytes: TBytes; var Offset: Integer): TBytes;
function UnpackAndReturnInt(Bytes: TBytes; var Offset: Integer): Integer;
function UnpackAndReturnFloat(Bytes: TBytes; var Offset: Integer): Single;
const
OSC_OK = 0;
OSC_UNRECOGNIZED_TYPETAG = 1;
OSC_CONVERT_ERROR = 2;
implementation
uses
Math, IdGlobal {$IFNDEF FPC}, WinSock {$ENDIF};
function MakeOSCFloat(value: Single): TBytes;
var
intg: Integer;
begin
intg := PInteger(@value)^;
{$IFDEF FPC}
intg := BEtoN(intg);
{$ELSE}
intg := htonl(intg);
{$ENDIF}
Result := TBytes(IdGlobal.RawToBytes(intg, SizeOf(intg)));
end;
function SwapEndianInt64(Value: Int64): Int64; // from AI!
var
ByteArray: array[0..7] of Byte absolute Value;
Temp: Byte;
begin
Temp := ByteArray[0];
ByteArray[0] := ByteArray[7];
ByteArray[7] := Temp;
Temp := ByteArray[1];
ByteArray[1] := ByteArray[6];
ByteArray[6] := Temp;
Temp := ByteArray[2];
ByteArray[2] := ByteArray[5];
ByteArray[5] := Temp;
Temp := ByteArray[3];
ByteArray[3] := ByteArray[4];
ByteArray[4] := Temp;
Result := Value;
end;
function MakeOSCDouble(value: Double): TBytes;
var
intg: Int64;
begin
intg := PInt64(@value)^;
intg := SwapEndianInt64(intg);
Result := TBytes(IdGlobal.RawToBytes(intg, SizeOf(intg)));
end;
function MakeOSCInt(value: Integer): TBytes;
begin
{$IFDEF FPC}
value := BEtoN(value);
{$ELSE}
value := htonl(value);
{$ENDIF}
Result := TBytes(IdGlobal.RawToBytes(value, SizeOf(value)));
end;
function OSCtoInt(Value: TBytes): Integer;
begin
Result := htonl(IdGlobal.BytesToInt32(TIdBytes(Value)));
end;
//function OSCtoFloat(Value: TBytes): Single;
//type
// TBytesAndFloat = packed record
// case Integer of
// 1: (ValueF: Single);
// 2: (a, b, c, d: Byte);
// end;
//begin
// Result := PSingle(@htonl(IdGlobal.BytesToLongInt(TIdBytes(Value))));
//end;
function MakeOSCString(value: String): TBytes;
var i, ln: Integer;
begin
ln := TEncoding.UTF8.GetByteCount(value);
ln := ln + (4 - ln mod 4);
SetLength(Result, ln);
{$IFDEF NEXTGEN}
ln := TEncoding.UTF8.GetBytes(value, 0, Length(value), Result, 0);
{$ELSE}
ln := TEncoding.UTF8.GetBytes(value, 1, Length(value), Result, 0);
{$ENDIF}
for i := ln to High(Result) do
result[i] := 0;
end;
function MakeOSCBlob(value: TBytes): TBytes;
var i, ln: Integer;
begin
ln := Length(Value) + 4; // add size of Int32
ln := ln + (ln mod 4);
Result := MakeOSCInt(Length(Value)); // 1st Int32 is content size
SetLength(Result, ln);
IdGlobal.CopyTIdBytes(TIdBytes(value), 0, TIdBytes(Result), 4, Length(Value));
for i := ln to High(Result) do
result[i] := 0;
end;
function UnpackInt(Bytes: TBytes; var Offset: Integer): TBytes;
var
i: Integer;
begin
SetLength(Result, SizeOf(Integer));
// Copy bytes and change byte order
for i := 0 to High(Result) do
Result[i] := Bytes[Offset + High(Result) - i];
Inc(Offset, SizeOf(Integer));
end;
function CopyInt(Bytes: TBytes; var Offset: Integer): TBytes;
var
i: Integer;
begin
SetLength(Result, SizeOf(Integer));
// Copy bytes and do NOT change byte order
for i := 0 to High(Result) do
Result[i] := Bytes[Offset + i];
Inc(Offset, SizeOf(Integer));
end;
function UnpackString(Bytes: TBytes; var Offset: Integer): TBytes;
var
off: Integer;
begin
// Strings are null terminated. Find position of null.
off := Offset;
while (off < Length(Bytes)) and (Bytes[off] <> 0) do
Inc(off);
// Retrieve the string.
SetLength(Result, off - Offset);
IdGlobal.CopyTIdBytes(TIdBytes(Bytes), Offset, TIdBytes(Result), 0, Length(Result));
// Increase the offset by a multiple of 4.
Offset := off + (4 - off mod 4);
end;
function CopyString(Bytes: TBytes; var Offset: Integer): TBytes;
var
off: Integer;
begin
// Strings are null terminated. Find position of null.
off := Offset;
while (off < Length(Bytes)) and (Bytes[off] <> 0) do
Inc(off);
off := off + (4 - off mod 4);
// Retrieve the string.
SetLength(Result, off - Offset);
IdGlobal.CopyTIdBytes(TIdBytes(Bytes), Offset, TIdBytes(Result), 0, Length(Result));
// Increase the offset by a multiple of 4.
Offset := off;
end;
function UnpackBlob(Bytes: TBytes; var Offset: Integer): TBytes;
var
off: Integer;
Len: Cardinal;
begin
// Read length of blob
Len := UnpackAndReturnInt(Bytes, Offset);
// Retrieve the blo
SetLength(Result, Len);
IdGlobal.CopyTIdBytes(TIdBytes(Bytes), Offset, TIdBytes(Result), 0, Len);
off := Offset + Len;
// Increase the offset by a multiple of 4.
Offset := off + (4 - off mod 4);
end;
function UnpackFloat(Bytes: TBytes; var Offset: Integer): TBytes;
var
//value: Integer;
i: Integer;
begin
SetLength(Result, SizeOf(Single));
// Copy bytes and change byte order
for i := 0 to High(Result) do
begin
Result[i] := Bytes[Offset + High(Result) - i];
end;
Inc(Offset, SizeOf(Single));
end;
function UnpackDouble(Bytes: TBytes; var Offset: Integer): TBytes;
var
//value: Integer;
i: Integer;
begin
SetLength(Result, SizeOf(Double));
// Copy bytes and change byte order
for i := 0 to High(Result) do
begin
Result[i] := Bytes[Offset + High(Result) - i];
end;
Inc(Offset, SizeOf(Double));
end;
function CopyFloat(Bytes: TBytes; var Offset: Integer): TBytes;
var
i: Integer;
begin
SetLength(Result, SizeOf(Single));
// Copy bytes and do NOT change byte order
for i := 0 to High(Result) do
Result[i] := Bytes[Offset + i];
Inc(Offset, SizeOf(Single));
end;
function CopyDouble(Bytes: TBytes; var Offset: Integer): TBytes;
var
i: Integer;
begin
SetLength(Result, SizeOf(Double));
// Copy bytes and do NOT change byte order
for i := 0 to High(Result) do
Result[i] := Bytes[Offset + i];
Inc(Offset, SizeOf(Double));
end;
function UnpackAndReturnInt(Bytes: TBytes; var Offset: Integer): Integer;
var
resultBytes: TBytes;
begin
resultBytes := UnpackInt(Bytes, Offset);
Result := PInteger(Pointer(resultBytes))^;
end;
function UnpackAndReturnFloat(Bytes: TBytes; var Offset: Integer): Single;
var
resultBytes: TBytes;
begin
resultBytes := UnpackFloat(Bytes, Offset);
Result := PSingle(Pointer(resultBytes))^;
end;
function UnpackAndReturnDouble(Bytes: TBytes; var Offset: Integer): Double;
var
resultBytes: TBytes;
begin
resultBytes := UnpackDouble(Bytes, Offset);
Result := PDouble(Pointer(resultBytes))^;
end;
constructor TOSCMessage.Create(Address: string);
begin
FAddress := Address;
Create(nil);
end;
constructor TOSCMessage.Create(Bytes: TBytes);
begin
inherited Create(Bytes);
FTypeTags := ',';
FArguments := TList<TBytes>.Create;
FIsDecoded := false;
end;
destructor TOSCMessage.Destroy;
begin
FArguments.Free;
inherited;
end;
function TOSCMessage.AddAsBytes(const TypeTag: Char; const Value: String; const
fs: TFormatSettings): HResult;
begin
Result := OSC_OK;
try
if TypeTag = 'f' then
AddFloat(StrToFloat(Value, fs))
else if TypeTag = 'i' then
AddInteger(StrToInt(Value))
else if TypeTag = 's' then
AddString(Value)
else if TypeTag = 'd' then
AddDouble(StrToFloat(Value))
else
Result := OSC_UNRECOGNIZED_TYPETAG;
except on EConvertError do
Result := OSC_CONVERT_ERROR;
end;
end;
procedure TOSCMessage.AddFloat(Value: Single);
begin
FTypeTags := FTypeTags + 'f';
FArguments.Add(MakeOSCFloat(Value));
end;
procedure TOSCMessage.AddDouble(Value: Double);
begin
FTypeTags := FTypeTags + 'd';
FArguments.Add(MakeOSCDouble(Value));
end;
procedure TOSCMessage.AddInteger(Value: Integer);
begin
FTypeTags := FTypeTags + 'i';
FArguments.Add(MakeOSCInt(Value));
end;
procedure TOSCMessage.AddString(Value: String);
begin
FTypeTags := FTypeTags + 's';
FArguments.Add(MakeOSCString(Value));
end;
procedure TOSCMessage.AddBlob(Value: TBytes);
begin
FTypeTags := FTypeTags + 'b';
FArguments.Add(MakeOSCBlob(Value));
end;
procedure TOSCMessage.Decode;
var
i, offset: Integer;
begin
if FIsDecoded then
exit;
offset := FTypeTagOffset;
FTypeTags := TEncoding.ASCII.GetString(UnpackString(FBytes, offset));
for i := 1 to Length(FTypeTags) - 1 do
begin
if FTypeTags[i+1] = 's' then
FArguments.Add(UnpackString(FBytes, offset))
else if FTypeTags[i+1] = 'i' then
FArguments.Add(CopyInt(FBytes, offset)) // SZ: UnpackInt -> CopyInt Bugfix: Adding an int and then immediately reading it back reverses the byte order -> do the decoding in GetArgument instead
else if FTypeTags[i+1] = 'f' then
FArguments.Add(CopyFloat(FBytes, offset)) // SZ: UnpackFloat -> CopyFloat
else if FTypeTags[i+1] = 'd' then
FArguments.Add(CopyDouble(FBytes, offset))
else if FTypeTags[i+1] = 'b' then
FArguments.Add(UnpackBlob(FBytes, offset));
end;
FIsDecoded := true;
end;
function TOSCMessage.GetArgument(Index: Integer): TBytes;
begin
Result := FArguments[Index];
end;
function TOSCMessage.GetArgumentAsFloat(Index: Integer): Single;
var
Idx: Integer;
begin
Idx := 0;
Result := UnpackAndReturnFloat(FArguments[Index], Idx); // Result := PSingle(Pointer(FArguments[Index]))^; // SZ (fixed problem described in TOSCMessage.Decode)
end;
function TOSCMessage.GetArgumentAsDouble(Index: Integer): Double;
var
Idx: Integer;
begin
Idx := 0;
Result := UnpackAndReturnDouble(FArguments[Index], Idx);
end;
function TOSCMessage.GetArgumentAsInt(Index: Integer): Integer;
begin
Result := OSCtoInt(FArguments[Index]); //Result := PInteger(Pointer(FArguments[Index]))^; //SZ (fixed problem described in TOSCMessage.Decode)
end;
function TOSCMessage.GetArgumentAsString(Index: Integer): string;
begin
{$IF compilerversion >= 30}
Result := StringOf(FArguments[Index]).TrimRight([#0]);
{$ELSE}
Result := TrimRight(StringOf(FArguments[Index]));
{$IFEND}
end;
function TOSCMessage.GetArgumentCount: Integer;
begin
Result := FArguments.Count;
end;
function TOSCMessage.GetTypeTag(Index: Integer): string;
begin
{$IF compilerversion >= 30}
Result := FTypeTags[Index + 1 + Low(FTypeTags)]; // Result := FTypeTags[Index + 2]; // iOS zero based strings
{$ELSE}
Result := FTypeTags[Index + 1 + 1]
{$IFEND}
end;
function TOSCMessage.MatchAddress(Address: String): TOSCMessage;
begin
if not FMatched
and MatchPattern(PChar(FAddress), PChar(Address)) then
begin
FMatched := true;
Result := Self
end
else
Result := nil;
end;
function TOSCMessage.ToOSCBytes: TBytes;
var
i, j: Integer;
resultList: TList<Byte>;
tmp: array of Byte;
MissingBytes: Integer;
begin
resultList := TList<Byte>.Create;
resultList.AddRange(MakeOSCString(FAddress));
resultList.AddRange(MakeOSCString(FTypeTags));
for i := 0 to FArguments.Count - 1 do
begin
resultList.AddRange(FArguments[i]);
MissingBytes := resultlist.Count mod 4; //SZ
if MissingBytes <> 0 then
begin
SetLength(tmp, MissingBytes);
for j := 0 to MissingBytes - 1 do
tmp[I] := 0;
resultlist.AddRange(tmp);
end;
end;
{$IFDEF DX100_UP}
Result := resultList.ToArray();
{$ELSE}
SetLength(Result, resultList.Count);
for I := 0 to resultList.Count - 1 do
Result[I] := resultList[I];
{$ENDIF}
resultList.Free;
end;
procedure TOSCMessage.Unmatch;
begin
FMatched := false;
end;
class function TOSCMessage.Unpack(Bytes: TBytes; PacketOffset, Count: Integer;
TimeTag: Extended = 0): TOSCPacket;
begin
Result := TOSCMessage.Create(Bytes);
//for now decode address only
(Result as TOSCMessage).Address := TEncoding.ASCII.GetString(UnpackString(Bytes, PacketOffset));
(Result as TOSCMessage).TimeTag := TimeTag;
//save offset for later decoding on demand
(Result as TOSCMessage).TypeTagOffset := PacketOffset;
(Result as TOSCMessage).IsDecoded := false;
end;
constructor TOSCBundle.Create(Bytes: TBytes);
begin
inherited Create(Bytes);
FPackets := TObjectList<TOSCPacket>.Create;
FPackets.OwnsObjects := true;
end;
destructor TOSCBundle.Destroy;
begin
FPackets.Free;
inherited;
end;
procedure TOSCBundle.Add(const Packet: TOSCPacket);
begin
FPackets.Add(Packet);
end;
function TOSCBundle.MatchAddress(Address: String): TOSCMessage;
var
i: Integer;
begin
Result := nil;
for i := 0 to FPackets.Count - 1 do
begin
Result := (FPackets[i] as TOSCPacket).MatchAddress(Address);
if Assigned(Result) then
break;
end;
end;
function TOSCBundle.ToOSCBytes: TBytes;
var
i: Integer;
packet: TBytes;
resultList: TList<Byte>;
begin
resultList := TList<Byte>.Create;
resultList.AddRange(MakeOSCString('#bundle'));
resultList.AddRange(TEncoding.UTF8.GetBytes(#0#0#0#0#0#0#0#1)); //immediately
for i := 0 to FPackets.Count - 1 do
begin
packet := (FPackets[i] as TOSCPacket).ToOSCBytes;
resultList.AddRange(MakeOSCInt(Length(packet)));
resultList.AddRange(packet);
end;
{$IFDEF DX100_UP}
Result := resultList.ToArray();
{$ELSE}
SetLength(Result, resultList.Count);
for I := 0 to resultList.Count - 1 do
Result[I] := resultList[I];
{$ENDIF}
resultList.Free;
end;
procedure TOSCBundle.Unmatch;
var
i: Integer;
begin
for i := 0 to FPackets.Count - 1 do
(FPackets[i] as TOSCPacket).UnMatch;
end;
class function TOSCBundle.Unpack(Bytes: TBytes; PacketOffset, Count: Integer;
TimeTag: Extended = 0): TOSCPacket;
var
packetLength: Integer;
tt1, tt2: Cardinal;
begin
Result := TOSCBundle.Create(Bytes);
//advance the '#bundle' string
UnpackString(Bytes, PacketOffset);
//advance the timestamp
tt1 := Cardinal(UnpackAndReturnInt(Bytes, PacketOffset));
tt2 := Cardinal(UnpackAndReturnInt(Bytes, PacketOffset));
TimeTag := tt1 + tt2 / power(2, 32);
while PacketOffset < Count do
begin
packetLength := UnpackAndReturnInt(Bytes, PacketOffset);
//note: PacketOffset is always from the very beginning of Bytes!
//not the beginning of the current packet.
(Result as TOSCBundle).Add(TOSCPacket.Unpack(Bytes, PacketOffset, PacketOffset + packetLength, TimeTag));
Inc(PacketOffset, packetLength);
end;
end;
constructor TOSCPacket.Create(Bytes: TBytes);
begin
FBytes := Bytes;
end;
// we know that pattern[0] == '[' and test[0] != 0 */
function TOSCPacket.MatchBrackets(pMessage, pAddress: PChar): Boolean;
var
negated: Boolean;
p, p1, p2: PChar;
begin
p := pMessage;
Result := false;
negated := false;
Inc(pMessage);
if pMessage^ = #0 then
begin
//LogWarningFMT('Unterminated [ in message: %s', [FInput[0]]);
Dec(pMessage);
exit;
end;
if pMessage^ = '!' then
begin
negated := true;
Inc(p);
end;
Dec(pMessage);
Result := negated;
while p^ <> ']' do
begin
if p^ = #0 then
begin
//LogWarningFMT('Unterminated [ in message: %s', [FInput[0]]);
exit;
end;
p1 := p + 1; // sizeOf(PChar);
p2 := p1 + 1; //sizeOf(PChar);
if (p1^ = '-')
and (p2^ <> #0) then
if (Ord(pAddress^) >= Ord(p^))
and (Ord(pAddress^) <= Ord(p2^)) then
begin
Result := not negated;
break;
end;
if p^ = pAddress^ then
begin
Result := not negated;
break;
end;
Inc(p);
end;
if Result = false then
exit;
while p^ <> ']' do
begin
if p^ = #0 then
begin
//LogWarningFMT('Unterminated [ in message: %s', [FInput[0]]);
exit;
end;
Inc(p);
end;
Inc(p);
pMessage := p;
Inc(pAddress);
Result := MatchPattern(p, pAddress);
end;
function TOSCPacket.MatchList(pMessage, pAddress: PChar): Boolean;
var
p, tp: PChar;
begin
Result := false;
p := pMessage;
tp := pAddress;
while p^ <> '}' do
begin
if p^ = #0 then
begin
//LogWarningFMT('Unterminated { in message: %s', [FInput[0]]);
exit;
end;
Inc(p);
end;
// for(restOfPattern = pattern; *restOfPattern != '}'; restOfPattern++) {
// if (*restOfPattern == 0) {
// OSCWarning("Unterminated { in pattern \".../%s/...\"", theWholePattern);
// return FALSE;
// }
//}
Inc(p); // skip close curly brace
Inc(pMessage); // skip open curly brace
while true do
begin
if pMessage^ = ',' then
begin
if MatchPattern(p, tp) then
begin
Result := true;
pMessage := p;
pAddress := tp;
exit;
end
else
begin
tp := pAddress;
Inc(pMessage);
end;
end
else if pMessage^ = '}' then
begin
Result := MatchPattern(p, tp);
pMessage := p;
pAddress := tp;
exit;
end
else if pMessage^ = tp^ then
begin
Inc(pMessage);
Inc(tp);
end
else
begin
tp := pAddress;
while (pMessage^ <> ',')
and (pMessage^ <> '}') do
Inc(pMessage);
if pMessage^ = ',' then
Inc(pMessage);
end;
end;
end;
function TOSCPacket.MatchPattern(pMessage, pAddress: PChar): Boolean;
begin
if (pMessage = nil)
or (pMessage^ = #0) then
begin
Result := pAddress^ = #0;
exit;
end;
if pAddress^ = #0 then
begin
if pMessage^ = '*' then
begin
Result := MatchPattern(pMessage + 1, pAddress);
exit;
end
else
begin
Result := false;
exit;
end;
end;
case pMessage^ of
#0 : Result := pAddress^ = #0;
'?': Result := MatchPattern(pMessage + 1, pAddress + 1);
'*':
begin
if MatchPattern(pMessage + 1, pAddress) then
Result := true
else
Result := MatchPattern(pMessage, pAddress + 1);
end;
']','}':
begin
//LogWarningFMT('Spurious %s in message: %s', [pMessage^, FInput[0]]);
Result := false;
end;
'[': Result := MatchBrackets(pMessage, pAddress);
'{': Result := MatchList(pMessage, pAddress);
{'\\':
begin
if pMessage^ + 1 = #0 then
Result := pAddress^ = #0
else if pMessage^ + 1 = pAddress^
Result := MatchPattern(pMessage + 2, pAddress + 1)
else
Result := false;
end; }
else
if pMessage^ = pAddress^ then
Result := MatchPattern(pMessage + 1,pAddress + 1)
else
Result := false;
end;
end;
class function TOSCPacket.Unpack(Bytes: TBytes; Count: Integer): TOSCPacket;
begin
Result := UnPack(Bytes, 0, Count);
end;
class function TOSCPacket.Unpack(Bytes: TBytes; Offset, Count: Integer;
TimeTag: Extended = 0): TOSCPacket;
begin
if Char(Bytes[Offset]) = '#' then
Result := TOSCBundle.UnPack(Bytes, Offset, Count)
else
Result := TOSCMessage.UnPack(Bytes, Offset, Count, TimeTag);
end;
end.