lv2_osc.h 17.2 KB
Newer Older
Hanspeter Portner's avatar
Hanspeter Portner committed
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
/*
 * Copyright (c) 2015 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#ifndef _LV2_OSC_H_
#define _LV2_OSC_H_

#include <math.h> // INFINITY

#include <lv2/lv2plug.in/ns/lv2core/lv2.h>
#include <lv2/lv2plug.in/ns/ext/urid/urid.h>
#include <lv2/lv2plug.in/ns/ext/atom/atom.h>
#include <lv2/lv2plug.in/ns/ext/atom/forge.h>
#include <lv2/lv2plug.in/ns/ext/midi/midi.h>

29
#define OSC_URI								"http://open-music-kontrollers.ch/lv2/osc"
Hanspeter Portner's avatar
Hanspeter Portner committed
30 31
#define OSC_PREFIX						OSC_URI "#"	

32
#define OSC__Event						OSC_PREFIX "Event"						// event
33 34
#define OSC__schedule					OSC_PREFIX "schedule"					// feature

Hanspeter Portner's avatar
Hanspeter Portner committed
35 36 37 38 39 40 41 42
#define OSC__Bundle						OSC_PREFIX "Bundle"						// object otype
#define OSC__Message					OSC_PREFIX "Message"					// object otype
#define OSC__bundleTimestamp	OSC_PREFIX "bundleTimestamp"	// property key
#define OSC__bundleItems			OSC_PREFIX "bundleItems"			// property key
#define OSC__messagePath			OSC_PREFIX "messagePath"			// property key
#define OSC__messageFormat		OSC_PREFIX "messageFormat"		// property key
#define OSC__messageArguments	OSC_PREFIX "messageArguments"	// property key

43 44
typedef void *osc_schedule_handle_t;
typedef struct _osc_schedule_t osc_schedule_t;
Hanspeter Portner's avatar
Hanspeter Portner committed
45 46
typedef struct _osc_forge_t osc_forge_t;

47 48 49 50
typedef void (*osc_bundle_push_cb_t)(uint64_t timestamp, void *data);
typedef void (*osc_bundle_pop_cb_t)(void *data);
typedef void (*osc_message_cb_t)(const char *path, const char *fmt,
	const LV2_Atom_Tuple *arguments, void *data);
51

52 53 54 55 56 57 58 59 60 61 62
typedef int64_t (*osc_schedule_osc2frames_t)(osc_schedule_handle_t handle,
	uint64_t timestamp);
typedef uint64_t (*osc_schedule_frames2osc_t)(osc_schedule_handle_t handle,
	int64_t frames);

struct _osc_schedule_t {
	osc_schedule_osc2frames_t osc2frames;
	osc_schedule_frames2osc_t frames2osc;
	osc_schedule_handle_t handle;
};

Hanspeter Portner's avatar
Hanspeter Portner committed
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
struct _osc_forge_t {
	LV2_URID OSC_Event;

	LV2_URID OSC_Bundle;
	LV2_URID OSC_Message;

	LV2_URID OSC_bundleTimestamp;
	LV2_URID OSC_bundleItems;

	LV2_URID OSC_messagePath;
	LV2_URID OSC_messageFormat;
	LV2_URID OSC_messageArguments;

	LV2_URID MIDI_MidiEvent;
	
	LV2_URID ATOM_Object;
};

static inline void
osc_forge_init(osc_forge_t *oforge, LV2_URID_Map *map)
{
	oforge->OSC_Event = map->map(map->handle, OSC__Event);

	oforge->OSC_Bundle = map->map(map->handle, OSC__Bundle);
	oforge->OSC_Message = map->map(map->handle, OSC__Message);

	oforge->OSC_bundleTimestamp = map->map(map->handle, OSC__bundleTimestamp);
	oforge->OSC_bundleItems = map->map(map->handle, OSC__bundleItems);

	oforge->OSC_messagePath = map->map(map->handle, OSC__messagePath);
	oforge->OSC_messageFormat = map->map(map->handle, OSC__messageFormat);
	oforge->OSC_messageArguments = map->map(map->handle, OSC__messageArguments);
	
	oforge->MIDI_MidiEvent = map->map(map->handle, LV2_MIDI__MidiEvent);
	
	oforge->ATOM_Object = map->map(map->handle, LV2_ATOM__Object);
}

static inline int
osc_atom_is_bundle(osc_forge_t *oforge, const LV2_Atom_Object *obj)
{
	return (obj->atom.type == oforge->ATOM_Object)
		&& (obj->body.otype == oforge->OSC_Bundle);
}

static inline void
osc_atom_bundle_unpack(osc_forge_t *oforge, const LV2_Atom_Object *obj,
	const LV2_Atom_Long **timestamp, const LV2_Atom_Tuple **items)
{
	*timestamp = NULL;
	*items = NULL;

	LV2_Atom_Object_Query q [] = {
		{ oforge->OSC_bundleTimestamp, (const LV2_Atom **)timestamp },
		{ oforge->OSC_bundleItems, (const LV2_Atom **)items },
		LV2_ATOM_OBJECT_QUERY_END
	};

	lv2_atom_object_query(obj, q);
}

static inline int
osc_atom_is_message(osc_forge_t *oforge, const LV2_Atom_Object *obj)
{
	return (obj->atom.type == oforge->ATOM_Object)
		&& (obj->body.otype == oforge->OSC_Message);
}

static inline void
osc_atom_message_unpack(osc_forge_t *oforge, const LV2_Atom_Object *obj,
	const LV2_Atom_String **path, const LV2_Atom_String **format,
	const LV2_Atom_Tuple **arguments)
{
	*path = NULL;
	*format = NULL;
	*arguments = NULL;

	LV2_Atom_Object_Query q [] = {
		{ oforge->OSC_messagePath, (const LV2_Atom **)path },
		{ oforge->OSC_messageFormat, (const LV2_Atom **)format },
		{ oforge->OSC_messageArguments, (const LV2_Atom **)arguments },
		LV2_ATOM_OBJECT_QUERY_END
	};

	lv2_atom_object_query(obj, q);
}

static inline void osc_atom_event_unroll(osc_forge_t *oforge,
151 152
	const LV2_Atom_Object *obj, osc_bundle_push_cb_t bundle_push_cb,
	osc_bundle_pop_cb_t bundle_pop_cb, osc_message_cb_t message_cb, void *data);
Hanspeter Portner's avatar
Hanspeter Portner committed
153 154

static inline void
155 156
osc_atom_message_unroll(osc_forge_t *oforge, const LV2_Atom_Object *obj,
	osc_message_cb_t message_cb, void *data)
Hanspeter Portner's avatar
Hanspeter Portner committed
157 158 159 160 161 162 163 164 165 166
{
	const LV2_Atom_String* path;
	const LV2_Atom_String* fmt;
	const LV2_Atom_Tuple* args;

	osc_atom_message_unpack(oforge, obj, &path, &fmt, &args);

	const char *path_str = path ? LV2_ATOM_BODY_CONST(path) : NULL;
	const char *fmt_str = fmt ? LV2_ATOM_BODY_CONST(fmt) : NULL;

167 168
	if(message_cb)
		message_cb(path_str, fmt_str, args, data);
Hanspeter Portner's avatar
Hanspeter Portner committed
169 170 171 172
}

static inline void
osc_atom_bundle_unroll(osc_forge_t *oforge, const LV2_Atom_Object *obj,
173 174
	osc_bundle_push_cb_t bundle_push_cb, osc_bundle_pop_cb_t bundle_pop_cb,
	osc_message_cb_t message_cb, void *data)
Hanspeter Portner's avatar
Hanspeter Portner committed
175 176 177 178 179 180
{
	const LV2_Atom_Long* timestamp;
	const LV2_Atom_Tuple* items;

	osc_atom_bundle_unpack(oforge, obj, &timestamp, &items);

181
	uint64_t timestamp_body = timestamp ? (uint64_t)timestamp->body : 1ULL;
Hanspeter Portner's avatar
Hanspeter Portner committed
182

183 184 185
	if(bundle_push_cb)
		bundle_push_cb(timestamp_body, data);

Hanspeter Portner's avatar
Hanspeter Portner committed
186
	// iterate over tuple body
187
	if(items)
Hanspeter Portner's avatar
Hanspeter Portner committed
188
	{
189 190 191 192 193 194 195
		for(const LV2_Atom *itr = lv2_atom_tuple_begin(items);
			!lv2_atom_tuple_is_end(LV2_ATOM_BODY(items), items->atom.size, itr);
			itr = lv2_atom_tuple_next(itr))
		{
			osc_atom_event_unroll(oforge, (const LV2_Atom_Object *)itr,
				bundle_push_cb, bundle_pop_cb, message_cb, data);
		}
Hanspeter Portner's avatar
Hanspeter Portner committed
196
	}
197 198 199

	if(bundle_pop_cb)
		bundle_pop_cb(data);
Hanspeter Portner's avatar
Hanspeter Portner committed
200 201 202 203
}

static inline void
osc_atom_event_unroll(osc_forge_t *oforge, const LV2_Atom_Object *obj,
204 205
	osc_bundle_push_cb_t bundle_push_cb, osc_bundle_pop_cb_t bundle_pop_cb,
	osc_message_cb_t message_cb, void *data)
Hanspeter Portner's avatar
Hanspeter Portner committed
206 207
{
	if(osc_atom_is_bundle(oforge, obj))
208 209 210 211
	{
		osc_atom_bundle_unroll(oforge, obj, bundle_push_cb, bundle_pop_cb,
			message_cb, data);
	}
Hanspeter Portner's avatar
Hanspeter Portner committed
212
	else if(osc_atom_is_message(oforge, obj))
213 214 215
	{
		osc_atom_message_unroll(oforge, obj, message_cb, data);
	}
Hanspeter Portner's avatar
Hanspeter Portner committed
216 217
}

218
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
219 220 221
osc_forge_bundle_push(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	LV2_Atom_Forge_Frame frame [2], uint64_t timestamp)
{
222
	if(!lv2_atom_forge_object(forge, &frame[0], 0, oforge->OSC_Bundle))
223 224 225 226 227 228
		return 0;

	if(!lv2_atom_forge_key(forge, oforge->OSC_bundleTimestamp))
		return 0;
	if(!lv2_atom_forge_long(forge, timestamp))
		return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
229

230 231
	if(!lv2_atom_forge_key(forge, oforge->OSC_bundleItems))
		return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
232

233
	return lv2_atom_forge_tuple(forge, &frame[1]);
Hanspeter Portner's avatar
Hanspeter Portner committed
234 235 236 237 238 239 240 241 242 243
}

static inline void
osc_forge_bundle_pop(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	LV2_Atom_Forge_Frame frame [2])
{
	lv2_atom_forge_pop(forge, &frame[1]);
	lv2_atom_forge_pop(forge, &frame[0]);
}

244
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
245 246 247
osc_forge_message_push(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	LV2_Atom_Forge_Frame frame [2], const char *path, const char *fmt)
{
248
	if(!lv2_atom_forge_object(forge, &frame[0], 0, oforge->OSC_Message))
249 250 251 252 253 254
		return 0;

	if(!lv2_atom_forge_key(forge, oforge->OSC_messagePath))
		return 0;
	if(!lv2_atom_forge_string(forge, path, strlen(path)))
		return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
255

256 257 258 259
	if(!lv2_atom_forge_key(forge, oforge->OSC_messageFormat))
		return 0;
	if(!lv2_atom_forge_string(forge, fmt, strlen(fmt)))
		return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
260

261 262
	if(!lv2_atom_forge_key(forge, oforge->OSC_messageArguments))
		return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
263

264
	return lv2_atom_forge_tuple(forge, &frame[1]);
Hanspeter Portner's avatar
Hanspeter Portner committed
265 266 267 268 269 270 271 272 273
}

static inline void
osc_forge_message_pop(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	LV2_Atom_Forge_Frame frame [2])
{
	lv2_atom_forge_pop(forge, &frame[1]);
	lv2_atom_forge_pop(forge, &frame[0]);
}
274

275
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
276 277
osc_forge_int32(osc_forge_t *oforge, LV2_Atom_Forge *forge, int32_t i)
{
278
	return lv2_atom_forge_int(forge, i);
Hanspeter Portner's avatar
Hanspeter Portner committed
279 280
}

281
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
282 283
osc_forge_float(osc_forge_t *oforge, LV2_Atom_Forge *forge, float f)
{
284
	return lv2_atom_forge_float(forge, f);
Hanspeter Portner's avatar
Hanspeter Portner committed
285 286
}

287
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
288 289
osc_forge_string(osc_forge_t *oforge, LV2_Atom_Forge *forge, const char *s)
{
290
	return lv2_atom_forge_string(forge, s, strlen(s));
Hanspeter Portner's avatar
Hanspeter Portner committed
291 292
}

293
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
294 295
osc_forge_symbol(osc_forge_t *oforge, LV2_Atom_Forge *forge, const char *s)
{
296
	return lv2_atom_forge_string(forge, s, strlen(s));
Hanspeter Portner's avatar
Hanspeter Portner committed
297 298
}

299
static inline LV2_Atom_Forge_Ref
300
osc_forge_blob(osc_forge_t *oforge, LV2_Atom_Forge *forge, uint32_t size,
Hanspeter Portner's avatar
Hanspeter Portner committed
301 302
	const uint8_t *b)
{
303 304 305 306 307
	LV2_Atom_Forge_Ref ref;
	if(!(ref = lv2_atom_forge_atom(forge, size, forge->Chunk)))
		return 0;
	if(!(ref = lv2_atom_forge_raw(forge, b, size)))
		return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
308
	lv2_atom_forge_pad(forge, size);
309 310

	return ref;
Hanspeter Portner's avatar
Hanspeter Portner committed
311 312
}

313
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
314 315
osc_forge_int64(osc_forge_t *oforge, LV2_Atom_Forge *forge, int64_t h)
{
316
	return lv2_atom_forge_long(forge, h);
Hanspeter Portner's avatar
Hanspeter Portner committed
317 318
}

319
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
320 321
osc_forge_double(osc_forge_t *oforge, LV2_Atom_Forge *forge, double d)
{
322
	return lv2_atom_forge_double(forge, d);
Hanspeter Portner's avatar
Hanspeter Portner committed
323 324
}

325
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
326 327
osc_forge_timestamp(osc_forge_t *oforge, LV2_Atom_Forge *forge, uint64_t t)
{
328
	return lv2_atom_forge_long(forge, t);
Hanspeter Portner's avatar
Hanspeter Portner committed
329 330
}

331
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
332 333
osc_forge_char(osc_forge_t *oforge, LV2_Atom_Forge *forge, char c)
{
334
	return lv2_atom_forge_int(forge, c);
Hanspeter Portner's avatar
Hanspeter Portner committed
335 336
}

337
static inline LV2_Atom_Forge_Ref
338 339
osc_forge_midi(osc_forge_t *oforge, LV2_Atom_Forge *forge, uint32_t size,
	const uint8_t *m)
Hanspeter Portner's avatar
Hanspeter Portner committed
340
{
341
	LV2_Atom_Forge_Ref ref;
342
	if(!(ref = lv2_atom_forge_atom(forge, size, oforge->MIDI_MidiEvent)))
343
		return 0;
344
	if(!(ref = lv2_atom_forge_raw(forge, m, size)))
345
		return 0;
346
	lv2_atom_forge_pad(forge, size);
347 348

	return ref;
Hanspeter Portner's avatar
Hanspeter Portner committed
349 350
}

351
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
352 353 354 355
osc_forge_message_varlist(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const char *path, const char *fmt, va_list args)
{
	LV2_Atom_Forge_Frame frame [2];
356
	LV2_Atom_Forge_Ref ref;
Hanspeter Portner's avatar
Hanspeter Portner committed
357

358 359
	if(!(ref = osc_forge_message_push(oforge, forge, frame, path, fmt)))
		return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
360 361 362 363 364 365

	for(const char *type = fmt; *type; type++)
	{
		switch(*type)
		{
			case 'i':
366
			{
367 368
				if(!(ref =osc_forge_int32(oforge, forge, va_arg(args, int32_t))))
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
369
				break;
370
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
371
			case 'f':
372
			{
373 374
				if(!(ref = osc_forge_float(oforge, forge, (float)va_arg(args, double))))
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
375
				break;
376
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
377
			case 's':
378
			{
379 380
				if(!(ref = osc_forge_string(oforge, forge, va_arg(args, const char *))))
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
381
				break;
382
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
383
			case 'S':
384
			{
385 386
				if(!(ref = osc_forge_symbol(oforge, forge, va_arg(args, const char *))))
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
387
				break;
388
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
389 390
			case 'b':
			{
391
				uint32_t size = va_arg(args, uint32_t);
Hanspeter Portner's avatar
Hanspeter Portner committed
392
				const uint8_t *b = va_arg(args, const uint8_t *);
393 394
				if(!(ref = osc_forge_blob(oforge, forge, size, b)))
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
395 396 397 398
				break;
			}
			
			case 'h':
399
			{
400 401
				if(!(ref = osc_forge_int64(oforge, forge, va_arg(args, int64_t))))
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
402
				break;
403
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
404
			case 'd':
405
			{
406 407
				if(!(ref = osc_forge_double(oforge, forge, va_arg(args, double))))
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
408
				break;
409
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
410
			case 't':
411
			{
412 413
				if(!(ref = osc_forge_timestamp(oforge, forge, va_arg(args, uint64_t))))
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
414
				break;
415
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
416 417
			
			case 'c':
418
			{
419 420
				if(!(ref = osc_forge_char(oforge, forge, (char)va_arg(args, unsigned int))))
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
421
				break;
422
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
423
			case 'm':
424 425 426 427
			{
				int32_t size = va_arg(args, int32_t);
				const uint8_t *m = va_arg(args, const uint8_t *);
				if(!(ref = osc_forge_midi(oforge, forge, size, m)))
428
					return 0;
Hanspeter Portner's avatar
Hanspeter Portner committed
429
				break;
430
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
431 432 433 434 435
			
			case 'T':
			case 'F':
			case 'N':
			case 'I':
436
			{
Hanspeter Portner's avatar
Hanspeter Portner committed
437
				break;
438
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
439

440
			default: // unknown argument type
441
			{
442
				return 0;
443
			}
Hanspeter Portner's avatar
Hanspeter Portner committed
444 445 446 447
		}
	}

	osc_forge_message_pop(oforge, forge, frame);
448 449

	return ref;
Hanspeter Portner's avatar
Hanspeter Portner committed
450 451
}

452
static inline LV2_Atom_Forge_Ref
Hanspeter Portner's avatar
Hanspeter Portner committed
453 454 455 456 457 458
osc_forge_message_vararg(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const char *path, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);

459 460
	LV2_Atom_Forge_Ref ref;
	ref = osc_forge_message_varlist(oforge, forge, path, fmt, args);
Hanspeter Portner's avatar
Hanspeter Portner committed
461 462

	va_end(args);
463 464

	return ref;
Hanspeter Portner's avatar
Hanspeter Portner committed
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
static inline const LV2_Atom *
osc_deforge_int32(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, int32_t *i)
{
	if(!atom || (atom->type != forge->Int) )
		return NULL;

	*i = ((const LV2_Atom_Int *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
osc_deforge_float(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, float *f)
{
	if(!atom || (atom->type != forge->Float) )
		return NULL;

	*f = ((const LV2_Atom_Float *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
osc_deforge_string(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, const char **s)
{
	if(!atom || (atom->type != forge->String) )
		return NULL;

	*s = LV2_ATOM_BODY_CONST(atom);

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
osc_deforge_symbol(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, const char **s)
{
	if(!atom || (atom->type != forge->String) )
		return NULL;

	*s = LV2_ATOM_BODY_CONST(atom);

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
osc_deforge_blob(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, uint32_t *size, const uint8_t **b)
{
	if(!atom || (atom->type != forge->Chunk) )
		return NULL;

	*size = atom->size;
	*b = LV2_ATOM_BODY_CONST(atom);

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
osc_deforge_int64(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, int64_t *h)
{
	if(!atom || (atom->type != forge->Long) )
		return NULL;

	*h = ((const LV2_Atom_Long *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
osc_deforge_double(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, double *d)
{
	if(!atom || (atom->type != forge->Double) )
		return NULL;

	*d = ((const LV2_Atom_Double *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
osc_deforge_timestamp(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, uint64_t *t)
{
	if(!atom || (atom->type != forge->Long) )
		return NULL;

	*t = ((const LV2_Atom_Long *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
osc_deforge_char(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, char *c)
{
	if(!atom || (atom->type != forge->Int) )
		return NULL;

	*c = ((const LV2_Atom_Int *)atom)->body;

	return lv2_atom_tuple_next(atom);
}

static inline const LV2_Atom *
osc_deforge_midi(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, uint32_t *size, const uint8_t **m)
{
	if(!atom || (atom->type != oforge->MIDI_MidiEvent) )
		return NULL;

	*size = atom->size;
	*m = LV2_ATOM_BODY_CONST(atom);

	return lv2_atom_tuple_next(atom);
}

589 590 591 592
static inline const LV2_Atom *
osc_deforge_message_varlist(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, const char *fmt, va_list args)
{
593
	for(const char *type = fmt; *type; type++)
594 595 596 597 598 599
	{
		switch(*type)
		{
			case 'i':
			{
				int32_t *i = va_arg(args, int32_t *);
600 601
				if(!(atom = osc_deforge_int32(oforge, forge, atom, i)))
					return NULL;
602 603 604 605 606
				break;
			}
			case 'f':
			{
				float *f = va_arg(args, float *);
607 608
				if(!(atom = osc_deforge_float(oforge, forge, atom, f)))
					return NULL;
609 610 611 612 613
				break;
			}
			case 's':
			{
				const char **s = va_arg(args, const char **);
614 615
				if(!(atom = osc_deforge_string(oforge, forge, atom, s)))
					return NULL;
616 617 618 619 620
				break;
			}
			case 'S':
			{
				const char **s = va_arg(args, const char **);
621 622
				if(!(atom = osc_deforge_symbol(oforge, forge, atom, s)))
					return NULL;
623 624 625 626
				break;
			}
			case 'b':
			{
627
				uint32_t *size = va_arg(args, uint32_t *);
628
				const uint8_t **b = va_arg(args, const uint8_t **);
629 630
				if(!(atom = osc_deforge_blob(oforge, forge, atom, size, b)))
					return NULL;
631 632 633 634 635 636
				break;
			}
			
			case 'h':
			{
				int64_t *h = va_arg(args, int64_t *);
637 638
				if(!(atom = osc_deforge_int64(oforge, forge, atom, h)))
					return NULL;
639 640 641 642 643
				break;
			}
			case 'd':
			{
				double *d = va_arg(args, double *);
644 645
				if(!(atom = osc_deforge_double(oforge, forge, atom, d)))
					return NULL;
646 647 648 649
				break;
			}
			case 't':
			{
650 651 652
				uint64_t *t = va_arg(args, uint64_t *);
				if(!(atom = osc_deforge_timestamp(oforge, forge, atom, t)))
					return NULL;
653 654 655 656 657 658
				break;
			}
			
			case 'c':
			{
				char *c = va_arg(args, char *);
659 660
				if(!(atom = osc_deforge_char(oforge, forge, atom, c)))
					return NULL;
661 662 663 664
				break;
			}
			case 'm':
			{
665
				uint32_t *size = va_arg(args, uint32_t *);
666
				const uint8_t **m = va_arg(args, const uint8_t **);
667 668
				if(!(atom = osc_deforge_midi(oforge, forge, atom, size, m)))
					return NULL;
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
				break;
			}
			
			case 'T':
			case 'F':
			case 'N':
			case 'I':
			{
				break;
			}

			default: // unknown argument type
			{
				return NULL;
			}
		}
	}

	return atom;
}

static inline const LV2_Atom *
osc_deforge_message_vararg(osc_forge_t *oforge, LV2_Atom_Forge *forge,
	const LV2_Atom *atom, const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);

	atom = osc_deforge_message_varlist(oforge, forge, atom, fmt, args);

	va_end(args);

	return atom;
}

Hanspeter Portner's avatar
Hanspeter Portner committed
704
#endif // _LV2_OSC_H_