1 /************************************************************************
2  ************************************************************************
3     FAUST compiler
4     Copyright (C) 2003-2014 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 #ifndef WASM_DYNAMIC_DSP_AUX_H
23 #define WASM_DYNAMIC_DSP_AUX_H
24 
25 #include <cstdlib>
26 #include <string>
27 #include <vector>
28 
29 #include "wasm_dsp_aux.hh"
30 
31 class EXPORT wasm_dynamic_dsp_factory : public wasm_dsp_factory {
32    protected:
33    public:
wasm_dynamic_dsp_factory()34     wasm_dynamic_dsp_factory() {}
wasm_dynamic_dsp_factory(dsp_factory_base * factory)35     wasm_dynamic_dsp_factory(dsp_factory_base* factory) : wasm_dsp_factory(factory) {}
36 
~wasm_dynamic_dsp_factory()37     virtual ~wasm_dynamic_dsp_factory() {}
38 
39     static wasm_dsp_factory* createWasmDSPFactoryFromString2(const std::string& name_app,
40                                                              const std::string& dsp_content,
41                                                              const std::vector<std::string>& argv,
42                                                              bool internal_memory);
43 
44     static std::string generateWasmFromString2(const std::string& name_app,
45                                                 const std::string& dsp_content,
46                                                 const std::vector<std::string>& argv,
47                                                 bool internal_memory);
48 };
49 
50 EXPORT wasm_dsp_factory* createWasmDSPFactoryFromFile(const std::string& filename, int argc, const char* argv[],
51                                                       std::string& error_msg, bool internal_memory);
52 
53 EXPORT wasm_dsp_factory* createWasmDSPFactoryFromString(const std::string& name_app, const std::string& dsp_content,
54                                                         int argc, const char* argv[], std::string& error_msg,
55                                                         bool internal_memory);
56 
57 EXPORT wasm_dsp_factory* createWasmDSPFactoryFromSignals(const std::string& name_app, tvec signals,
58                                                         int argc, const char* argv[], std::string& error_msg,
59                                                         bool internal_memory);
60 
61 EXPORT std::string generateWasmFromString(const std::string& name_app, const std::string& dsp_content,
62                                           int argc, const char* argv[], std::string& error_msg,
63                                           bool internal_memory);
64 
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
69 EXPORT wasm_dsp_factory* createWasmCDSPFactoryFromFile2(const char* filename, int argc, const char* argv[],
70                                                         char* error_msg, bool internal_memory);
71 
72 EXPORT wasm_dsp_factory* createWasmCDSPFactoryFromString2(const char* name_app, const char* dsp_content, int argc,
73                                                           const char* argv[], char* error_msg, bool internal_memory);
74 
75 EXPORT wasm_dsp_factory* createWasmCDSPFactoryFromSignals2(const char* name_app, tvec signals,
76                                                            int argc, const char* argv[],
77                                                            char* error_msg,
78                                                            bool internal_memory);
79 
80 EXPORT bool deleteWasmCDSPFactory(wasm_dsp_factory* factory);
81 
82 EXPORT wasm_dsp_factory* readWasmCDSPFactoryFromMachine(const char* wasm_code, char* error_msg);
83 
84 EXPORT char* writeWasmCDSPFactoryToMachine(wasm_dsp_factory* factory);
85 
86 EXPORT wasm_dsp_factory* readWasmCDSPFactoryFromMachineFile(const char* wasm_code_path, char* error_msg);
87 
88 EXPORT void writeWasmCDSPFactoryToMachineFile(wasm_dsp_factory* factory, const char* wasm_code_path);
89 
90 /*
91  Contains WASM code to be exchanged with the JS side.
92 */
93 typedef struct {
94     char*       fWASMCode;
95     int         fWASMCodeSize;
96     const char* fJSHelpers;
97 } WasmModule;
98 
99 /**
100  * Create a Faust DSP WebAssembly module and additional helper functions from a DSP source code as a file.
101  *
102  * @param filename - the DSP filename
103  * @param argc - the number of parameters in argv array
104  * @param argv - the array of parameters
105  * @param error_msg - the error string to be filled, has to be 4096 characters long
106  * @param internal_memory - whether the memory is allocated inside the module (= faster DSP fields access) or from the
107  * JS context
108  *
109  * @return a valid WebAssembly module and additional helper functions as a WasmRes struct on success (to be deleted by
110  * the caller), otherwise a null pointer.
111  */
112 EXPORT WasmModule* createWasmCDSPFactoryFromFile(const char* filename, int argc, const char* argv[], char* error_msg,
113                                                  bool internal_memory);
114 
115 /**
116  * Create a Faust DSP WebAssembly module and additional helper functions from a DSP source code as a string.
117  *
118  * @param name_app - the name of the Faust program
119  * @param dsp_content - the Faust program as a string
120  * @param argc - the number of parameters in argv array
121  * @param argv - the array of parameters
122  * @param error_msg - the error string to be filled, has to be 4096 characters long
123  * @param internal_memory - whether the memory is allocated inside the module (= faster DSP fields access) or from the
124  * JS context
125  *
126  * @return a valid WebAssembly module and additional helper functions as a WasmRes struct on success (to be deleted by
127  * the caller), otherwise a null pointer.
128  */
129 EXPORT WasmModule* createWasmCDSPFactoryFromString(const char* name_app, const char* dsp_content, int argc,
130                                                    const char* argv[], char* error_msg, bool internal_memory);
131 
132 /**
133  * Get the WebAssembly module from the WasmRes structure.
134  *
135  * @param module - the WebAssembly module
136  *
137  * @return the WebAssembly module as an array of bytes.
138  */
139 EXPORT const char* getWasmCModule(WasmModule* module);
140 
141 /**
142  * Get the WebAssembly module size.
143  *
144  * @param module - the WebAssembly module
145  *
146  * @return the WebAssembly module size.
147  */
148 EXPORT int getWasmCModuleSize(WasmModule* module);
149 
150 /**
151  * Get the additional helper functions module from the WasmRes structure.
152  *
153  * @param module - the WebAssembly module
154  *
155  * @return the additional helper functions as a string.
156  */
157 EXPORT const char* getWasmCHelpers(WasmModule* module);
158 
159 /**
160  * The free function to be used on memory returned by createWasmCDSPFactoryFromString.
161  *
162  * @param module - the WebAssembly module
163  *
164  * @param ptr - the WasmRes structure to be deleted.
165  */
166 EXPORT void freeWasmCModule(WasmModule* module);
167 
168 /**
169  * Get the error message after an exception occured.
170  *
171  * @return the error as a static string.
172  */
173 EXPORT const char* getErrorAfterException();
174 
175 /**
176  *  Cleanup library global context after an exception occured.
177  *
178  */
179 EXPORT void cleanupAfterException();
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif
186