libpd-sys 0.3.4

Rust bindings for libpd
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
451
452
453
454
455
456
457
458
/*
 * Copyright (c) 2012 Peter Brinkmann (peter.brinkmann@gmail.com)
 * Copyright (c) 2022 libpd team
 *
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
 *
 * See https://github.com/libpd/libpd/wiki for documentation
 *
 */

#include "z_queued.h"

#include <stdlib.h>
#include <string.h>

#include "../z_hooks.h"
#include "z_ringbuffer.h"

#define BUFFER_SIZE 16384

typedef struct _queued_stuff {
  t_libpdhooks hooks;
  t_libpd_printhook printhook;
  ring_buffer *pd_receive_buffer;
  ring_buffer *midi_receive_buffer;
  char temp_buffer[BUFFER_SIZE];
} queued_stuff;

#define QUEUEDSTUFF ((queued_stuff *)(LIBPDSTUFF->i_queued))

typedef struct _pd_params {
  enum {
    LIBPD_PRINT, LIBPD_BANG, LIBPD_FLOAT,
    LIBPD_SYMBOL, LIBPD_LIST, LIBPD_MESSAGE,
  } type;
  const char *src;
  t_float x;
  const char *sym;
  int argc;
} pd_params;

typedef struct _midi_params {
  enum {
    LIBPD_NOTEON, LIBPD_CONTROLCHANGE, LIBPD_PROGRAMCHANGE, LIBPD_PITCHBEND,
    LIBPD_AFTERTOUCH, LIBPD_POLYAFTERTOUCH, LIBPD_MIDIBYTE
  } type;
  int midi1;
  int midi2;
  int midi3;
} midi_params;

#define S_PD_PARAMS sizeof(pd_params)
#define S_MIDI_PARAMS sizeof(midi_params)
#define S_ATOM sizeof(t_atom)

static void receive_print(pd_params *p, char **buffer) {
  if (QUEUEDSTUFF->printhook) {
    QUEUEDSTUFF->printhook(*buffer);
  }
  *buffer += p->argc;
}

static void receive_bang(pd_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_banghook) {
    QUEUEDSTUFF->hooks.h_banghook(p->src);
  }
}

static void receive_float(pd_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_floathook) {
    QUEUEDSTUFF->hooks.h_floathook(p->src, (float)p->x);
  }
  else if (QUEUEDSTUFF->hooks.h_doublehook) {
    QUEUEDSTUFF->hooks.h_doublehook(p->src, (double)p->x);
  }
}

static void receive_symbol(pd_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_symbolhook) {
    QUEUEDSTUFF->hooks.h_symbolhook(p->src, p->sym);
  }
}

static void receive_list(pd_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_listhook) {
    QUEUEDSTUFF->hooks.h_listhook(p->src, p->argc, (t_atom *) *buffer);
  }
  *buffer += p->argc * S_ATOM;
}

static void receive_message(pd_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_messagehook) {
    QUEUEDSTUFF->hooks.h_messagehook(p->src, p->sym, p->argc, (t_atom *) *buffer);
  }
  *buffer += p->argc * S_ATOM;
}

#define LIBPD_WORD_ALIGN 8

static void internal_printhook(const char *s) {
  static char padding[LIBPD_WORD_ALIGN];
  queued_stuff *queued = QUEUEDSTUFF;
  int len = (int) strlen(s) + 1; // remember terminating null char
  int rest = len % LIBPD_WORD_ALIGN;
  if (rest) rest = LIBPD_WORD_ALIGN - rest;
  int total = len + rest;
  if (rb_available_to_write(queued->pd_receive_buffer) >= S_PD_PARAMS + total) {
    pd_params p = {LIBPD_PRINT, NULL, 0.0f, NULL, total};
    rb_write_to_buffer(queued->pd_receive_buffer, 3,
        (const char *)&p, S_PD_PARAMS, s, len, padding, rest);
  }
}

static void internal_banghook(const char *src) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->pd_receive_buffer) >= S_PD_PARAMS) {
    pd_params p = {LIBPD_BANG, src, 0.0f, NULL, 0};
    rb_write_to_buffer(queued->pd_receive_buffer, 1, (const char *)&p, S_PD_PARAMS);
  }
}

static void internal_floathook(const char *src, float x) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->pd_receive_buffer) >= S_PD_PARAMS) {
    pd_params p = {LIBPD_FLOAT, src, x, NULL, 0};
    rb_write_to_buffer(queued->pd_receive_buffer, 1, (const char *)&p, S_PD_PARAMS);
  }
}

static void internal_doublehook(const char *src, double x) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->pd_receive_buffer) >= S_PD_PARAMS) {
    pd_params p = {LIBPD_FLOAT, src, (t_float)x, NULL, 0};
    rb_write_to_buffer(queued->pd_receive_buffer, 1, (const char *)&p, S_PD_PARAMS);
  }
}

static void internal_symbolhook(const char *src, const char *sym) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->pd_receive_buffer) >= S_PD_PARAMS) {
    pd_params p = {LIBPD_SYMBOL, src, 0.0f, sym, 0};
    rb_write_to_buffer(queued->pd_receive_buffer, 1, (const char *)&p, S_PD_PARAMS);
  }
}

static void internal_listhook(const char *src, int argc, t_atom *argv) {
  queued_stuff *queued = QUEUEDSTUFF;
  int n = argc * S_ATOM;
  if (rb_available_to_write(queued->pd_receive_buffer) >= S_PD_PARAMS + n) {
    pd_params p = {LIBPD_LIST, src, 0.0f, NULL, argc};
    rb_write_to_buffer(queued->pd_receive_buffer, 2,
        (const char *)&p, S_PD_PARAMS, (const char *)argv, n);
  }
}

static void internal_messagehook(const char *src, const char* sym,
  int argc, t_atom *argv) {
  queued_stuff *queued = QUEUEDSTUFF;
  int n = argc * S_ATOM;
  if (rb_available_to_write(queued->pd_receive_buffer) >= S_PD_PARAMS + n) {
    pd_params p = {LIBPD_MESSAGE, src, 0.0f, sym, argc};
    rb_write_to_buffer(queued->pd_receive_buffer, 2,
        (const char *)&p, S_PD_PARAMS, (const char *)argv, n);
  }
}

static void receive_noteon(midi_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_noteonhook) {
    QUEUEDSTUFF->hooks.h_noteonhook(p->midi1, p->midi2, p->midi3);
  }
}

static void receive_controlchange(midi_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_controlchangehook) {
    QUEUEDSTUFF->hooks.h_controlchangehook(p->midi1, p->midi2, p->midi3);
  }
}

static void receive_programchange(midi_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_programchangehook) {
    QUEUEDSTUFF->hooks.h_programchangehook(p->midi1, p->midi2);
  }
}

static void receive_pitchbend(midi_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_pitchbendhook) {
    QUEUEDSTUFF->hooks.h_pitchbendhook(p->midi1, p->midi2);
  }
}

static void receive_aftertouch(midi_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_aftertouchhook) {
    QUEUEDSTUFF->hooks.h_aftertouchhook(p->midi1, p->midi2);
  }
}

static void receive_polyaftertouch(midi_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_polyaftertouchhook) {
    QUEUEDSTUFF->hooks.h_polyaftertouchhook(p->midi1, p->midi2, p->midi3);
  }
}

static void receive_midibyte(midi_params *p, char **buffer) {
  if (QUEUEDSTUFF->hooks.h_midibytehook) {
    QUEUEDSTUFF->hooks.h_midibytehook(p->midi1, p->midi2);
  }
}

static void internal_noteonhook(int channel, int pitch, int velocity) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->midi_receive_buffer) >= S_MIDI_PARAMS) {
    midi_params p = {LIBPD_NOTEON, channel, pitch, velocity};
    rb_write_to_buffer(queued->midi_receive_buffer, 1, (const char *)&p, S_MIDI_PARAMS);
  }
}

static void internal_controlchangehook(int channel, int controller, int value) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->midi_receive_buffer) >= S_MIDI_PARAMS) {
    midi_params p = {LIBPD_CONTROLCHANGE, channel, controller, value};
    rb_write_to_buffer(queued->midi_receive_buffer, 1, (const char *)&p, S_MIDI_PARAMS);
  }
}

static void internal_programchangehook(int channel, int value) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->midi_receive_buffer) >= S_MIDI_PARAMS) {
    midi_params p = {LIBPD_PROGRAMCHANGE, channel, value, 0};
    rb_write_to_buffer(queued->midi_receive_buffer, 1, (const char *)&p, S_MIDI_PARAMS);
  }
}

static void internal_pitchbendhook(int channel, int value) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->midi_receive_buffer) >= S_MIDI_PARAMS) {
    midi_params p = {LIBPD_PITCHBEND, channel, value, 0};
    rb_write_to_buffer(queued->midi_receive_buffer, 1, (const char *)&p, S_MIDI_PARAMS);
  }
}

static void internal_aftertouchhook(int channel, int value) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->midi_receive_buffer) >= S_MIDI_PARAMS) {
    midi_params p = {LIBPD_AFTERTOUCH, channel, value, 0};
    rb_write_to_buffer(queued->midi_receive_buffer, 1, (const char *)&p, S_MIDI_PARAMS);
  }
}

static void internal_polyaftertouchhook(int channel, int pitch, int value) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->midi_receive_buffer) >= S_MIDI_PARAMS) {
    midi_params p = {LIBPD_POLYAFTERTOUCH, channel, pitch, value};
    rb_write_to_buffer(queued->midi_receive_buffer, 1, (const char *)&p, S_MIDI_PARAMS);
  }
}

static void internal_midibytehook(int port, int byte) {
  queued_stuff *queued = QUEUEDSTUFF;
  if (rb_available_to_write(queued->midi_receive_buffer) >= S_MIDI_PARAMS) {
    midi_params p = {LIBPD_MIDIBYTE, port, byte, 0};
    rb_write_to_buffer(queued->midi_receive_buffer, 1, (const char *)&p, S_MIDI_PARAMS);
  }
}

void libpd_set_queued_printhook(const t_libpd_printhook hook) {
  QUEUEDSTUFF->printhook = hook;
}

void libpd_set_queued_banghook(const t_libpd_banghook hook) {
  QUEUEDSTUFF->hooks.h_banghook = hook;
}

void libpd_set_queued_floathook(const t_libpd_floathook hook) {
  QUEUEDSTUFF->hooks.h_floathook = hook;
  QUEUEDSTUFF->hooks.h_doublehook = NULL;
}

void libpd_set_queued_doublehook(const t_libpd_doublehook hook) {
  QUEUEDSTUFF->hooks.h_floathook = NULL;
  QUEUEDSTUFF->hooks.h_doublehook = hook;
}

void libpd_set_queued_symbolhook(const t_libpd_symbolhook hook) {
  QUEUEDSTUFF->hooks.h_symbolhook = hook;
}

void libpd_set_queued_listhook(const t_libpd_listhook hook) {
  QUEUEDSTUFF->hooks.h_listhook = hook;
}

void libpd_set_queued_messagehook(const t_libpd_messagehook hook) {
  QUEUEDSTUFF->hooks.h_messagehook = hook;
}

void libpd_set_queued_noteonhook(const t_libpd_noteonhook hook) {
  QUEUEDSTUFF->hooks.h_noteonhook = hook;
}

void libpd_set_queued_controlchangehook(const t_libpd_controlchangehook hook) {
  QUEUEDSTUFF->hooks.h_controlchangehook = hook;
}

void libpd_set_queued_programchangehook(const t_libpd_programchangehook hook) {
  QUEUEDSTUFF->hooks.h_programchangehook = hook;
}

void libpd_set_queued_pitchbendhook(const t_libpd_pitchbendhook hook) {
  QUEUEDSTUFF->hooks.h_pitchbendhook = hook;
}

void libpd_set_queued_aftertouchhook(const t_libpd_aftertouchhook hook) {
  QUEUEDSTUFF->hooks.h_aftertouchhook = hook;
}

void libpd_set_queued_polyaftertouchhook(const t_libpd_polyaftertouchhook hook) {
  QUEUEDSTUFF->hooks.h_polyaftertouchhook = hook;
}

void libpd_set_queued_midibytehook(const t_libpd_midibytehook hook) {
  QUEUEDSTUFF->hooks.h_midibytehook = hook;
}

static void queued_stuff_free(void *p) {
  queued_stuff *queued = (queued_stuff *)p;
  if (queued->pd_receive_buffer) rb_free(queued->pd_receive_buffer);
  if (queued->midi_receive_buffer) rb_free(queued->midi_receive_buffer);
}

int libpd_queued_init(void) {
  int ret = libpd_init();

  libpd_set_printhook(internal_printhook);
  libpd_set_banghook(internal_banghook);
  libpd_set_doublehook(internal_doublehook);
  libpd_set_symbolhook(internal_symbolhook);
  libpd_set_listhook(internal_listhook);
  libpd_set_messagehook(internal_messagehook);

  libpd_set_noteonhook(internal_noteonhook);
  libpd_set_controlchangehook(internal_controlchangehook);
  libpd_set_programchangehook(internal_programchangehook);
  libpd_set_pitchbendhook(internal_pitchbendhook);
  libpd_set_aftertouchhook(internal_aftertouchhook);
  libpd_set_polyaftertouchhook(internal_polyaftertouchhook);
  libpd_set_midibytehook(internal_midibytehook);

  t_libpdimp *imp = LIBPDSTUFF;
  if (!imp->i_queued) {
    queued_stuff *queued = (queued_stuff *)calloc(1, sizeof(queued_stuff));
    if(!queued) goto cleanup;
    queued->pd_receive_buffer = rb_create(BUFFER_SIZE);
    if (!queued->pd_receive_buffer) goto cleanup;
    queued->midi_receive_buffer = rb_create(BUFFER_SIZE);
    if (!queued->midi_receive_buffer) goto cleanup;
    imp->i_queued = (void *)queued;
    imp->i_queued_freehook = queued_stuff_free;
  }
  return ret;
cleanup:
  libpd_queued_release();
  return -2;
}

void libpd_queued_release(void) {
  t_libpdimp *imp = LIBPDSTUFF;
  if (imp->i_queued) {
    queued_stuff_free(imp->i_queued);
    imp->i_queued = NULL;
    imp->i_queued_freehook = NULL;
  }
}

void libpd_queued_receive_pd_messages(void) {
  queued_stuff *queued = QUEUEDSTUFF;
  size_t available = rb_available_to_read(queued->pd_receive_buffer);
  if (!available) return;
  rb_read_from_buffer(queued->pd_receive_buffer, queued->temp_buffer, (int)available);
  char *end = queued->temp_buffer + available;
  char *buffer = queued->temp_buffer;
  while (buffer < end) {
    pd_params *p = (pd_params *)buffer;
    buffer += S_PD_PARAMS;
    switch (p->type) {
      case LIBPD_PRINT: {
        receive_print(p, &buffer);
        break;
      }
      case LIBPD_BANG: {
        receive_bang(p, &buffer);
        break;
      }
      case LIBPD_FLOAT: {
        receive_float(p, &buffer);
        break;
      }
      case LIBPD_SYMBOL: {
        receive_symbol(p, &buffer);
        break;
      }
      case LIBPD_LIST: {
        receive_list(p, &buffer);
        break;
      }
      case LIBPD_MESSAGE: {
        receive_message(p, &buffer);
        break;
      }
      default:
        break;
    }
  }
}

void libpd_queued_receive_midi_messages(void) {
  queued_stuff *queued = QUEUEDSTUFF;
  size_t available = rb_available_to_read(queued->midi_receive_buffer);
  if (!available) return;
  rb_read_from_buffer(queued->midi_receive_buffer, queued->temp_buffer, (int)available);
  char *end = queued->temp_buffer + available;
  char *buffer = queued->temp_buffer;
  while (buffer < end) {
    midi_params *p = (midi_params *)buffer;
    buffer += S_MIDI_PARAMS;
    switch (p->type) {
      case LIBPD_NOTEON: {
        receive_noteon(p, &buffer);
        break;
      }
      case LIBPD_CONTROLCHANGE: {
        receive_controlchange(p, &buffer);
        break;
      }
      case LIBPD_PROGRAMCHANGE: {
        receive_programchange(p, &buffer);
        break;
      }
      case LIBPD_PITCHBEND: {
        receive_pitchbend(p, &buffer);
        break;
      }
      case LIBPD_AFTERTOUCH: {
        receive_aftertouch(p, &buffer);
        break;
      }
      case LIBPD_POLYAFTERTOUCH: {
        receive_polyaftertouch(p, &buffer);
        break;
      }
      case LIBPD_MIDIBYTE: {
        receive_midibyte(p, &buffer);
        break;
      }
      default:
        break;
    }
  }
}