27#include "faust/dsp/interpreter-dsp.h"
38 std::mutex& GetCompileMutex()
40 static std::mutex sMutex;
44 std::filesystem::path ToPath(
const std::string& utf8Path)
47 return std::filesystem::u8path(utf8Path);
49 return std::filesystem::path(utf8Path);
53 std::string ToUTF8(
const std::filesystem::path& path)
55 const auto str = path.u8string();
56 return std::string(str.begin(), str.end());
59 void LoadFaustLibrary()
61#if defined OS_WIN && defined FAUST_DLL_PATH
63 static HMODULE sModule = LoadLibraryExA(FAUST_DLL_PATH, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
64 assert(sModule &&
"Could not load faust.dll");
69FaustGen::FaustGen(
const char* name,
const char* inputDSPFile,
int nVoices,
const char* libraryPath)
70: IPlugFaust(name, nVoices)
74 if (CStringHasContents(libraryPath))
77 if (CStringHasContents(inputDSPFile))
79 mDSPFile = inputDSPFile;
83 if (
sizeof(FAUSTFLOAT) ==
sizeof(
double))
84 mCompileOptions.push_back(
"-double");
96 std::lock_guard<std::mutex> lock(GetCompileMutex());
97 deleteInterpreterDSPFactory(mFactory);
103 bool success =
false;
107 std::string sourceCode;
109 if (ReadFile(sourceCode))
110 success = Compile(sourceCode);
112 mErrorMessage =
"Could not read " + mDSPFile;
114 else if (mSourceCode.size())
116 success = Compile(mSourceCode);
121 DBGMSG(
"FaustGen-%s: %s\n", mName.Get(), mErrorMessage.c_str());
133 std::string sourceCode;
135 if (!ReadFile(sourceCode))
137 mErrorMessage =
"Could not read " + mDSPFile;
141 const bool success = Compile(sourceCode);
150 const bool success = Compile(sourceCode);
157void FaustGen::OnCompiled(
bool success)
160 mOnCompileFunc(success);
167 if (CStringHasContents(path) && std::find(mLibraryPaths.begin(), mLibraryPaths.end(), path) == mLibraryPaths.end())
168 mLibraryPaths.push_back(path);
174 const auto faustDir = ToPath(resourcesPath) /
"faust";
176 if (!CStringHasContents(resourcesPath) || !std::filesystem::is_directory(faustDir, ec))
178 DBGMSG(
"FaustGen-%s: no bundled Faust files in %s\n", mName.Get(), resourcesPath);
182 const std::string faustDirStr = ToUTF8(faustDir);
183 mLibraryPaths.erase(std::remove(mLibraryPaths.begin(), mLibraryPaths.end(), faustDirStr), mLibraryPaths.end());
184 mLibraryPaths.insert(mLibraryPaths.begin(), faustDirStr);
186 if (mDSPFile.size() && !std::filesystem::exists(ToPath(mDSPFile), ec))
188 const auto bundledFile = faustDir / ToPath(mDSPFile).filename();
190 if (std::filesystem::exists(bundledFile, ec))
192 DBGMSG(
"FaustGen-%s: using bundled %s\n", mName.Get(), ToUTF8(bundledFile).c_str());
193 mDSPFile = ToUTF8(bundledFile);
200 if (CStringHasContents(option))
201 mCompileOptions.push_back(option);
206 if (enable && !mFileWatchTimer && mDSPFile.size())
208 mFileWatchTimer = std::unique_ptr<Timer>(
Timer::Create(std::bind(&FaustGen::OnTimer,
this, std::placeholders::_1), FAUST_RECOMPILE_INTERVAL));
210 else if (!enable && mFileWatchTimer)
212 mFileWatchTimer->Stop();
213 mFileWatchTimer =
nullptr;
217bool FaustGen::Compile(
const std::string& sourceCode)
219 std::vector<std::string> args = mCompileOptions;
221 for (
const auto& path : mLibraryPaths)
223 args.push_back(
"-I");
224 args.push_back(path);
227 std::vector<const char*> argv;
229 for (
const auto& arg : args)
230 argv.push_back(arg.c_str());
232 argv.push_back(
nullptr);
235 interpreter_dsp_factory* pFactory =
nullptr;
236 ::dsp* pDSP =
nullptr;
239 std::lock_guard<std::mutex> lock(GetCompileMutex());
240 pFactory = createInterpreterDSPFactoryFromString(mName.Get(), sourceCode,
static_cast<int>(args.size()), argv.data(), error);
243 pDSP = pFactory->createDSPInstance();
246 if (!pFactory || !pDSP)
248 mErrorMessage = error.size() ? error :
"Unknown error";
249 DBGMSG(
"FaustGen-%s: compilation failed: %s\n", mName.Get(), mErrorMessage.c_str());
253 std::lock_guard<std::mutex> lock(GetCompileMutex());
254 deleteInterpreterDSPFactory(pFactory);
265 std::lock_guard<std::mutex> lock(GetCompileMutex());
266 deleteInterpreterDSPFactory(mFactory);
270 mSourceCode = sourceCode;
271 mErrorMessage.clear();
273 DBGMSG(
"FaustGen-%s: compiled, %i input(s), %i output(s), %i parameter(s)\n", mName.Get(), NInputs(), NOutputs(),
NParams());
278void FaustGen::CompileDefault()
281 const int nOutputs = GetMaxNOutputs() > -1 ? GetMaxNOutputs() : 2;
282 const int nInputs = std::min(GetMaxNInputs() > -1 ? GetMaxNInputs() : 2, nOutputs);
284 std::string code =
"process = ";
287 code +=
"par(i, " + std::to_string(nInputs) +
", _)";
289 if (nOutputs > nInputs)
290 code += std::string(nInputs > 0 ?
", " :
"") +
"par(i, " + std::to_string(nOutputs - nInputs) +
", 0)";
297 const std::string errorMessage = mErrorMessage;
299 mErrorMessage = errorMessage;
302bool FaustGen::ReadFile(std::string& contents)
305 const auto path = ToPath(mDSPFile);
306 const auto modifiedTime = std::filesystem::last_write_time(path, ec);
311 std::ifstream file(path, std::ios::in | std::ios::binary);
316 std::stringstream buffer;
317 buffer << file.rdbuf();
318 contents = buffer.str();
319 mFileModifiedTime = modifiedTime.time_since_epoch().count();
324void FaustGen::OnTimer(
Timer& timer)
327 const auto modifiedTime = std::filesystem::last_write_time(ToPath(mDSPFile), ec);
329 if (ec || modifiedTime.time_since_epoch().count() == mFileModifiedTime)
332 std::string sourceCode;
334 if (!ReadFile(sourceCode) || sourceCode == mSourceCode)
337 DBGMSG(
"FaustGen-%s: %s changed, recompiling\n", mName.Get(), mDSPFile.c_str());
339 const bool success = Compile(sourceCode);
Utility functions and macros.
void SetBundleResourcePath(const char *resourcesPath)
Use the .dsp file and Faust libraries that iplug_add_faust(... BUNDLE) copies into the bundle's resou...
void AddLibraryPath(const char *path)
Add a path that is searched for imported .lib/.dsp files.
void Init() override
Compile the DSP.
void SetAutoRecompile(bool enable)
Watch the .dsp file and recompile it when it changes.
bool LoadFile(const char *path)
Load and compile a .dsp file, replacing the current DSP if compilation succeeds.
FaustGen(const char *name, const char *inputDSPFile=nullptr, int nVoices=0, const char *libraryPath=FAUST_SHARE_PATH)
bool SetSourceCode(const char *sourceCode)
Compile Faust source code, replacing the current DSP if compilation succeeds.
void AddCompileOption(const char *option)
Add a compiler option such as "-vec" (see faust –help)
void InformHostIfParamsChanged()
If SetDSP() re-initialized the linked plug-in parameters (i.e.
void ReleaseDSP()
Destroys the DSP, waiting for a block that is being processed to finish first.
void SetDSP(::dsp *pMonoDSP)
Takes ownership of a newly created DSP, wraps it for polyphony if required, (re)builds the parameters...
static Timer * Create(ITimerFunction func, uint32_t intervalMs)
On Linux, callbacks run on a worker; returns nullptr if that worker cannot be created.