forked from grame-cncm/faust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynthfile.cpp
473 lines (372 loc) · 12.7 KB
/
synthfile.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
/************************************************************************
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 ARCHITECTURE SECTION (part 1/2)****************/
/************************************************************************
FAUST Architecture File
Copyright (C) 2003-2011 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.
************************************************************************
************************************************************************/
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <sndfile.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// g++ -O3 -lm -lsynthfile myfx.cpp
using namespace std;
struct Meta : map<const char*, const char*>
{
void declare (const char* key, const char* value) { (*this)[key]=value; }
};
// abs is now predefined
//template<typename T> T abs (T a) { return (a<T(0)) ? -a : a; }
inline int lsr (int x, int n) { return int(((unsigned int)x) >> n); }
/******************************************************************************
*******************************************************************************
VECTOR INTRINSICS
*******************************************************************************
*******************************************************************************/
<<includeIntrinsic>>
/******************************************************************************
*******************************************************************************
USER INTERFACE
*******************************************************************************
*******************************************************************************/
class UI
{
bool fStopped;
public:
UI() : fStopped(false) {}
virtual ~UI() {}
// -- active widgets
virtual void addButton(const char* label, float* zone) = 0;
virtual void addToggleButton(const char* label, float* zone) = 0;
virtual void addCheckButton(const char* label, float* zone) = 0;
virtual void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step) = 0;
virtual void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step) = 0;
virtual void addNumEntry(const char* label, float* zone, float init, float min, float max, float step) = 0;
// -- passive widgets
virtual void addNumDisplay(const char* label, float* zone, int precision) = 0;
virtual void addTextDisplay(const char* label, float* zone, char* names[], float min, float max) = 0;
virtual void addHorizontalBargraph(const char* label, float* zone, float min, float max) = 0;
virtual void addVerticalBargraph(const char* label, float* zone, float min, float max) = 0;
// -- frames and labels
virtual void openFrameBox(const char* label) = 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 show() = 0;
virtual void run() = 0;
void stop() { fStopped = true; }
bool stopped() { return fStopped; }
virtual void declare(float* zone, const char* key, const char* value) {}
};
struct param {
float* fZone; float fMin; float fMax;
param(float* z, float init, float a, float b) : fZone(z), fMin(a), fMax(b) { *z = init; }
};
class CMDUI : public UI
{
int fArgc;
char** fArgv;
char* fOutFile;
long fNumframes;
stack<string> fPrefix;
map<string, param> fKeyParam;
void openAnyBox(const char* label)
{
string prefix;
if (label && label[0]) {
prefix = fPrefix.top() + "-" + label;
} else {
prefix = fPrefix.top();
}
fPrefix.push(prefix);
}
string simplify(const string& src)
{
int i=0;
int level=0;
string dst;
while (src[i] ) {
switch (level) {
case 0 :
case 1 :
case 2 :
// Skip the begin of the label "--foo-"
// until 3 '-' have been read
if (src[i]=='-') { level++; }
break;
case 3 :
// copy the content, but skip non alphnum
// and content in parenthesis
switch (src[i]) {
case '(' :
case '[' :
level++;
break;
case '-' :
dst += '-';
break;
default :
if (isalnum(src[i])) {
dst+= tolower(src[i]);
}
}
break;
default :
// here we are inside parenthesis and
// we skip the content until we are back to
// level 3
switch (src[i]) {
case '(' :
case '[' :
level++;
break;
case ')' :
case ']' :
level--;
break;
default :
break;
}
}
i++;
}
return dst;
}
public:
CMDUI(int argc, char *argv[]) : UI(), fArgc(argc), fArgv(argv), fNumframes(44100), fOutFile("out.wav")
{ fPrefix.push("-"); }
virtual ~CMDUI() {}
void addOption(const char* label, float* zone, float init, float min, float max)
{
string fullname = "-" + simplify(fPrefix.top() + "-" + label);
fKeyParam.insert(make_pair(fullname, param(zone, init, min, max)));
}
virtual void addButton(const char* label, float* zone)
{
addOption(label,zone,0,0,1);
}
virtual void addToggleButton(const char* label, float* zone)
{
addOption(label,zone,0,0,1);
}
virtual void addCheckButton(const char* label, float* zone)
{
addOption(label,zone,0,0,1);
}
virtual void addVerticalSlider(const char* label, float* zone, float init, float min, float max, float step)
{
addOption(label,zone,init,min,max);
}
virtual void addHorizontalSlider(const char* label, float* zone, float init, float min, float max, float step)
{
addOption(label,zone,init,min,max);
}
virtual void addNumEntry(const char* label, float* zone, float init, float min, float max, float step)
{
addOption(label,zone,init,min,max);
}
// -- passive widgets
virtual void addNumDisplay(const char* label, float* zone, int precision) {}
virtual void addTextDisplay(const char* label, float* zone, char* names[], float min, float max) {}
virtual void addHorizontalBargraph(const char* label, float* zone, float min, float max) {}
virtual void addVerticalBargraph(const char* label, float* zone, float min, float max) {}
virtual void openFrameBox(const char* label) { openAnyBox(label); }
virtual void openTabBox(const char* label) { openAnyBox(label); }
virtual void openHorizontalBox(const char* label) { openAnyBox(label); }
virtual void openVerticalBox(const char* label) { openAnyBox(label); }
virtual void closeBox() { fPrefix.pop(); }
virtual void show() {}
virtual void run() {}
void printhelp()
{
cerr << "usage: " << fArgv[0] << " [options]" << endl;
cerr << " [options]: " << endl;
cerr << " samples: number of samples to generate, default is 44100 (1 second of sound)" << endl;
cerr << " -o outfile: name of the output file, default is 'out.wav'" << endl;
if (fKeyParam.size()) {
map<string, param>::iterator i;
cerr << " [faust module options]: \n";
for (i = fKeyParam.begin(); i != fKeyParam.end(); i++) {
cout << " " << i->first << " [" << i->second.fMin << ".." << i->second.fMax <<" ] \n";
}
}
exit(1);
}
void process_command()
{
map<string, param>::iterator p;
for (int i = 1; i < fArgc; i++) {
if (fArgv[i][0] == '-') {
if ( (strcmp(fArgv[i], "-help") == 0)
|| (strcmp(fArgv[i], "-h") == 0)
|| (strcmp(fArgv[i], "--help") == 0) ) {
printhelp();
}
if (strcmp(fArgv[i], "-o") == 0) {
fOutFile = fArgv[i+1];
}
else {
p = fKeyParam.find(fArgv[i]);
if (p == fKeyParam.end()) {
cout << fArgv[0] << ": unrecognized option " << fArgv[i] << "\n";
printhelp();
}
*(p->second.fZone) = float(strtod(fArgv[i+1], NULL));
}
i++;
}
else {
fNumframes = strtol(fArgv[i], NULL, 10);
if (fNumframes <= 0 ) printhelp();
}
}
}
char* output_file() { return fOutFile; }
long num_frames() { return fNumframes; }
void process_init()
{
map<string, param>::iterator p;
for (int i = 1; i < fArgc; i++) {
if (fArgv[i][0] == '-') {
p = fKeyParam.find(fArgv[i]);
if (p != fKeyParam.end()) {
*(p->second.fZone) = float(strtod(fArgv[i+1], NULL));
i++;
}
}
}
}
};
//----------------------------------------------------------------
// d�inition du processeur de signal
//----------------------------------------------------------------
class dsp {
protected:
int fSamplingFreq;
public:
dsp() {}
virtual ~dsp() {}
virtual int getNumInputs() = 0;
virtual int getNumOutputs() = 0;
virtual void buildUserInterface(UI* interface) = 0;
virtual void init(int samplingRate) = 0;
virtual void compute(int len, float** inputs, float** outputs) = 0;
virtual void conclude() {}
};
/********************END ARCHITECTURE SECTION (part 1/2)****************/
/**************************BEGIN USER SECTION **************************/
<<includeclass>>
/***************************END USER SECTION ***************************/
/*******************BEGIN ARCHITECTURE SECTION (part 2/2)***************/
mydsp DSP;
class Interleaver
{
int fNumFrames;
int fNumInputs;
int fNumOutputs;
float* fInputs[256];
float* fOutput;
public:
Interleaver(int numFrames, int numInputs, int numOutputs)
{
fNumFrames = numFrames;
fNumInputs = max(numInputs, numOutputs);
fNumOutputs = numOutputs;
// allocate separate input channels
for (int i = 0; i < fNumInputs; i++) {
fInputs[i] = (float*) calloc (fNumFrames, sizeof(float));
}
// allocate interleaved output channel
fOutput = (float*) calloc(fNumFrames*fNumOutputs, sizeof(float));
}
~Interleaver()
{
// free separate input channels
for (int i = 0; i < fNumInputs; i++) {
free(fInputs[i]);
}
// free interleaved output channel
free(fOutput);
}
float** inputs() { return fInputs; }
float* output() { return fOutput; }
void interleave()
{
for (int s = 0; s < fNumFrames; s++) {
for (int c = 0; c < fNumOutputs; c++) {
fOutput[c + s*fNumOutputs] = fInputs[c][s];
}
}
}
};
#define kFrames 512
#define kSampleRate 44100
int main(int argc, char *argv[] )
{
CMDUI* interface = new CMDUI(argc, argv);
DSP.buildUserInterface(interface);
interface->process_command();
// open output file
SNDFILE* out_sf;
SF_INFO out_info = { interface->num_frames(), kSampleRate, DSP.getNumOutputs(),
SF_FORMAT_WAV|SF_FORMAT_PCM_16|SF_ENDIAN_LITTLE, 0, 0};
out_sf = sf_open(interface->output_file(), SFM_WRITE, &out_info);
if (out_sf == NULL) {
cerr << "Error: ";
sf_perror(out_sf);
exit(1);
}
// create interleaver
Interleaver ilv (kFrames, DSP.getNumOutputs(), DSP.getNumOutputs());
// init signal processor
DSP.init(kSampleRate);
interface->process_init();
// process all samples
int frames = interface->num_frames();
int nbf;
do {
if (frames > kFrames) {
nbf = kFrames;
frames -= kFrames;
}
else {
nbf = frames;
frames = 0;
}
DSP.compute(nbf, 0, ilv.inputs());
ilv.interleave();
sf_writef_float(out_sf, ilv.output(), nbf);
} while (nbf);
sf_close(out_sf);
}
/********************END ARCHITECTURE SECTION (part 2/2)****************/