-
Notifications
You must be signed in to change notification settings - Fork 21
/
genlib.cpp
617 lines (505 loc) · 15 KB
/
genlib.cpp
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
459
460
461
462
463
464
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*******************************************************************************************************************
Cycling '74 License for Max-Generated Code for Export
Copyright (c) 2016 Cycling '74
The code that Max generates automatically and that end users are capable of exporting and using, and any
associated documentation files (the “Software”) is a work of authorship for which Cycling '74 is the author
and owner for copyright purposes. A license is hereby granted, free of charge, to any person obtaining a
copy of the Software (“Licensee”) to use, copy, modify, merge, publish, and distribute copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The Software is licensed to Licensee only for non-commercial use. Users who wish to make commercial use of the
Software must contact the copyright owner to determine if a license for commercial use is available, and the
terms and conditions for same, which may include fees or royalties. For commercial use, please send inquiries
to licensing@cycling74.com. The determination of whether a use is commercial use or non-commercial use is based
upon the use, not the user. The Software may be used by individuals, institutions, governments, corporations, or
other business whether for-profit or non-profit so long as the use itself is not a commercialization of the
materials or a use that generates or is intended to generate income, revenue, sales or profit.
The above copyright notice and this license shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*******************************************************************************************************************/
/*
ohmtech-rdi: Silent warnings begin
*/
#if defined (__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wcast-qual"
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec"
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#pragma clang diagnostic ignored "-Wcast-align"
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
#pragma clang diagnostic ignored "-Wunreachable-code-return"
#elif defined (__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wsized-deallocation"
#endif
/*
ohmtech-rdi: Silent warnings end
*/
#include "genlib.h"
#include "genlib_exportfunctions.h"
#include <stdlib.h> // not cstdlib (causes problems with ARM embedded compiler)
#include <cstdio>
#include <cstring>
#ifndef MSP_ON_CLANG
# include <cmath>
#endif
#ifdef __APPLE__
# include <malloc/malloc.h>
#elif !defined(GEN_WINDOWS) // WIN32?
# include <malloc.h>
# define malloc_size malloc_usable_size
#endif
#ifndef GENLIB_NO_JSON
#ifdef MSP_ON_CLANG
# include "json.c"
# include "json_builder.c"
#endif
#include "json.h"
#include "json_builder.h"
#endif
// DATA_MAXIMUM_ELEMENTS * 8 bytes = 256 mb limit
#define DATA_MAXIMUM_ELEMENTS (33554432)
#include "erb/detail/fnc.h"
//////////// export_genlib.cpp ////////////
// export version
void my_memset(void *p, int c, long size);
void my_memcpy(void *dst, const void *src, long size);
/*
ohmtech-rdi: Our allocation system is done in erb
*/
t_ptr sysmem_newptr(t_ptr_size size)
{
//return (t_ptr)malloc(size);
return (t_ptr)erb::allocate_bytes_auto(size);
}
t_ptr sysmem_newptrclear(t_ptr_size size)
{
//t_ptr p = (t_ptr)malloc(size);
t_ptr p = (t_ptr)erb::allocate_bytes_auto(size);
if (p)
my_memset(p, 0, size);
return p;
}
/*
ohmtech-rdi: Not supported (but doesn't seem to be used)
*/
t_ptr sysmem_resizeptr(void *ptr, t_ptr_size newsize)
{
//return (t_ptr)realloc(ptr, newsize);
#if defined (erb_TARGET_DAISY)
asm ("bkpt 255");
#elif (erb_RAM_MEM_POOL_SIZE_SIMULATOR_CHECK)
throw std::bad_alloc ();
#endif
return nullptr;
}
/*
ohmtech-rdi: Not supported (but doesn't seem to be used)
*/
t_ptr sysmem_resizeptrclear(void *ptr, t_ptr_size newsize)
{
/*size_t oldsize = malloc_size(ptr);
t_ptr p = (t_ptr)realloc(ptr, newsize);
if (p) {
if (newsize > oldsize)
my_memset((char *)p + oldsize, 0, newsize - oldsize);
}
return p;*/
#if defined (erb_TARGET_DAISY)
asm ("bkpt 255");
#elif (erb_RAM_MEM_POOL_SIZE_SIMULATOR_CHECK)
throw std::bad_alloc ();
#endif
return nullptr;
}
/*
ohmtech-rdi: Not supported (but doesn't seem to be used)
*/
t_ptr_size sysmem_ptrsize(void *ptr)
{
//return malloc_size(ptr);
#if defined (erb_TARGET_DAISY)
asm ("bkpt 255");
#elif (erb_RAM_MEM_POOL_SIZE_SIMULATOR_CHECK)
throw std::bad_alloc ();
#endif
return 0;
}
/*
ohmtech-rdi: Our allocation system is done in erb. Just "leak" memory, that
will be reclaim differently for the simulator.
*/
void sysmem_freeptr(void *ptr)
{
//free(ptr); <-- ohmtech-rdi
}
void sysmem_copyptr(const void *src, void *dst, t_ptr_size bytes)
{
my_memcpy(dst, src, bytes);
}
void my_memset(void *p, int c, long size)
{
char *p2 = (char *)p;
int i;
for (i = 0; i < size; i++, p2++)
*p2 = char(c);
}
void my_memcpy(void *dst, const void *src, long size)
{
char *s2 = (char *)src;
char *d2 = (char *)dst;
int i;
for (i = 0; i < size; i++, s2++, d2++)
*d2 = *s2;
}
void set_zero64(t_sample *memory, long size)
{
long i;
for (i = 0; i < size; i++, memory++) {
*memory = 0.;
}
}
/*
ohmtech-rdi:
*/
unsigned long genlib_ticks() {
return 0; /* as in oopsy */
}
void genlib_report_error(const char *s)
{
#ifndef GEN_NO_STDLIB
fprintf(stderr, "%s\n", s);
#endif
}
void genlib_report_message(const char *s)
{
#ifndef GEN_NO_STDLIB
fprintf(stdout, "%s\n", s);
#endif
}
unsigned long systime_ticks(void)
{
return 0; // Gen code can deal with this
}
// NEED THIS FOR WINDOWS:
/*
ohmtech-rdi: We are using operator new in Sram
*/
/*void *operator new(size_t size) { return sysmem_newptr(size); }
void *operator new[](size_t size) { return sysmem_newptr(size); }
void operator delete(void *p) throw() { sysmem_freeptr(p); }
void operator delete[](void *p) throw() { sysmem_freeptr(p); }*/
void *genlib_obtain_reference_from_string(const char *name)
{
return 0; // to be implemented
}
// the rest is stuff to isolate gensym, attrs, atoms, buffers etc.
t_genlib_buffer *genlib_obtain_buffer_from_reference(void *ref)
{
return 0; // to be implemented
}
t_genlib_err genlib_buffer_edit_begin(t_genlib_buffer *b)
{
return 0; // to be implemented
}
t_genlib_err genlib_buffer_edit_end(t_genlib_buffer *b, long valid)
{
return 0; // to be implemented
}
t_genlib_err genlib_buffer_getinfo(t_genlib_buffer *b, t_genlib_buffer_info *info)
{
return 0; // to be implemented
}
char *genlib_reference_getname(void *ref)
{
return 0; // to be implemented
}
void genlib_buffer_dirty(t_genlib_buffer *b)
{
// to be implemented
}
t_genlib_err genlib_buffer_perform_begin(t_genlib_buffer *b)
{
return 0; // to be implemented
}
void genlib_buffer_perform_end(t_genlib_buffer *b)
{
// to be implemented
}
t_sample gen_msp_pow(t_sample value, t_sample power)
{
return pow(value, power);
}
typedef struct {
t_genlib_data_info info;
t_sample cursor; // used by Delay
//t_symbol * name;
} t_dsp_gen_data;
typedef struct {
size_t frames;
size_t channels;
const void * data; // interleaved 32-bit floats
} t_erb_sample_data;
void genlib_data_setbuffer(t_genlib_data *b, void *ref)
{
t_erb_sample_data *data = (t_erb_sample_data *)ref;
genlib_data_resize(b, data->frames, data->channels);
t_dsp_gen_data *self = (t_dsp_gen_data *)b;
memcpy(self->info.data, data->data, sizeof(t_sample) * data->frames * data->channels);
}
/*
ohmtech-rdi: Using erb malloc
*/
t_genlib_data *genlib_obtain_data_from_reference(void *ref)
{
//t_dsp_gen_data *self = (t_dsp_gen_data *)malloc(sizeof(t_dsp_gen_data));
t_dsp_gen_data *self = (t_dsp_gen_data *)erb::allocate_bytes_auto(sizeof(t_dsp_gen_data));
self->info.dim = 0;
self->info.channels = 0;
self->info.data = 0;
self->cursor = 0;
return (t_genlib_data *)self;
}
t_genlib_err genlib_data_getinfo(t_genlib_data *b, t_genlib_data_info *info)
{
t_dsp_gen_data *self = (t_dsp_gen_data *)b;
info->dim = self->info.dim;
info->channels = self->info.channels;
info->data = self->info.data;
return GENLIB_ERR_NONE;
}
void genlib_data_release(t_genlib_data *b)
{
t_dsp_gen_data *self = (t_dsp_gen_data *)b;
if (self->info.data) {
genlib_sysmem_freeptr(self->info.data);
self->info.data = 0;
}
}
long genlib_data_getcursor(t_genlib_data *b)
{
t_dsp_gen_data *self = (t_dsp_gen_data *)b;
return long(self->cursor);
}
void genlib_data_setcursor(t_genlib_data *b, long cursor)
{
t_dsp_gen_data *self = (t_dsp_gen_data *)b;
self->cursor = t_sample(cursor);
}
void genlib_data_resize(t_genlib_data *b, long s, long c)
{
t_dsp_gen_data *self = (t_dsp_gen_data *)b;
size_t sz, oldsz, copysz;
t_sample *old = 0;
t_sample *replaced = 0;
int i, j, copydim, copychannels, olddim, oldchannels;
//printf("data resize %d %d\n", s, c);
// cache old for copying:
old = self->info.data;
olddim = self->info.dim;
oldchannels = self->info.channels;
// limit [data] size:
if (s * c > DATA_MAXIMUM_ELEMENTS) {
s = DATA_MAXIMUM_ELEMENTS/c;
genlib_report_message("warning: constraining [data] to < 256MB");
}
// bytes required:
sz = sizeof(t_sample) * s * c;
oldsz = sizeof(t_sample) * olddim * oldchannels;
if (old && sz == oldsz) {
// no need to re-allocate, just resize
// careful, audio thread may still be using it:
if (s > olddim) {
self->info.channels = c;
self->info.dim = s;
} else {
self->info.dim = s;
self->info.channels = c;
}
set_zero64(self->info.data, s * c);
return;
} else {
// allocate new:
replaced = (t_sample *)sysmem_newptr(sz);
// check allocation:
if (replaced == 0) {
genlib_report_error("allocating [data]: out of memory");
// try to reallocate with a default/minimal size instead:
if (s > 512 || c > 1) {
genlib_data_resize((t_genlib_data *)self, 512, 1);
} else {
// if this fails, then Max is kaput anyway...
genlib_data_resize((t_genlib_data *)self, 4, 1);
}
return;
}
// fill with zeroes:
set_zero64(replaced, s * c);
// copy in old data:
if (old) {
// frames to copy:
// clamped:
copydim = olddim > s ? s : olddim;
// use memcpy if channels haven't changed:
if (c == oldchannels) {
copysz = sizeof(t_sample) * copydim * c;
//post("reset resize (same channels) %p %p, %d", self->info.data, old, copysz);
memcpy(replaced, old, copysz);
} else {
// memcpy won't work if channels have changed,
// because data is interleaved.
// clamp channels copied:
copychannels = oldchannels > c ? c : oldchannels;
//post("reset resize (different channels) %p %p, %d %d", self->info.data, old, copydim, copychannels);
for (i = 0; i < copydim; i++) {
for (j = 0; j < copychannels; j++) {
replaced[j + i * c] = old[j + i * oldchannels];
}
}
}
}
// now update info:
if (old == 0) {
self->info.data = replaced;
self->info.dim = s;
self->info.channels = c;
} else {
// need to be careful; the audio thread may still be using it
// since dsp_gen_data is preserved through edits
// the order of resizing has to be carefully done
// to prevent indexing out of bounds
// (or maybe I'm being too paranoid here...)
if (oldsz > sz) {
// shrink size first
if (s > olddim) {
self->info.channels = c;
self->info.dim = s;
} else {
self->info.dim = s;
self->info.channels = c;
}
self->info.data = replaced;
} else {
// shrink size after
self->info.data = replaced;
if (s > olddim) {
self->info.channels = c;
self->info.dim = s;
} else {
self->info.dim = s;
self->info.channels = c;
}
}
// done with old:
sysmem_freeptr(old);
}
}
}
void genlib_reset_complete(void *data)
{
}
#ifndef GENLIB_NO_JSON
void genlib_build_json(CommonState *cself, json_value **jsonvalue, getparameter_method getmethod)
{
int i;
*jsonvalue = json_object_new(0);
for (i = 0; i < cself->numparams; i++) {
t_param val;
(getmethod)(cself, i, &val);
json_object_push(*jsonvalue, cself->params[i].name, json_double_new(val));
}
}
size_t genlib_getstatesize(CommonState *cself, getparameter_method getmethod)
{
size_t size;
json_value *jsonvalue;
genlib_build_json(cself, &jsonvalue, getmethod);
size = json_measure(jsonvalue);
json_builder_free(jsonvalue);
return size;
}
short genlib_getstate(CommonState *cself, char *state, getparameter_method getmethod)
{
json_value *jsonvalue;
genlib_build_json(cself, &jsonvalue, getmethod);
json_serialize(state, jsonvalue);
json_builder_free(jsonvalue);
return 0;
}
static void *json_custom_alloc(size_t size, int zero, void *user_data)
{
return zero ? genlib_sysmem_newptrclear(size) : genlib_sysmem_newptr(size);
}
static void json_custom_free(void *ptr, void *user_data)
{
genlib_sysmem_freeptr(ptr);
}
short genlib_setstate(CommonState *cself, const char *state, setparameter_method setmethod)
{
json_settings settings;
char error[256];
memset(&settings, 0, sizeof(json_settings));
settings.mem_alloc = &json_custom_alloc;
settings.mem_free = &json_custom_free;
json_value *value = json_parse_ex(&settings, state, strlen(state), error);
if (value == NULL)
return 1;
if (value->type == json_object) {
unsigned int i;
for (i = 0; i < value->u.object.length; i++) {
char *name = NULL;
t_param val = 0;
int j;
if (value->u.object.values[i].value->type == json_double) {
name = value->u.object.values[i].name;
val = t_param(value->u.object.values[i].value->u.dbl);
} else if (value->u.object.values[i].value->type == json_integer) {
name = value->u.object.values[i].name;
val = t_param(value->u.object.values[i].value->u.integer);
}
if (name) {
for (j = 0; j < cself->numparams; j++) {
if (!strcmp(cself->params[j].name, name)) {
(setmethod)(cself, j, val, NULL);
}
}
}
}
}
json_value_free_ex(&settings, value);
return 0;
}
#else
size_t genlib_getstatesize(CommonState *cself, getparameter_method getmethod)
{
return 0;
}
short genlib_getstate(CommonState *cself, char *state, getparameter_method getmethod)
{
return 0;
}
short genlib_setstate(CommonState *cself, const char *state, setparameter_method setmethod)
{
return 0;
}
#endif
/*
ohmtech-rdi: Silent warnings begin
*/
#if defined (__clang__)
#pragma clang diagnostic pop
#elif defined (__GNUC__)
#pragma GCC diagnostic pop
#endif
/*
ohmtech-rdi: Silent warnings end
*/