Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CarlaEngine.cpp 81KB

11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
7 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. /* TODO:
  18. * - complete processRack(): carefully add to input, sorted events
  19. * - implement processPatchbay()
  20. * - implement oscSend_control_switch_plugins()
  21. * - something about the peaks?
  22. */
  23. #include "CarlaEngineInternal.hpp"
  24. #include "CarlaPlugin.hpp"
  25. #include "CarlaBackendUtils.hpp"
  26. #include "CarlaBinaryUtils.hpp"
  27. #include "CarlaEngineUtils.hpp"
  28. #include "CarlaMathUtils.hpp"
  29. #include "CarlaPipeUtils.hpp"
  30. #include "CarlaStateUtils.hpp"
  31. #include "CarlaMIDI.h"
  32. #include "jackbridge/JackBridge.hpp"
  33. #include "water/files/File.h"
  34. #include "water/streams/MemoryOutputStream.h"
  35. #include "water/xml/XmlDocument.h"
  36. #include "water/xml/XmlElement.h"
  37. // FIXME Remove on 2.1 release
  38. #include "lv2/atom.h"
  39. using water::Array;
  40. using water::CharPointer_UTF8;
  41. using water::File;
  42. using water::MemoryOutputStream;
  43. using water::String;
  44. using water::StringArray;
  45. using water::XmlDocument;
  46. using water::XmlElement;
  47. CARLA_BACKEND_START_NAMESPACE
  48. // -----------------------------------------------------------------------
  49. // Carla Engine
  50. CarlaEngine::CarlaEngine()
  51. : pData(new ProtectedData(this))
  52. {
  53. carla_debug("CarlaEngine::CarlaEngine()");
  54. }
  55. CarlaEngine::~CarlaEngine()
  56. {
  57. carla_debug("CarlaEngine::~CarlaEngine()");
  58. delete pData;
  59. }
  60. // -----------------------------------------------------------------------
  61. // Static calls
  62. uint CarlaEngine::getDriverCount()
  63. {
  64. carla_debug("CarlaEngine::getDriverCount()");
  65. uint count = 0;
  66. if (jackbridge_is_ok())
  67. count += 1;
  68. #ifndef BUILD_BRIDGE
  69. count += getRtAudioApiCount();
  70. #endif
  71. return count;
  72. }
  73. const char* CarlaEngine::getDriverName(const uint index2)
  74. {
  75. carla_debug("CarlaEngine::getDriverName(%i)", index2);
  76. uint index(index2);
  77. if (jackbridge_is_ok() && index-- == 0)
  78. return "JACK";
  79. #ifndef BUILD_BRIDGE
  80. if (const uint count = getRtAudioApiCount())
  81. {
  82. if (index < count)
  83. return getRtAudioApiName(index);
  84. }
  85. #endif
  86. carla_stderr("CarlaEngine::getDriverName(%i) - invalid index", index2);
  87. return nullptr;
  88. }
  89. const char* const* CarlaEngine::getDriverDeviceNames(const uint index2)
  90. {
  91. carla_debug("CarlaEngine::getDriverDeviceNames(%i)", index2);
  92. uint index(index2);
  93. if (jackbridge_is_ok() && index-- == 0)
  94. {
  95. static const char* ret[3] = { "Auto-Connect ON", "Auto-Connect OFF", nullptr };
  96. return ret;
  97. }
  98. #ifndef BUILD_BRIDGE
  99. if (const uint count = getRtAudioApiCount())
  100. {
  101. if (index < count)
  102. return getRtAudioApiDeviceNames(index);
  103. }
  104. #endif
  105. carla_stderr("CarlaEngine::getDriverDeviceNames(%i) - invalid index", index2);
  106. return nullptr;
  107. }
  108. const EngineDriverDeviceInfo* CarlaEngine::getDriverDeviceInfo(const uint index2, const char* const deviceName)
  109. {
  110. carla_debug("CarlaEngine::getDriverDeviceInfo(%i, \"%s\")", index2, deviceName);
  111. uint index(index2);
  112. if (jackbridge_is_ok() && index-- == 0)
  113. {
  114. static EngineDriverDeviceInfo devInfo;
  115. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE;
  116. devInfo.bufferSizes = nullptr;
  117. devInfo.sampleRates = nullptr;
  118. return &devInfo;
  119. }
  120. #ifndef BUILD_BRIDGE
  121. if (const uint count = getRtAudioApiCount())
  122. {
  123. if (index < count)
  124. return getRtAudioDeviceInfo(index, deviceName);
  125. }
  126. #endif
  127. carla_stderr("CarlaEngine::getDriverDeviceNames(%i, \"%s\") - invalid index", index2, deviceName);
  128. return nullptr;
  129. }
  130. CarlaEngine* CarlaEngine::newDriverByName(const char* const driverName)
  131. {
  132. CARLA_SAFE_ASSERT_RETURN(driverName != nullptr && driverName[0] != '\0', nullptr);
  133. carla_debug("CarlaEngine::newDriverByName(\"%s\")", driverName);
  134. if (std::strcmp(driverName, "JACK") == 0)
  135. return newJack();
  136. #ifndef BUILD_BRIDGE
  137. // -------------------------------------------------------------------
  138. // common
  139. if (std::strcmp(driverName, "Dummy") == 0)
  140. return newRtAudio(AUDIO_API_NULL);
  141. if (std::strncmp(driverName, "JACK ", 5) == 0)
  142. return newRtAudio(AUDIO_API_JACK);
  143. if (std::strcmp(driverName, "OSS") == 0)
  144. return newRtAudio(AUDIO_API_OSS);
  145. // -------------------------------------------------------------------
  146. // linux
  147. if (std::strcmp(driverName, "ALSA") == 0)
  148. return newRtAudio(AUDIO_API_ALSA);
  149. if (std::strcmp(driverName, "PulseAudio") == 0)
  150. return newRtAudio(AUDIO_API_PULSEAUDIO);
  151. // -------------------------------------------------------------------
  152. // macos
  153. if (std::strcmp(driverName, "CoreAudio") == 0)
  154. return newRtAudio(AUDIO_API_COREAUDIO);
  155. // -------------------------------------------------------------------
  156. // windows
  157. if (std::strcmp(driverName, "ASIO") == 0)
  158. return newRtAudio(AUDIO_API_ASIO);
  159. if (std::strcmp(driverName, "DirectSound") == 0)
  160. return newRtAudio(AUDIO_API_DIRECTSOUND);
  161. if (std::strcmp(driverName, "WASAPI") == 0)
  162. return newRtAudio(AUDIO_API_WASAPI);
  163. #endif
  164. carla_stderr("CarlaEngine::newDriverByName(\"%s\") - invalid driver name", driverName);
  165. return nullptr;
  166. }
  167. // -----------------------------------------------------------------------
  168. // Constant values
  169. uint CarlaEngine::getMaxClientNameSize() const noexcept
  170. {
  171. return STR_MAX/2;
  172. }
  173. uint CarlaEngine::getMaxPortNameSize() const noexcept
  174. {
  175. return STR_MAX;
  176. }
  177. uint CarlaEngine::getCurrentPluginCount() const noexcept
  178. {
  179. return pData->curPluginCount;
  180. }
  181. uint CarlaEngine::getMaxPluginNumber() const noexcept
  182. {
  183. return pData->maxPluginNumber;
  184. }
  185. // -----------------------------------------------------------------------
  186. // Virtual, per-engine type calls
  187. bool CarlaEngine::close()
  188. {
  189. carla_debug("CarlaEngine::close()");
  190. if (pData->curPluginCount != 0)
  191. {
  192. pData->aboutToClose = true;
  193. removeAllPlugins();
  194. }
  195. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  196. if (pData->osc.isControlRegistered())
  197. oscSend_control_exit();
  198. #endif
  199. pData->close();
  200. callback(ENGINE_CALLBACK_ENGINE_STOPPED, 0, 0, 0, 0.0f, nullptr);
  201. return true;
  202. }
  203. bool CarlaEngine::usesConstantBufferSize() const noexcept
  204. {
  205. return true;
  206. }
  207. void CarlaEngine::idle() noexcept
  208. {
  209. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull,);
  210. CARLA_SAFE_ASSERT_RETURN(pData->nextPluginId == pData->maxPluginNumber,);
  211. CARLA_SAFE_ASSERT_RETURN(getType() != kEngineTypePlugin,);
  212. for (uint i=0; i < pData->curPluginCount; ++i)
  213. {
  214. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  215. if (plugin != nullptr && plugin->isEnabled())
  216. {
  217. const uint hints(plugin->getHints());
  218. if ((hints & PLUGIN_HAS_CUSTOM_UI) != 0 && (hints & PLUGIN_NEEDS_UI_MAIN_THREAD) != 0)
  219. {
  220. try {
  221. plugin->uiIdle();
  222. } CARLA_SAFE_EXCEPTION_CONTINUE("Plugin uiIdle");
  223. }
  224. }
  225. }
  226. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  227. pData->osc.idle();
  228. #endif
  229. }
  230. CarlaEngineClient* CarlaEngine::addClient(CarlaPlugin* const)
  231. {
  232. return new CarlaEngineClient(*this);
  233. }
  234. // -----------------------------------------------------------------------
  235. // Plugin management
  236. bool CarlaEngine::addPlugin(const BinaryType btype, const PluginType ptype,
  237. const char* const filename, const char* const name, const char* const label, const int64_t uniqueId,
  238. const void* const extra, const uint options)
  239. {
  240. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  241. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  242. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  243. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId <= pData->maxPluginNumber, "Invalid engine internal data");
  244. #endif
  245. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  246. CARLA_SAFE_ASSERT_RETURN_ERR(btype != BINARY_NONE, "Invalid plugin binary mode");
  247. CARLA_SAFE_ASSERT_RETURN_ERR(ptype != PLUGIN_NONE, "Invalid plugin type");
  248. CARLA_SAFE_ASSERT_RETURN_ERR((filename != nullptr && filename[0] != '\0') || (label != nullptr && label[0] != '\0'), "Invalid plugin filename and label");
  249. carla_debug("CarlaEngine::addPlugin(%i:%s, %i:%s, \"%s\", \"%s\", \"%s\", " P_INT64 ", %p, %u)", btype, BinaryType2Str(btype), ptype, PluginType2Str(ptype), filename, name, label, uniqueId, extra, options);
  250. uint id;
  251. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  252. CarlaPlugin* oldPlugin = nullptr;
  253. if (pData->nextPluginId < pData->curPluginCount)
  254. {
  255. id = pData->nextPluginId;
  256. pData->nextPluginId = pData->maxPluginNumber;
  257. oldPlugin = pData->plugins[id].plugin;
  258. CARLA_SAFE_ASSERT_RETURN_ERR(oldPlugin != nullptr, "Invalid replace plugin Id");
  259. }
  260. else
  261. #endif
  262. {
  263. id = pData->curPluginCount;
  264. if (id == pData->maxPluginNumber)
  265. {
  266. setLastError("Maximum number of plugins reached");
  267. return false;
  268. }
  269. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  270. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins[id].plugin == nullptr, "Invalid engine internal data");
  271. #endif
  272. }
  273. CarlaPlugin::Initializer initializer = {
  274. this,
  275. id,
  276. filename,
  277. name,
  278. label,
  279. uniqueId,
  280. options
  281. };
  282. CarlaPlugin* plugin = nullptr;
  283. CarlaString bridgeBinary(pData->options.binaryDir);
  284. if (bridgeBinary.isNotEmpty())
  285. {
  286. #ifndef CARLA_OS_WIN
  287. if (btype == BINARY_NATIVE)
  288. {
  289. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-native";
  290. }
  291. else
  292. #endif
  293. {
  294. switch (btype)
  295. {
  296. case BINARY_POSIX32:
  297. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-posix32";
  298. break;
  299. case BINARY_POSIX64:
  300. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-posix64";
  301. break;
  302. case BINARY_WIN32:
  303. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-win32.exe";
  304. break;
  305. case BINARY_WIN64:
  306. bridgeBinary += CARLA_OS_SEP_STR "carla-bridge-win64.exe";
  307. break;
  308. default:
  309. bridgeBinary.clear();
  310. break;
  311. }
  312. }
  313. if (! File(bridgeBinary.buffer()).existsAsFile())
  314. bridgeBinary.clear();
  315. }
  316. // Prefer bridges for some specific plugins
  317. bool preferBridges = pData->options.preferPluginBridges;
  318. #ifndef BUILD_BRIDGE
  319. if (! preferBridges)
  320. {
  321. if (ptype == PLUGIN_LV2 && label != nullptr)
  322. {
  323. if (std::strncmp(label, "http://calf.sourceforge.net/plugins/", 36) == 0 ||
  324. std::strcmp(label, "http://factorial.hu/plugins/lv2/ir") == 0 ||
  325. std::strstr(label, "v1.sourceforge.net/lv2") != nullptr)
  326. {
  327. preferBridges = true;
  328. }
  329. }
  330. }
  331. #endif // ! BUILD_BRIDGE
  332. const bool canBeBridged = ptype != PLUGIN_INTERNAL
  333. && ptype != PLUGIN_SF2
  334. && ptype != PLUGIN_SFZ
  335. && ptype != PLUGIN_JACK;
  336. if (canBeBridged && (btype != BINARY_NATIVE || (preferBridges && bridgeBinary.isNotEmpty())))
  337. {
  338. if (bridgeBinary.isNotEmpty())
  339. {
  340. plugin = CarlaPlugin::newBridge(initializer, btype, ptype, bridgeBinary);
  341. }
  342. else
  343. {
  344. setLastError("This Carla build cannot handle this binary");
  345. return false;
  346. }
  347. }
  348. else
  349. {
  350. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  351. bool use16Outs;
  352. #endif
  353. setLastError("Invalid or unsupported plugin type");
  354. switch (ptype)
  355. {
  356. case PLUGIN_NONE:
  357. break;
  358. case PLUGIN_LADSPA:
  359. plugin = CarlaPlugin::newLADSPA(initializer, (const LADSPA_RDF_Descriptor*)extra);
  360. break;
  361. case PLUGIN_DSSI:
  362. plugin = CarlaPlugin::newDSSI(initializer);
  363. break;
  364. case PLUGIN_LV2:
  365. plugin = CarlaPlugin::newLV2(initializer);
  366. break;
  367. case PLUGIN_VST2:
  368. plugin = CarlaPlugin::newVST2(initializer);
  369. break;
  370. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  371. case PLUGIN_INTERNAL:
  372. plugin = CarlaPlugin::newNative(initializer);
  373. break;
  374. case PLUGIN_SF2:
  375. use16Outs = (extra != nullptr && std::strcmp((const char*)extra, "true") == 0);
  376. plugin = CarlaPlugin::newFluidSynth(initializer, use16Outs);
  377. break;
  378. case PLUGIN_SFZ:
  379. plugin = CarlaPlugin::newSFZero(initializer);
  380. break;
  381. case PLUGIN_JACK:
  382. plugin = CarlaPlugin::newJackApp(initializer);
  383. break;
  384. #else
  385. case PLUGIN_INTERNAL:
  386. case PLUGIN_SF2:
  387. case PLUGIN_SFZ:
  388. case PLUGIN_JACK:
  389. setLastError("Plugin bridges cannot handle this binary");
  390. break;
  391. #endif
  392. }
  393. }
  394. if (plugin == nullptr)
  395. return false;
  396. plugin->reload();
  397. bool canRun = true;
  398. /**/ if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  399. {
  400. if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  401. {
  402. setLastError("Carla's rack mode cannot work with plugins that have CV ports, sorry!");
  403. canRun = false;
  404. }
  405. }
  406. else if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  407. {
  408. /**/ if (plugin->getMidiInCount() > 1 || plugin->getMidiOutCount() > 1)
  409. {
  410. setLastError("Carla's patchbay mode cannot work with plugins that have multiple MIDI ports, sorry!");
  411. canRun = false;
  412. }
  413. else if (plugin->getCVInCount() > 0 || plugin->getCVInCount() > 0)
  414. {
  415. setLastError("CV ports in patchbay mode is still TODO");
  416. canRun = false;
  417. }
  418. }
  419. if (! canRun)
  420. {
  421. delete plugin;
  422. return false;
  423. }
  424. EnginePluginData& pluginData(pData->plugins[id]);
  425. pluginData.plugin = plugin;
  426. pluginData.insPeak[0] = 0.0f;
  427. pluginData.insPeak[1] = 0.0f;
  428. pluginData.outsPeak[0] = 0.0f;
  429. pluginData.outsPeak[1] = 0.0f;
  430. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  431. if (oldPlugin != nullptr)
  432. {
  433. CARLA_SAFE_ASSERT(! pData->loadingProject);
  434. const ScopedThreadStopper sts(this);
  435. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  436. pData->graph.replacePlugin(oldPlugin, plugin);
  437. const bool wasActive = oldPlugin->getInternalParameterValue(PARAMETER_ACTIVE) >= 0.5f;
  438. const float oldDryWet = oldPlugin->getInternalParameterValue(PARAMETER_DRYWET);
  439. const float oldVolume = oldPlugin->getInternalParameterValue(PARAMETER_VOLUME);
  440. delete oldPlugin;
  441. if (plugin->getHints() & PLUGIN_CAN_DRYWET)
  442. plugin->setDryWet(oldDryWet, true, true);
  443. if (plugin->getHints() & PLUGIN_CAN_VOLUME)
  444. plugin->setVolume(oldVolume, true, true);
  445. plugin->setActive(wasActive, true, true);
  446. plugin->setEnabled(true);
  447. callback(ENGINE_CALLBACK_RELOAD_ALL, id, 0, 0, 0.0f, nullptr);
  448. }
  449. else if (! pData->loadingProject)
  450. #endif
  451. {
  452. plugin->setEnabled(true);
  453. ++pData->curPluginCount;
  454. callback(ENGINE_CALLBACK_PLUGIN_ADDED, id, 0, 0, 0.0f, plugin->getName());
  455. if (getType() != kEngineTypeBridge)
  456. plugin->setActive(true, false, true);
  457. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  458. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  459. pData->graph.addPlugin(plugin);
  460. #endif
  461. }
  462. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  463. plugin->registerToOscClient();
  464. #endif
  465. return true;
  466. }
  467. bool CarlaEngine::addPlugin(const PluginType ptype, const char* const filename, const char* const name, const char* const label, const int64_t uniqueId, const void* const extra)
  468. {
  469. return addPlugin(BINARY_NATIVE, ptype, filename, name, label, uniqueId, extra, 0x0);
  470. }
  471. bool CarlaEngine::removePlugin(const uint id)
  472. {
  473. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  474. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  475. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  476. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  477. #endif
  478. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  479. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  480. carla_debug("CarlaEngine::removePlugin(%i)", id);
  481. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  482. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to remove");
  483. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  484. const ScopedThreadStopper sts(this);
  485. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  486. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  487. pData->graph.removePlugin(plugin);
  488. const ScopedActionLock sal(this, kEnginePostActionRemovePlugin, id, 0);
  489. /*
  490. for (uint i=id; i < pData->curPluginCount; ++i)
  491. {
  492. CarlaPlugin* const plugin2(pData->plugins[i].plugin);
  493. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  494. plugin2->updateOscURL();
  495. }
  496. */
  497. # if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  498. if (isOscControlRegistered())
  499. oscSend_control_remove_plugin(id);
  500. # endif
  501. #else
  502. pData->curPluginCount = 0;
  503. carla_zeroStructs(pData->plugins, 1);
  504. #endif
  505. delete plugin;
  506. callback(ENGINE_CALLBACK_PLUGIN_REMOVED, id, 0, 0, 0.0f, nullptr);
  507. return true;
  508. }
  509. bool CarlaEngine::removeAllPlugins()
  510. {
  511. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  512. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  513. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  514. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextPluginId == pData->maxPluginNumber, "Invalid engine internal data");
  515. #endif
  516. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  517. carla_debug("CarlaEngine::removeAllPlugins()");
  518. if (pData->curPluginCount == 0)
  519. return true;
  520. const ScopedThreadStopper sts(this);
  521. const uint curPluginCount(pData->curPluginCount);
  522. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  523. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  524. pData->graph.removeAllPlugins();
  525. # if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  526. if (isOscControlRegistered())
  527. {
  528. for (uint i=0; i < curPluginCount; ++i)
  529. oscSend_control_remove_plugin(curPluginCount-i-1);
  530. }
  531. # endif
  532. #endif
  533. const ScopedActionLock sal(this, kEnginePostActionZeroCount, 0, 0);
  534. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  535. for (uint i=0; i < curPluginCount; ++i)
  536. {
  537. EnginePluginData& pluginData(pData->plugins[i]);
  538. if (pluginData.plugin != nullptr)
  539. {
  540. delete pluginData.plugin;
  541. pluginData.plugin = nullptr;
  542. }
  543. pluginData.insPeak[0] = 0.0f;
  544. pluginData.insPeak[1] = 0.0f;
  545. pluginData.outsPeak[0] = 0.0f;
  546. pluginData.outsPeak[1] = 0.0f;
  547. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  548. }
  549. return true;
  550. }
  551. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  552. const char* CarlaEngine::renamePlugin(const uint id, const char* const newName)
  553. {
  554. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  555. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  556. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  557. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  558. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  559. CARLA_SAFE_ASSERT_RETURN_ERRN(newName != nullptr && newName[0] != '\0', "Invalid plugin name");
  560. carla_debug("CarlaEngine::renamePlugin(%i, \"%s\")", id, newName);
  561. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  562. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin != nullptr, "Could not find plugin to rename");
  563. CARLA_SAFE_ASSERT_RETURN_ERRN(plugin->getId() == id, "Invalid engine internal data");
  564. const char* const uniqueName(getUniquePluginName(newName));
  565. CARLA_SAFE_ASSERT_RETURN_ERRN(uniqueName != nullptr, "Unable to get new unique plugin name");
  566. plugin->setName(uniqueName);
  567. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  568. pData->graph.renamePlugin(plugin, uniqueName);
  569. delete[] uniqueName;
  570. return plugin->getName();
  571. }
  572. bool CarlaEngine::clonePlugin(const uint id)
  573. {
  574. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  575. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  576. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  577. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  578. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  579. carla_debug("CarlaEngine::clonePlugin(%i)", id);
  580. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  581. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to clone");
  582. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  583. char label[STR_MAX+1];
  584. carla_zeroChars(label, STR_MAX+1);
  585. plugin->getLabel(label);
  586. const uint pluginCountBefore(pData->curPluginCount);
  587. if (! addPlugin(plugin->getBinaryType(), plugin->getType(),
  588. plugin->getFilename(), plugin->getName(), label, plugin->getUniqueId(),
  589. plugin->getExtraStuff(), plugin->getOptionsEnabled()))
  590. return false;
  591. CARLA_SAFE_ASSERT_RETURN_ERR(pluginCountBefore+1 == pData->curPluginCount, "No new plugin found");
  592. if (CarlaPlugin* const newPlugin = pData->plugins[pluginCountBefore].plugin)
  593. newPlugin->loadStateSave(plugin->getStateSave());
  594. return true;
  595. }
  596. bool CarlaEngine::replacePlugin(const uint id) noexcept
  597. {
  598. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  599. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  600. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount != 0, "Invalid engine internal data");
  601. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  602. carla_debug("CarlaEngine::replacePlugin(%i)", id);
  603. // might use this to reset
  604. if (id == pData->maxPluginNumber)
  605. {
  606. pData->nextPluginId = pData->maxPluginNumber;
  607. return true;
  608. }
  609. CARLA_SAFE_ASSERT_RETURN_ERR(id < pData->curPluginCount, "Invalid plugin Id");
  610. CarlaPlugin* const plugin(pData->plugins[id].plugin);
  611. CARLA_SAFE_ASSERT_RETURN_ERR(plugin != nullptr, "Could not find plugin to replace");
  612. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  613. pData->nextPluginId = id;
  614. return true;
  615. }
  616. bool CarlaEngine::switchPlugins(const uint idA, const uint idB) noexcept
  617. {
  618. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  619. CARLA_SAFE_ASSERT_RETURN_ERR(pData->plugins != nullptr, "Invalid engine internal data");
  620. CARLA_SAFE_ASSERT_RETURN_ERR(pData->curPluginCount >= 2, "Invalid engine internal data");
  621. CARLA_SAFE_ASSERT_RETURN_ERR(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  622. CARLA_SAFE_ASSERT_RETURN_ERR(idA != idB, "Invalid operation, cannot switch plugin with itself");
  623. CARLA_SAFE_ASSERT_RETURN_ERR(idA < pData->curPluginCount, "Invalid plugin Id");
  624. CARLA_SAFE_ASSERT_RETURN_ERR(idB < pData->curPluginCount, "Invalid plugin Id");
  625. carla_debug("CarlaEngine::switchPlugins(%i)", idA, idB);
  626. CarlaPlugin* const pluginA(pData->plugins[idA].plugin);
  627. CarlaPlugin* const pluginB(pData->plugins[idB].plugin);
  628. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  629. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA != nullptr, "Could not find plugin to switch");
  630. CARLA_SAFE_ASSERT_RETURN_ERR(pluginA->getId() == idA, "Invalid engine internal data");
  631. CARLA_SAFE_ASSERT_RETURN_ERR(pluginB->getId() == idB, "Invalid engine internal data");
  632. const ScopedThreadStopper sts(this);
  633. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  634. pData->graph.replacePlugin(pluginA, pluginB);
  635. const ScopedActionLock sal(this, kEnginePostActionSwitchPlugins, idA, idB);
  636. // TODO
  637. /*
  638. pluginA->updateOscURL();
  639. pluginB->updateOscURL();
  640. if (isOscControlRegistered())
  641. oscSend_control_switch_plugins(idA, idB);
  642. */
  643. return true;
  644. }
  645. #endif
  646. CarlaPlugin* CarlaEngine::getPlugin(const uint id) const noexcept
  647. {
  648. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  649. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->plugins != nullptr, "Invalid engine internal data");
  650. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->curPluginCount != 0, "Invalid engine internal data");
  651. #endif
  652. CARLA_SAFE_ASSERT_RETURN_ERRN(pData->nextAction.opcode == kEnginePostActionNull, "Invalid engine internal data");
  653. CARLA_SAFE_ASSERT_RETURN_ERRN(id < pData->curPluginCount, "Invalid plugin Id");
  654. return pData->plugins[id].plugin;
  655. }
  656. CarlaPlugin* CarlaEngine::getPluginUnchecked(const uint id) const noexcept
  657. {
  658. return pData->plugins[id].plugin;
  659. }
  660. const char* CarlaEngine::getUniquePluginName(const char* const name) const
  661. {
  662. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull, nullptr);
  663. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', nullptr);
  664. carla_debug("CarlaEngine::getUniquePluginName(\"%s\")", name);
  665. CarlaString sname;
  666. sname = name;
  667. if (sname.isEmpty())
  668. {
  669. sname = "(No name)";
  670. return sname.dup();
  671. }
  672. const std::size_t maxNameSize(carla_minConstrained<uint>(getMaxClientNameSize(), 0xff, 6U) - 6); // 6 = strlen(" (10)") + 1
  673. if (maxNameSize == 0 || ! isRunning())
  674. return sname.dup();
  675. sname.truncate(maxNameSize);
  676. sname.replace(':', '.'); // ':' is used in JACK1 to split client/port names
  677. for (uint i=0; i < pData->curPluginCount; ++i)
  678. {
  679. CARLA_SAFE_ASSERT_BREAK(pData->plugins[i].plugin != nullptr);
  680. // Check if unique name doesn't exist
  681. if (const char* const pluginName = pData->plugins[i].plugin->getName())
  682. {
  683. if (sname != pluginName)
  684. continue;
  685. }
  686. // Check if string has already been modified
  687. {
  688. const std::size_t len(sname.length());
  689. // 1 digit, ex: " (2)"
  690. if (sname[len-4] == ' ' && sname[len-3] == '(' && sname.isDigit(len-2) && sname[len-1] == ')')
  691. {
  692. const int number = sname[len-2] - '0';
  693. if (number == 9)
  694. {
  695. // next number is 10, 2 digits
  696. sname.truncate(len-4);
  697. sname += " (10)";
  698. //sname.replace(" (9)", " (10)");
  699. }
  700. else
  701. sname[len-2] = char('0' + number + 1);
  702. continue;
  703. }
  704. // 2 digits, ex: " (11)"
  705. if (sname[len-5] == ' ' && sname[len-4] == '(' && sname.isDigit(len-3) && sname.isDigit(len-2) && sname[len-1] == ')')
  706. {
  707. char n2 = sname[len-2];
  708. char n3 = sname[len-3];
  709. if (n2 == '9')
  710. {
  711. n2 = '0';
  712. n3 = static_cast<char>(n3 + 1);
  713. }
  714. else
  715. n2 = static_cast<char>(n2 + 1);
  716. sname[len-2] = n2;
  717. sname[len-3] = n3;
  718. continue;
  719. }
  720. }
  721. // Modify string if not
  722. sname += " (2)";
  723. }
  724. return sname.dup();
  725. }
  726. // -----------------------------------------------------------------------
  727. // Project management
  728. bool CarlaEngine::loadFile(const char* const filename)
  729. {
  730. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  731. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  732. carla_debug("CarlaEngine::loadFile(\"%s\")", filename);
  733. const String jfilename = String(CharPointer_UTF8(filename));
  734. File file(jfilename);
  735. CARLA_SAFE_ASSERT_RETURN_ERR(file.exists(), "Requested file does not exist or is not a readable");
  736. CarlaString baseName(file.getFileNameWithoutExtension().toRawUTF8());
  737. CarlaString extension(file.getFileExtension().replace(".","").toLowerCase().toRawUTF8());
  738. const uint curPluginId(pData->nextPluginId < pData->curPluginCount ? pData->nextPluginId : pData->curPluginCount);
  739. // -------------------------------------------------------------------
  740. // NOTE: please keep in sync with carla_get_supported_file_extensions!!
  741. if (extension == "carxp" || extension == "carxs")
  742. return loadProject(filename);
  743. // -------------------------------------------------------------------
  744. if (extension == "sf2" || extension == "sf3")
  745. return addPlugin(PLUGIN_SF2, filename, baseName, baseName, 0, nullptr);
  746. if (extension == "sfz")
  747. return addPlugin(PLUGIN_SFZ, filename, baseName, baseName, 0, nullptr);
  748. // -------------------------------------------------------------------
  749. if (
  750. #ifdef HAVE_SNDFILE
  751. extension == "aif" ||
  752. extension == "aifc" ||
  753. extension == "aiff" ||
  754. extension == "au" ||
  755. extension == "bwf" ||
  756. extension == "flac" ||
  757. extension == "htk" ||
  758. extension == "iff" ||
  759. extension == "mat4" ||
  760. extension == "mat5" ||
  761. extension == "oga" ||
  762. extension == "ogg" ||
  763. extension == "paf" ||
  764. extension == "pvf" ||
  765. extension == "pvf5" ||
  766. extension == "sd2" ||
  767. extension == "sf" ||
  768. extension == "snd" ||
  769. extension == "svx" ||
  770. extension == "vcc" ||
  771. extension == "w64" ||
  772. extension == "wav" ||
  773. extension == "xi" ||
  774. #endif
  775. #ifdef HAVE_FFMPEG
  776. extension == "3g2" ||
  777. extension == "3gp" ||
  778. extension == "aac" ||
  779. extension == "ac3" ||
  780. extension == "amr" ||
  781. extension == "ape" ||
  782. extension == "mp2" ||
  783. extension == "mp3" ||
  784. extension == "mpc" ||
  785. extension == "wma" ||
  786. # ifdef HAVE_SNDFILE
  787. // FFmpeg without sndfile
  788. extension == "flac" ||
  789. extension == "oga" ||
  790. extension == "ogg" ||
  791. extension == "w64" ||
  792. extension == "wav" ||
  793. # endif
  794. #endif
  795. false
  796. )
  797. {
  798. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "audiofile", 0, nullptr))
  799. {
  800. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  801. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  802. return true;
  803. }
  804. return false;
  805. }
  806. // -------------------------------------------------------------------
  807. if (extension == "mid" || extension == "midi")
  808. {
  809. if (addPlugin(PLUGIN_INTERNAL, nullptr, baseName, "midifile", 0, nullptr))
  810. {
  811. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  812. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "file", filename, true);
  813. return true;
  814. }
  815. return false;
  816. }
  817. // -------------------------------------------------------------------
  818. // ZynAddSubFX
  819. if (extension == "xmz" || extension == "xiz")
  820. {
  821. #ifdef HAVE_ZYN_DEPS
  822. CarlaString nicerName("Zyn - ");
  823. const std::size_t sep(baseName.find('-')+1);
  824. if (sep < baseName.length())
  825. nicerName += baseName.buffer()+sep;
  826. else
  827. nicerName += baseName;
  828. //nicerName
  829. if (addPlugin(PLUGIN_INTERNAL, nullptr, nicerName, "zynaddsubfx", 0, nullptr))
  830. {
  831. callback(ENGINE_CALLBACK_UI_STATE_CHANGED, curPluginId, 0, 0, 0.0f, nullptr);
  832. if (CarlaPlugin* const plugin = getPlugin(curPluginId))
  833. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, (extension == "xmz") ? "CarlaAlternateFile1" : "CarlaAlternateFile2", filename, true);
  834. return true;
  835. }
  836. return false;
  837. #else
  838. setLastError("This Carla build does not have ZynAddSubFX support");
  839. return false;
  840. #endif
  841. }
  842. // -------------------------------------------------------------------
  843. // Direct plugin binaries
  844. #ifdef CARLA_OS_MAC
  845. if (extension == "vst")
  846. return addPlugin(PLUGIN_VST2, filename, nullptr, nullptr, 0, nullptr);
  847. #else
  848. if (extension == "dll" || extension == "so")
  849. return addPlugin(getBinaryTypeFromFile(filename), PLUGIN_VST2, filename, nullptr, nullptr, 0, nullptr, 0x0);
  850. #endif
  851. // -------------------------------------------------------------------
  852. setLastError("Unknown file extension");
  853. return false;
  854. }
  855. bool CarlaEngine::loadProject(const char* const filename)
  856. {
  857. CARLA_SAFE_ASSERT_RETURN_ERR(pData->isIdling == 0, "An operation is still being processed, please wait for it to finish");
  858. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  859. carla_debug("CarlaEngine::loadProject(\"%s\")", filename);
  860. const String jfilename = String(CharPointer_UTF8(filename));
  861. File file(jfilename);
  862. CARLA_SAFE_ASSERT_RETURN_ERR(file.existsAsFile(), "Requested file does not exist or is not a readable file");
  863. XmlDocument xml(file);
  864. return loadProjectInternal(xml);
  865. }
  866. bool CarlaEngine::saveProject(const char* const filename)
  867. {
  868. CARLA_SAFE_ASSERT_RETURN_ERR(filename != nullptr && filename[0] != '\0', "Invalid filename");
  869. carla_debug("CarlaEngine::saveProject(\"%s\")", filename);
  870. MemoryOutputStream out;
  871. saveProjectInternal(out);
  872. const String jfilename = String(CharPointer_UTF8(filename));
  873. File file(jfilename);
  874. if (file.replaceWithData(out.getData(), out.getDataSize()))
  875. return true;
  876. setLastError("Failed to write file");
  877. return false;
  878. }
  879. // -----------------------------------------------------------------------
  880. // Information (base)
  881. uint CarlaEngine::getHints() const noexcept
  882. {
  883. return pData->hints;
  884. }
  885. uint32_t CarlaEngine::getBufferSize() const noexcept
  886. {
  887. return pData->bufferSize;
  888. }
  889. double CarlaEngine::getSampleRate() const noexcept
  890. {
  891. return pData->sampleRate;
  892. }
  893. const char* CarlaEngine::getName() const noexcept
  894. {
  895. return pData->name;
  896. }
  897. EngineProcessMode CarlaEngine::getProccessMode() const noexcept
  898. {
  899. return pData->options.processMode;
  900. }
  901. const EngineOptions& CarlaEngine::getOptions() const noexcept
  902. {
  903. return pData->options;
  904. }
  905. EngineTimeInfo CarlaEngine::getTimeInfo() const noexcept
  906. {
  907. return pData->timeInfo;
  908. }
  909. // -----------------------------------------------------------------------
  910. // Information (peaks)
  911. float CarlaEngine::getInputPeak(const uint pluginId, const bool isLeft) const noexcept
  912. {
  913. if (pluginId == MAIN_CARLA_PLUGIN_ID)
  914. {
  915. // get peak from first plugin, if available
  916. if (pData->curPluginCount > 0)
  917. return pData->plugins[0].insPeak[isLeft ? 0 : 1];
  918. return 0.0f;
  919. }
  920. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  921. return pData->plugins[pluginId].insPeak[isLeft ? 0 : 1];
  922. }
  923. float CarlaEngine::getOutputPeak(const uint pluginId, const bool isLeft) const noexcept
  924. {
  925. if (pluginId == MAIN_CARLA_PLUGIN_ID)
  926. {
  927. // get peak from last plugin, if available
  928. if (pData->curPluginCount > 0)
  929. return pData->plugins[pData->curPluginCount-1].outsPeak[isLeft ? 0 : 1];
  930. return 0.0f;
  931. }
  932. CARLA_SAFE_ASSERT_RETURN(pluginId < pData->curPluginCount, 0.0f);
  933. return pData->plugins[pluginId].outsPeak[isLeft ? 0 : 1];
  934. }
  935. // -----------------------------------------------------------------------
  936. // Callback
  937. void CarlaEngine::callback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept
  938. {
  939. #ifdef DEBUG
  940. if (action != ENGINE_CALLBACK_IDLE && action != ENGINE_CALLBACK_NOTE_ON && action != ENGINE_CALLBACK_NOTE_OFF)
  941. carla_debug("CarlaEngine::callback(%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  942. #endif
  943. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  944. if (pData->isIdling)
  945. #else
  946. if (pData->isIdling && action != ENGINE_CALLBACK_PATCHBAY_CLIENT_DATA_CHANGED)
  947. #endif
  948. {
  949. carla_stdout("callback while idling (%i:%s, %i, %i, %i, %f, \"%s\")", action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  950. }
  951. if (pData->callback != nullptr)
  952. {
  953. if (action == ENGINE_CALLBACK_IDLE)
  954. ++pData->isIdling;
  955. try {
  956. pData->callback(pData->callbackPtr, action, pluginId, value1, value2, value3, valueStr);
  957. #if defined(CARLA_OS_LINUX) && defined(__arm__)
  958. } catch (__cxxabiv1::__forced_unwind&) {
  959. carla_stderr2("Caught forced unwind exception in callback");
  960. throw;
  961. #endif
  962. } catch (...) {
  963. carla_safe_exception("callback", __FILE__, __LINE__);
  964. }
  965. if (action == ENGINE_CALLBACK_IDLE)
  966. --pData->isIdling;
  967. }
  968. }
  969. void CarlaEngine::setCallback(const EngineCallbackFunc func, void* const ptr) noexcept
  970. {
  971. carla_debug("CarlaEngine::setCallback(%p, %p)", func, ptr);
  972. pData->callback = func;
  973. pData->callbackPtr = ptr;
  974. }
  975. // -----------------------------------------------------------------------
  976. // File Callback
  977. const char* CarlaEngine::runFileCallback(const FileCallbackOpcode action, const bool isDir, const char* const title, const char* const filter) noexcept
  978. {
  979. CARLA_SAFE_ASSERT_RETURN(title != nullptr && title[0] != '\0', nullptr);
  980. CARLA_SAFE_ASSERT_RETURN(filter != nullptr, nullptr);
  981. carla_debug("CarlaEngine::runFileCallback(%i:%s, %s, \"%s\", \"%s\")", action, FileCallbackOpcode2Str(action), bool2str(isDir), title, filter);
  982. const char* ret = nullptr;
  983. if (pData->fileCallback != nullptr)
  984. {
  985. try {
  986. ret = pData->fileCallback(pData->fileCallbackPtr, action, isDir, title, filter);
  987. } CARLA_SAFE_EXCEPTION("runFileCallback");
  988. }
  989. return ret;
  990. }
  991. void CarlaEngine::setFileCallback(const FileCallbackFunc func, void* const ptr) noexcept
  992. {
  993. carla_debug("CarlaEngine::setFileCallback(%p, %p)", func, ptr);
  994. pData->fileCallback = func;
  995. pData->fileCallbackPtr = ptr;
  996. }
  997. // -----------------------------------------------------------------------
  998. // Transport
  999. void CarlaEngine::transportPlay() noexcept
  1000. {
  1001. pData->timeInfo.playing = true;
  1002. pData->time.setNeedsReset();
  1003. }
  1004. void CarlaEngine::transportPause() noexcept
  1005. {
  1006. if (pData->timeInfo.playing)
  1007. pData->time.pause();
  1008. else
  1009. pData->time.setNeedsReset();
  1010. }
  1011. void CarlaEngine::transportBPM(const double bpm) noexcept
  1012. {
  1013. try {
  1014. pData->time.setBPM(bpm);
  1015. } CARLA_SAFE_EXCEPTION("CarlaEngine::transportBPM");
  1016. }
  1017. void CarlaEngine::transportRelocate(const uint64_t frame) noexcept
  1018. {
  1019. pData->time.relocate(frame);
  1020. }
  1021. // -----------------------------------------------------------------------
  1022. // Error handling
  1023. const char* CarlaEngine::getLastError() const noexcept
  1024. {
  1025. return pData->lastError;
  1026. }
  1027. void CarlaEngine::setLastError(const char* const error) const noexcept
  1028. {
  1029. pData->lastError = error;
  1030. }
  1031. // -----------------------------------------------------------------------
  1032. // Misc
  1033. bool CarlaEngine::isAboutToClose() const noexcept
  1034. {
  1035. return pData->aboutToClose;
  1036. }
  1037. bool CarlaEngine::setAboutToClose() noexcept
  1038. {
  1039. carla_debug("CarlaEngine::setAboutToClose()");
  1040. pData->aboutToClose = true;
  1041. return (pData->isIdling == 0);
  1042. }
  1043. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1044. bool CarlaEngine::isLoadingProject() const noexcept
  1045. {
  1046. return pData->loadingProject;
  1047. }
  1048. #endif
  1049. // -----------------------------------------------------------------------
  1050. // Global options
  1051. void CarlaEngine::setOption(const EngineOption option, const int value, const char* const valueStr) noexcept
  1052. {
  1053. carla_debug("CarlaEngine::setOption(%i:%s, %i, \"%s\")", option, EngineOption2Str(option), value, valueStr);
  1054. if (isRunning())
  1055. {
  1056. switch (option)
  1057. {
  1058. case ENGINE_OPTION_PROCESS_MODE:
  1059. case ENGINE_OPTION_AUDIO_TRIPLE_BUFFER:
  1060. case ENGINE_OPTION_AUDIO_DEVICE:
  1061. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Cannot set this option while engine is running!",
  1062. option, EngineOption2Str(option), value, valueStr);
  1063. default:
  1064. break;
  1065. }
  1066. }
  1067. // do not un-force stereo for rack mode
  1068. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && option == ENGINE_OPTION_FORCE_STEREO && value != 0)
  1069. return;
  1070. switch (option)
  1071. {
  1072. case ENGINE_OPTION_DEBUG:
  1073. break;
  1074. case ENGINE_OPTION_PROCESS_MODE:
  1075. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_PROCESS_MODE_SINGLE_CLIENT && value <= ENGINE_PROCESS_MODE_BRIDGE,);
  1076. pData->options.processMode = static_cast<EngineProcessMode>(value);
  1077. break;
  1078. case ENGINE_OPTION_TRANSPORT_MODE:
  1079. CARLA_SAFE_ASSERT_RETURN(value >= ENGINE_TRANSPORT_MODE_DISABLED && value <= ENGINE_TRANSPORT_MODE_BRIDGE,);
  1080. CARLA_SAFE_ASSERT_RETURN(getType() == kEngineTypeJack || value != ENGINE_TRANSPORT_MODE_JACK,);
  1081. pData->options.transportMode = static_cast<EngineTransportMode>(value);
  1082. delete[] pData->options.transportExtra;
  1083. if (value >= ENGINE_TRANSPORT_MODE_DISABLED && valueStr != nullptr)
  1084. pData->options.transportExtra = carla_strdup_safe(valueStr);
  1085. else
  1086. pData->options.transportExtra = nullptr;
  1087. pData->time.setNeedsReset();
  1088. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  1089. // enable link now if needed
  1090. {
  1091. const bool linkEnabled = pData->options.transportExtra != nullptr && std::strstr(pData->options.transportExtra, ":link:") != nullptr;
  1092. pData->time.enableLink(linkEnabled);
  1093. }
  1094. #endif
  1095. break;
  1096. case ENGINE_OPTION_FORCE_STEREO:
  1097. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1098. pData->options.forceStereo = (value != 0);
  1099. break;
  1100. case ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  1101. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1102. CARLA_SAFE_ASSERT_RETURN(value == 0,);
  1103. #else
  1104. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1105. #endif
  1106. pData->options.preferPluginBridges = (value != 0);
  1107. break;
  1108. case ENGINE_OPTION_PREFER_UI_BRIDGES:
  1109. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1110. pData->options.preferUiBridges = (value != 0);
  1111. break;
  1112. case ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  1113. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1114. pData->options.uisAlwaysOnTop = (value != 0);
  1115. break;
  1116. case ENGINE_OPTION_MAX_PARAMETERS:
  1117. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1118. pData->options.maxParameters = static_cast<uint>(value);
  1119. break;
  1120. case ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  1121. CARLA_SAFE_ASSERT_RETURN(value >= 0,);
  1122. pData->options.uiBridgesTimeout = static_cast<uint>(value);
  1123. break;
  1124. case ENGINE_OPTION_AUDIO_BUFFER_SIZE:
  1125. CARLA_SAFE_ASSERT_RETURN(value >= 8,);
  1126. pData->options.audioBufferSize = static_cast<uint>(value);
  1127. break;
  1128. case ENGINE_OPTION_AUDIO_SAMPLE_RATE:
  1129. CARLA_SAFE_ASSERT_RETURN(value >= 22050,);
  1130. pData->options.audioSampleRate = static_cast<uint>(value);
  1131. break;
  1132. case ENGINE_OPTION_AUDIO_TRIPLE_BUFFER:
  1133. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1134. pData->options.audioTripleBuffer = (value != 0);
  1135. break;
  1136. case ENGINE_OPTION_AUDIO_DEVICE:
  1137. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr,);
  1138. if (pData->options.audioDevice != nullptr)
  1139. delete[] pData->options.audioDevice;
  1140. pData->options.audioDevice = carla_strdup_safe(valueStr);
  1141. break;
  1142. case ENGINE_OPTION_PLUGIN_PATH:
  1143. CARLA_SAFE_ASSERT_RETURN(value > PLUGIN_NONE,);
  1144. CARLA_SAFE_ASSERT_RETURN(value <= PLUGIN_SFZ,);
  1145. switch (value)
  1146. {
  1147. case PLUGIN_LADSPA:
  1148. if (pData->options.pathLADSPA != nullptr)
  1149. delete[] pData->options.pathLADSPA;
  1150. if (valueStr != nullptr)
  1151. pData->options.pathLADSPA = carla_strdup_safe(valueStr);
  1152. else
  1153. pData->options.pathLADSPA = nullptr;
  1154. break;
  1155. case PLUGIN_DSSI:
  1156. if (pData->options.pathDSSI != nullptr)
  1157. delete[] pData->options.pathDSSI;
  1158. if (valueStr != nullptr)
  1159. pData->options.pathDSSI = carla_strdup_safe(valueStr);
  1160. else
  1161. pData->options.pathDSSI = nullptr;
  1162. break;
  1163. case PLUGIN_LV2:
  1164. if (pData->options.pathLV2 != nullptr)
  1165. delete[] pData->options.pathLV2;
  1166. if (valueStr != nullptr)
  1167. pData->options.pathLV2 = carla_strdup_safe(valueStr);
  1168. else
  1169. pData->options.pathLV2 = nullptr;
  1170. break;
  1171. case PLUGIN_VST2:
  1172. if (pData->options.pathVST2 != nullptr)
  1173. delete[] pData->options.pathVST2;
  1174. if (valueStr != nullptr)
  1175. pData->options.pathVST2 = carla_strdup_safe(valueStr);
  1176. else
  1177. pData->options.pathVST2 = nullptr;
  1178. break;
  1179. case PLUGIN_SF2:
  1180. if (pData->options.pathSF2 != nullptr)
  1181. delete[] pData->options.pathSF2;
  1182. if (valueStr != nullptr)
  1183. pData->options.pathSF2 = carla_strdup_safe(valueStr);
  1184. else
  1185. pData->options.pathSF2 = nullptr;
  1186. break;
  1187. case PLUGIN_SFZ:
  1188. if (pData->options.pathSFZ != nullptr)
  1189. delete[] pData->options.pathSFZ;
  1190. if (valueStr != nullptr)
  1191. pData->options.pathSFZ = carla_strdup_safe(valueStr);
  1192. else
  1193. pData->options.pathSFZ = nullptr;
  1194. break;
  1195. default:
  1196. return carla_stderr("CarlaEngine::setOption(%i:%s, %i, \"%s\") - Invalid plugin type", option, EngineOption2Str(option), value, valueStr);
  1197. break;
  1198. }
  1199. break;
  1200. case ENGINE_OPTION_PATH_BINARIES:
  1201. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1202. if (pData->options.binaryDir != nullptr)
  1203. delete[] pData->options.binaryDir;
  1204. pData->options.binaryDir = carla_strdup_safe(valueStr);
  1205. break;
  1206. case ENGINE_OPTION_PATH_RESOURCES:
  1207. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1208. if (pData->options.resourceDir != nullptr)
  1209. delete[] pData->options.resourceDir;
  1210. pData->options.resourceDir = carla_strdup_safe(valueStr);
  1211. break;
  1212. case ENGINE_OPTION_PREVENT_BAD_BEHAVIOUR: {
  1213. CARLA_SAFE_ASSERT_RETURN(pData->options.binaryDir != nullptr && pData->options.binaryDir[0] != '\0',);
  1214. #ifdef CARLA_OS_LINUX
  1215. const ScopedEngineEnvironmentLocker _seel(this);
  1216. if (value != 0)
  1217. {
  1218. CarlaString interposerPath(CarlaString(pData->options.binaryDir) + "/libcarla_interposer-safe.so");
  1219. ::setenv("LD_PRELOAD", interposerPath.buffer(), 1);
  1220. }
  1221. else
  1222. {
  1223. ::unsetenv("LD_PRELOAD");
  1224. }
  1225. #endif
  1226. } break;
  1227. case ENGINE_OPTION_FRONTEND_WIN_ID: {
  1228. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1229. const long long winId(std::strtoll(valueStr, nullptr, 16));
  1230. CARLA_SAFE_ASSERT_RETURN(winId >= 0,);
  1231. pData->options.frontendWinId = static_cast<uintptr_t>(winId);
  1232. } break;
  1233. #if !defined(BUILD_BRIDGE_ALTERNATIVE_ARCH) && !defined(CARLA_OS_WIN)
  1234. case ENGINE_OPTION_WINE_EXECUTABLE:
  1235. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1236. if (pData->options.wine.executable != nullptr)
  1237. delete[] pData->options.wine.executable;
  1238. pData->options.wine.executable = carla_strdup_safe(valueStr);
  1239. break;
  1240. case ENGINE_OPTION_WINE_AUTO_PREFIX:
  1241. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1242. pData->options.wine.autoPrefix = (value != 0);
  1243. break;
  1244. case ENGINE_OPTION_WINE_FALLBACK_PREFIX:
  1245. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr && valueStr[0] != '\0',);
  1246. if (pData->options.wine.fallbackPrefix != nullptr)
  1247. delete[] pData->options.wine.fallbackPrefix;
  1248. pData->options.wine.fallbackPrefix = carla_strdup_safe(valueStr);
  1249. break;
  1250. case ENGINE_OPTION_WINE_RT_PRIO_ENABLED:
  1251. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  1252. pData->options.wine.rtPrio = (value != 0);
  1253. break;
  1254. case ENGINE_OPTION_WINE_BASE_RT_PRIO:
  1255. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 89,);
  1256. pData->options.wine.baseRtPrio = value;
  1257. break;
  1258. case ENGINE_OPTION_WINE_SERVER_RT_PRIO:
  1259. CARLA_SAFE_ASSERT_RETURN(value >= 1 && value <= 99,);
  1260. pData->options.wine.serverRtPrio = value;
  1261. break;
  1262. #endif
  1263. case ENGINE_OPTION_DEBUG_CONSOLE_OUTPUT:
  1264. break;
  1265. }
  1266. }
  1267. #ifndef BUILD_BRIDGE
  1268. // -----------------------------------------------------------------------
  1269. // OSC Stuff
  1270. bool CarlaEngine::isOscControlRegistered() const noexcept
  1271. {
  1272. # ifdef HAVE_LIBLO
  1273. return pData->osc.isControlRegistered();
  1274. # else
  1275. return false;
  1276. # endif
  1277. }
  1278. void CarlaEngine::idleOsc() const noexcept
  1279. {
  1280. # ifdef HAVE_LIBLO
  1281. pData->osc.idle();
  1282. # endif
  1283. }
  1284. const char* CarlaEngine::getOscServerPathTCP() const noexcept
  1285. {
  1286. # ifdef HAVE_LIBLO
  1287. return pData->osc.getServerPathTCP();
  1288. # else
  1289. return nullptr;
  1290. # endif
  1291. }
  1292. const char* CarlaEngine::getOscServerPathUDP() const noexcept
  1293. {
  1294. # ifdef HAVE_LIBLO
  1295. return pData->osc.getServerPathUDP();
  1296. # else
  1297. return nullptr;
  1298. # endif
  1299. }
  1300. #endif
  1301. // -----------------------------------------------------------------------
  1302. // Helper functions
  1303. EngineEvent* CarlaEngine::getInternalEventBuffer(const bool isInput) const noexcept
  1304. {
  1305. return isInput ? pData->events.in : pData->events.out;
  1306. }
  1307. // -----------------------------------------------------------------------
  1308. // Internal stuff
  1309. void CarlaEngine::bufferSizeChanged(const uint32_t newBufferSize)
  1310. {
  1311. carla_debug("CarlaEngine::bufferSizeChanged(%i)", newBufferSize);
  1312. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1313. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1314. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1315. {
  1316. pData->graph.setBufferSize(newBufferSize);
  1317. }
  1318. #endif
  1319. pData->time.updateAudioValues(newBufferSize, pData->sampleRate);
  1320. for (uint i=0; i < pData->curPluginCount; ++i)
  1321. {
  1322. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1323. if (plugin != nullptr && plugin->isEnabled())
  1324. {
  1325. plugin->tryLock(true);
  1326. plugin->bufferSizeChanged(newBufferSize);
  1327. plugin->unlock();
  1328. }
  1329. }
  1330. callback(ENGINE_CALLBACK_BUFFER_SIZE_CHANGED, 0, static_cast<int>(newBufferSize), 0, 0.0f, nullptr);
  1331. }
  1332. void CarlaEngine::sampleRateChanged(const double newSampleRate)
  1333. {
  1334. carla_debug("CarlaEngine::sampleRateChanged(%g)", newSampleRate);
  1335. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1336. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1337. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1338. {
  1339. pData->graph.setSampleRate(newSampleRate);
  1340. }
  1341. #endif
  1342. pData->time.updateAudioValues(pData->bufferSize, newSampleRate);
  1343. for (uint i=0; i < pData->curPluginCount; ++i)
  1344. {
  1345. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1346. if (plugin != nullptr && plugin->isEnabled())
  1347. {
  1348. plugin->tryLock(true);
  1349. plugin->sampleRateChanged(newSampleRate);
  1350. plugin->unlock();
  1351. }
  1352. }
  1353. callback(ENGINE_CALLBACK_SAMPLE_RATE_CHANGED, 0, 0, 0, static_cast<float>(newSampleRate), nullptr);
  1354. }
  1355. void CarlaEngine::offlineModeChanged(const bool isOfflineNow)
  1356. {
  1357. carla_debug("CarlaEngine::offlineModeChanged(%s)", bool2str(isOfflineNow));
  1358. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1359. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1360. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1361. {
  1362. pData->graph.setOffline(isOfflineNow);
  1363. }
  1364. #endif
  1365. for (uint i=0; i < pData->curPluginCount; ++i)
  1366. {
  1367. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1368. if (plugin != nullptr && plugin->isEnabled())
  1369. plugin->offlineModeChanged(isOfflineNow);
  1370. }
  1371. }
  1372. void CarlaEngine::setPluginPeaks(const uint pluginId, float const inPeaks[2], float const outPeaks[2]) noexcept
  1373. {
  1374. EnginePluginData& pluginData(pData->plugins[pluginId]);
  1375. pluginData.insPeak[0] = inPeaks[0];
  1376. pluginData.insPeak[1] = inPeaks[1];
  1377. pluginData.outsPeak[0] = outPeaks[0];
  1378. pluginData.outsPeak[1] = outPeaks[1];
  1379. }
  1380. void CarlaEngine::saveProjectInternal(water::MemoryOutputStream& outStream) const
  1381. {
  1382. // send initial prepareForSave first, giving time for bridges to act
  1383. for (uint i=0; i < pData->curPluginCount; ++i)
  1384. {
  1385. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1386. if (plugin != nullptr && plugin->isEnabled())
  1387. {
  1388. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1389. // deactivate bridge client-side ping check, since some plugins block during save
  1390. if (plugin->getHints() & PLUGIN_IS_BRIDGE)
  1391. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1392. #endif
  1393. plugin->prepareForSave();
  1394. }
  1395. }
  1396. outStream << "<?xml version='1.0' encoding='UTF-8'?>\n";
  1397. outStream << "<!DOCTYPE CARLA-PROJECT>\n";
  1398. outStream << "<CARLA-PROJECT VERSION='2.0'>\n";
  1399. const bool isPlugin(getType() == kEngineTypePlugin);
  1400. const EngineOptions& options(pData->options);
  1401. {
  1402. MemoryOutputStream outSettings(1024);
  1403. outSettings << " <EngineSettings>\n";
  1404. outSettings << " <ForceStereo>" << bool2str(options.forceStereo) << "</ForceStereo>\n";
  1405. outSettings << " <PreferPluginBridges>" << bool2str(options.preferPluginBridges) << "</PreferPluginBridges>\n";
  1406. outSettings << " <PreferUiBridges>" << bool2str(options.preferUiBridges) << "</PreferUiBridges>\n";
  1407. outSettings << " <UIsAlwaysOnTop>" << bool2str(options.uisAlwaysOnTop) << "</UIsAlwaysOnTop>\n";
  1408. outSettings << " <MaxParameters>" << String(options.maxParameters) << "</MaxParameters>\n";
  1409. outSettings << " <UIBridgesTimeout>" << String(options.uiBridgesTimeout) << "</UIBridgesTimeout>\n";
  1410. if (isPlugin)
  1411. {
  1412. outSettings << " <LADSPA_PATH>" << xmlSafeString(options.pathLADSPA, true) << "</LADSPA_PATH>\n";
  1413. outSettings << " <DSSI_PATH>" << xmlSafeString(options.pathDSSI, true) << "</DSSI_PATH>\n";
  1414. outSettings << " <LV2_PATH>" << xmlSafeString(options.pathLV2, true) << "</LV2_PATH>\n";
  1415. outSettings << " <VST2_PATH>" << xmlSafeString(options.pathVST2, true) << "</VST2_PATH>\n";
  1416. outSettings << " <SF2_PATH>" << xmlSafeString(options.pathSF2, true) << "</SF2_PATH>\n";
  1417. outSettings << " <SFZ_PATH>" << xmlSafeString(options.pathSFZ, true) << "</SFZ_PATH>\n";
  1418. }
  1419. outSettings << " </EngineSettings>\n";
  1420. outStream << outSettings;
  1421. }
  1422. if (pData->timeInfo.bbt.valid && ! isPlugin)
  1423. {
  1424. MemoryOutputStream outTransport(128);
  1425. outTransport << "\n <Transport>\n";
  1426. // outTransport << " <BeatsPerBar>" << pData->timeInfo.bbt.beatsPerBar << "</BeatsPerBar>\n";
  1427. outTransport << " <BeatsPerMinute>" << pData->timeInfo.bbt.beatsPerMinute << "</BeatsPerMinute>\n";
  1428. outTransport << " </Transport>\n";
  1429. outStream << outTransport;
  1430. }
  1431. char strBuf[STR_MAX+1];
  1432. for (uint i=0; i < pData->curPluginCount; ++i)
  1433. {
  1434. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1435. if (plugin != nullptr && plugin->isEnabled())
  1436. {
  1437. MemoryOutputStream outPlugin(4096), streamPlugin;
  1438. plugin->getStateSave(false).dumpToMemoryStream(streamPlugin);
  1439. outPlugin << "\n";
  1440. strBuf[0] = '\0';
  1441. plugin->getRealName(strBuf);
  1442. if (strBuf[0] != '\0')
  1443. outPlugin << " <!-- " << xmlSafeString(strBuf, true) << " -->\n";
  1444. outPlugin << " <Plugin>\n";
  1445. outPlugin << streamPlugin;
  1446. outPlugin << " </Plugin>\n";
  1447. outStream << outPlugin;
  1448. }
  1449. }
  1450. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1451. // tell bridges we're done saving
  1452. for (uint i=0; i < pData->curPluginCount; ++i)
  1453. {
  1454. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1455. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1456. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1457. }
  1458. // save internal connections
  1459. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1460. {
  1461. if (const char* const* const patchbayConns = getPatchbayConnections(false))
  1462. {
  1463. MemoryOutputStream outPatchbay(2048);
  1464. outPatchbay << "\n <Patchbay>\n";
  1465. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1466. {
  1467. const char* const connSource(patchbayConns[i]);
  1468. const char* const connTarget(patchbayConns[i+1]);
  1469. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1470. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1471. outPatchbay << " <Connection>\n";
  1472. outPatchbay << " <Source>" << xmlSafeString(connSource, true) << "</Source>\n";
  1473. outPatchbay << " <Target>" << xmlSafeString(connTarget, true) << "</Target>\n";
  1474. outPatchbay << " </Connection>\n";
  1475. }
  1476. outPatchbay << " </Patchbay>\n";
  1477. outStream << outPatchbay;
  1478. }
  1479. }
  1480. // if we're running inside some session-manager (and using JACK), let them handle the connections
  1481. bool saveExternalConnections;
  1482. /**/ if (isPlugin)
  1483. saveExternalConnections = false;
  1484. else if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  1485. saveExternalConnections = true;
  1486. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1487. saveExternalConnections = false;
  1488. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1489. saveExternalConnections = false;
  1490. else if (std::getenv("NSM_URL") != nullptr)
  1491. saveExternalConnections = false;
  1492. else
  1493. saveExternalConnections = true;
  1494. if (saveExternalConnections)
  1495. {
  1496. if (const char* const* const patchbayConns = getPatchbayConnections(true))
  1497. {
  1498. MemoryOutputStream outPatchbay(2048);
  1499. outPatchbay << "\n <ExternalPatchbay>\n";
  1500. for (int i=0; patchbayConns[i] != nullptr && patchbayConns[i+1] != nullptr; ++i, ++i )
  1501. {
  1502. const char* const connSource(patchbayConns[i]);
  1503. const char* const connTarget(patchbayConns[i+1]);
  1504. CARLA_SAFE_ASSERT_CONTINUE(connSource != nullptr && connSource[0] != '\0');
  1505. CARLA_SAFE_ASSERT_CONTINUE(connTarget != nullptr && connTarget[0] != '\0');
  1506. outPatchbay << " <Connection>\n";
  1507. outPatchbay << " <Source>" << xmlSafeString(connSource, true) << "</Source>\n";
  1508. outPatchbay << " <Target>" << xmlSafeString(connTarget, true) << "</Target>\n";
  1509. outPatchbay << " </Connection>\n";
  1510. }
  1511. outPatchbay << " </ExternalPatchbay>\n";
  1512. outStream << outPatchbay;
  1513. }
  1514. }
  1515. #endif
  1516. outStream << "</CARLA-PROJECT>\n";
  1517. }
  1518. static String findBinaryInCustomPath(const char* const searchPath, const char* const binary)
  1519. {
  1520. const StringArray searchPaths(StringArray::fromTokens(searchPath, CARLA_OS_SPLIT_STR, ""));
  1521. // try direct filename first
  1522. String jbinary(binary);
  1523. // adjust for current platform
  1524. #ifdef CARLA_OS_WIN
  1525. if (jbinary[0] == '/')
  1526. jbinary = "C:" + jbinary.replaceCharacter('/', '\\');
  1527. #else
  1528. if (jbinary[1] == ':' && (jbinary[2] == '\\' || jbinary[2] == '/'))
  1529. jbinary = jbinary.substring(2).replaceCharacter('\\', '/');
  1530. #endif
  1531. String filename = File(jbinary).getFileName();
  1532. int searchFlags = File::findFiles|File::ignoreHiddenFiles;
  1533. #ifdef CARLA_OS_MAC
  1534. if (filename.endsWithIgnoreCase(".vst"))
  1535. searchFlags |= File::findDirectories;
  1536. #endif
  1537. Array<File> results;
  1538. for (const String *it=searchPaths.begin(), *end=searchPaths.end(); it != end; ++it)
  1539. {
  1540. const File path(*it);
  1541. results.clear();
  1542. path.findChildFiles(results, searchFlags, true, filename);
  1543. if (results.size() > 0)
  1544. return results.getFirst().getFullPathName();
  1545. }
  1546. // try changing extension
  1547. #if defined(CARLA_OS_MAC)
  1548. if (filename.endsWithIgnoreCase(".dll") || filename.endsWithIgnoreCase(".so"))
  1549. filename = File(jbinary).getFileNameWithoutExtension() + ".dylib";
  1550. #elif defined(CARLA_OS_WIN)
  1551. if (filename.endsWithIgnoreCase(".dylib") || filename.endsWithIgnoreCase(".so"))
  1552. filename = File(jbinary).getFileNameWithoutExtension() + ".dll";
  1553. #else
  1554. if (filename.endsWithIgnoreCase(".dll") || filename.endsWithIgnoreCase(".dylib"))
  1555. filename = File(jbinary).getFileNameWithoutExtension() + ".so";
  1556. #endif
  1557. else
  1558. return String();
  1559. for (const String *it=searchPaths.begin(), *end=searchPaths.end(); it != end; ++it)
  1560. {
  1561. const File path(*it);
  1562. results.clear();
  1563. path.findChildFiles(results, searchFlags, true, filename);
  1564. if (results.size() > 0)
  1565. return results.getFirst().getFullPathName();
  1566. }
  1567. return String();
  1568. }
  1569. bool CarlaEngine::loadProjectInternal(water::XmlDocument& xmlDoc)
  1570. {
  1571. ScopedPointer<XmlElement> xmlElement(xmlDoc.getDocumentElement(true));
  1572. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to parse project file");
  1573. const String& xmlType(xmlElement->getTagName());
  1574. const bool isPreset(xmlType.equalsIgnoreCase("carla-preset"));
  1575. if (! (xmlType.equalsIgnoreCase("carla-project") || isPreset))
  1576. {
  1577. callback(ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0.0f, nullptr);
  1578. setLastError("Not a valid Carla project or preset file");
  1579. return false;
  1580. }
  1581. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1582. const ScopedValueSetter<bool> _svs(pData->loadingProject, true, false);
  1583. #endif
  1584. // completely load file
  1585. xmlElement = xmlDoc.getDocumentElement(false);
  1586. CARLA_SAFE_ASSERT_RETURN_ERR(xmlElement != nullptr, "Failed to completely parse project file");
  1587. if (pData->aboutToClose)
  1588. return true;
  1589. const bool isPlugin(getType() == kEngineTypePlugin);
  1590. // load engine settings first of all
  1591. if (XmlElement* const elem = isPreset ? nullptr : xmlElement->getChildByName("EngineSettings"))
  1592. {
  1593. for (XmlElement* settElem = elem->getFirstChildElement(); settElem != nullptr; settElem = settElem->getNextElement())
  1594. {
  1595. const String& tag(settElem->getTagName());
  1596. const String text(settElem->getAllSubText().trim());
  1597. /** some settings might be incorrect or require extra work,
  1598. so we call setOption rather than modifying them direly */
  1599. int option = -1;
  1600. int value = 0;
  1601. const char* valueStr = nullptr;
  1602. /**/ if (tag == "ForceStereo")
  1603. {
  1604. option = ENGINE_OPTION_FORCE_STEREO;
  1605. value = text == "true" ? 1 : 0;
  1606. }
  1607. else if (tag == "PreferPluginBridges")
  1608. {
  1609. option = ENGINE_OPTION_PREFER_PLUGIN_BRIDGES;
  1610. value = text == "true" ? 1 : 0;
  1611. }
  1612. else if (tag == "PreferUiBridges")
  1613. {
  1614. option = ENGINE_OPTION_PREFER_UI_BRIDGES;
  1615. value = text == "true" ? 1 : 0;
  1616. }
  1617. else if (tag == "UIsAlwaysOnTop")
  1618. {
  1619. option = ENGINE_OPTION_UIS_ALWAYS_ON_TOP;
  1620. value = text == "true" ? 1 : 0;
  1621. }
  1622. else if (tag == "MaxParameters")
  1623. {
  1624. option = ENGINE_OPTION_MAX_PARAMETERS;
  1625. value = text.getIntValue();
  1626. }
  1627. else if (tag == "UIBridgesTimeout")
  1628. {
  1629. option = ENGINE_OPTION_UI_BRIDGES_TIMEOUT;
  1630. value = text.getIntValue();
  1631. }
  1632. else if (isPlugin)
  1633. {
  1634. /**/ if (tag == "LADSPA_PATH")
  1635. {
  1636. option = ENGINE_OPTION_PLUGIN_PATH;
  1637. value = PLUGIN_LADSPA;
  1638. valueStr = text.toRawUTF8();
  1639. }
  1640. else if (tag == "DSSI_PATH")
  1641. {
  1642. option = ENGINE_OPTION_PLUGIN_PATH;
  1643. value = PLUGIN_DSSI;
  1644. valueStr = text.toRawUTF8();
  1645. }
  1646. else if (tag == "LV2_PATH")
  1647. {
  1648. option = ENGINE_OPTION_PLUGIN_PATH;
  1649. value = PLUGIN_LV2;
  1650. valueStr = text.toRawUTF8();
  1651. }
  1652. else if (tag == "VST2_PATH")
  1653. {
  1654. option = ENGINE_OPTION_PLUGIN_PATH;
  1655. value = PLUGIN_VST2;
  1656. valueStr = text.toRawUTF8();
  1657. }
  1658. else if (tag == "SF2_PATH")
  1659. {
  1660. option = ENGINE_OPTION_PLUGIN_PATH;
  1661. value = PLUGIN_SF2;
  1662. valueStr = text.toRawUTF8();
  1663. }
  1664. else if (tag == "SFZ_PATH")
  1665. {
  1666. option = ENGINE_OPTION_PLUGIN_PATH;
  1667. value = PLUGIN_SFZ;
  1668. valueStr = text.toRawUTF8();
  1669. }
  1670. }
  1671. if (option == -1)
  1672. {
  1673. // check old stuff, unhandled now
  1674. if (tag == "GIG_PATH")
  1675. continue;
  1676. // hmm something is wrong..
  1677. carla_stderr2("CarlaEngine::loadProjectInternal() - Unhandled option '%s'", tag.toRawUTF8());
  1678. continue;
  1679. }
  1680. setOption(static_cast<EngineOption>(option), value, valueStr);
  1681. }
  1682. }
  1683. if (pData->aboutToClose)
  1684. return true;
  1685. // now setup transport
  1686. if (XmlElement* const elem = (isPreset || isPlugin) ? nullptr : xmlElement->getChildByName("Transport"))
  1687. {
  1688. if (XmlElement* const bpmElem = elem->getChildByName("BeatsPerMinute"))
  1689. {
  1690. const String bpmText(bpmElem->getAllSubText().trim());
  1691. const double bpm = bpmText.getDoubleValue();
  1692. // some sane limits
  1693. if (bpm >= 20.0 && bpm < 400.0)
  1694. pData->time.setBPM(bpm);
  1695. }
  1696. }
  1697. if (pData->aboutToClose)
  1698. return true;
  1699. // and we handle plugins
  1700. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1701. {
  1702. const String& tagName(elem->getTagName());
  1703. if (isPreset || tagName == "Plugin")
  1704. {
  1705. CarlaStateSave stateSave;
  1706. stateSave.fillFromXmlElement(isPreset ? xmlElement.get() : elem);
  1707. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1708. if (pData->aboutToClose)
  1709. return true;
  1710. CARLA_SAFE_ASSERT_CONTINUE(stateSave.type != nullptr);
  1711. #ifndef BUILD_BRIDGE
  1712. // compatibility code to load projects with GIG files
  1713. // FIXME Remove on 2.1 release
  1714. if (std::strcmp(stateSave.type, "GIG") == 0)
  1715. {
  1716. if (addPlugin(PLUGIN_LV2, "", stateSave.name, "http://linuxsampler.org/plugins/linuxsampler", 0, nullptr))
  1717. {
  1718. const uint pluginId = pData->curPluginCount;
  1719. if (CarlaPlugin* const plugin = pData->plugins[pluginId].plugin)
  1720. {
  1721. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1722. if (pData->aboutToClose)
  1723. return true;
  1724. String lsState;
  1725. lsState << "0.35\n";
  1726. lsState << "18 0 Chromatic\n";
  1727. lsState << "18 1 Drum Kits\n";
  1728. lsState << "20 0\n";
  1729. lsState << "0 1 " << stateSave.binary << "\n";
  1730. lsState << "0 0 0 0 1 0 GIG\n";
  1731. plugin->setCustomData(LV2_ATOM__String, "http://linuxsampler.org/schema#state-string", lsState.toRawUTF8(), true);
  1732. plugin->restoreLV2State();
  1733. plugin->setDryWet(stateSave.dryWet, true, true);
  1734. plugin->setVolume(stateSave.volume, true, true);
  1735. plugin->setBalanceLeft(stateSave.balanceLeft, true, true);
  1736. plugin->setBalanceRight(stateSave.balanceRight, true, true);
  1737. plugin->setPanning(stateSave.panning, true, true);
  1738. plugin->setCtrlChannel(stateSave.ctrlChannel, true, true);
  1739. plugin->setActive(stateSave.active, true, true);
  1740. ++pData->curPluginCount;
  1741. plugin->setEnabled(true);
  1742. callback(ENGINE_CALLBACK_PLUGIN_ADDED, pluginId, 0, 0, 0.0f, plugin->getName());
  1743. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1744. pData->graph.addPlugin(plugin);
  1745. }
  1746. else
  1747. {
  1748. carla_stderr2("Failed to get new plugin, state will not be restored correctly\n");
  1749. }
  1750. }
  1751. else
  1752. {
  1753. carla_stderr2("Failed to load a linuxsampler LV2 plugin, GIG file won't be loaded");
  1754. }
  1755. continue;
  1756. }
  1757. #endif
  1758. const void* extraStuff = nullptr;
  1759. static const char kTrue[] = "true";
  1760. const PluginType ptype(getPluginTypeFromString(stateSave.type));
  1761. switch (ptype)
  1762. {
  1763. case PLUGIN_SF2:
  1764. if (CarlaString(stateSave.label).endsWith(" (16 outs)"))
  1765. extraStuff = kTrue;
  1766. // fall through
  1767. case PLUGIN_LADSPA:
  1768. case PLUGIN_DSSI:
  1769. case PLUGIN_VST2:
  1770. case PLUGIN_SFZ:
  1771. if (stateSave.binary != nullptr && stateSave.binary[0] != '\0' &&
  1772. ! (File::isAbsolutePath(stateSave.binary) && File(stateSave.binary).exists()))
  1773. {
  1774. const char* searchPath;
  1775. switch (ptype)
  1776. {
  1777. case PLUGIN_LADSPA: searchPath = pData->options.pathLADSPA; break;
  1778. case PLUGIN_DSSI: searchPath = pData->options.pathDSSI; break;
  1779. case PLUGIN_VST2: searchPath = pData->options.pathVST2; break;
  1780. case PLUGIN_SF2: searchPath = pData->options.pathSF2; break;
  1781. case PLUGIN_SFZ: searchPath = pData->options.pathSFZ; break;
  1782. default: searchPath = nullptr; break;
  1783. }
  1784. if (searchPath != nullptr && searchPath[0] != '\0')
  1785. {
  1786. carla_stderr("Plugin binary '%s' doesn't exist on this filesystem, let's look for it...",
  1787. stateSave.binary);
  1788. String result = findBinaryInCustomPath(searchPath, stateSave.binary);
  1789. if (result.isEmpty())
  1790. {
  1791. switch (ptype)
  1792. {
  1793. case PLUGIN_LADSPA: searchPath = std::getenv("LADSPA_PATH"); break;
  1794. case PLUGIN_DSSI: searchPath = std::getenv("DSSI_PATH"); break;
  1795. case PLUGIN_VST2: searchPath = std::getenv("VST_PATH"); break;
  1796. case PLUGIN_SF2: searchPath = std::getenv("SF2_PATH"); break;
  1797. case PLUGIN_SFZ: searchPath = std::getenv("SFZ_PATH"); break;
  1798. default: searchPath = nullptr; break;
  1799. }
  1800. if (searchPath != nullptr && searchPath[0] != '\0')
  1801. result = findBinaryInCustomPath(searchPath, stateSave.binary);
  1802. }
  1803. if (result.isNotEmpty())
  1804. {
  1805. delete[] stateSave.binary;
  1806. stateSave.binary = carla_strdup(result.toRawUTF8());
  1807. carla_stderr("Found it! :)");
  1808. }
  1809. else
  1810. {
  1811. carla_stderr("Damn, we failed... :(");
  1812. }
  1813. }
  1814. }
  1815. break;
  1816. default:
  1817. break;
  1818. }
  1819. if (addPlugin(getBinaryTypeFromFile(stateSave.binary), ptype, stateSave.binary,
  1820. stateSave.name, stateSave.label, stateSave.uniqueId, extraStuff, stateSave.options))
  1821. {
  1822. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1823. const uint pluginId = pData->curPluginCount;
  1824. #else
  1825. const uint pluginId = 0;
  1826. #endif
  1827. if (CarlaPlugin* const plugin = pData->plugins[pluginId].plugin)
  1828. {
  1829. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1830. if (pData->aboutToClose)
  1831. return true;
  1832. // deactivate bridge client-side ping check, since some plugins block during load
  1833. if ((plugin->getHints() & PLUGIN_IS_BRIDGE) != 0 && ! isPreset)
  1834. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "false", false);
  1835. plugin->loadStateSave(stateSave);
  1836. /* NOTE: The following code is the same as the end of addPlugin().
  1837. * When project is loading we do not enable the plugin right away,
  1838. * as we want to load state first.
  1839. */
  1840. plugin->setEnabled(true);
  1841. ++pData->curPluginCount;
  1842. callback(ENGINE_CALLBACK_PLUGIN_ADDED, pluginId, 0, 0, 0.0f, plugin->getName());
  1843. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1844. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1845. pData->graph.addPlugin(plugin);
  1846. #endif
  1847. }
  1848. else
  1849. {
  1850. carla_stderr2("Failed to get new plugin, state will not be restored correctly\n");
  1851. }
  1852. }
  1853. else
  1854. {
  1855. carla_stderr2("Failed to load a plugin, error was:\n%s", getLastError());
  1856. }
  1857. }
  1858. if (isPreset)
  1859. {
  1860. callback(ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0.0f, nullptr);
  1861. return true;
  1862. }
  1863. }
  1864. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1865. // tell bridges we're done loading
  1866. for (uint i=0; i < pData->curPluginCount; ++i)
  1867. {
  1868. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1869. if (plugin != nullptr && plugin->isEnabled() && (plugin->getHints() & PLUGIN_IS_BRIDGE) != 0)
  1870. plugin->setCustomData(CUSTOM_DATA_TYPE_STRING, "__CarlaPingOnOff__", "true", false);
  1871. }
  1872. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1873. if (pData->aboutToClose)
  1874. return true;
  1875. // and now we handle connections (internal)
  1876. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1877. {
  1878. const bool isUsingExternal(pData->graph.isUsingExternal());
  1879. if (XmlElement* const elem = xmlElement->getChildByName("Patchbay"))
  1880. {
  1881. CarlaString sourcePort, targetPort;
  1882. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  1883. {
  1884. const String& patchTag(patchElem->getTagName());
  1885. if (patchTag != "Connection")
  1886. continue;
  1887. sourcePort.clear();
  1888. targetPort.clear();
  1889. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  1890. {
  1891. const String& tag(connElem->getTagName());
  1892. const String text(connElem->getAllSubText().trim());
  1893. /**/ if (tag == "Source")
  1894. sourcePort = xmlSafeString(text, false).toRawUTF8();
  1895. else if (tag == "Target")
  1896. targetPort = xmlSafeString(text, false).toRawUTF8();
  1897. }
  1898. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  1899. restorePatchbayConnection(false, sourcePort, targetPort, !isUsingExternal);
  1900. }
  1901. }
  1902. callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1903. if (pData->aboutToClose)
  1904. return true;
  1905. }
  1906. // if we're running inside some session-manager (and using JACK), let them handle the external connections
  1907. bool loadExternalConnections;
  1908. /**/ if (isPlugin)
  1909. loadExternalConnections = false;
  1910. else if (std::strcmp(getCurrentDriverName(), "JACK") != 0)
  1911. loadExternalConnections = true;
  1912. else if (std::getenv("CARLA_DONT_MANAGE_CONNECTIONS") != nullptr)
  1913. loadExternalConnections = false;
  1914. else if (std::getenv("LADISH_APP_NAME") != nullptr)
  1915. loadExternalConnections = false;
  1916. else if (std::getenv("NSM_URL") != nullptr)
  1917. loadExternalConnections = false;
  1918. else
  1919. loadExternalConnections = true;
  1920. // plus external connections too
  1921. if (loadExternalConnections)
  1922. {
  1923. const bool isUsingExternal(pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY ||
  1924. pData->graph.isUsingExternal());
  1925. for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement())
  1926. {
  1927. const String& tagName(elem->getTagName());
  1928. // check if we want to load patchbay-mode connections into an external (multi-client) graph
  1929. if (tagName == "Patchbay")
  1930. {
  1931. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1932. continue;
  1933. }
  1934. // or load external patchbay connections
  1935. else if (tagName != "ExternalPatchbay")
  1936. {
  1937. continue;
  1938. }
  1939. CarlaString sourcePort, targetPort;
  1940. for (XmlElement* patchElem = elem->getFirstChildElement(); patchElem != nullptr; patchElem = patchElem->getNextElement())
  1941. {
  1942. const String& patchTag(patchElem->getTagName());
  1943. if (patchTag != "Connection")
  1944. continue;
  1945. sourcePort.clear();
  1946. targetPort.clear();
  1947. for (XmlElement* connElem = patchElem->getFirstChildElement(); connElem != nullptr; connElem = connElem->getNextElement())
  1948. {
  1949. const String& tag(connElem->getTagName());
  1950. const String text(connElem->getAllSubText().trim());
  1951. /**/ if (tag == "Source")
  1952. sourcePort = xmlSafeString(text, false).toRawUTF8();
  1953. else if (tag == "Target")
  1954. targetPort = xmlSafeString(text, false).toRawUTF8();
  1955. }
  1956. if (sourcePort.isNotEmpty() && targetPort.isNotEmpty())
  1957. restorePatchbayConnection(true, sourcePort, targetPort, isUsingExternal);
  1958. }
  1959. break;
  1960. }
  1961. }
  1962. #endif
  1963. callback(ENGINE_CALLBACK_PROJECT_LOAD_FINISHED, 0, 0, 0, 0.0f, nullptr);
  1964. return true;
  1965. }
  1966. // -----------------------------------------------------------------------
  1967. CARLA_BACKEND_END_NAMESPACE