LMMS
Loading...
Searching...
No Matches
osctl.h
Go to the documentation of this file.
1/* Calf DSP Library
2 * Open Sound Control primitives
3 *
4 * Copyright (C) 2007 Krzysztof Foltman
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#ifndef __CALF_OSCTL_H
23#define __CALF_OSCTL_H
24
25#include <string>
26#include <vector>
27#include <string.h>
28#include <errno.h>
29#include <iostream>
30#ifndef _MSC_VER
31#include <netinet/in.h>
32#include <netdb.h>
33#else
34#include <winsock2.h>
35#pragma comment(lib,"Ws2_32.lib")
36#endif
37
38namespace osctl
39{
40
42{
43 osc_i32 = 'i',
44 osc_f32 = 'f',
46 osc_blob = 'b',
47
48 // unsupported
49 osc_i64 = 'h',
50 osc_ts = 't',
51 osc_f64 = 'd',
53 osc_char = 'c',
54 osc_rgba = 'r',
55 osc_midi = 'm',
56 osc_true = 'T',
57 osc_false = 'F',
58 osc_nil = 'N',
59 osc_inf = 'I',
62};
63
64extern const char *osc_type_name(osc_type type);
65
66struct osc_exception: public std::exception
67{
68 virtual const char *what() const throw() { return "OSC parsing error"; }
69};
70
71struct osc_read_exception: public std::exception
72{
73 virtual const char *what() const throw() { return "OSC buffer underflow"; }
74};
75
76struct osc_write_exception: public std::exception
77{
78 virtual const char *what() const throw() { return "OSC buffer overflow"; }
79};
80
82{
83 static bool read(uint8_t *dest, uint32_t bytes)
84 {
85 return false;
86 }
87 static bool write(uint8_t *dest, uint32_t bytes)
88 {
89 return true;
90 }
91 static void clear()
92 {
93 }
94};
95
97{
100
102 {
103 ptr = NULL;
104 pos = count = size = 0;
105 }
106 raw_buffer(uint8_t *_ptr, uint32_t _count, uint32_t _size)
107 {
108 set(_ptr, _count, _size);
109 }
110 inline void set(uint8_t *_ptr, uint32_t _count, uint32_t _size)
111 {
112 ptr = _ptr;
113 pos = 0;
114 count = _count;
115 size = _size;
116 }
117 bool read(uint8_t *dest, uint32_t bytes)
118 {
119 if (pos + bytes > count)
120 return false;
121 memcpy(dest, ptr + pos, bytes);
122 pos += bytes;
123 return true;
124 }
125 bool write(const uint8_t *src, uint32_t bytes)
126 {
127 if (count + bytes > size)
128 return false;
129 memcpy(ptr + count, src, bytes);
130 count += bytes;
131 return true;
132 }
134 {
135 return count - pos;
136 }
138 {
139 return size - count;
140 }
142 {
143 return 4 - (count & 3);
144 }
145 void clear()
146 {
147 pos = 0;
148 count = 0;
149 }
150 int tell()
151 {
152 return pos;
153 }
154 void seek(int _pos)
155 {
156 pos = _pos;
157 }
158};
159
161{
162 std::string data;
164
166 {
167 pos = 0;
168 size = 1048576;
169 }
170 string_buffer(std::string _data, int _size = 1048576)
171 {
172 data = _data;
173 pos = 0;
174 size = _size;
175 }
176 bool read(uint8_t *dest, uint32_t bytes)
177 {
178 if (pos + bytes > data.length())
179 return false;
180 memcpy(dest, &data[pos], bytes);
181 pos += bytes;
182 return true;
183 }
184 bool write(const uint8_t *src, uint32_t bytes)
185 {
186 if (data.length() + bytes > size)
187 return false;
188 uint32_t wpos = data.length();
189 data.resize(wpos + bytes);
190 memcpy(&data[wpos], src, bytes);
191 return true;
192 }
193 inline int read_left()
194 {
195 return data.length() - pos;
196 }
197 inline int write_left()
198 {
199 return size - data.length();
200 }
202 {
203 return 4 - (data.length() & 3);
204 }
205 void clear()
206 {
207 data.clear();
208 pos = 0;
209 }
210 int tell()
211 {
212 return pos;
213 }
214 void seek(int _pos)
215 {
216 pos = _pos;
217 }
218};
219
220template<class Buffer, class TypeBuffer = null_buffer, bool Throw = true>
222{
223 Buffer &buffer;
224 TypeBuffer *type_buffer;
225 bool error;
226
227 osc_stream(Buffer &_buffer) : buffer(_buffer), type_buffer(NULL), error(false) {}
228 osc_stream(Buffer &_buffer, TypeBuffer &_type_buffer) : buffer(_buffer), type_buffer(&_type_buffer), error(false) {}
229 inline void pad()
230 {
231 uint32_t zero = 0;
232 write(&zero, buffer.write_misalignment());
233 }
234 inline void read(void *dest, uint32_t bytes)
235 {
236 if (!buffer.read((uint8_t *)dest, bytes))
237 {
238 if (Throw)
239 throw osc_read_exception();
240 else
241 {
242 error = true;
243 memset(dest, 0, bytes);
244 }
245 }
246 }
247 inline void write(const void *src, uint32_t bytes)
248 {
249 if (!buffer.write((const uint8_t *)src, bytes))
250 {
251 if (Throw)
252 throw osc_write_exception();
253 else
254 error = true;
255 }
256 }
257 inline void clear()
258 {
259 buffer.clear();
260 if (type_buffer)
261 type_buffer->clear();
262 }
263 inline void write_type(char ch)
264 {
265 if (type_buffer)
266 type_buffer->write((uint8_t *)&ch, 1);
267 }
268};
269
272
274{
276 : string_buffer(), osc_strstream(static_cast<string_buffer &>(*this))
277 {
278 }
279};
280
285
293
294template<class Buffer, class TypeBuffer>
295inline osc_stream<Buffer, TypeBuffer> &
296operator <<(osc_stream<Buffer, TypeBuffer> &s, uint32_t val)
297{
298 val = htonl(val);
299 s.write(&val, 4);
300 s.write_type(osc_i32);
301 return s;
302}
303
304template<class Buffer, class TypeBuffer>
307{
308 s.read(&val, 4);
309 val = htonl(val);
310 return s;
311}
312
313template<class Buffer, class TypeBuffer>
314inline osc_stream<Buffer, TypeBuffer> &
316{
317 s.read(&val, 4);
318 val = htonl(val);
319 return s;
320}
321
322template<class Buffer, class TypeBuffer>
323inline osc_stream<Buffer, TypeBuffer> &
324operator <<(osc_stream<Buffer, TypeBuffer> &s, float val)
325{
326 union { float v; uint32_t i; } val2;
327 val2.v = val;
328 val2.i = htonl(val2.i);
329 s.write(&val2.i, 4);
330 s.write_type(osc_f32);
331 return s;
332}
333
334template<class Buffer, class TypeBuffer>
337{
338 union { float v; uint32_t i; } val2;
339 s.read(&val2.i, 4);
340 val2.i = htonl(val2.i);
341 val = val2.v;
342 return s;
343}
344
345template<class Buffer, class TypeBuffer>
346inline osc_stream<Buffer, TypeBuffer> &
347operator <<(osc_stream<Buffer, TypeBuffer> &s, const std::string &str)
348{
349 s.write(&str[0], str.length());
350 s.pad();
351 s.write_type(osc_string);
352 return s;
353}
354
355template<class Buffer, class TypeBuffer>
358{
359 // inefficient...
360 char five[5];
361 five[4] = '\0';
362 str.resize(0);
363 while(1)
364 {
365 s.read(five, 4);
366 if (five[0] == '\0')
367 break;
368 str += five;
369 if (!five[1] || !five[2] || !five[3])
370 break;
371 }
372 return s;
373}
374
375template<class Buffer, class TypeBuffer, class DestBuffer>
376inline osc_stream<Buffer, TypeBuffer> &
378{
379 uint32_t nlen = 0;
380 s.read(&nlen, 4);
381 uint32_t len = htonl(nlen);
382 // write length in network order
383 for (uint32_t i = 0; i < len; i += 1024)
384 {
385 uint8_t tmp[1024];
386 uint32_t part = std::min((uint32_t)1024, len - i);
387 s.read(tmp, part);
388 buf.write(tmp, part);
389 }
390 // pad
391 s.read(&nlen, 4 - (len & 3));
392 return s;
393}
394
395template<class Buffer, class TypeBuffer, class SrcBuffer>
396inline osc_stream<Buffer, TypeBuffer> &
398{
399 uint32_t len = buf.read_left();
400 uint32_t nlen = ntohl(len);
401 s.write(&nlen, 4);
402 // write length in network order
403 for (uint32_t i = 0; i < len; i += 1024)
404 {
405 uint8_t tmp[1024];
406 uint32_t part = std::min((uint32_t)1024, len - i);
407 buf.read(tmp, part);
408 s.write(tmp, part);
409 }
410 s.pad();
411 s.write_type(osc_blob);
412 return s;
413}
414
415template<class Buffer, class TypeBuffer>
416inline osc_stream<Buffer, TypeBuffer> &
421
422template<class Buffer, class TypeBuffer>
423inline osc_stream<Buffer, TypeBuffer> &
425{
426 return read_buffer_from_osc_stream(s, str);
427}
428
429template<class Buffer, class TypeBuffer>
430inline osc_stream<Buffer, TypeBuffer> &
431operator <<(osc_stream<Buffer, TypeBuffer> &s, raw_buffer &str)
432{
433 return write_buffer_to_osc_stream(s, str);
434}
435
436template<class Buffer, class TypeBuffer>
439{
440 return write_buffer_to_osc_stream(s, str);
441}
442
443// XXXKF: I don't support reading binary blobs yet
444
445struct osc_net_bad_address: public std::exception
446{
447 std::string addr, error_msg;
448 osc_net_bad_address(const char *_addr)
449 {
450 addr = _addr;
451 error_msg = "Incorrect OSC URI: " + addr;
452 }
453 virtual const char *what() const throw() { return error_msg.c_str(); }
454 virtual ~osc_net_bad_address() throw () {}
455};
456
457struct osc_net_exception: public std::exception
458{
460 std::string command, error_msg;
461 osc_net_exception(const char *cmd, int _errno = errno)
462 {
463 command = cmd;
464 net_errno = _errno;
465 error_msg = "OSC error in "+command+": "+strerror(_errno);
466 }
467 virtual const char *what() const throw() { return error_msg.c_str(); }
468 virtual ~osc_net_exception() throw () {}
469};
470#ifdef _MSC_VER
471//TODO: add replacement for osc_net_dns_exception
472#else
473struct osc_net_dns_exception: public std::exception
474{
476 std::string command, error_msg;
477 osc_net_dns_exception(const char *cmd, int _errno = h_errno)
478 {
479 command = cmd;
480 net_errno = _errno;
481 error_msg = "OSC error in "+command+": "+hstrerror(_errno);
482 }
483 virtual const char *what() const throw() { return error_msg.c_str(); }
484 virtual ~osc_net_dns_exception() throw () {}
485};
486#endif
487template<class OscStream>
489{
490 virtual void receive_osc_message(std::string address, std::string type_tag, OscStream &buffer)=0;
491 virtual ~osc_message_sink() {}
492};
493
494template<class OscStream, class DumpStream>
495struct osc_message_dump: public osc_message_sink<OscStream>
496{
497 DumpStream &stream;
498 osc_message_dump(DumpStream &_stream) : stream(_stream) {}
499
500 virtual void receive_osc_message(std::string address, std::string type_tag, OscStream &buffer)
501 {
502 int pos = buffer.buffer.tell();
503 stream << "address: " << address << ", type tag: " << type_tag << std::endl;
504 for (unsigned int i = 0; i < type_tag.size(); i++)
505 {
506 stream << "Argument " << i << " is ";
507 switch(type_tag[i])
508 {
509 case 'i':
510 {
512 buffer >> val;
513 stream << val;
514 break;
515 }
516 case 'f':
517 {
518 float val;
519 buffer >> val;
520 stream << val;
521 break;
522 }
523 case 's':
524 {
525 std::string val;
526 buffer >> val;
527 stream << val;
528 break;
529 }
530 case 'b':
531 {
533 buffer >> val;
534 stream << "blob (" << val.data.length() << " bytes)";
535 break;
536 }
537 default:
538 {
539 stream << "unknown - cannot parse more arguments" << std::endl;
540 i = type_tag.size();
541 break;
542 }
543 }
544 stream << std::endl;
545 }
546 stream << std::flush;
547 buffer.buffer.seek(pos);
548 }
549};
550
551};
552
553#endif
#define NULL
Definition CarlaBridgeFormat.cpp:30
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
unsigned v[N_MAX]
Definition inflate.c:1584
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
int val
Definition jpeglib.h:956
int int32_t
Definition mid.cpp:97
unsigned int uint32_t
Definition mid.cpp:100
unsigned char uint8_t
Definition mid.cpp:98
Definition giface.h:31
osc_stream< Buffer, TypeBuffer > & write_buffer_to_osc_stream(osc_stream< Buffer, TypeBuffer > &s, SrcBuffer &buf)
Definition osctl.h:397
osc_stream< Buffer, TypeBuffer > & read_buffer_from_osc_stream(osc_stream< Buffer, TypeBuffer > &s, DestBuffer &buf)
Definition osctl.h:377
osc_stream< Buffer, TypeBuffer > & operator<<(osc_stream< Buffer, TypeBuffer > &s, uint32_t val)
Definition osctl.h:296
osc_stream< string_buffer, string_buffer > osc_typed_strstream
Definition osctl.h:271
const char * osc_type_name(osc_type type)
Definition osctl.cpp:3
osc_stream< Buffer, TypeBuffer > & operator>>(osc_stream< Buffer, TypeBuffer > &s, uint32_t &val)
Definition osctl.h:306
osc_stream< string_buffer > osc_strstream
Definition osctl.h:270
osc_type
Definition osctl.h:42
@ osc_inf
Definition osctl.h:59
@ osc_i32
Definition osctl.h:43
@ osc_ts
Definition osctl.h:50
@ osc_nil
Definition osctl.h:58
@ osc_end_array
Definition osctl.h:61
@ osc_i64
Definition osctl.h:49
@ osc_true
Definition osctl.h:56
@ osc_false
Definition osctl.h:57
@ osc_f32
Definition osctl.h:44
@ osc_string_alt
Definition osctl.h:52
@ osc_f64
Definition osctl.h:51
@ osc_string
Definition osctl.h:45
@ osc_rgba
Definition osctl.h:54
@ osc_char
Definition osctl.h:53
@ osc_blob
Definition osctl.h:46
@ osc_start_array
Definition osctl.h:60
@ osc_midi
Definition osctl.h:55
#define false
Definition ordinals.h:83
Definition osctl.h:82
static bool read(uint8_t *dest, uint32_t bytes)
Definition osctl.h:83
static void clear()
Definition osctl.h:91
static bool write(uint8_t *dest, uint32_t bytes)
Definition osctl.h:87
Definition osctl.h:67
virtual const char * what() const
Definition osctl.h:68
osc_inline_strstream()
Definition osctl.h:275
osc_inline_typed_strstream()
Definition osctl.h:288
DumpStream & stream
Definition osctl.h:497
osc_message_dump(DumpStream &_stream)
Definition osctl.h:498
virtual void receive_osc_message(std::string address, std::string type_tag, OscStream &buffer)
Definition osctl.h:500
Definition osctl.h:489
virtual ~osc_message_sink()
Definition osctl.h:491
virtual void receive_osc_message(std::string address, std::string type_tag, OscStream &buffer)=0
std::string error_msg
Definition osctl.h:447
virtual ~osc_net_bad_address()
Definition osctl.h:454
std::string addr
Definition osctl.h:447
osc_net_bad_address(const char *_addr)
Definition osctl.h:448
virtual const char * what() const
Definition osctl.h:453
virtual ~osc_net_dns_exception()
Definition osctl.h:484
int net_errno
Definition osctl.h:475
osc_net_dns_exception(const char *cmd, int _errno=h_errno)
Definition osctl.h:477
std::string command
Definition osctl.h:476
std::string error_msg
Definition osctl.h:476
virtual const char * what() const
Definition osctl.h:483
virtual const char * what() const
Definition osctl.h:467
osc_net_exception(const char *cmd, int _errno=errno)
Definition osctl.h:461
virtual ~osc_net_exception()
Definition osctl.h:468
std::string command
Definition osctl.h:460
std::string error_msg
Definition osctl.h:460
int net_errno
Definition osctl.h:459
Definition osctl.h:72
virtual const char * what() const
Definition osctl.h:73
Definition osctl.h:282
string_buffer buf_types
Definition osctl.h:283
string_buffer buf_data
Definition osctl.h:283
Definition osctl.h:222
void read(void *dest, uint32_t bytes)
Definition osctl.h:234
void write(const void *src, uint32_t bytes)
Definition osctl.h:247
void pad()
Definition osctl.h:229
void clear()
Definition osctl.h:257
null_buffer * type_buffer
Definition osctl.h:224
osc_stream(Buffer &_buffer)
Definition osctl.h:227
void write_type(char ch)
Definition osctl.h:263
bool error
Definition osctl.h:225
osc_stream(Buffer &_buffer, TypeBuffer &_type_buffer)
Definition osctl.h:228
string_buffer & buffer
Definition osctl.h:223
Definition osctl.h:77
virtual const char * what() const
Definition osctl.h:78
Definition osctl.h:97
void set(uint8_t *_ptr, uint32_t _count, uint32_t _size)
Definition osctl.h:110
void clear()
Definition osctl.h:145
int write_left()
Definition osctl.h:137
int read_left()
Definition osctl.h:133
int tell()
Definition osctl.h:150
raw_buffer(uint8_t *_ptr, uint32_t _count, uint32_t _size)
Definition osctl.h:106
int write_misalignment()
Definition osctl.h:141
bool write(const uint8_t *src, uint32_t bytes)
Definition osctl.h:125
uint32_t count
Definition osctl.h:99
uint32_t pos
Definition osctl.h:99
raw_buffer()
Definition osctl.h:101
uint32_t size
Definition osctl.h:99
void seek(int _pos)
Definition osctl.h:154
bool read(uint8_t *dest, uint32_t bytes)
Definition osctl.h:117
uint8_t * ptr
Definition osctl.h:98
Definition osctl.h:161
std::string data
Definition osctl.h:162
bool read(uint8_t *dest, uint32_t bytes)
Definition osctl.h:176
int write_left()
Definition osctl.h:197
bool write(const uint8_t *src, uint32_t bytes)
Definition osctl.h:184
string_buffer()
Definition osctl.h:165
void clear()
Definition osctl.h:205
uint32_t pos
Definition osctl.h:163
void seek(int _pos)
Definition osctl.h:214
int read_left()
Definition osctl.h:193
string_buffer(std::string _data, int _size=1048576)
Definition osctl.h:170
uint32_t size
Definition osctl.h:163
int tell()
Definition osctl.h:210
int write_misalignment()
Definition osctl.h:201
memcpy(hh, h, RAND_HEAD_LEN)