/
Message.pm6
245 lines (213 loc) · 6.26 KB
/
Message.pm6
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
use v6;
unit class Net::OSC::Message;
use Net::OSC::Types;
=begin pod
=head1 NAME
Net::OSC::Message - Implements OSC message packing and unpacking
=head1 METHODS
=begin code
method new(:$path = '/', :@args, :$!is64bit = True)
=end code
Set :is64bit to false to force messages to be packed to 32bit types
this option may be required to talk to some versions of Max and other old OSC implementations.
=end pod
use Numeric::Pack :ALL;
my %type-map32 =
Int.^name, 'i',
IntStr.^name, 'i',
Num.^name, 'f',
Rat.^name, 'f',
RatStr.^name, 'f',
FatRat.^name, 'f',
Str.^name, 's',
Blob.^name, 'b',
;
my %type-map64 =
Int.^name, 'i',
IntStr.^name, 'i',
Num.^name, 'd',
Rat.^name, 'd',
RatStr.^name, 'd',
FatRat.^name, 'd',
Str.^name, 's',
Blob.^name, 'b',
;
has OSCPath $.path = '/';
has Str @!type-list;
has @!args;
has Bool $.is64bit = True;
submethod BUILD(:@!args, :$!path = '/', :$!is64bit = True) {
self!update-type-list(@!args);
}
method type-string() returns Str
#= Returns the current type string of this messages content.
#= See OSC types for possible values.
{
return @!type-list.join: '' if @!type-list.elems > 0;
''
}
method pick-osc-type($arg) returns Str
#= Returns the character representing the OSC type $arg would be packed as
#= by this Message object.
{
#say "Choosing type for $arg of type {$arg.WHAT.perl}";
my $type-map = $!is64bit ?? %type-map64 !! %type-map32;
if $arg.WHAT.perl ~~ $type-map {
return $type-map{$arg.WHAT.perl};
}
else {
die "Unable to map $arg of type { $arg.WHAT.perl } to OSC type!";
}
}
method !update-type-list(*@args){
for @args -> $arg {
@!type-list.push: self.pick-osc-type($arg);
}
}
method args(*@new-args) returns Seq
#= Adds any arguments as args to the object and returns the current message arguments.
#= The OSC type of the argument will be determined according the the current OSC types map.
{
if @new-args {
@!args.push(|@new-args);
self!update-type-list(|@new-args);
}
gather for @!args -> $arg {
take $arg;
}
}
method set-args(*@new-args)
#= Clears the message args lists and sets it to the arguments provided.
#= The OSC type of the argument will be determined according the the current OSC types map.
{
@!args = ();
@!type-list = ();
self.args(@new-args) if @new-args;
}
method type-map() returns Seq
#= Returns the current OSC type map of the message.
#= This will change depending on the is64bit flag.
{
($!is64bit ?? %type-map64 !! %type-map32).pairs;
}
method package() returns Blob
#= Returns a Buf of the packed OSC message.
#= See unpackage to turn a Buf into a Message object.
{
self.pack-string($!path)
~ self.pack-string(",{ self.type-string() }")
~ self!pack-args();
}
method !pack-args() returns Buf
#= Map OSC arg types to a packing routine
{
return Buf.new unless @!args.elems > 0;
Buf.new( (gather for @!args Z @!type-list -> ($arg, $type) {
#say "Packing '$arg' of OSC type '$type' with pattern '%pack-map{$type}'";
given $type {
when 'f' {
take pack-float($arg, :byte-order(big-endian));
}
when 'd' {
take pack-double($arg, :byte-order(big-endian));
}
when 'i' {
take pack-int32($arg, :byte-order(big-endian));
}
when 's' {
take self.pack-string($arg);
}
default {
die "No type map defined for '$_' unable to add { $arg.gist } to OSC message.";
}
}
}).map( { |$_[0..*] } ) )
}
#returns a new Message object
method unpackage(Buf $packed-osc) returns Net::OSC::Message
#= Returns a Net::OSC::Message from a Buf where the content of the Buf is an OSC message.
#= Will die on unhandled OSC type and behaviour is currently undefined on non OSC message Bufs.
{
#say "Unpacking message of {$packed-osc.elems} byte(s):";
#say $packed-osc.map( { sprintf('%4s', $_.base(16)) } ).rotor(8, :partial).join("\n");
my $path = '';
my @types;
my @args;
my $read-pointer = 0;
my $buffer-width = 1;
my $message-part = 0; # 0 = path, 1 = type string, 2 = args
#Closure for string parsing, operates on this scope of variables
my $extract-string = sub {
#say "Unpacking string";
$buffer-width = 4;
my $arg = '';
my $chars;
repeat {
$chars = $packed-osc.subbuf($read-pointer, $buffer-width);
$read-pointer += $buffer-width;
for $chars.decode('ISO-8859-1').comb -> $char {
if $char eq "\0" {
$buffer-width = 0; #signal end of string
last;
}
$arg ~= $char;
}
} while $buffer-width == 4 and $read-pointer < $packed-osc.elems;
#say "'$arg'";
$arg;
}
#start parse
$path = $extract-string.();
@types = $extract-string.().comb: /\w/; #extract type chars and ignore the ','
while $read-pointer < $packed-osc.elems {
given @types.shift -> $type {
when $type eq 'f' {
$buffer-width = 4;
my $buf = $packed-osc.subbuf($read-pointer, $buffer-width);
@args.push: unpack-float $buf, :byte-order(big-endian);
$read-pointer += $buffer-width;
}
when $type eq 'd' {
$buffer-width = 8;
my $buf = $packed-osc.subbuf($read-pointer, $buffer-width);
@args.push: unpack-double $buf, :byte-order(big-endian);
$read-pointer += $buffer-width;
}
when $type eq 'i' {
$buffer-width = 4;
my $buf = $packed-osc.subbuf($read-pointer, $buffer-width);
@args.push: unpack-int32 $buf, :byte-order(big-endian);
$read-pointer += $buffer-width;
}
when $type eq 's' {
@args.push: $extract-string.();
}
default {
die "Unhandled type '$type'";
}
}
}
Net::OSC::Message.new(
:$path,
:@args
);
}
method buf2bin(Buf $bits) returns Array
#= Returns a binary array of the content of a Buf. Useful for debugging.
{
my @bin;
for 0 .. ($bits.elems - 1) {
@bin.push: |sprintf( '%08d', $bits[$_].base(2) ).comb;
}
@bin
}
method bits2buf(@bits) returns Buf
#= Returns a Buf from a binary array. Not super useful.
{
Buf.new: @bits.rotor(8).map: { self.unpack-int($_, :signed(False)) };
}
method pack-string(Str $string) returns Blob
#= Returns a Blob of a string packed for OSC transmission.
{
( $string ~ ( "\0" x 4 - ( $string.chars % 4) ) ).encode('ISO-8859-1')
}