forked from grame-cncm/faust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpure.cpp
456 lines (385 loc) · 14.4 KB
/
pure.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
/************************************************************************
************************************************************************
FAUST Architecture File
Copyright (C) 2009-2011 Albert Graef <[email protected]>
---------------------------------------------------------------------
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 the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
************************************************************************
************************************************************************/
/* Pure architecture for Faust. This is similar to the Q architecture, but
uses double precision for the audio buffers and control variables. See
http://purelang.bitbucket.org for a Pure module which can load these
extensions. */
#include <stdlib.h>
#include <math.h>
#include <list>
#include <map>
using namespace std;
typedef pair<const char*,const char*> strpair;
struct Meta
{
list< strpair > data;
void declare(const char* key, const char* value)
{ data.push_back(strpair(key, value)); }
};
inline int lsr(int x, int n) { return int(((unsigned int)x) >> n); }
/******************************************************************************
*******************************************************************************
VECTOR INTRINSICS
*******************************************************************************
*******************************************************************************/
//inline void *aligned_calloc(size_t nmemb, size_t size) { return (void*)((unsigned)(calloc((nmemb*size)+15,sizeof(char)))+15 & 0xfffffff0); }
//inline void *aligned_calloc(size_t nmemb, size_t size) { return (void*)((size_t)(calloc((nmemb*size)+15,sizeof(char)))+15 & ~15); }
<<includeIntrinsic>>
/******************************************************************************
*******************************************************************************
ABSTRACT USER INTERFACE
*******************************************************************************
*******************************************************************************/
class UI
{
bool fStopped;
public:
UI() : fStopped(false) {}
virtual ~UI() {}
virtual void addButton(const char* label, double* zone) = 0;
virtual void addCheckButton(const char* label, double* zone) = 0;
virtual void addVerticalSlider(const char* label, double* zone, double init, double min, double max, double step) = 0;
virtual void addHorizontalSlider(const char* label, double* zone, double init, double min, double max, double step) = 0;
virtual void addNumEntry(const char* label, double* zone, double init, double min, double max, double step) = 0;
virtual void addHorizontalBargraph(const char* label, double* zone, double min, double max) = 0;
virtual void addVerticalBargraph(const char* label, double* zone, double min, double max) = 0;
virtual void openTabBox(const char* label) = 0;
virtual void openHorizontalBox(const char* label) = 0;
virtual void openVerticalBox(const char* label) = 0;
virtual void closeBox() = 0;
virtual void run() = 0;
void stop() { fStopped = true; }
bool stopped() { return fStopped; }
virtual void declare(double* zone, const char* key, const char* value) {}
};
/***************************************************************************
Pure UI interface
***************************************************************************/
enum ui_elem_type_t {
UI_BUTTON, UI_CHECK_BUTTON,
UI_V_SLIDER, UI_H_SLIDER, UI_NUM_ENTRY,
UI_V_BARGRAPH, UI_H_BARGRAPH,
UI_END_GROUP, UI_V_GROUP, UI_H_GROUP, UI_T_GROUP
};
struct ui_elem_t {
ui_elem_type_t type;
const char *label;
double *zone;
void *ref;
double init, min, max, step;
};
class PureUI : public UI
{
public:
int nelems;
ui_elem_t *elems;
map< int, list<strpair> > metadata;
PureUI();
virtual ~PureUI();
protected:
void add_elem(ui_elem_type_t type, const char *label = NULL);
void add_elem(ui_elem_type_t type, const char *label, double *zone);
void add_elem(ui_elem_type_t type, const char *label, double *zone,
double init, double min, double max, double step);
void add_elem(ui_elem_type_t type, const char *label, double *zone,
double min, double max);
public:
virtual void addButton(const char* label, double* zone);
virtual void addCheckButton(const char* label, double* zone);
virtual void addVerticalSlider(const char* label, double* zone, double init, double min, double max, double step);
virtual void addHorizontalSlider(const char* label, double* zone, double init, double min, double max, double step);
virtual void addNumEntry(const char* label, double* zone, double init, double min, double max, double step);
virtual void addHorizontalBargraph(const char* label, double* zone, double min, double max);
virtual void addVerticalBargraph(const char* label, double* zone, double min, double max);
virtual void openTabBox(const char* label);
virtual void openHorizontalBox(const char* label);
virtual void openVerticalBox(const char* label);
virtual void closeBox();
virtual void run();
virtual void declare(double* zone, const char* key, const char* value);
};
PureUI::PureUI()
{
nelems = 0;
elems = NULL;
}
PureUI::~PureUI()
{
if (elems) free(elems);
}
void PureUI::declare(double* zone, const char* key, const char* value)
{
map< int, list<strpair> >::iterator it = metadata.find(nelems);
if (it != metadata.end())
it->second.push_back(strpair(key, value));
else
metadata[nelems] = list<strpair>(1, strpair(key, value));
}
inline void PureUI::add_elem(ui_elem_type_t type, const char *label)
{
ui_elem_t *elems1 = (ui_elem_t*)realloc(elems, (nelems+1)*sizeof(ui_elem_t));
if (elems1)
elems = elems1;
else
return;
elems[nelems].type = type;
elems[nelems].label = label;
elems[nelems].zone = NULL;
elems[nelems].ref = NULL;
elems[nelems].init = 0.0;
elems[nelems].min = 0.0;
elems[nelems].max = 0.0;
elems[nelems].step = 0.0;
nelems++;
}
inline void PureUI::add_elem(ui_elem_type_t type, const char *label, double *zone)
{
ui_elem_t *elems1 = (ui_elem_t*)realloc(elems, (nelems+1)*sizeof(ui_elem_t));
if (elems1)
elems = elems1;
else
return;
elems[nelems].type = type;
elems[nelems].label = label;
elems[nelems].zone = zone;
elems[nelems].ref = NULL;
elems[nelems].init = 0.0;
elems[nelems].min = 0.0;
elems[nelems].max = 0.0;
elems[nelems].step = 0.0;
nelems++;
}
inline void PureUI::add_elem(ui_elem_type_t type, const char *label, double *zone,
double init, double min, double max, double step)
{
ui_elem_t *elems1 = (ui_elem_t*)realloc(elems, (nelems+1)*sizeof(ui_elem_t));
if (elems1)
elems = elems1;
else
return;
elems[nelems].type = type;
elems[nelems].label = label;
elems[nelems].zone = zone;
elems[nelems].ref = NULL;
elems[nelems].init = init;
elems[nelems].min = min;
elems[nelems].max = max;
elems[nelems].step = step;
nelems++;
}
inline void PureUI::add_elem(ui_elem_type_t type, const char *label, double *zone,
double min, double max)
{
ui_elem_t *elems1 = (ui_elem_t*)realloc(elems, (nelems+1)*sizeof(ui_elem_t));
if (elems1)
elems = elems1;
else
return;
elems[nelems].type = type;
elems[nelems].label = label;
elems[nelems].zone = zone;
elems[nelems].ref = NULL;
elems[nelems].init = 0.0;
elems[nelems].min = min;
elems[nelems].max = max;
elems[nelems].step = 0.0;
nelems++;
}
void PureUI::addButton(const char* label, double* zone)
{ add_elem(UI_BUTTON, label, zone); }
void PureUI::addCheckButton(const char* label, double* zone)
{ add_elem(UI_CHECK_BUTTON, label, zone); }
void PureUI::addVerticalSlider(const char* label, double* zone, double init, double min, double max, double step)
{ add_elem(UI_V_SLIDER, label, zone, init, min, max, step); }
void PureUI::addHorizontalSlider(const char* label, double* zone, double init, double min, double max, double step)
{ add_elem(UI_H_SLIDER, label, zone, init, min, max, step); }
void PureUI::addNumEntry(const char* label, double* zone, double init, double min, double max, double step)
{ add_elem(UI_NUM_ENTRY, label, zone, init, min, max, step); }
void PureUI::addHorizontalBargraph(const char* label, double* zone, double min, double max)
{ add_elem(UI_H_BARGRAPH, label, zone, min, max); }
void PureUI::addVerticalBargraph(const char* label, double* zone, double min, double max)
{ add_elem(UI_V_BARGRAPH, label, zone, min, max); }
void PureUI::openTabBox(const char* label)
{ add_elem(UI_T_GROUP, label); }
void PureUI::openHorizontalBox(const char* label)
{ add_elem(UI_H_GROUP, label); }
void PureUI::openVerticalBox(const char* label)
{ add_elem(UI_V_GROUP, label); }
void PureUI::closeBox()
{ add_elem(UI_END_GROUP); }
void PureUI::run() {}
/******************************************************************************
*******************************************************************************
FAUST DSP
*******************************************************************************
*******************************************************************************/
//----------------------------------------------------------------
// abstract definition of a signal processor
//----------------------------------------------------------------
class dsp {
protected:
int fSampleRate;
public:
// internal freelist for custom voice allocation
dsp *prev, *next;
dsp() {}
virtual ~dsp() {}
virtual int getNumInputs() = 0;
virtual int getNumOutputs() = 0;
virtual void buildUserInterface(UI* interface) = 0;
virtual void init(int sample_date) = 0;
virtual void compute(int len, double** inputs, double** outputs) = 0;
};
//----------------------------------------------------------------------------
// FAUST generated signal processor
//----------------------------------------------------------------------------
/* Define FAUSTFLOAT so that audio buffers and control values are always
represented as double pointers. This probably requires a recent Faust
version (0.9.9.6 or later should be ok). */
#define FAUSTFLOAT double
<<includeclass>>
#include <assert.h>
// Define this to get some debugging output.
//#define DEBUG
#ifdef DEBUG
#include <stdio.h>
#define FAUST_CN "mydsp"
#endif
/* Dynamic voice allocation. We go to some lengths here to make this as
realtime-friendly as possible. The idea is that we keep a pool of allocated
mydsp instances. When a dsp is freed with deldsp(), it's in fact never
deleted, but put at the end of a freelist from where it may eventually be
reused by a subsequent call to newdsp(). By these means, the number of
actual calls to malloc() can be kept to a minimum. In addition, a small
number of voices are preallocated in static memory (1 by default in the
present implementation, but you can set this at compile time by redefining
the NVOICES constant accordingly). If you choose a suitable NVOICES value,
chances are that your application may never need to allocate dsp instances
on the heap at all. Also, even if dsp instances have to be created
dynamically, they are allocated in chunks of NVOICES units, in order to
reduce the calls to malloc(). Thus we generally recommend to set NVOICES to
a value >1 which best suits your application. */
#ifndef NVOICES
#define NVOICES 1
#endif
// Make sure that NVOICES is at least 1.
#if NVOICES<1
#undefine NVOICES
#define NVOICES 1
#endif
struct dspmem_t {
char x[sizeof(mydsp)];
};
struct mem_t {
dspmem_t mem[NVOICES];
mem_t *next;
};
// statically and dynamically allocated dsp instances
static mem_t mem0, *mem;
// beginning and end of the freelist
static mydsp *first, *last;
/* This is supposed to be executed when the module gets unloaded. You'll need
a recent gcc version (or compatible) to make this work. */
void __attribute__ ((destructor)) mydsp_fini(void)
{
if (!mem) return;
mem = mem->next;
while (mem) {
mem_t *mem1 = mem->next;
free(mem); mem = mem1;
}
}
/* The class factory, used to create and destroy mydsp objects in the client.
This is implemented using C linkage to facilitate dlopen access. */
#include <new>
extern "C" mydsp *newdsp()
{
if (!mem) {
mem = &mem0; mem->next = 0;
// initialize the freelist with the statically allocated voices
mydsp *prev = 0, *next = (mydsp*)&mem->mem[0];
first = next;
for (int i = 0; i < NVOICES; i++) {
void *p = &mem->mem[i];
mydsp *d = new(p) mydsp;
d->prev = prev; prev = d;
d->next = ++next;
}
last = prev; last->next = 0;
#ifdef DEBUG
fprintf(stderr, ">>> %s: preallocated %d voices\n", FAUST_CN, NVOICES);
#endif
}
assert(mem);
if (!first) {
// allocate a new chunk of voices and add them to the freelist
mem_t *block = (mem_t*)calloc(1, sizeof(mem_t));
block->next = mem->next; mem->next = block;
mydsp *prev = 0, *next = (mydsp*)&block->mem[0];
first = next;
for (int i = 0; i < NVOICES; i++) {
void *p = &block->mem[i];;
mydsp *d = new(p) mydsp;
d->prev = prev; prev = d;
d->next = ++next;
}
last = prev; last->next = 0;
#ifdef DEBUG
fprintf(stderr, ">>> %s: allocated %d voices\n", FAUST_CN, NVOICES);
#endif
}
assert(first && last);
mydsp *d = first;
if (first == last) {
// freelist is now empty
first = last = 0;
} else {
// remove d from the freelist
first = (mydsp*)first->next;
}
d->prev = d->next = 0;
#ifdef DEBUG
fprintf(stderr, ">>> %s: allocating instance %p\n", FAUST_CN, d);
#endif
return d;
}
extern "C" void deldsp(mydsp* d)
{
#ifdef DEBUG
fprintf(stderr, ">>> %s: freeing instance %p\n", FAUST_CN, d);
#endif
// add d to the freelist
assert(!d->prev && !d->next);
if (last) {
last->next = d; d->prev = last; last = d;
} else
first = last = d;
}
extern "C" Meta *newmeta()
{
Meta *m = new Meta;
mydsp tmp_dsp;
tmp_dsp.metadata(m);
return m;
}
extern "C" void delmeta(Meta* m)
{
delete m;
}