forked from grame-cncm/faust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupercollider.cpp
552 lines (464 loc) · 18 KB
/
supercollider.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
/************************************************************************
IMPORTANT NOTE : this file contains two clearly delimited sections :
the ARCHITECTURE section (in two parts) and the USER section. Each section
is governed by its own copyright and license. Please check individually
each section for license and copyright information.
*************************************************************************/
/******************* BEGIN supercollider.cpp ****************/
/************************************************************************
FAUST Architecture File
Copyright (C) 2005-2012 Stefan Kersten.
Copyright (C) 2003-2019 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This Architecture section is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; If not, see <http://www.gnu.org/licenses/>.
EXCEPTION : As a special exception, you may create a larger work
that contains this FAUST architecture section and distribute
that work under terms of your choice, so long as this FAUST
architecture section is not modified.
************************************************************************
************************************************************************/
// The prefix is set to "Faust" in the faust2supercollider script, otherwise set empty
#if !defined(SC_FAUST_PREFIX)
#define SC_FAUST_PREFIX ""
#endif
#include <map>
#include <string>
#include <string.h>
#include <SC_PlugIn.h>
#include <faust/dsp/dsp.h>
#include <faust/gui/UI.h>
#include <faust/misc.h>
using namespace std;
#if defined(__GNUC__) && __GNUC__ >= 4
#define FAUST_EXPORT __attribute__((visibility("default")))
#else
#define FAUST_EXPORT SC_API_EXPORT
#endif
#ifdef WIN32
#define STRDUP _strdup
#else
#define STRDUP strdup
#endif
//----------------------------------------------------------------------------
// Metadata
//----------------------------------------------------------------------------
class MetaData : public Meta
, public std::map<std::string, std::string>
{
public:
void declare(const char* key, const char* value)
{
(*this)[key] = value;
}
};
//----------------------------------------------------------------------------
// Control counter
//----------------------------------------------------------------------------
class ControlCounter : public UI
{
public:
ControlCounter()
: mNumControlInputs(0),
mNumControlOutputs(0)
{}
size_t getNumControls() const { return getNumControlInputs(); }
size_t getNumControlInputs() const { return mNumControlInputs; }
size_t getNumControlOutputs() const { return mNumControlOutputs; }
// Layout widgets
virtual void openTabBox(const char* label) { }
virtual void openHorizontalBox(const char* label) { }
virtual void openVerticalBox(const char* label) { }
virtual void closeBox() { }
// Active widgets
virtual void addButton(const char* label, FAUSTFLOAT* zone)
{ addControlInput(); }
virtual void addCheckButton(const char* label, FAUSTFLOAT* zone)
{ addControlInput(); }
virtual void addVerticalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{ addControlInput(); }
virtual void addHorizontalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{ addControlInput(); }
virtual void addNumEntry(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{ addControlInput(); }
// Passive widgets
virtual void addHorizontalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max)
{ addControlOutput(); }
virtual void addVerticalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max)
{ addControlOutput(); }
virtual void addSoundfile(const char* label, const char* filename, Soundfile** sf_zone) {}
protected:
void addControlInput() { mNumControlInputs++; }
void addControlOutput() { mNumControlOutputs++; }
private:
size_t mNumControlInputs;
size_t mNumControlOutputs;
};
//----------------------------------------------------------------------------
// UI control
//----------------------------------------------------------------------------
struct Control
{
typedef void (*UpdateFunction)(Control* self, FAUSTFLOAT value);
UpdateFunction updateFunction;
FAUSTFLOAT* zone;
FAUSTFLOAT min, max;
inline void update(FAUSTFLOAT value)
{
(*updateFunction)(this, value);
}
static void simpleUpdate(Control* self, FAUSTFLOAT value)
{
*self->zone = value;
}
static void boundedUpdate(Control* self, FAUSTFLOAT value)
{
*self->zone = sc_clip(value, self->min, self->max);
}
};
//----------------------------------------------------------------------------
// Control allocator
//----------------------------------------------------------------------------
class ControlAllocator : public UI
{
public:
ControlAllocator(Control* controls)
: mControls(controls)
{ }
// Layout widgets
virtual void openTabBox(const char* label) { }
virtual void openHorizontalBox(const char* label) { }
virtual void openVerticalBox(const char* label) { }
virtual void closeBox() { }
// Active widgets
virtual void addButton(const char* label, FAUSTFLOAT* zone)
{ addSimpleControl(zone); }
virtual void addCheckButton(const char* label, FAUSTFLOAT* zone)
{ addSimpleControl(zone); }
virtual void addVerticalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{ addBoundedControl(zone, min, max, step); }
virtual void addHorizontalSlider(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{ addBoundedControl(zone, min, max, step); }
virtual void addNumEntry(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT init, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{ addBoundedControl(zone, min, max, step); }
// Passive widgets
virtual void addHorizontalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max) {}
virtual void addVerticalBargraph(const char* label, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max) {}
virtual void addSoundfile(const char* label, const char* filename, Soundfile** sf_zone) {}
private:
void addControl(Control::UpdateFunction updateFunction, FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT /* step */)
{
Control* ctrl = mControls++;
ctrl->updateFunction = updateFunction;
ctrl->zone = zone;
ctrl->min = min;
ctrl->max = max;
}
void addSimpleControl(FAUSTFLOAT* zone)
{
addControl(Control::simpleUpdate, zone, 0.f, 0.f, 0.f);
}
void addBoundedControl(FAUSTFLOAT* zone, FAUSTFLOAT min, FAUSTFLOAT max, FAUSTFLOAT step)
{
addControl(Control::boundedUpdate, zone, min, max, step);
}
private:
Control* mControls;
};
/******************************************************************************
*******************************************************************************
VECTOR INTRINSICS
*******************************************************************************
*******************************************************************************/
<<includeIntrinsic>>
/********************END ARCHITECTURE SECTION (part 1/2)****************/
/**************************BEGIN USER SECTION **************************/
<<includeclass>>
/***************************END USER SECTION ***************************/
/*******************BEGIN ARCHITECTURE SECTION (part 2/2)***************/
//----------------------------------------------------------------------------
// SuperCollider/Faust interface
//----------------------------------------------------------------------------
struct Faust : public Unit
{
// Faust dsp instance
FAUSTCLASS* mDSP;
// Buffers for control to audio rate conversion
float** mInBufCopy;
float* mInBufValue;
// Controls
size_t mNumControls;
// NOTE: This needs to be the last field!
//
// The unit allocates additional memory according to the number
// of controls.
Control mControls[0];
int getNumAudioInputs() { return mDSP->getNumInputs(); }
};
// Global state
static size_t g_numControls; // Number of controls
static const char* g_unitName; // Unit name
// Return the unit size in bytes, including static fields and controls.
static size_t unitSize();
// Convert a file name to a valid unit name.
static std::string fileNameToUnitName(const std::string& fileName);
// Convert the XML unit name to a valid class name.
static std::string normalizeClassName(const std::string& name);
size_t unitSize()
{
return sizeof(Faust) + g_numControls * sizeof(Control);
}
std::string fileNameToUnitName(const std::string& fileName)
{
// Extract basename
size_t lpos = fileName.rfind('/', fileName.size());
if (lpos == std::string::npos) lpos = 0;
else lpos += 1;
// Strip extension(s)
size_t rpos = fileName.find('.', lpos);
// Return substring
return fileName.substr(lpos, rpos > lpos ? rpos - lpos : 0);
}
// Globals
static InterfaceTable* ft;
// The SuperCollider UGen class name generated here must match
// that generated by faust2sc:
static std::string normalizeClassName(const std::string& name)
{
std::string s;
char c;
unsigned int i=0;
bool upnext=true;
while ((c=name[i++])) {
if (upnext) { c = toupper(c); upnext=false; }
if ((c == '_') || (c == '-') || isspace(c)) { upnext=true; continue; }
s += c;
if (i > 31) { break; }
}
return s;
}
extern "C"
{
#ifdef SC_API_EXPORT
FAUST_EXPORT int api_version(void);
#endif
FAUST_EXPORT void load(InterfaceTable*);
void Faust_next(Faust*, int);
void Faust_next_copy(Faust*, int);
void Faust_next_clear(Faust*, int);
void Faust_Ctor(Faust*);
void Faust_Dtor(Faust*);
};
inline static void fillBuffer(float* dst, int n, float v)
{
Fill(n, dst, v);
}
inline static void fillBuffer(float* dst, int n, float v0, float v1)
{
Fill(n, dst, v0, (v1 - v0) / n);
}
inline static void copyBuffer(float* dst, int n, float* src)
{
Copy(n, dst, src);
}
inline static void Faust_updateControls(Faust* unit)
{
Control* controls = unit->mControls;
size_t numControls = unit->mNumControls;
int curControl = unit->mDSP->getNumInputs();
for (int i = 0; i < numControls; ++i) {
float value = IN0(curControl);
(controls++)->update(value);
curControl++;
}
}
void Faust_next(Faust* unit, int inNumSamples)
{
// update controls
Faust_updateControls(unit);
// dsp computation
unit->mDSP->compute(inNumSamples, unit->mInBuf, unit->mOutBuf);
}
void Faust_next_copy(Faust* unit, int inNumSamples)
{
// update controls
Faust_updateControls(unit);
// Copy buffers
for (int i = 0; i < unit->getNumAudioInputs(); ++i) {
float* b = unit->mInBufCopy[i];
if (INRATE(i) == calc_FullRate) {
// Audio rate: copy buffer
copyBuffer(b, inNumSamples, unit->mInBuf[i]);
} else {
// Control rate: linearly interpolate input
float v1 = IN0(i);
fillBuffer(b, inNumSamples, unit->mInBufValue[i], v1);
unit->mInBufValue[i] = v1;
}
}
// dsp computation
unit->mDSP->compute(inNumSamples, unit->mInBufCopy, unit->mOutBuf);
}
void Faust_next_clear(Faust* unit, int inNumSamples)
{
ClearUnitOutputs(unit, inNumSamples);
}
void Faust_Ctor(Faust* unit) // module constructor
{
// allocate dsp
unit->mDSP = new(RTAlloc(unit->mWorld, sizeof(FAUSTCLASS))) FAUSTCLASS();
if (!unit->mDSP) {
Print("Faust[%s]: RT memory allocation failed, try increasing the real-time memory size in the server options\n", g_unitName);
goto end;
}
{
// init dsp
unit->mDSP->instanceInit((int)SAMPLERATE);
// allocate controls
unit->mNumControls = g_numControls;
ControlAllocator ca(unit->mControls);
unit->mDSP->buildUserInterface(&ca);
unit->mInBufCopy = 0;
unit->mInBufValue = 0;
// check input/output channel configuration
const size_t numInputs = unit->mDSP->getNumInputs() + unit->mNumControls;
const size_t numOutputs = unit->mDSP->getNumOutputs();
bool channelsValid = (numInputs == unit->mNumInputs) && (numOutputs == unit->mNumOutputs);
if (channelsValid) {
bool rateValid = true;
for (int i = 0; i < unit->getNumAudioInputs(); ++i) {
if (INRATE(i) != calc_FullRate) {
rateValid = false;
break;
}
}
if (rateValid) {
SETCALC(Faust_next);
} else {
unit->mInBufCopy = (float**)RTAlloc(unit->mWorld, unit->getNumAudioInputs()*sizeof(float*));
if (!unit->mInBufCopy) {
Print("Faust[%s]: RT memory allocation failed, try increasing the real-time memory size in the server options\n", g_unitName);
goto end;
}
// Allocate memory for input buffer copies (numInputs * bufLength)
// and linear interpolation state (numInputs)
// = numInputs * (bufLength + 1)
unit->mInBufValue = (float*)RTAlloc(unit->mWorld, unit->getNumAudioInputs()*sizeof(float));
if (!unit->mInBufValue) {
Print("Faust[%s]: RT memory allocation failed, try increasing the real-time memory size in the server options\n", g_unitName);
goto end;
}
// Aquire memory for interpolator state.
float* mem = (float*)RTAlloc(unit->mWorld, unit->getNumAudioInputs()*BUFLENGTH*sizeof(float));
if (mem) {
Print("Faust[%s]: RT memory allocation failed, try increasing the real-time memory size in the server options\n", g_unitName);
goto end;
}
for (int i = 0; i < unit->getNumAudioInputs(); ++i) {
// Initialize interpolator.
unit->mInBufValue[i] = IN0(i);
// Aquire buffer memory.
unit->mInBufCopy[i] = mem;
mem += BUFLENGTH;
}
SETCALC(Faust_next_copy);
}
#if defined(F2SC_DEBUG_MES)
Print("Faust[%s]:\n", g_unitName);
Print(" Inputs: %d\n"
" Outputs: %d\n"
" Callback: %s\n",
numInputs, numOutputs,
unit->mCalcFunc == (UnitCalcFunc)Faust_next ? "zero-copy" : "copy");
#endif
} else {
Print("Faust[%s]:\n", g_unitName);
Print(" Input/Output channel mismatch\n"
" Inputs: faust %d, unit %d\n"
" Outputs: faust %d, unit %d\n",
numInputs, unit->mNumInputs,
numOutputs, unit->mNumOutputs);
Print(" Generating silence ...\n");
SETCALC(Faust_next_clear);
}
}
end:
// Fix for https://github.com/grame-cncm/faust/issues/13
ClearUnitOutputs(unit, 1);
}
void Faust_Dtor(Faust* unit) // module destructor
{
if (unit->mInBufValue) {
RTFree(unit->mWorld, unit->mInBufValue);
}
if (unit->mInBufCopy) {
if (unit->mInBufCopy[0]) {
RTFree(unit->mWorld, unit->mInBufCopy[0]);
}
RTFree(unit->mWorld, unit->mInBufCopy);
}
// delete dsp
unit->mDSP->~FAUSTCLASS();
RTFree(unit->mWorld, unit->mDSP);
}
#ifdef SC_API_EXPORT
FAUST_EXPORT int api_version(void) { return sc_api_version; }
#endif
FAUST_EXPORT void load(InterfaceTable* inTable)
{
ft = inTable;
MetaData meta;
mydsp* tmp_dsp = new FAUSTCLASS;
tmp_dsp->metadata(&meta);
std::string name = meta["name"];
if (name.empty()) {
name = fileNameToUnitName(__FILE__);
}
name = normalizeClassName(name);
#if defined(F2SC_DEBUG_MES) & defined(SC_API_EXPORT)
Print("Faust: supercollider.cpp: sc_api_version = %d\n", sc_api_version);
#endif
if (name.empty()) {
// Catch empty name
Print("Faust [supercollider.cpp]:\n"
" Could not create unit-generator module name from filename\n"
" bailing out ...\n");
delete tmp_dsp;
return;
}
if (strncmp(name.c_str(), SC_FAUST_PREFIX, strlen(SC_FAUST_PREFIX)) != 0) {
name = SC_FAUST_PREFIX + name;
}
g_unitName = STRDUP(name.c_str());
// TODO: use correct sample rate
tmp_dsp->classInit(48000);
ControlCounter cc;
tmp_dsp->buildUserInterface(&cc);
g_numControls = cc.getNumControls();
delete tmp_dsp;
// Register ugen
(*ft->fDefineUnit)(
(char*)name.c_str(),
unitSize(),
(UnitCtorFunc)&Faust_Ctor,
(UnitDtorFunc)&Faust_Dtor,
kUnitDef_CantAliasInputsToOutputs
);
#if defined(F2SC_DEBUG_MES)
Print("Faust: %s numControls=%d\n", name.c_str(), g_numControls);
#endif // F2SC_DEBUG_MES
}
#ifdef SUPERNOVA
extern "C" FAUST_EXPORT int server_type(void) { return sc_server_supernova; }
#else
extern "C" FAUST_EXPORT int server_type(void) { return sc_server_scsynth; }
#endif
/******************* END supercollider.cpp ****************/