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

CarlaEngine.cpp 81KB

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