-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscplay.c
173 lines (142 loc) · 3.51 KB
/
oscplay.c
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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <lo/lo.h>
#include <lo/lo_lowlevel.h>
struct timespec clk_step = {
.tv_sec = 0,
.tv_nsec = 1e4
};
static void
_error (int num, const char *msg, const char *where)
{
fprintf (stderr, "lo server error #%i '%s' at %s\n", num, msg, where);
}
int
main (int argc, char **argv)
{
lo_address addr = NULL;
FILE *file = NULL;
double delay = 0.0;
int c;
while ( (c = getopt (argc, argv, "d:i:o:")) != -1)
switch (c)
{
case 'd':
{
delay = atof (optarg);
}
case 'i':
{
if (!strcmp (optarg, "-"))
file = stdin;
else
file = fopen (optarg, "rb");
break;
}
case 'o':
{
addr = lo_address_new_from_url (optarg);
break;
}
case '?':
if ( (optopt == 'd') || (optopt == 'i') || (optopt == 'o') )
fprintf (stderr, "Option `-%c' requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
return (1);
}
if (!addr || !file)
{
fprintf (stderr, "usage: %s [-d DELAY] -i FILE|- -o PROTOCOL://HOST:PORT\n\n", argv[0]);
return (1);
}
int first = 1;
lo_timetag offset;
lo_timetag last = LO_TT_IMMEDIATE;
uint8_t buf[1024];
while (!feof (file))
{
size_t read;
read = fread (&buf[0], 1, 16, file); // read #bundle and timestamp
if (read != 16)
break;
// check whether message is a bundle
if (strcmp (&buf[0], "#bundle"))
fprintf (stderr, "message is not a bundle\n");
lo_timetag *tt_ptr = (lo_timetag *)&buf[8];
lo_timetag tt = *tt_ptr;
tt.sec = lo_otoh32 (tt.sec);
tt.frac = lo_otoh32 (tt.frac);
//printf ("%x:%x\n", tt.sec, tt.frac);
if (first)
{
lo_timetag_now (&offset);
offset.sec -= tt.sec;
if (offset.frac - tt.frac > offset.frac)
offset.sec -= 1;
offset.frac -= tt.frac;
if (delay > 0.0)
{
uint32_t dsec = floor (delay);
uint32_t dfrac = (delay - dsec) * 0xffffffff;
offset.sec += dsec;
if (offset.frac + dfrac < offset.frac)
offset.sec += 1;
offset.frac += dfrac;
}
first = 0;
}
tt.sec += offset.sec;
if (tt.frac + offset.frac < tt.frac)
tt.sec += 1;
tt.frac += offset.frac;
size_t ptr = 16;
char c;
lo_bundle bundle = lo_bundle_new (tt);
while ( (c = fgetc (file)) != '#') // while not next bundle
{
ungetc (c, file);
read = fread (&buf[ptr], 1, 4, file);
if (read != 4)
break;
int32_t *size_ptr = (int32_t *)&buf[ptr];
int32_t size = *size_ptr;
size = lo_otoh32 (size);
read = fread (&buf[ptr+4], 1, size, file);
if (read != size)
break;
char *path = lo_get_path (&buf[ptr+4], size);
lo_message msg = lo_message_deserialise (&buf[ptr+4], size, NULL);
lo_bundle_add_message (bundle, path, msg);
ptr += 4 + size;
}
ungetc (c, file);
// our own schedular
lo_timetag now;
lo_timetag_now (&now);
double diff = lo_timetag_diff (tt, now);
if (diff > 0)
{
clk_step.tv_sec = floor (diff);
clk_step.tv_nsec = (diff - clk_step.tv_sec) * 1e9;
nanosleep (&clk_step, NULL);
}
lo_send_bundle (addr, bundle);
lo_bundle_free_messages (bundle);
// we cannot send everything in one bulk, as the receiver needs time to acutally process the messages, if not, half of them will be lost, we therefore have to wait
//usleep (400);
}
if (file != stdin)
fclose (file);
return 0;
}