1 /************************************************************************
2  ************************************************************************
3     FAUST compiler
4     Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
5     ---------------------------------------------------------------------
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  ************************************************************************
20  ************************************************************************/
21 
22 #include <libgen.h>
23 
24 #include "Text.hh"
25 #include "interpreter_dynamic_dsp_aux.hh"
26 #include "libfaust.h"
27 #include "lock_api.hh"
28 
29 using namespace std;
30 
createInterpreterDSPFactoryFromFile(const string & filename,int argc,const char * argv[],string & error_msg)31 EXPORT interpreter_dsp_factory* createInterpreterDSPFactoryFromFile(const string& filename, int argc,
32                                                                     const char* argv[], string& error_msg)
33 {
34     string base = basename((char*)filename.c_str());
35     size_t pos  = filename.find(".dsp");
36 
37     if (pos != string::npos) {
38         return createInterpreterDSPFactoryFromString(base.substr(0, pos), pathToContent(filename), argc, argv,
39                                                      error_msg);
40     } else {
41         error_msg = "File Extension is not the one expected (.dsp expected)\n";
42         return nullptr;
43     }
44 }
45 
createInterpreterDSPFactoryFromString(const string & name_app,const string & dsp_content,int argc,const char * argv[],string & error_msg)46 EXPORT interpreter_dsp_factory* createInterpreterDSPFactoryFromString(const string& name_app, const string& dsp_content,
47                                                                       int argc, const char* argv[], string& error_msg)
48 {
49     LOCK_API
50     string expanded_dsp_content, sha_key;
51 
52     if ((expanded_dsp_content = sha1FromDSP(name_app, dsp_content, argc, argv, sha_key)) == "") {
53         return nullptr;
54     } else {
55 
56         dsp_factory_table<SDsp_factory>::factory_iterator it;
57         if (gInterpreterFactoryTable.getFactory(sha_key, it)) {
58             SDsp_factory sfactory = (*it).first;
59             sfactory->addReference();
60             return sfactory;
61         } else {
62             try {
63                 int         argc1 = 0;
64                 const char* argv1[64];
65                 argv1[argc1++] = "faust";
66                 argv1[argc1++] = "-lang";
67                 argv1[argc1++] = "interp";
68                 argv1[argc1++] = "-o";
69                 argv1[argc1++] = "string";
70                 // Copy arguments
71                 for (int i = 0; i < argc; i++) {
72                     argv1[argc1++] = argv[i];
73                 }
74                 argv1[argc1] = nullptr;  // NULL terminated argv
75 
76                 dsp_factory_base* dsp_factory_aux = createFactory(name_app.c_str(), dsp_content.c_str(), argc1, argv1, error_msg, true);
77                 if (dsp_factory_aux) {
78                     dsp_factory_aux->setName(name_app);
79                     interpreter_dsp_factory* factory = new interpreter_dsp_factory(dsp_factory_aux);
80                     gInterpreterFactoryTable.setFactory(factory);
81                     factory->setSHAKey(sha_key);
82                     factory->setDSPCode(expanded_dsp_content);
83                     return factory;
84                 } else {
85                     return nullptr;
86                 }
87             } catch (faustexception& e) {
88                 error_msg = e.what();
89                 return nullptr;
90             }
91         }
92     }
93 }
94 
createInterpreterDSPFactoryFromSignals(const std::string & name_app,tvec signals,int argc,const char * argv[],std::string & error_msg)95 EXPORT interpreter_dsp_factory* createInterpreterDSPFactoryFromSignals(const std::string& name_app, tvec signals,
96                                                                        int argc, const char* argv[], std::string& error_msg)
97 {
98     LOCK_API
99     try {
100         int         argc1 = 0;
101         const char* argv1[64];
102         argv1[argc1++] = "faust";
103         argv1[argc1++] = "-lang";
104         argv1[argc1++] = "interp";
105         argv1[argc1++] = "-o";
106         argv1[argc1++] = "string";
107         // Copy arguments
108         for (int i = 0; i < argc; i++) {
109             argv1[argc1++] = argv[i];
110         }
111         argv1[argc1] = nullptr;  // NULL terminated argv
112 
113         dsp_factory_base* dsp_factory_aux = createFactory(name_app.c_str(), signals, argc1, argv1, error_msg);
114         if (dsp_factory_aux) {
115             dsp_factory_aux->setName(name_app);
116             interpreter_dsp_factory* factory = new interpreter_dsp_factory(dsp_factory_aux);
117             gInterpreterFactoryTable.setFactory(factory);
118             return factory;
119         } else {
120             return nullptr;
121         }
122     } catch (faustexception& e) {
123         error_msg = e.what();
124         return nullptr;
125     }
126 }
127 
createInterpreterDSPFactoryFromBoxes(const std::string & name_app,Tree box,int argc,const char * argv[],std::string & error_msg)128 EXPORT interpreter_dsp_factory* createInterpreterDSPFactoryFromBoxes(const std::string& name_app, Tree box,
129                                                                      int argc, const char* argv[],
130                                                                      std::string& error_msg)
131 {
132     LOCK_API
133     try {
134         tvec signals = boxesToSignalsAux(box);
135         return createInterpreterDSPFactoryFromSignals(name_app, signals, argc, argv, error_msg);
136     } catch (faustexception& e) {
137         error_msg = e.Message();
138         return nullptr;
139     }
140 }
141