-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.c
311 lines (256 loc) · 7.92 KB
/
server.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <lo/lo.h>
#include <sys/types.h>
#include <math.h>
#include <sys/time.h>
#include <assert.h>
#include "server.h"
#include "audio.h"
#include "config.h"
#ifdef ZEROMQ
#include <zmq.h>
#define MAXOSCSZ 1024
#endif
void error(int num, const char *m, const char *path);
int trigger_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data);
int generic_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data);
/**/
void error(int num, const char *msg, const char *path) {
printf("liblo server error %d in path %s: %s\n", num, path, msg);
}
/**/
int generic_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data) {
int i;
printf("path: <%s>\n", path);
for (i=0; i<argc; i++) {
printf("arg %d '%c' ", i, types[i]);
lo_arg_pp(types[i], argv[i]);
printf("\n");
}
printf("\n");
return 1;
}
/**/
int play_handler(const char *path, const char *types, lo_arg **argv,
int argc, void *data, void *user_data) {
/* lo_timetag ts = lo_message_get_timestamp(data); */
double when = (double) argv[0]->i + (((double) argv[1]->i) / 1000000.0);
#ifdef SUBLATENCY
when -= SUBLATENCY;
#endif
int poffset = 2;
float cps = argv[2]->f;
poffset = 3;
// printf("timing info: when, cps = %f\t%f a %i b %i\n", when, cps, argv[0]->i, argv[1]->i);
char *sample_name = (char *) argv[0+poffset];
float offset = argv[1+poffset]->f;
//when += offset;
float start = argv[2+poffset]->f;
float end = argv[3+poffset]->f;
float speed = argv[4+poffset]->f;
float pan = argv[5+poffset]->f;
float velocity = argv[6+poffset]->f;
char *vowel_s = (char *) argv[7+poffset];
float cutoff = argv[8+poffset]->f;
float resonance = argv[9+poffset]->f;
float accelerate = argv[10+poffset]->f;
float shape = argv[11+poffset]->f;
float gain = argc > (13+poffset) ? argv[13+poffset]->f : 0;
int cutgroup = argc > (14+poffset) ? argv[14+poffset]->i : 0;
float delay = argc > (15+poffset) ? argv[15+poffset]->f : 0;
float delaytime = argc > (16+poffset) ? argv[16+poffset]->f : 0;
float delayfeedback = argc > (17+poffset) ? argv[17+poffset]->f : 0;
float crush = argc > (18+poffset) ? argv[18+poffset]->f : 0;
int coarse = argc > (19+poffset) ? argv[19+poffset]->i : 0;
float hcutoff = argc > (20+poffset) ? argv[20+poffset]->f : 0;
float hresonance = argc > (21+poffset) ? argv[21+poffset]->f : 0;
float bandf = argc > (22+poffset) ? argv[22+poffset]->f : 0;
float bandq = argc > (23+poffset) ? argv[23+poffset]->f : 0;
char *unit_name = argc > (24+poffset) ? (char *) argv[24+poffset] : "r";
int sample_loop = argc > (25+poffset) ? floor(argv[25+poffset]->f) : 0;
int sample_n = argc > (26+poffset) ? floor(argv[26+poffset]->f) : 0;
float attack = argc > (27+poffset) ? argv[27+poffset]->f : 0;
float hold = argc > (28+poffset) ? argv[28+poffset]->f : 0;
float release = argc > (29+poffset) ? argv[29+poffset]->f : 0;
int orbit = argc > (30+poffset) ? argv[30+poffset]->i : 0;
//printf("orb: %d\n", orbit);
static bool extraWarned = false;
if (argc > 30+poffset && !extraWarned) {
printf("play server unexpectedly received extra parameters, maybe update Dirt?\n");
extraWarned = true;
}
if (speed == 0) {
return(0);
}
int vowelnum = -1;
switch(vowel_s[0]) {
case 'a': case 'A': vowelnum = 0; break;
case 'e': case 'E': vowelnum = 1; break;
case 'i': case 'I': vowelnum = 2; break;
case 'o': case 'O': vowelnum = 3; break;
case 'u': case 'U': vowelnum = 4; break;
}
//printf("vowel: %s num: %d\n", vowel_s, vowelnum);
int unit = -1;
switch(unit_name[0]) {
// rate
case 'r': case 'R': unit = 'r'; break;
// sec
case 's': case 'S': unit = 's'; break;
// cycle
case 'c': case 'C': unit = 'c'; break;
}
t_sound *sound = new_sound();
if (sound == NULL) {
//printf("hit max sounds (%d)\n", MAXSOUNDS);
return(0);
}
sound->active = 1;
sound->speed = speed;
sound->pan = pan;
sound->start = start;
sound->end = end;
sound->velocity = velocity;
sound->formant_vowelnum = vowelnum;
sound->cutoff = cutoff / CUTOFFRATIO;
sound->resonance = resonance;
sound->accelerate = accelerate;
sound->shape = (shape != 0);
shape = fabs(shape);
shape = (shape > 0.99f)?0.99f:shape;
sound->shape_k = (2.0f * shape) / (1.0f - shape);
sound->delay = delay;
sound->delaytime = delaytime;
sound->delayfeedback = delayfeedback;
sound->gain = powf(gain/2, 4);
sound->cutgroup = cutgroup;
sound->crush = crush;
sound->coarse = coarse;
sound->hcutoff = hcutoff;
sound->hresonance = hresonance;
sound->bandf = bandf;
sound->bandq = bandq;
sound->sample_loop = sample_loop;
sound->unit = unit;
sound->offset = offset;
sound->cps = cps;
sound->when = when;
sound->orbit = (orbit <= MAX_ORBIT) ? orbit : MAX_ORBIT;
//printf("orbit: %d\n", sound->orbit);
if (sample_n) {
sample_n = abs(sample_n);
snprintf(sound->samplename, MAXPATHSIZE, "%s:%d",
sample_name,
sample_n);
}
else {
strncpy(sound->samplename, sample_name, MAXPATHSIZE);
}
sound->attack = attack;
sound->hold = hold;
sound->release = release;
audio_play(sound);
return(0);
}
/**/
#ifdef ZEROMQ
void *zmqthread(void *data){
void *context = zmq_ctx_new ();
void *subscriber = zmq_socket (context, ZMQ_SUB);
void *buffer = (void *) malloc(MAXOSCSZ);
int rc = zmq_connect (subscriber, ZEROMQ);
lo_server s = lo_server_new("7772", error);
lo_server_add_method(s, "/play", "iisffffffsffffififfffiffffi",
play_handler,
NULL
);
lo_server_add_method(s, "/play", "iisffffffsffffififfffiffff",
play_handler,
NULL
);
lo_server_add_method(s, "/play", "iisffffffsffffififfffifff",
play_handler,
NULL
);
lo_server_add_method(s, "/play", "iisffffffsffffififff",
play_handler,
NULL
);
lo_server_add_method(s, NULL, NULL, generic_handler, NULL);
assert(rc == 0);
// Subscribe to all
rc = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE,
NULL, 0);
assert (rc == 0);
while(1) {
int size = zmq_recv(subscriber, buffer, MAXOSCSZ, 0);
if (size > 0) {
lo_server_dispatch_data(s, buffer, size);
}
else {
printf("oops.\n");
}
}
return(NULL);
}
#endif
/**/
extern int server_init(char *osc_port) {
lo_server_thread st = lo_server_thread_new(osc_port, error);
lo_server_thread_add_method(st, "/play", NULL, play_handler, NULL);
lo_server_thread_add_method(st, NULL, NULL, generic_handler, NULL);
lo_server_thread_start(st);
#ifdef ZEROMQ
pthread_t t;
pthread_create(&t, NULL, (void *(*)(void *)) zmqthread, NULL);
#endif
return(1);
}
extern void osc_send_pitch(double starttime, unsigned int chunk,
float pitch, float flux, float centroid) {
static lo_address t = NULL;
static int pid = 0;
if (t == NULL) {
t = lo_address_new(NULL, "6010");
}
if (pid == 0) {
pid = (int) getpid();
}
//printf("send [%d] %f\n", chunk, pitch);
// pid, starttime, chunk, v_pitch, v_flux, v_centroid
lo_send(t, "/chunk", "ififff",
pid,
starttime,
(int) chunk,
pitch,
flux,
centroid
);
}
extern void osc_send_play(double when, int lowchunk, float pitch, float flux, float centroid) {
static lo_address t = NULL;
static int pid = 0;
if (t == NULL) {
t = lo_address_new(NULL, "6010");
}
if (pid == 0) {
pid = (int) getpid();
}
printf("play [%d] %f\n", lowchunk, pitch);
// pid, starttime, chunk, v_pitch, v_flux, v_centroid
lo_send(t, "/play", "iiiifff",
pid,
(int) when,
(int) ((when - floor(when)) * 1000000.0f),
lowchunk,
pitch,
flux,
centroid
);
}