[go: up one dir, main page]

Menu

[901c6f]: / src / tools / oscsendfile.c  Maximize  Restore  History

Download this file

451 lines (412 with data), 12.8 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
/*
* oscsend - Send OpenSound Control message.
*
* Copyright (C) 2016 Joseph Malloch <joseph.malloch@gmail.com>
* Copyright (C) 2008 Kentaro Fukuchi <kentaro@fukuchi.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program 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
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <config.h>
#include <limits.h>
#include <unistd.h>
#include <lo/lo.h>
static FILE* input_file = 0;
static double multiplier = 1.0/((double)(1LL<<32));
void usage(void)
{
printf("oscsendfile version %s\n"
"Copyright (C) 2016 Joseph Malloch\n"
" adapted from oscsend.c (C) 2008 Kentaro Fukuchi\n\n"
"Usage: oscsend hostname port file <speed>\n"
"or oscsend url file <speed>\n"
"Send OpenSound Control messages from a file via UDP.\n\n"
"Description\n"
"hostname: specifies the remote host's name.\n"
"port : specifies the port number to connect to the remote host.\n"
"url : specifies the destination parameters using a liblo URL.\n"
" e.g. UDP \"osc.udp://localhost:9000\"\n"
" Multicast \"osc.udp://224.0.1.9:9000\"\n"
" TCP \"osc.tcp://localhost:9000\"\n"
"speed : specifies a speed multiplier.\n",
VERSION);
printf("Example\n"
"$ oscsendfile localhost 7777 myfile.txt 2.5\n");
}
lo_message create_message(char **argv)
{
/* Note:
* argv[0] <- types
* argv[1..] <- values
*/
int i, argi;
lo_message message;
const char *types;
char *arg;
int values;
message = lo_message_new();
if (argv[0] == NULL) {
/* empty message is allowed. */
values = 0;
} else {
types = argv[0];
values = strlen(types);
}
argi = 1;
for (i = 0; i < values; i++) {
switch(types[i]) {
case LO_INT32:
case LO_FLOAT:
case LO_STRING:
case LO_BLOB:
case LO_INT64:
case LO_TIMETAG:
case LO_DOUBLE:
case LO_SYMBOL:
case LO_CHAR:
case LO_MIDI:
arg = argv[argi];
if (arg == NULL) {
fprintf(stderr, "Value #%d is not given.\n", i + 1);
goto EXIT;
}
break;
default:
break;
}
switch (types[i]) {
case LO_INT32:
{
char *endp;
int64_t v;
v = strtol(arg, &endp, 10);
if (*endp != '\0') {
fprintf(stderr, "An invalid value was given: '%s'\n",
arg);
goto EXIT;
}
if ((v == LONG_MAX || v == LONG_MIN) && errno == ERANGE) {
fprintf(stderr, "Value out of range: '%s'\n", arg);
goto EXIT;
}
if (v > INT_MAX || v < INT_MIN) {
fprintf(stderr, "Value out of range: '%s'\n", arg);
goto EXIT;
}
lo_message_add_int32(message, (int32_t) v);
argi++;
break;
}
case LO_INT64:
{
char *endp;
int64_t v;
v = strtoll(arg, &endp, 10);
if (*endp != '\0') {
fprintf(stderr, "An invalid value was given: '%s'\n",
arg);
goto EXIT;
}
if ((v == LONG_MAX || v == LONG_MIN) && errno == ERANGE) {
fprintf(stderr, "Value out of range: '%s'\n", arg);
goto EXIT;
}
lo_message_add_int64(message, v);
argi++;
break;
}
case LO_FLOAT:
{
char *endp;
float v;
#ifdef __USE_ISOC99
v = strtof(arg, &endp);
#else
v = (float) strtod(arg, &endp);
#endif /* __USE_ISOC99 */
if (*endp != '\0') {
fprintf(stderr, "An invalid value was given: '%s'\n",
arg);
goto EXIT;
}
lo_message_add_float(message, v);
argi++;
break;
}
case LO_DOUBLE:
{
char *endp;
double v;
v = strtod(arg, &endp);
if (*endp != '\0') {
perror(NULL);
fprintf(stderr, "An invalid value was given: '%s'\n",
arg);
goto EXIT;
}
lo_message_add_double(message, v);
argi++;
break;
}
case LO_STRING:
case LO_SYMBOL:
if (arg[strlen(arg)-1] == '"')
arg[strlen(arg)-1] = '\0';
if (arg[0] == '"')
lo_message_add_string(message, arg+1);
else
lo_message_add_string(message, arg);
argi++;
break;
case LO_CHAR:
lo_message_add_char(message, arg[0]);
argi++;
break;
case LO_MIDI:
{
unsigned int midi;
uint8_t packet[4];
int ret;
ret = sscanf(arg, "%08x", &midi);
if (ret != 1) {
fprintf(stderr,
"An invalid hexadecimal value was given: '%s'\n",
arg);
goto EXIT;
}
packet[0] = (midi >> 24) & 0xff;
packet[1] = (midi >> 16) & 0xff;
packet[2] = (midi >> 8) & 0xff;
packet[3] = midi & 0xff;
lo_message_add_midi(message, packet);
argi++;
break;
}
case LO_TRUE:
lo_message_add_true(message);
break;
case LO_FALSE:
lo_message_add_false(message);
break;
case LO_NIL:
lo_message_add_nil(message);
break;
case LO_INFINITUM:
lo_message_add_infinitum(message);
break;
default:
fprintf(stderr, "Type '%c' is not supported or invalid.\n",
types[i]);
goto EXIT;
break;
}
}
return message;
EXIT:
lo_message_free(message);
return NULL;
}
void timetag_add(lo_timetag *tt, lo_timetag addend)
{
tt->sec += addend.sec;
tt->frac += addend.frac;
if (tt->frac < addend.frac) // overflow
tt->sec++;
}
void timetag_subtract(lo_timetag *tt, lo_timetag subtrahend)
{
tt->sec -= subtrahend.sec;
if (tt->frac < subtrahend.frac) // overflow
--tt->sec;
tt->frac -= subtrahend.frac;
}
double timetag_diff(const lo_timetag a, const lo_timetag b)
{
return ((double)a.sec - (double)b.sec +
((double)a.frac - (double)b.frac) * multiplier);
}
double timetag_double(const lo_timetag tt)
{
return (double)tt.sec + (double)tt.frac * multiplier;
}
void timetag_multiply(lo_timetag *tt, double d)
{
d *= timetag_double(*tt);
tt->sec = floor(d);
d -= tt->sec;
tt->frac = (uint32_t) (d * (double)(1LL<<32));
}
int send_file(lo_address target, double speed) {
double speedmul = 1.0 / speed;
const char *delim = " \r\n";
char *args[64];
char str[1024], *p, *s, *path;
lo_timetag tt_start = {0, 0}, tt_start_file, tt_now;
lo_timetag tt1 = {0, 0}, tt2 = {0, 0}, *tt_last = &tt1, *tt_this = &tt2;
int tt_init = 0, ret;
lo_message m;
lo_bundle b = 0;
lo_timetag_now(&tt_start);
while (fgets(str, 1024, input_file)) {
path = 0;
s = strtok_r(str, delim, &p);
m = lo_message_new();
if (!m)
return 1;
if (s) {
// check if first arg is timetag
if (s[0] != '/') {
// first argument is timetag
char *ttstr = strtok(s, ".");
if (ttstr) {
tt_this->sec = strtoul(ttstr, NULL, 16);
if (!tt_init)
tt_start_file.sec = tt_this->sec;
}
ttstr = strtok(0, ".");
if (ttstr) {
tt_this->frac = strtoul(ttstr, NULL, 16);
if (!tt_init)
tt_start_file.frac = tt_this->frac;
}
if (1) {
timetag_subtract(tt_this, tt_start_file);
timetag_multiply(tt_this, speedmul);
timetag_add(tt_this, tt_start);
}
else {
if (!tt_init)
timetag_subtract(&tt_start, tt_start_file);
timetag_add(tt_this, tt_start);
}
tt_init = 1;
s = strtok_r(0, delim, &p);
}
else {
tt_this->sec = 0;
tt_this->frac = 1;
}
}
if (s)
path = s;
else
continue;
s = strtok_r(0, delim, &p);
if (s)
args[0] = s; // types
int i = 0;
while ((s = strtok_r(0, delim, &p))) {
args[++i] = s;
}
m = create_message(args);
if (!m) {
fprintf(stderr, "Failed to create OSC message.\n");
return 1;
}
if (b && memcmp(tt_this, tt_last, sizeof(lo_timetag))==0) {
lo_bundle_add_message(b, path, m);
}
else {
// wait for timestamp?
lo_timetag_now(&tt_now);
double wait_time = timetag_diff(*tt_last, tt_now);
if (wait_time > 0.) {
usleep(wait_time * 1000000);
}
if (b) {
ret = lo_send_bundle(target, b);
}
b = lo_bundle_new(*tt_this);
lo_bundle_add_message(b, path, m);
lo_timetag *tmp = tt_last;
tt_last = tt_this;
tt_this = tmp;
}
if (ret == -1)
return ret;
}
if (b) {
// wait for timestamp?
lo_timetag_now(&tt_now);
double wait_time = timetag_diff(*tt_last, tt_now);
if (wait_time > 0.) {
usleep(wait_time * 1000000);
}
lo_send_bundle(target, b);
}
return 0;
}
int main(int argc, char **argv)
{
lo_address target;
int ret, i=1;
if (argc < 2) {
usage();
exit(1);
}
if (argv[i] == NULL) {
fprintf(stderr, "No hostname is given.\n");
exit(1);
}
if (strstr(argv[i], "://")!=0) {
target = lo_address_new_from_url(argv[i]);
if (target == NULL) {
fprintf(stderr, "Failed to open %s\n", argv[i]);
exit(1);
}
i++;
}
else if (argv[i+1] == NULL) {
fprintf(stderr, "No port number is given.\n");
exit(1);
}
else {
target = lo_address_new(argv[i], argv[i+1]);
if (target == NULL) {
fprintf(stderr, "Failed to open %s:%s\n", argv[i], argv[i+1]);
exit(1);
}
lo_address_set_ttl(target, 1);
i += 2;
}
if (argv[i] == NULL) {
fprintf(stderr, "No %s given.\n", argc == i+1 ? "filename" : "path");
exit(1);
}
// next arg should be filename
input_file = fopen(argv[i], "r");
if (!input_file) {
fprintf(stderr, "Failed to open file `%s' for reading.\n", argv[i]);
exit(1);
}
double speed = 1.0;
if (argc > i+1) {
// optional speed argument
speed = atof(argv[i+1]);
}
ret = send_file(target, speed);
if (ret == -1) {
fprintf(stderr, "An error occured: %s\n",
lo_address_errstr(target));
exit(1);
}
return 0;
}