-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFaustGenFactory.cpp
More file actions
581 lines (494 loc) · 15.8 KB
/
Copy pathFaustGenFactory.cpp
File metadata and controls
581 lines (494 loc) · 15.8 KB
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
/*
==============================================================================
FaustGenFactory.cpp
Based on Faustgen~ by GRAME
Author: Oliver Larkin
Faustgen~ license:
***********************************************************************
FAUST Architecture File
Copyright (C) 2010-2015 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 "FaustGenFactory.h"
#include "FaustAudioPluginInstance.h"
#include <cstdio>
int FaustgenFactory::gFaustCounter = 0;
std::map<String, FaustgenFactory*> FaustgenFactory::gFactoryMap;
#if JUCE_MAC
static std::string getTarget()
{
int tmp;
return (sizeof(&tmp) == 8) ? "" : "i386-apple-darwin10.6.0";
}
#else
static std::string getTarget() { return ""; }
#endif
FaustgenFactory::FaustgenFactory(const String& name, const File& path, const File& svgPath)
: svgThread(this)
{
fUpdateInstance = 0;
fName = name;
fDSPfactory = nullptr;
fSourceCode = DEFAULT_CODE;
if(gFaustCounter == 0) {
startMTDSPFactories();
}
gFaustCounter++;
fFaustNumber = gFaustCounter;
// Built the complete resource path
fLibraryPath.add(path);
fDrawPath = svgPath;
}
FaustgenFactory::~FaustgenFactory()
{
freeDSPFactory();
removeSVG();
}
void FaustgenFactory::freeDSPFactory()
{
const ScopedLock lock(fDSPMutex);
// Free all instances
std::set<FaustAudioPluginInstance*>::const_iterator it;
for (it = fInstances.begin(); it != fInstances.end(); it++) {
(*it)->freeDSP();
}
deleteDSPFactory(fDSPfactory);
fDSPfactory = nullptr;
}
llvm_dsp_factory* FaustgenFactory::createFactoryFromBitcode()
{
return readDSPFactoryFromBitcode(fBitCode.toStdString(), getTarget(), LLVM_OPTIMIZATION);
/*
Alternate model using machine code
return readDSPFactoryFromMachine(decoded_bitcode);
*/
/*
Alternate model using LLVM IR
return readDSPFactoryFromIR(*fBitCode, getTarget(), LLVM_OPTIMIZATION);
*/
}
llvm_dsp_factory* FaustgenFactory::createFactoryFromSourceCode(FaustAudioPluginInstance* instance)
{
defaultCompileOptions();
// Prepare compile options
std::string error;
const char* argv[64];
memset(argv, 0, 64 * sizeof(char*));
jassert(fCompileOptions.size() < 64);
for (auto opt = 0; opt < fCompileOptions.size(); opt++)
{
argv[opt] = (char*) fCompileOptions.getReference(opt).toRawUTF8();
}
argv[fCompileOptions.size()] = 0; // NULL terminated argv
llvm_dsp_factory* factory = createDSPFactoryFromString(getTMPName().toStdString(), fSourceCode.toStdString(), fCompileOptions.size(), argv, getTarget(), error, LLVM_OPTIMIZATION);
if (factory)
{
return factory;
}
else
{
if (fUpdateInstance == instance)
{
instance->highlightON(error);
}
LOG("Invalid Faust code or compile options:" + error);
return 0;
}
}
dsp* FaustgenFactory::createDSPAux(FaustAudioPluginInstance* instance)
{
dsp* dsp = nullptr;
std::string error;
String logStr;
// Factory already allocated
if (fDSPfactory)
{
dsp = fDSPfactory->createDSPInstance();
logStr << "Factory already allocated, " << dsp->getNumInputs() << " input(s), " << dsp->getNumOutputs() << " output(s)";
goto end;
}
// Tries to create from bitcode
if (fBitCode.length() > 0)
{
fDSPfactory = createFactoryFromBitcode();
if (fDSPfactory)
{
dsp = fDSPfactory->createDSPInstance();
logStr << "Compilation from bitcode succeeded, " << dsp->getNumInputs() << " input(s), " << dsp->getNumOutputs() << " output(s)";
goto end;
}
}
// Otherwise tries to create from source code
if (fSourceCode.length() > 0)
{
fDSPfactory = createFactoryFromSourceCode(instance);
if (fDSPfactory)
{
dsp = fDSPfactory->createDSPInstance();
logStr << "Compilation from source code succeeded, " << dsp->getNumInputs() << " input(s), " << dsp->getNumOutputs() << " output(s)";
goto end;
}
}
// Otherwise creates default DSP keeping the same input/output number
fDSPfactory = createDSPFactoryFromString("default", DEFAULT_CODE, 0, 0, getTarget(), error, LLVM_OPTIMIZATION);
jassert(fDSPfactory);
if (fDSPfactory)
{
dsp = fDSPfactory->createDSPInstance();
logStr << "Allocation of default DSP succeeded, " << dsp->getNumInputs() << " input(s), " << dsp->getNumOutputs() << " output(s)";
}
end:
LOG(logStr);
jassert(dsp);
// Prepare JSON
JSONUI builder(dsp->getNumInputs(), dsp->getNumOutputs());
dsp->metadata(&builder);
dsp->buildUserInterface(&builder);
fJSON = String(builder.JSON());
//LOG(fJSON);
return dsp;
}
void FaustgenFactory::addLibraryPath(const File& libraryPath)
{
fLibraryPath.addIfNotAlreadyThere(libraryPath);
}
void FaustgenFactory::addCompileOption(const String& key, const String& value)
{
if ((value != String::empty) && !fCompileOptions.contains(value))
{
fCompileOptions.add(key);
fCompileOptions.add(value);
}
}
void FaustgenFactory::addCompileOption(const String& value)
{
if ((value != String::empty))
{
fCompileOptions.addIfNotAlreadyThere(value);
}
}
void FaustgenFactory::printCompileOptions()
{
if (fCompileOptions.size() > 0)
{
LOG("-----------------------------");
for (int opt = 0; opt < fCompileOptions.size(); opt++)
{
LOG("Compile option =" + fCompileOptions.getReference(opt));
}
LOG("-----------------------------");
}
}
void FaustgenFactory::defaultCompileOptions()
{
fCompileOptions.clear();
// By default when double
if (sizeof(FAUSTFLOAT) == 8)
addCompileOption("-double");
// if (fDrawPath != File::nonexistent) {
// addCompileOption("-svg");
// addCompileOption("-sn"); //use --simple-names (without arguments) during block-diagram generation
// addCompileOption("-sd"); //try to further --simplify-diagrams before drawing them
// }
for (int path=0;path<fLibraryPath.getNumPaths();path++)
addCompileOption("-I", fLibraryPath[path].getFullPathName());
if (fDrawPath != File::nonexistent)
addCompileOption("-O", fDrawPath.getFullPathName());
//addCompileOption("-o", "tmp1.cpp");
for (int opt = 0; opt < fExtraOptions.size(); opt++)
{
addCompileOption(fExtraOptions.getReference(opt));
}
// Vector mode by default
/*
addCompileOption("-vec");
addCompileOption("-lv");
addCompileOption("1");
*/
}
void FaustgenFactory::getStateInformation (XmlElement& xml)
{
xml.setAttribute ("version", FAUSTGEN_VERSION);
if (fSourceCode.length())
{
xml.setAttribute ("sourcecode", fSourceCode);
}
if (fDSPfactory)
{
std::string bitcode = writeDSPFactoryToBitcode(fDSPfactory);
xml.setAttribute ("bitcode", String(bitcode));
}
}
void FaustgenFactory::setStateInformation (XmlElement& xml)
{
String faustgen_version = xml.getStringAttribute("version");
String sourcecode;
String bitcode;
if (faustgen_version == String::empty)
{
LOG("Cannot read \"version\" key, so ignore bitcode, force recompilation and use default compileoptions");
goto default_sourcecode;
}
else if (faustgen_version != FAUSTGEN_VERSION)
{
String logStr;
logStr << "Older version of faustgen (" << FAUSTGEN_VERSION << "versus " << faustgen_version << "), so ignore bitcode, force recompilation and use default compileoptions";
LOG(logStr);
goto read_sourcecode;
}
bitcode = xml.getStringAttribute("bitcode");
if (bitcode != String::empty)
{
fBitCode = bitcode;
}
read_sourcecode:
sourcecode = xml.getStringAttribute("sourcecode");
if (sourcecode != String::empty)
{
fSourceCode = sourcecode;
return;
}
default_sourcecode:
fSourceCode = DEFAULT_CODE;
}
void FaustgenFactory::startSVGThread()
{
svgThread.startThread();
}
void FaustgenFactory::generateSVG()
{
// To be sure we get a correct SVG diagram...
removeSVG();
// Prepare compile options
std::string error;
const char* argv[64];
memset(argv, 0, 64 * sizeof(char*));
jassert(fCompileOptions.size() < 64);
auto opt = 0;
for (opt = 0; opt < fCompileOptions.size(); opt++)
{
argv[opt] = (char*) fCompileOptions.getReference(opt).toRawUTF8();
}
argv[opt++] = "-svg";
argv[opt++] = "-sn";
argv[opt++] = "-sd";
if (fDrawPath != File::nonexistent)
{
// Generate SVG file
if (!generateAuxFilesFromString(getTMPName().toStdString(), fSourceCode.toStdString(), fCompileOptions.size() + 3, argv, error))
{
//TODO: if there is an error here STOP
LOG("Generate SVG error : " + error);
}
File svgFile = getSVGFile();
if (svgFile.exists())
{
#if 0
File htmlFile(fDrawPath.getFullPathName() + "/" + getSVGFolderName() + "/index.html");
htmlFile.appendText(HTML_WRAPPER);
#else
XmlDocument svgXML(svgFile);
ScopedPointer<XmlElement> mainElement (svgXML.getDocumentElement());
mainElement->setAttribute("width", "100%");
mainElement->setAttribute("height", "100%");
mainElement->writeToFile(svgFile, String::empty);
#endif
}
}
}
void FaustgenFactory::removeSVG()
{
if (fDrawPath != File::nonexistent)
{
getSVGFile().getParentDirectory().deleteRecursively();
}
}
void FaustgenFactory::displaySVG()
{
// // Try to open SVG svg diagram file inside a web browser
// if (!tryOpenSVG())
// {
// LOG("SVG diagram not available, recompile to produce it");
//
// // Force recompilation to produce it
// llvm_dsp_factory* factory = createFactoryFromSourceCode(0);
// //deleteDSPFactory(factory); // commented out in faustgen~
//
// // Open the SVG diagram file inside a web browser
// openSVG();
// }
}
File FaustgenFactory::getSVGFile()
{
File svgPathForThisInstance(fDrawPath.getChildFile(getSVGFolderName()));
return svgPathForThisInstance.getChildFile("process.svg");
}
String FaustgenFactory::getHTMLURI()
{
File svgPathForThisInstance(fDrawPath.getChildFile(getSVGFolderName()));
String URI;
#if 0
URI << "file://" << svgPathForThisInstance.getChildFile("index.html").getFullPathName(); //TODO: will this work on windows?
#else
URI << "file://" << getSVGFile().getFullPathName(); //TODO: will this work on windows?
#endif
return URI;
}
String FaustgenFactory::getSVGFolderName()
{
String name;
name << getTMPName() << "-svg";
return name;
}
String FaustgenFactory::getTMPName()
{
String name;
name << "faustgen-" << getpid() << "-" << fFaustNumber; //TODO: will this work on windows?
return name;
}
void FaustgenFactory::updateSourceCode(String sourceCode, FaustAudioPluginInstance* instance)
{
// Recompile only if text has been changed
if (fSourceCode != sourceCode)
{
// Update all instances
std::set<FaustAudioPluginInstance*>::const_iterator it;
for (it = fInstances.begin(); it != fInstances.end(); it++)
{
(*it)->highlightOFF();
}
// Delete the existing Faust module
freeDSPFactory();
fSourceCode = sourceCode;
// Update all instances
fUpdateInstance = instance;
for (it = fInstances.begin(); it != fInstances.end(); it++)
{
(*it)->createDSP();
}
}
else
{
LOG("DSP code has not been changed...");
}
}
//void FaustgenFactory::read(File path)
//{
// char filename[MAX_FILENAME_CHARS];
// short path = 0;
// long type = 'TEXT';
// short err;
// t_filehandle fh;
//
// // No filename, so open load dialog
// if (s == gensym("")) {
// filename[0] = 0;
// if (open_dialog(filename, &path, (t_fourcc*)&type, (t_fourcc*)&type, 1)) {
// LOG("Faust DSP file not found");
// return;
// }
// // Otherwise locate the file
// } else {
// strcpy(filename, s->s_name);
// if (locatefile_extended(filename, &path, (t_fourcc*)&type, (t_fourcc*)&type, 1)) {
// LOG("Faust DSP file '%s' not found", filename);
// return;
// }
// }
//
// // File found, open it and recompile DSP
// err = path_opensysfile(filename, path, &fh, READ_PERM);
// if (err) {
// LOG("Faust DSP file '%s' cannot be opened", filename);
// return;
// }
//
// // Delete the existing Faust module
// freeDSPFactory();
//
// // Free the memory allocated for fBitCode
// free_bitcode();
//
// err = sysfile_readtextfile(fh, fSourceCode, 0, (t_sysfile_text_flags)(TEXT_LB_UNIX | TEXT_NULL_TERMINATE));
// if (err) {
// LOG("Faust DSP file '%s' cannot be read", filename);
// }
//
// sysfile_close(fh);
// fSourceCodeSize = sysmem_handlesize(fSourceCode);
//
// // Add DSP file enclosing folder pathname in the '-I' list
// char full_path[MAX_FILENAME_CHARS];
// if (path_topathname(path, filename, full_path) == 0) {
// addLibraryPath(getFolderFromFilename(full_path));
// }
//
// // Update all instances
// set<faustgen*>::const_iterator it;
// for (it = fInstances.begin(); it != fInstances.end(); it++) {
// (*it)->updateSourcecode();
// }
//}
//void FaustgenFactory::write(File path)
//{
// char filename[MAX_FILENAME_CHARS];
// short path = 0;
// long type = 'TEXT';
// short err;
// t_filehandle fh;
//
// // No filename, so open save dialog
// if (s == gensym("")) {
// filename[0] = 0;
// if (saveas_dialog(filename, &path, NULL)) {
// LOG("Faust DSP file not found");
// return;
// } else {
// err = path_createsysfile(filename, path, type, &fh);
// if (err) {
// LOG("Faust DSP file '%s' cannot be created", filename);
// return;
// }
// }
// // Otherwise locate or create the file
// } else {
// strcpy(filename, s->s_name);
// if (locatefile_extended(filename, &path, (t_fourcc*)&type, (t_fourcc*)&type, 1)) {
// LOG("Faust DSP file '%s' not found, so tries to create it", filename);
// err = path_createsysfile(filename, path, type, &fh);
// if (err) {
// LOG("Faust DSP file '%s' cannot be created", filename);
// return;
// }
// } else {
// err = path_opensysfile(filename, path, &fh, WRITE_PERM);
// if (err) {
// LOG("Faust DSP file '%s' cannot be opened", filename);
// return;
// }
// }
// }
//
// err = sysfile_writetextfile(fh, fSourceCode, (t_sysfile_text_flags)(TEXT_LB_UNIX | TEXT_NULL_TERMINATE));
// if (err) {
// LOG("Faust DSP file '%s' cannot be written", filename);
// }
// sysfile_close(fh);
//}