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 80KB

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