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

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