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.

CarlaEngineNative.cpp 72KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2014 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. #include "CarlaDefines.h"
  18. #ifdef BUILD_BRIDGE
  19. # error This file should not be compiled if building bridge
  20. #endif
  21. #include "CarlaEngineInternal.hpp"
  22. #include "CarlaPlugin.hpp"
  23. #include "CarlaBackendUtils.hpp"
  24. #include "CarlaBase64Utils.hpp"
  25. #include "CarlaBinaryUtils.hpp"
  26. #include "CarlaMathUtils.hpp"
  27. #include "CarlaStateUtils.hpp"
  28. #include "CarlaExternalUI.hpp"
  29. #include "CarlaHost.h"
  30. #include "CarlaNative.hpp"
  31. #include "juce_audio_basics.h"
  32. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  33. # include "juce_gui_basics.h"
  34. #else
  35. namespace juce {
  36. # include "juce_events/messages/juce_Initialisation.h"
  37. } // namespace juce
  38. #endif
  39. using juce::File;
  40. using juce::FloatVectorOperations;
  41. using juce::MemoryOutputStream;
  42. using juce::ScopedPointer;
  43. using juce::String;
  44. using juce::XmlDocument;
  45. using juce::XmlElement;
  46. static bool gNeedsJuceHandling = false;
  47. static int gJuceReferenceCounter = 0;
  48. CARLA_BACKEND_START_NAMESPACE
  49. // -----------------------------------------------------------------------
  50. class CarlaEngineNativeUI : public CarlaExternalUI
  51. {
  52. public:
  53. CarlaEngineNativeUI(CarlaEngine* const engine)
  54. : fEngine(engine),
  55. fIsReady(false),
  56. leakDetector_CarlaEngineNativeUI()
  57. {
  58. carla_debug("CarlaEngineNativeUI::CarlaEngineNativeUI(%p)", engine);
  59. }
  60. ~CarlaEngineNativeUI() noexcept override
  61. {
  62. carla_debug("CarlaEngineNativeUI::~CarlaEngineNativeUI()");
  63. }
  64. bool isReady() const noexcept
  65. {
  66. return fIsReady;
  67. }
  68. protected:
  69. bool msgReceived(const char* const msg) noexcept override
  70. {
  71. if (CarlaExternalUI::msgReceived(msg))
  72. return true;
  73. bool ok = true;
  74. if (std::strcmp(msg, "set_engine_option") == 0)
  75. {
  76. uint32_t option;
  77. int32_t value;
  78. const char* valueStr = nullptr;
  79. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(option), true);
  80. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(value), true);
  81. readNextLineAsString(valueStr); // can be null
  82. try {
  83. fEngine->setOption(static_cast<EngineOption>(option), value, valueStr);
  84. } CARLA_SAFE_EXCEPTION("setOption");
  85. if (valueStr != nullptr)
  86. delete[] valueStr;
  87. }
  88. else if (std::strcmp(msg, "load_file") == 0)
  89. {
  90. const char* filename;
  91. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  92. try {
  93. ok = fEngine->loadFile(filename);
  94. } CARLA_SAFE_EXCEPTION("loadFile");
  95. delete[] filename;
  96. }
  97. else if (std::strcmp(msg, "load_project") == 0)
  98. {
  99. const char* filename;
  100. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  101. try {
  102. ok = fEngine->loadProject(filename);
  103. } CARLA_SAFE_EXCEPTION("loadProject");
  104. delete[] filename;
  105. }
  106. else if (std::strcmp(msg, "save_project") == 0)
  107. {
  108. const char* filename;
  109. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  110. try {
  111. ok = fEngine->saveProject(filename);
  112. } CARLA_SAFE_EXCEPTION("saveProject");
  113. delete[] filename;
  114. }
  115. else if (std::strcmp(msg, "patchbay_connect") == 0)
  116. {
  117. uint32_t groupA, portA, groupB, portB;
  118. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(groupA), true);
  119. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(portA), true);
  120. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(groupB), true);
  121. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(portB), true);
  122. try {
  123. ok = fEngine->patchbayConnect(groupA, portA, groupB, portB);
  124. } CARLA_SAFE_EXCEPTION("patchbayConnect");
  125. }
  126. else if (std::strcmp(msg, "patchbay_disconnect") == 0)
  127. {
  128. uint32_t connectionId;
  129. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(connectionId), true);
  130. try {
  131. ok = fEngine->patchbayDisconnect(connectionId);
  132. } CARLA_SAFE_EXCEPTION("patchbayDisconnect");
  133. }
  134. else if (std::strcmp(msg, "patchbay_refresh") == 0)
  135. {
  136. try {
  137. ok = fEngine->patchbayRefresh(false);
  138. } CARLA_SAFE_EXCEPTION("patchbayRefresh");
  139. }
  140. else if (std::strcmp(msg, "transport_play") == 0)
  141. {
  142. fEngine->transportPlay();
  143. }
  144. else if (std::strcmp(msg, "transport_pause") == 0)
  145. {
  146. fEngine->transportPause();
  147. }
  148. else if (std::strcmp(msg, "transport_relocate") == 0)
  149. {
  150. uint64_t frame;
  151. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(frame), true);
  152. fEngine->transportRelocate(frame);
  153. }
  154. else if (std::strcmp(msg, "add_plugin") == 0)
  155. {
  156. uint32_t btype, ptype;
  157. const char* filename = nullptr;
  158. const char* name;
  159. const char* label;
  160. int64_t uniqueId;
  161. uint options;
  162. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(btype), true);
  163. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(ptype), true);
  164. readNextLineAsString(filename); // can be null
  165. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(name), true);
  166. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(label), true);
  167. CARLA_SAFE_ASSERT_RETURN(readNextLineAsLong(uniqueId), true);
  168. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(options), true);
  169. if (filename != nullptr && std::strcmp(filename, "(null)") == 0)
  170. {
  171. delete[] filename;
  172. filename = nullptr;
  173. }
  174. if (std::strcmp(name, "(null)") == 0)
  175. {
  176. delete[] name;
  177. name = nullptr;
  178. }
  179. ok = fEngine->addPlugin(static_cast<BinaryType>(btype), static_cast<PluginType>(ptype),
  180. filename, name, label, uniqueId, nullptr, options);
  181. if (filename != nullptr)
  182. delete[] filename;
  183. if (name != nullptr)
  184. delete[] name;
  185. delete[] label;
  186. }
  187. else if (std::strcmp(msg, "remove_plugin") == 0)
  188. {
  189. uint32_t pluginId;
  190. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  191. ok = fEngine->removePlugin(pluginId);
  192. }
  193. else if (std::strcmp(msg, "remove_all_plugins") == 0)
  194. {
  195. ok = fEngine->removeAllPlugins();
  196. }
  197. else if (std::strcmp(msg, "rename_plugin") == 0)
  198. {
  199. uint32_t pluginId;
  200. const char* newName;
  201. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  202. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(newName), true);
  203. // TODO
  204. /*const char* name =*/ fEngine->renamePlugin(pluginId, newName);
  205. delete[] newName;
  206. }
  207. else if (std::strcmp(msg, "clone_plugin") == 0)
  208. {
  209. uint32_t pluginId;
  210. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  211. ok = fEngine->clonePlugin(pluginId);
  212. }
  213. else if (std::strcmp(msg, "replace_plugin") == 0)
  214. {
  215. uint32_t pluginId;
  216. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  217. ok = fEngine->replacePlugin(pluginId);
  218. }
  219. else if (std::strcmp(msg, "switch_plugins") == 0)
  220. {
  221. uint32_t pluginIdA, pluginIdB;
  222. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginIdA), true);
  223. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginIdB), true);
  224. ok = fEngine->switchPlugins(pluginIdA, pluginIdB);
  225. }
  226. else if (std::strcmp(msg, "load_plugin_state") == 0)
  227. {
  228. uint32_t pluginId;
  229. const char* filename;
  230. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  231. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  232. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  233. plugin->loadStateFromFile(filename);
  234. delete[] filename;
  235. }
  236. else if (std::strcmp(msg, "save_plugin_state") == 0)
  237. {
  238. uint32_t pluginId;
  239. const char* filename;
  240. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  241. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  242. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  243. plugin->saveStateToFile(filename);
  244. delete[] filename;
  245. }
  246. else if (std::strcmp(msg, "set_option") == 0)
  247. {
  248. uint32_t pluginId, option;
  249. bool yesNo;
  250. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  251. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(option), true);
  252. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(yesNo), true);
  253. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  254. plugin->setOption(option, yesNo, false);
  255. }
  256. else if (std::strcmp(msg, "set_active") == 0)
  257. {
  258. uint32_t pluginId;
  259. bool onOff;
  260. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  261. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  262. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  263. plugin->setActive(onOff, true, false);
  264. }
  265. else if (std::strcmp(msg, "set_drywet") == 0)
  266. {
  267. uint32_t pluginId;
  268. float value;
  269. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  270. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  271. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  272. plugin->setDryWet(value, true, false);
  273. }
  274. else if (std::strcmp(msg, "set_volume") == 0)
  275. {
  276. uint32_t pluginId;
  277. float value;
  278. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  279. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  280. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  281. plugin->setVolume(value, true, false);
  282. }
  283. else if (std::strcmp(msg, "set_balance_left") == 0)
  284. {
  285. uint32_t pluginId;
  286. float value;
  287. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  288. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  289. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  290. plugin->setBalanceLeft(value, true, false);
  291. }
  292. else if (std::strcmp(msg, "set_balance_right") == 0)
  293. {
  294. uint32_t pluginId;
  295. float value;
  296. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  297. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  298. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  299. plugin->setBalanceRight(value, true, false);
  300. }
  301. else if (std::strcmp(msg, "set_panning") == 0)
  302. {
  303. uint32_t pluginId;
  304. float value;
  305. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  306. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  307. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  308. plugin->setPanning(value, true, false);
  309. }
  310. else if (std::strcmp(msg, "set_ctrl_channel") == 0)
  311. {
  312. uint32_t pluginId;
  313. int32_t channel;
  314. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  315. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(channel), true);
  316. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS, true);
  317. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  318. plugin->setCtrlChannel(int8_t(channel), true, false);
  319. }
  320. else if (std::strcmp(msg, "set_parameter_value") == 0)
  321. {
  322. uint32_t pluginId, parameterId;
  323. float value;
  324. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  325. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(parameterId), true);
  326. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  327. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  328. plugin->setParameterValue(parameterId, value, true, true, false);
  329. }
  330. else if (std::strcmp(msg, "set_parameter_midi_channel") == 0)
  331. {
  332. uint32_t pluginId, parameterId, channel;
  333. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  334. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(parameterId), true);
  335. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  336. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  337. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  338. plugin->setParameterMidiChannel(parameterId, static_cast<uint8_t>(channel), true, false);
  339. }
  340. else if (std::strcmp(msg, "set_parameter_midi_cc") == 0)
  341. {
  342. uint32_t pluginId, parameterId;
  343. int32_t cc;
  344. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  345. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(parameterId), true);
  346. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(cc), true);
  347. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL, true);
  348. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  349. plugin->setParameterMidiCC(parameterId, static_cast<int16_t>(cc), true, false);
  350. }
  351. else if (std::strcmp(msg, "set_program") == 0)
  352. {
  353. uint32_t pluginId;
  354. int32_t index;
  355. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  356. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(index), true);
  357. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  358. plugin->setProgram(index, true, true, false);
  359. }
  360. else if (std::strcmp(msg, "set_midi_program") == 0)
  361. {
  362. uint32_t pluginId;
  363. int32_t index;
  364. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  365. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(index), true);
  366. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  367. plugin->setMidiProgram(index, true, true, false);
  368. }
  369. else if (std::strcmp(msg, "set_custom_data") == 0)
  370. {
  371. uint32_t pluginId;
  372. const char* type;
  373. const char* key;
  374. const char* value;
  375. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  376. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(type), true);
  377. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  378. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  379. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  380. plugin->setCustomData(type, key, value, true);
  381. delete[] type;
  382. delete[] key;
  383. delete[] value;
  384. }
  385. else if (std::strcmp(msg, "set_chunk_data") == 0)
  386. {
  387. uint32_t pluginId;
  388. const char* cdata;
  389. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  390. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(cdata), true);
  391. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  392. {
  393. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(cdata));
  394. plugin->setChunkData(chunk.data(), chunk.size());
  395. }
  396. delete[] cdata;
  397. }
  398. else if (std::strcmp(msg, "prepare_for_save") == 0)
  399. {
  400. uint32_t pluginId;
  401. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  402. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  403. plugin->prepareForSave();
  404. }
  405. else if (std::strcmp(msg, "reset_parameters") == 0)
  406. {
  407. uint32_t pluginId;
  408. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  409. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  410. plugin->resetParameters();
  411. }
  412. else if (std::strcmp(msg, "randomize_parameters") == 0)
  413. {
  414. uint32_t pluginId;
  415. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  416. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  417. plugin->randomizeParameters();
  418. }
  419. else if (std::strcmp(msg, "send_midi_note") == 0)
  420. {
  421. uint32_t pluginId, channel, note, velocity;
  422. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  423. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  424. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(note), true);
  425. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(velocity), true);
  426. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  427. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_VALUE, true);
  428. CARLA_SAFE_ASSERT_RETURN(velocity < MAX_MIDI_VALUE, true);
  429. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  430. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), static_cast<uint8_t>(velocity), true, true, false);
  431. }
  432. else if (std::strcmp(msg, "show_custom_ui") == 0)
  433. {
  434. uint32_t pluginId;
  435. bool yesNo;
  436. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  437. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(yesNo), true);
  438. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  439. plugin->showCustomUI(yesNo);
  440. }
  441. else if (std::strcmp(msg, "ready") == 0)
  442. {
  443. fIsReady = true;
  444. }
  445. else
  446. {
  447. carla_stderr("CarlaEngineNativeUI::msgReceived : %s", msg);
  448. return false;
  449. }
  450. if (! ok)
  451. {
  452. const CarlaMutexLocker cml(getPipeLock());
  453. writeMessage("error\n", 6);
  454. writeAndFixMessage(fEngine->getLastError());
  455. flushMessages();
  456. }
  457. return true;
  458. }
  459. private:
  460. CarlaEngine* const fEngine;
  461. bool fIsReady;
  462. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineNativeUI)
  463. };
  464. // -----------------------------------------------------------------------
  465. class CarlaEngineNative : public CarlaEngine
  466. {
  467. public:
  468. CarlaEngineNative(const NativeHostDescriptor* const host, const bool isPatchbay, const uint32_t inChan = 2, uint32_t outChan = 0)
  469. : CarlaEngine(),
  470. pHost(host),
  471. kIsPatchbay(isPatchbay),
  472. fIsActive(false),
  473. fIsRunning(false),
  474. fUiServer(this),
  475. fOptionsForced(false),
  476. leakDetector_CarlaEngineNative()
  477. {
  478. carla_debug("CarlaEngineNative::CarlaEngineNative()");
  479. CARLA_SAFE_ASSERT_INT(gJuceReferenceCounter >= 0, gJuceReferenceCounter);
  480. if (gNeedsJuceHandling && ++gJuceReferenceCounter == 1)
  481. juce::initialiseJuce_GUI();
  482. carla_zeroChar(fTmpBuf, STR_MAX+1);
  483. pData->bufferSize = pHost->get_buffer_size(pHost->handle);
  484. pData->sampleRate = pHost->get_sample_rate(pHost->handle);
  485. if (outChan == 0)
  486. outChan = inChan;
  487. // set-up engine
  488. if (kIsPatchbay)
  489. {
  490. pData->options.processMode = ENGINE_PROCESS_MODE_PATCHBAY;
  491. pData->options.transportMode = ENGINE_TRANSPORT_MODE_PLUGIN;
  492. pData->options.forceStereo = false;
  493. pData->options.preferPluginBridges = false;
  494. pData->options.preferUiBridges = false;
  495. init("Carla-Patchbay");
  496. pData->graph.create(inChan, outChan);
  497. }
  498. else
  499. {
  500. CARLA_SAFE_ASSERT(inChan == 2);
  501. CARLA_SAFE_ASSERT(outChan == 2);
  502. pData->options.processMode = ENGINE_PROCESS_MODE_CONTINUOUS_RACK;
  503. pData->options.transportMode = ENGINE_TRANSPORT_MODE_PLUGIN;
  504. pData->options.forceStereo = true;
  505. pData->options.preferPluginBridges = false;
  506. pData->options.preferUiBridges = false;
  507. init("Carla-Rack");
  508. pData->graph.create(0, 0);
  509. }
  510. if (pData->options.resourceDir != nullptr)
  511. delete[] pData->options.resourceDir;
  512. if (pData->options.binaryDir != nullptr)
  513. delete[] pData->options.binaryDir;
  514. pData->options.resourceDir = carla_strdup(pHost->resourceDir);
  515. pData->options.binaryDir = carla_strdup(carla_get_library_folder());
  516. setCallback(_ui_server_callback, this);
  517. }
  518. ~CarlaEngineNative() override
  519. {
  520. CARLA_SAFE_ASSERT(! fIsActive);
  521. carla_debug("CarlaEngineNative::~CarlaEngineNative() - START");
  522. pData->aboutToClose = true;
  523. fIsRunning = false;
  524. removeAllPlugins();
  525. //runPendingRtEvents();
  526. close();
  527. pData->graph.destroy();
  528. if (gNeedsJuceHandling && --gJuceReferenceCounter == 0)
  529. juce::shutdownJuce_GUI();
  530. carla_debug("CarlaEngineNative::~CarlaEngineNative() - END");
  531. }
  532. protected:
  533. // -------------------------------------
  534. // CarlaEngine virtual calls
  535. bool init(const char* const clientName) override
  536. {
  537. carla_debug("CarlaEngineNative::init(\"%s\")", clientName);
  538. fIsRunning = true;
  539. if (! pData->init(clientName))
  540. {
  541. close();
  542. setLastError("Failed to init internal data");
  543. return false;
  544. }
  545. pData->bufferSize = pHost->get_buffer_size(pHost->handle);
  546. pData->sampleRate = pHost->get_sample_rate(pHost->handle);
  547. return true;
  548. }
  549. bool close() override
  550. {
  551. fIsRunning = false;
  552. CarlaEngine::close();
  553. return true;
  554. }
  555. bool isRunning() const noexcept override
  556. {
  557. return fIsRunning;
  558. }
  559. bool isOffline() const noexcept override
  560. {
  561. return pHost->is_offline(pHost->handle);
  562. }
  563. EngineType getType() const noexcept override
  564. {
  565. return kEngineTypePlugin;
  566. }
  567. const char* getCurrentDriverName() const noexcept override
  568. {
  569. return "Plugin";
  570. }
  571. // -------------------------------------------------------------------
  572. const char* renamePlugin(const uint id, const char* const newName) override
  573. {
  574. if (const char* const retName = CarlaEngine::renamePlugin(id, newName))
  575. {
  576. uiServerCallback(ENGINE_CALLBACK_PLUGIN_RENAMED, id, 0, 0, 0.0f, retName);
  577. return retName;
  578. }
  579. return nullptr;
  580. }
  581. // -------------------------------------------------------------------
  582. void bufferSizeChanged(const uint32_t newBufferSize)
  583. {
  584. if (pData->bufferSize == newBufferSize)
  585. return;
  586. {
  587. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  588. fUiServer.writeAndFixMessage("buffer-size");
  589. std::sprintf(fTmpBuf, "%i\n", newBufferSize);
  590. fUiServer.writeMessage(fTmpBuf);
  591. fUiServer.flushMessages();
  592. }
  593. pData->bufferSize = newBufferSize;
  594. CarlaEngine::bufferSizeChanged(newBufferSize);
  595. }
  596. void sampleRateChanged(const double newSampleRate)
  597. {
  598. if (carla_compareFloats(pData->sampleRate, newSampleRate))
  599. return;
  600. {
  601. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  602. const ScopedLocale csl;
  603. fUiServer.writeAndFixMessage("sample-rate");
  604. std::sprintf(fTmpBuf, "%f\n", newSampleRate);
  605. fUiServer.writeMessage(fTmpBuf);
  606. fUiServer.flushMessages();
  607. }
  608. pData->sampleRate = newSampleRate;
  609. CarlaEngine::sampleRateChanged(newSampleRate);
  610. }
  611. // -------------------------------------------------------------------
  612. void uiServerSendPluginInfo(CarlaPlugin* const plugin)
  613. {
  614. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  615. const uint pluginId(plugin->getId());
  616. std::sprintf(fTmpBuf, "PLUGIN_INFO_%i\n", pluginId);
  617. fUiServer.writeMessage(fTmpBuf);
  618. std::sprintf(fTmpBuf, "%i:%i:%i:" P_INT64 ":%i:%i\n", plugin->getType(), plugin->getCategory(), plugin->getHints(), plugin->getUniqueId(), plugin->getOptionsAvailable(), plugin->getOptionsEnabled());
  619. fUiServer.writeMessage(fTmpBuf);
  620. if (const char* const filename = plugin->getFilename())
  621. {
  622. std::sprintf(fTmpBuf, "%s", filename);
  623. fUiServer.writeAndFixMessage(fTmpBuf);
  624. }
  625. else
  626. fUiServer.writeMessage("\n");
  627. if (const char* const name = plugin->getName())
  628. {
  629. std::sprintf(fTmpBuf, "%s", name);
  630. fUiServer.writeAndFixMessage(fTmpBuf);
  631. }
  632. else
  633. fUiServer.writeMessage("\n");
  634. if (const char* const iconName = plugin->getIconName())
  635. {
  636. std::sprintf(fTmpBuf, "%s", iconName);
  637. fUiServer.writeAndFixMessage(fTmpBuf);
  638. }
  639. else
  640. fUiServer.writeMessage("\n");
  641. plugin->getRealName(fTmpBuf);
  642. fUiServer.writeAndFixMessage(fTmpBuf);
  643. plugin->getLabel(fTmpBuf);
  644. fUiServer.writeAndFixMessage(fTmpBuf);
  645. plugin->getMaker(fTmpBuf);
  646. fUiServer.writeAndFixMessage(fTmpBuf);
  647. plugin->getCopyright(fTmpBuf);
  648. fUiServer.writeAndFixMessage(fTmpBuf);
  649. std::sprintf(fTmpBuf, "AUDIO_COUNT_%i:%i:%i\n", pluginId, plugin->getAudioInCount(), plugin->getAudioOutCount());
  650. fUiServer.writeMessage(fTmpBuf);
  651. std::sprintf(fTmpBuf, "MIDI_COUNT_%i:%i:%i\n", pluginId, plugin->getMidiInCount(), plugin->getMidiOutCount());
  652. fUiServer.writeMessage(fTmpBuf);
  653. fUiServer.flushMessages();
  654. }
  655. void uiServerSendPluginParameters(CarlaPlugin* const plugin)
  656. {
  657. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  658. const ScopedLocale csl;
  659. const uint pluginId(plugin->getId());
  660. for (int32_t i=PARAMETER_ACTIVE; i>PARAMETER_MAX; --i)
  661. {
  662. std::sprintf(fTmpBuf, "PARAMVAL_%i:%i\n", pluginId, i);
  663. fUiServer.writeMessage(fTmpBuf);
  664. std::sprintf(fTmpBuf, "%f\n", plugin->getInternalParameterValue(i));
  665. fUiServer.writeMessage(fTmpBuf);
  666. fUiServer.flushMessages();
  667. }
  668. uint32_t ins, outs, count;
  669. plugin->getParameterCountInfo(ins, outs);
  670. count = plugin->getParameterCount();
  671. std::sprintf(fTmpBuf, "PARAMETER_COUNT_%i:%i:%i:%i\n", pluginId, ins, outs, count);
  672. fUiServer.writeMessage(fTmpBuf);
  673. for (uint32_t i=0; i<count; ++i)
  674. {
  675. const ParameterData& paramData(plugin->getParameterData(i));
  676. const ParameterRanges& paramRanges(plugin->getParameterRanges(i));
  677. std::sprintf(fTmpBuf, "PARAMETER_DATA_%i:%i\n", pluginId, i);
  678. fUiServer.writeMessage(fTmpBuf);
  679. std::sprintf(fTmpBuf, "%i:%i:%i:%i\n", paramData.type, paramData.hints, paramData.midiChannel, paramData.midiCC);
  680. fUiServer.writeMessage(fTmpBuf);
  681. plugin->getParameterName(i, fTmpBuf);
  682. fUiServer.writeAndFixMessage(fTmpBuf);
  683. plugin->getParameterUnit(i, fTmpBuf);
  684. fUiServer.writeAndFixMessage(fTmpBuf);
  685. std::sprintf(fTmpBuf, "PARAMETER_RANGES_%i:%i\n", pluginId, i);
  686. fUiServer.writeMessage(fTmpBuf);
  687. std::sprintf(fTmpBuf, "%f:%f:%f:%f:%f:%f\n", paramRanges.def, paramRanges.min, paramRanges.max, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  688. fUiServer.writeMessage(fTmpBuf);
  689. std::sprintf(fTmpBuf, "PARAMVAL_%i:%i\n", pluginId, i);
  690. fUiServer.writeMessage(fTmpBuf);
  691. std::sprintf(fTmpBuf, "%f\n", plugin->getParameterValue(i));
  692. fUiServer.writeMessage(fTmpBuf);
  693. }
  694. fUiServer.flushMessages();
  695. }
  696. void uiServerSendPluginPrograms(CarlaPlugin* const plugin)
  697. {
  698. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  699. const uint pluginId(plugin->getId());
  700. uint32_t count = plugin->getProgramCount();
  701. std::sprintf(fTmpBuf, "PROGRAM_COUNT_%i:%i:%i\n", pluginId, count, plugin->getCurrentProgram());
  702. fUiServer.writeMessage(fTmpBuf);
  703. for (uint32_t i=0; i<count; ++i)
  704. {
  705. std::sprintf(fTmpBuf, "PROGRAM_NAME_%i:%i\n", pluginId, i);
  706. fUiServer.writeMessage(fTmpBuf);
  707. plugin->getProgramName(i, fTmpBuf);
  708. fUiServer.writeAndFixMessage(fTmpBuf);
  709. }
  710. fUiServer.flushMessages();
  711. count = plugin->getMidiProgramCount();
  712. std::sprintf(fTmpBuf, "MIDI_PROGRAM_COUNT_%i:%i:%i\n", pluginId, count, plugin->getCurrentMidiProgram());
  713. fUiServer.writeMessage(fTmpBuf);
  714. for (uint32_t i=0; i<count; ++i)
  715. {
  716. std::sprintf(fTmpBuf, "MIDI_PROGRAM_DATA_%i:%i\n", pluginId, i);
  717. fUiServer.writeMessage(fTmpBuf);
  718. const MidiProgramData& mpData(plugin->getMidiProgramData(i));
  719. std::sprintf(fTmpBuf, "%i:%i\n", mpData.bank, mpData.program);
  720. fUiServer.writeMessage(fTmpBuf);
  721. std::sprintf(fTmpBuf, "%s", mpData.name);
  722. fUiServer.writeAndFixMessage(fTmpBuf);
  723. }
  724. fUiServer.flushMessages();
  725. }
  726. void uiServerSendPluginProperties(CarlaPlugin* const plugin)
  727. {
  728. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  729. const uint pluginId(plugin->getId());
  730. uint32_t count = plugin->getCustomDataCount();
  731. std::sprintf(fTmpBuf, "CUSTOM_DATA_COUNT_%i:%i\n", pluginId, count);
  732. fUiServer.writeMessage(fTmpBuf);
  733. for (uint32_t i=0; i<count; ++i)
  734. {
  735. const CustomData& customData(plugin->getCustomData(i));
  736. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  737. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_PROPERTY) != 0)
  738. continue;
  739. std::sprintf(fTmpBuf, "CUSTOM_DATA_%i:%i\n", pluginId, i);
  740. fUiServer.writeMessage(fTmpBuf);
  741. fUiServer.writeAndFixMessage(customData.type);
  742. fUiServer.writeAndFixMessage(customData.key);
  743. fUiServer.writeAndFixMessage(customData.value);
  744. }
  745. fUiServer.flushMessages();
  746. }
  747. void uiServerCallback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr)
  748. {
  749. if (! fIsRunning)
  750. return;
  751. if (! fUiServer.isPipeRunning())
  752. return;
  753. CarlaPlugin* plugin;
  754. switch (action)
  755. {
  756. case ENGINE_CALLBACK_UPDATE:
  757. plugin = getPlugin(pluginId);
  758. if (plugin != nullptr && plugin->isEnabled())
  759. {
  760. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  761. uiServerSendPluginProperties(plugin);
  762. }
  763. break;
  764. case ENGINE_CALLBACK_RELOAD_INFO:
  765. plugin = getPlugin(pluginId);
  766. if (plugin != nullptr && plugin->isEnabled())
  767. {
  768. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  769. uiServerSendPluginInfo(plugin);
  770. }
  771. break;
  772. case ENGINE_CALLBACK_RELOAD_PARAMETERS:
  773. plugin = getPlugin(pluginId);
  774. if (plugin != nullptr && plugin->isEnabled())
  775. {
  776. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  777. uiServerSendPluginParameters(plugin);
  778. }
  779. break;
  780. case ENGINE_CALLBACK_RELOAD_PROGRAMS:
  781. plugin = getPlugin(pluginId);
  782. if (plugin != nullptr && plugin->isEnabled())
  783. {
  784. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  785. uiServerSendPluginPrograms(plugin);
  786. }
  787. break;
  788. case ENGINE_CALLBACK_RELOAD_ALL:
  789. case ENGINE_CALLBACK_PLUGIN_ADDED:
  790. plugin = getPlugin(pluginId);
  791. if (plugin != nullptr && plugin->isEnabled())
  792. {
  793. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  794. uiServerSendPluginInfo(plugin);
  795. uiServerSendPluginParameters(plugin);
  796. uiServerSendPluginPrograms(plugin);
  797. uiServerSendPluginProperties(plugin);
  798. }
  799. break;
  800. default:
  801. break;
  802. }
  803. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  804. const ScopedLocale csl;
  805. std::sprintf(fTmpBuf, "ENGINE_CALLBACK_%i\n", int(action));
  806. fUiServer.writeMessage(fTmpBuf);
  807. std::sprintf(fTmpBuf, "%u\n", pluginId);
  808. fUiServer.writeMessage(fTmpBuf);
  809. std::sprintf(fTmpBuf, "%i\n", value1);
  810. fUiServer.writeMessage(fTmpBuf);
  811. std::sprintf(fTmpBuf, "%i\n", value2);
  812. fUiServer.writeMessage(fTmpBuf);
  813. std::sprintf(fTmpBuf, "%f\n", value3);
  814. fUiServer.writeMessage(fTmpBuf);
  815. fUiServer.writeAndFixMessage(valueStr != nullptr ? valueStr : "");
  816. fUiServer.flushMessages();
  817. }
  818. void uiServerInfo()
  819. {
  820. CARLA_SAFE_ASSERT_RETURN(fIsRunning,);
  821. CARLA_SAFE_ASSERT_RETURN(fUiServer.isPipeRunning(),);
  822. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  823. fUiServer.writeAndFixMessage("max-plugin-number");
  824. std::sprintf(fTmpBuf, "%i\n", pData->maxPluginNumber);
  825. fUiServer.writeMessage(fTmpBuf);
  826. fUiServer.writeAndFixMessage("buffer-size");
  827. std::sprintf(fTmpBuf, "%i\n", pData->bufferSize);
  828. fUiServer.writeMessage(fTmpBuf);
  829. const ScopedLocale csl;
  830. fUiServer.writeAndFixMessage("sample-rate");
  831. std::sprintf(fTmpBuf, "%f\n", pData->sampleRate);
  832. fUiServer.writeMessage(fTmpBuf);
  833. fUiServer.flushMessages();
  834. }
  835. void uiServerOptions()
  836. {
  837. CARLA_SAFE_ASSERT_RETURN(fIsRunning,);
  838. CARLA_SAFE_ASSERT_RETURN(fUiServer.isPipeRunning(),);
  839. const EngineOptions& options(pData->options);
  840. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  841. const char* const optionsForcedStr(fOptionsForced ? "true\n" : "false\n");
  842. const std::size_t optionsForcedStrSize(fOptionsForced ? 5 : 6);
  843. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PROCESS_MODE);
  844. fUiServer.writeMessage(fTmpBuf);
  845. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  846. std::sprintf(fTmpBuf, "%i\n", options.processMode);
  847. fUiServer.writeMessage(fTmpBuf);
  848. fUiServer.flushMessages();
  849. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_TRANSPORT_MODE);
  850. fUiServer.writeMessage(fTmpBuf);
  851. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  852. std::sprintf(fTmpBuf, "%i\n", options.transportMode);
  853. fUiServer.writeMessage(fTmpBuf);
  854. fUiServer.flushMessages();
  855. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_FORCE_STEREO);
  856. fUiServer.writeMessage(fTmpBuf);
  857. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  858. fUiServer.writeMessage(options.forceStereo ? "true\n" : "false\n");
  859. fUiServer.flushMessages();
  860. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PREFER_PLUGIN_BRIDGES);
  861. fUiServer.writeMessage(fTmpBuf);
  862. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  863. fUiServer.writeMessage(options.preferPluginBridges ? "true\n" : "false\n");
  864. fUiServer.flushMessages();
  865. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PREFER_UI_BRIDGES);
  866. fUiServer.writeMessage(fTmpBuf);
  867. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  868. fUiServer.writeMessage(options.preferUiBridges ? "true\n" : "false\n");
  869. fUiServer.flushMessages();
  870. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_UIS_ALWAYS_ON_TOP);
  871. fUiServer.writeMessage(fTmpBuf);
  872. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  873. fUiServer.writeMessage(options.uisAlwaysOnTop ? "true\n" : "false\n");
  874. fUiServer.flushMessages();
  875. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_MAX_PARAMETERS);
  876. fUiServer.writeMessage(fTmpBuf);
  877. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  878. std::sprintf(fTmpBuf, "%i\n", options.maxParameters);
  879. fUiServer.writeMessage(fTmpBuf);
  880. fUiServer.flushMessages();
  881. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_UI_BRIDGES_TIMEOUT);
  882. fUiServer.writeMessage(fTmpBuf);
  883. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  884. std::sprintf(fTmpBuf, "%i\n", options.uiBridgesTimeout);
  885. fUiServer.writeMessage(fTmpBuf);
  886. fUiServer.flushMessages();
  887. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PATH_BINARIES);
  888. fUiServer.writeMessage(fTmpBuf);
  889. fUiServer.writeMessage("true\n", 5);
  890. std::sprintf(fTmpBuf, "%s\n", options.binaryDir);
  891. fUiServer.writeMessage(fTmpBuf);
  892. fUiServer.flushMessages();
  893. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PATH_RESOURCES);
  894. fUiServer.writeMessage(fTmpBuf);
  895. fUiServer.writeMessage("true\n", 5);
  896. std::sprintf(fTmpBuf, "%s\n", options.resourceDir);
  897. fUiServer.writeMessage(fTmpBuf);
  898. fUiServer.flushMessages();
  899. }
  900. // -------------------------------------------------------------------
  901. // Plugin parameter calls
  902. uint32_t getParameterCount() const
  903. {
  904. if (CarlaPlugin* const plugin = _getFirstPlugin())
  905. return plugin->getParameterCount();
  906. return 0;
  907. }
  908. const NativeParameter* getParameterInfo(const uint32_t index) const
  909. {
  910. if (CarlaPlugin* const plugin = _getFirstPlugin())
  911. {
  912. if (index < plugin->getParameterCount())
  913. {
  914. static NativeParameter param;
  915. static char strBufName[STR_MAX+1];
  916. static char strBufUnit[STR_MAX+1];
  917. const ParameterData& paramData(plugin->getParameterData(index));
  918. const ParameterRanges& paramRanges(plugin->getParameterRanges(index));
  919. plugin->getParameterName(index, strBufName);
  920. plugin->getParameterUnit(index, strBufUnit);
  921. uint hints = 0x0;
  922. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  923. hints |= NATIVE_PARAMETER_IS_BOOLEAN;
  924. if (paramData.hints & PARAMETER_IS_INTEGER)
  925. hints |= NATIVE_PARAMETER_IS_INTEGER;
  926. if (paramData.hints & PARAMETER_IS_LOGARITHMIC)
  927. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  928. if (paramData.hints & PARAMETER_IS_AUTOMABLE)
  929. hints |= NATIVE_PARAMETER_IS_AUTOMABLE;
  930. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  931. hints |= NATIVE_PARAMETER_USES_SAMPLE_RATE;
  932. if (paramData.hints & PARAMETER_USES_SCALEPOINTS)
  933. hints |= NATIVE_PARAMETER_USES_SCALEPOINTS;
  934. if (paramData.hints & PARAMETER_USES_CUSTOM_TEXT)
  935. hints |= NATIVE_PARAMETER_USES_CUSTOM_TEXT;
  936. if (paramData.type == PARAMETER_INPUT || paramData.type == PARAMETER_OUTPUT)
  937. {
  938. if (paramData.hints & PARAMETER_IS_ENABLED)
  939. hints |= NATIVE_PARAMETER_IS_ENABLED;
  940. if (paramData.type == PARAMETER_OUTPUT)
  941. hints |= NATIVE_PARAMETER_IS_OUTPUT;
  942. }
  943. param.hints = static_cast<NativeParameterHints>(hints);
  944. param.name = strBufName;
  945. param.unit = strBufUnit;
  946. param.ranges.def = paramRanges.def;
  947. param.ranges.min = paramRanges.min;
  948. param.ranges.max = paramRanges.max;
  949. param.ranges.step = paramRanges.step;
  950. param.ranges.stepSmall = paramRanges.stepSmall;
  951. param.ranges.stepLarge = paramRanges.stepLarge;
  952. param.scalePointCount = 0; // TODO
  953. param.scalePoints = nullptr;
  954. return &param;
  955. }
  956. }
  957. return nullptr;
  958. }
  959. float getParameterValue(const uint32_t index) const
  960. {
  961. if (CarlaPlugin* const plugin = _getFirstPlugin())
  962. {
  963. if (index < plugin->getParameterCount())
  964. return plugin->getParameterValue(index);
  965. }
  966. return 0.0f;
  967. }
  968. const char* getParameterText(const uint32_t index /*, const float value*/) const
  969. {
  970. if (CarlaPlugin* const plugin = _getFirstPlugin())
  971. {
  972. if (index < plugin->getParameterCount())
  973. {
  974. static char strBuf[STR_MAX+1];
  975. carla_zeroChar(strBuf, STR_MAX+1);
  976. plugin->getParameterText(index, /*value,*/ strBuf);
  977. return strBuf;
  978. }
  979. }
  980. return nullptr;
  981. }
  982. // -------------------------------------------------------------------
  983. // Plugin midi-program calls
  984. uint32_t getMidiProgramCount() const
  985. {
  986. if (CarlaPlugin* const plugin = _getFirstPlugin())
  987. return plugin->getMidiProgramCount();
  988. return 0;
  989. }
  990. const NativeMidiProgram* getMidiProgramInfo(const uint32_t index) const
  991. {
  992. if (CarlaPlugin* const plugin = _getFirstPlugin())
  993. {
  994. if (index < plugin->getMidiProgramCount())
  995. {
  996. static NativeMidiProgram midiProg;
  997. {
  998. const MidiProgramData& midiProgData(plugin->getMidiProgramData(index));
  999. midiProg.bank = midiProgData.bank;
  1000. midiProg.program = midiProgData.program;
  1001. midiProg.name = midiProgData.name;
  1002. }
  1003. return &midiProg;
  1004. }
  1005. }
  1006. return nullptr;
  1007. }
  1008. // -------------------------------------------------------------------
  1009. // Plugin state calls
  1010. void setParameterValue(const uint32_t index, const float value)
  1011. {
  1012. if (CarlaPlugin* const plugin = _getFirstPlugin())
  1013. {
  1014. if (index < plugin->getParameterCount())
  1015. plugin->setParameterValue(index, value, false, false, false);
  1016. }
  1017. }
  1018. void setMidiProgram(const uint8_t, const uint32_t bank, const uint32_t program)
  1019. {
  1020. if (CarlaPlugin* const plugin = _getFirstPlugin())
  1021. plugin->setMidiProgramById(bank, program, false, false, false);
  1022. }
  1023. // -------------------------------------------------------------------
  1024. // Plugin process calls
  1025. void activate()
  1026. {
  1027. #if 0
  1028. for (uint32_t i=0; i < pData->curPluginCount; ++i)
  1029. {
  1030. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1031. if (plugin == nullptr || ! plugin->isEnabled())
  1032. continue;
  1033. plugin->setActive(true, true, false);
  1034. }
  1035. #endif
  1036. fIsActive = true;
  1037. }
  1038. void deactivate()
  1039. {
  1040. fIsActive = false;
  1041. #if 0
  1042. for (uint32_t i=0; i < pData->curPluginCount; ++i)
  1043. {
  1044. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1045. if (plugin == nullptr || ! plugin->isEnabled())
  1046. continue;
  1047. plugin->setActive(false, true, false);
  1048. }
  1049. #endif
  1050. // just in case
  1051. //runPendingRtEvents();
  1052. }
  1053. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount)
  1054. {
  1055. const PendingRtEventsRunner prt(this);
  1056. // ---------------------------------------------------------------
  1057. // Time Info
  1058. const NativeTimeInfo* const timeInfo(pHost->get_time_info(pHost->handle));
  1059. pData->timeInfo.playing = timeInfo->playing;
  1060. pData->timeInfo.frame = timeInfo->frame;
  1061. pData->timeInfo.usecs = timeInfo->usecs;
  1062. pData->timeInfo.valid = 0x0;
  1063. if (timeInfo->bbt.valid)
  1064. {
  1065. pData->timeInfo.valid |= EngineTimeInfo::kValidBBT;
  1066. pData->timeInfo.bbt.bar = timeInfo->bbt.bar;
  1067. pData->timeInfo.bbt.beat = timeInfo->bbt.beat;
  1068. pData->timeInfo.bbt.tick = timeInfo->bbt.tick;
  1069. pData->timeInfo.bbt.barStartTick = timeInfo->bbt.barStartTick;
  1070. pData->timeInfo.bbt.beatsPerBar = timeInfo->bbt.beatsPerBar;
  1071. pData->timeInfo.bbt.beatType = timeInfo->bbt.beatType;
  1072. pData->timeInfo.bbt.ticksPerBeat = timeInfo->bbt.ticksPerBeat;
  1073. pData->timeInfo.bbt.beatsPerMinute = timeInfo->bbt.beatsPerMinute;
  1074. }
  1075. // ---------------------------------------------------------------
  1076. // Do nothing if no plugins and rack mode
  1077. if (pData->curPluginCount == 0 && ! kIsPatchbay)
  1078. {
  1079. FloatVectorOperations::copy(outBuffer[0], inBuffer[0], static_cast<int>(frames));
  1080. FloatVectorOperations::copy(outBuffer[1], inBuffer[1], static_cast<int>(frames));
  1081. return;
  1082. }
  1083. // ---------------------------------------------------------------
  1084. // initialize events
  1085. carla_zeroStruct<EngineEvent>(pData->events.in, kMaxEngineEventInternalCount);
  1086. carla_zeroStruct<EngineEvent>(pData->events.out, kMaxEngineEventInternalCount);
  1087. // ---------------------------------------------------------------
  1088. // events input (before processing)
  1089. {
  1090. uint32_t engineEventIndex = 0;
  1091. for (uint32_t i=0; i < midiEventCount && engineEventIndex < kMaxEngineEventInternalCount; ++i)
  1092. {
  1093. const NativeMidiEvent& midiEvent(midiEvents[i]);
  1094. EngineEvent& engineEvent(pData->events.in[engineEventIndex++]);
  1095. engineEvent.time = midiEvent.time;
  1096. engineEvent.fillFromMidiData(midiEvent.size, midiEvent.data);
  1097. if (engineEventIndex >= kMaxEngineEventInternalCount)
  1098. break;
  1099. }
  1100. }
  1101. if (kIsPatchbay)
  1102. {
  1103. // -----------------------------------------------------------
  1104. // process
  1105. pData->graph.process(pData, inBuffer, outBuffer, frames);
  1106. }
  1107. else
  1108. {
  1109. // -----------------------------------------------------------
  1110. // create audio buffers
  1111. const float* inBuf[2] = { inBuffer[0], inBuffer[1] };
  1112. /* */ float* outBuf[2] = { outBuffer[0], outBuffer[1] };
  1113. // -----------------------------------------------------------
  1114. // process
  1115. pData->graph.processRack(pData, inBuf, outBuf, frames);
  1116. }
  1117. // ---------------------------------------------------------------
  1118. // events output (after processing)
  1119. carla_zeroStruct<EngineEvent>(pData->events.in, kMaxEngineEventInternalCount);
  1120. {
  1121. NativeMidiEvent midiEvent;
  1122. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  1123. {
  1124. const EngineEvent& engineEvent(pData->events.out[i]);
  1125. if (engineEvent.type == kEngineEventTypeNull)
  1126. break;
  1127. midiEvent.time = engineEvent.time;
  1128. if (engineEvent.type == CarlaBackend::kEngineEventTypeControl)
  1129. {
  1130. midiEvent.port = 0;
  1131. engineEvent.ctrl.convertToMidiData(engineEvent.channel, midiEvent.size, midiEvent.data);
  1132. }
  1133. else if (engineEvent.type == kEngineEventTypeMidi)
  1134. {
  1135. if (engineEvent.midi.size > 4 || engineEvent.midi.dataExt != nullptr)
  1136. continue;
  1137. midiEvent.port = engineEvent.midi.port;
  1138. midiEvent.size = engineEvent.midi.size;
  1139. midiEvent.data[0] = static_cast<uint8_t>(engineEvent.midi.data[0] + engineEvent.channel);
  1140. for (uint8_t j=1; j < midiEvent.size; ++j)
  1141. midiEvent.data[j] = engineEvent.midi.data[j];
  1142. }
  1143. else
  1144. {
  1145. carla_stderr("Unknown event type...");
  1146. continue;
  1147. }
  1148. pHost->write_midi_event(pHost->handle, &midiEvent);
  1149. }
  1150. }
  1151. }
  1152. // -------------------------------------------------------------------
  1153. // Plugin UI calls
  1154. void uiShow(const bool show)
  1155. {
  1156. if (show)
  1157. {
  1158. if (fUiServer.isPipeRunning())
  1159. {
  1160. fUiServer.writeFocusMessage();
  1161. return;
  1162. }
  1163. CarlaString path(pHost->resourceDir);
  1164. if (kIsPatchbay)
  1165. path += CARLA_OS_SEP_STR "carla-plugin-patchbay";
  1166. else
  1167. path += CARLA_OS_SEP_STR "carla-plugin";
  1168. // #ifdef CARLA_OS_WIN
  1169. // path += ".exe";
  1170. // #endif
  1171. carla_stdout("Trying to start carla-plugin using \"%s\"", path.buffer());
  1172. fUiServer.setData(path, pData->sampleRate, pHost->uiName);
  1173. if (! fUiServer.startPipeServer(false))
  1174. {
  1175. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UI_UNAVAILABLE, 0, 0, nullptr, 0.0f);
  1176. return;
  1177. }
  1178. uiServerInfo();
  1179. uiServerOptions();
  1180. uiServerCallback(ENGINE_CALLBACK_ENGINE_STARTED, 0, pData->options.processMode, pData->options.transportMode, 0.0f, "Plugin");
  1181. fUiServer.writeShowMessage();
  1182. for (uint i=0; i < pData->curPluginCount; ++i)
  1183. {
  1184. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1185. if (plugin != nullptr && plugin->isEnabled())
  1186. {
  1187. uiServerCallback(ENGINE_CALLBACK_PLUGIN_ADDED, i, 0, 0, 0.0f, plugin->getName());
  1188. }
  1189. }
  1190. if (kIsPatchbay)
  1191. patchbayRefresh(false);
  1192. if (std::getenv("CARLA_PLUGIN_EMBED_WINID") != nullptr)
  1193. {
  1194. carla_stdout("Using Carla plugin embedded, waiting for it to be ready...");
  1195. for (; fUiServer.isPipeRunning() && ! fUiServer.isReady();)
  1196. fUiServer.idlePipe();
  1197. carla_stdout("Done!");
  1198. }
  1199. }
  1200. else
  1201. {
  1202. fUiServer.stopPipeServer(5000);
  1203. // hide all custom uis
  1204. for (uint i=0; i < pData->curPluginCount; ++i)
  1205. {
  1206. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1207. if (plugin != nullptr && plugin->isEnabled())
  1208. {
  1209. if (plugin->getHints() & PLUGIN_HAS_CUSTOM_UI)
  1210. {
  1211. try {
  1212. plugin->showCustomUI(false);
  1213. } CARLA_SAFE_EXCEPTION_CONTINUE("Plugin showCustomUI (hide)");
  1214. }
  1215. }
  1216. }
  1217. }
  1218. }
  1219. void uiIdle()
  1220. {
  1221. for (uint i=0; i < pData->curPluginCount; ++i)
  1222. {
  1223. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1224. if (plugin != nullptr && plugin->isEnabled())
  1225. {
  1226. const uint hints(plugin->getHints());
  1227. if ((hints & PLUGIN_HAS_CUSTOM_UI) != 0 && (hints & PLUGIN_NEEDS_UI_MAIN_THREAD) != 0)
  1228. {
  1229. try {
  1230. plugin->uiIdle();
  1231. } CARLA_SAFE_EXCEPTION_CONTINUE("Plugin uiIdle");
  1232. }
  1233. }
  1234. }
  1235. #ifdef HAVE_LIBLO
  1236. pData->osc.idle();
  1237. #endif
  1238. fUiServer.idlePipe();
  1239. if (fUiServer.isPipeRunning())
  1240. {
  1241. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  1242. #ifndef CARLA_OS_WIN
  1243. const EngineTimeInfo& timeInfo(pData->timeInfo);
  1244. const ScopedLocale csl;
  1245. // send transport
  1246. fUiServer.writeAndFixMessage("transport");
  1247. fUiServer.writeMessage(timeInfo.playing ? "true\n" : "false\n");
  1248. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  1249. {
  1250. std::sprintf(fTmpBuf, P_UINT64 ":%i:%i:%i\n", timeInfo.frame, timeInfo.bbt.bar, timeInfo.bbt.beat, timeInfo.bbt.tick);
  1251. fUiServer.writeMessage(fTmpBuf);
  1252. std::sprintf(fTmpBuf, "%f\n", timeInfo.bbt.beatsPerMinute);
  1253. fUiServer.writeMessage(fTmpBuf);
  1254. }
  1255. else
  1256. {
  1257. std::sprintf(fTmpBuf, P_UINT64 ":0:0:0\n", timeInfo.frame);
  1258. fUiServer.writeMessage(fTmpBuf);
  1259. fUiServer.writeMessage("0.0\n");
  1260. }
  1261. fUiServer.flushMessages();
  1262. #endif
  1263. // send peaks and param outputs for all plugins
  1264. for (uint i=0; i < pData->curPluginCount; ++i)
  1265. {
  1266. const EnginePluginData& plugData(pData->plugins[i]);
  1267. const CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1268. std::sprintf(fTmpBuf, "PEAKS_%i\n", i);
  1269. fUiServer.writeMessage(fTmpBuf);
  1270. std::sprintf(fTmpBuf, "%f:%f:%f:%f\n", plugData.insPeak[0], plugData.insPeak[1], plugData.outsPeak[0], plugData.outsPeak[1]);
  1271. fUiServer.writeMessage(fTmpBuf);
  1272. fUiServer.flushMessages();
  1273. for (uint32_t j=0, count=plugin->getParameterCount(); j < count; ++j)
  1274. {
  1275. if (! plugin->isParameterOutput(j))
  1276. continue;
  1277. std::sprintf(fTmpBuf, "PARAMVAL_%i:%i\n", i, j);
  1278. fUiServer.writeMessage(fTmpBuf);
  1279. std::sprintf(fTmpBuf, "%f\n", plugin->getParameterValue(j));
  1280. fUiServer.writeMessage(fTmpBuf);
  1281. fUiServer.flushMessages();
  1282. }
  1283. }
  1284. }
  1285. switch (fUiServer.getAndResetUiState())
  1286. {
  1287. case CarlaExternalUI::UiNone:
  1288. case CarlaExternalUI::UiShow:
  1289. break;
  1290. case CarlaExternalUI::UiCrashed:
  1291. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UI_UNAVAILABLE, 0, 0, nullptr, 0.0f);
  1292. break;
  1293. case CarlaExternalUI::UiHide:
  1294. pHost->ui_closed(pHost->handle);
  1295. fUiServer.stopPipeServer(2000);
  1296. break;
  1297. }
  1298. }
  1299. // -------------------------------------------------------------------
  1300. // Plugin state calls
  1301. char* getState() const
  1302. {
  1303. MemoryOutputStream out;
  1304. saveProjectInternal(out);
  1305. return strdup(out.toString().toRawUTF8());
  1306. }
  1307. void setState(const char* const data)
  1308. {
  1309. // remove all plugins first, no lock
  1310. fIsRunning = false;
  1311. removeAllPlugins();
  1312. fIsRunning = true;
  1313. // stopped during removeAllPlugins()
  1314. if (! pData->thread.isThreadRunning())
  1315. pData->thread.startThread();
  1316. fOptionsForced = true;
  1317. const String state(data);
  1318. XmlDocument xml(state);
  1319. loadProjectInternal(xml);
  1320. }
  1321. // -------------------------------------------------------------------
  1322. public:
  1323. #define handlePtr ((CarlaEngineNative*)handle)
  1324. static NativePluginHandle _instantiateRack(const NativeHostDescriptor* host)
  1325. {
  1326. return new CarlaEngineNative(host, false);
  1327. }
  1328. static NativePluginHandle _instantiatePatchbay(const NativeHostDescriptor* host)
  1329. {
  1330. return new CarlaEngineNative(host, true);
  1331. }
  1332. static NativePluginHandle _instantiatePatchbay3s(const NativeHostDescriptor* host)
  1333. {
  1334. return new CarlaEngineNative(host, true, 3, 2);
  1335. }
  1336. static NativePluginHandle _instantiatePatchbay16(const NativeHostDescriptor* host)
  1337. {
  1338. return new CarlaEngineNative(host, true, 16);
  1339. }
  1340. static NativePluginHandle _instantiatePatchbay32(const NativeHostDescriptor* host)
  1341. {
  1342. return new CarlaEngineNative(host, true, 32);
  1343. }
  1344. static void _cleanup(NativePluginHandle handle)
  1345. {
  1346. delete handlePtr;
  1347. }
  1348. static uint32_t _get_parameter_count(NativePluginHandle handle)
  1349. {
  1350. return handlePtr->getParameterCount();
  1351. }
  1352. static const NativeParameter* _get_parameter_info(NativePluginHandle handle, uint32_t index)
  1353. {
  1354. return handlePtr->getParameterInfo(index);
  1355. }
  1356. static float _get_parameter_value(NativePluginHandle handle, uint32_t index)
  1357. {
  1358. return handlePtr->getParameterValue(index);
  1359. }
  1360. static const char* _get_parameter_text(NativePluginHandle handle, uint32_t index /*, float value*/)
  1361. {
  1362. return handlePtr->getParameterText(index /*, value*/);
  1363. }
  1364. static uint32_t _get_midi_program_count(NativePluginHandle handle)
  1365. {
  1366. return handlePtr->getMidiProgramCount();
  1367. }
  1368. static const NativeMidiProgram* _get_midi_program_info(NativePluginHandle handle, uint32_t index)
  1369. {
  1370. return handlePtr->getMidiProgramInfo(index);
  1371. }
  1372. static void _set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  1373. {
  1374. handlePtr->setParameterValue(index, value);
  1375. }
  1376. static void _set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  1377. {
  1378. handlePtr->setMidiProgram(channel, bank, program);
  1379. }
  1380. static void _ui_show(NativePluginHandle handle, bool show)
  1381. {
  1382. handlePtr->uiShow(show);
  1383. }
  1384. static void _ui_idle(NativePluginHandle handle)
  1385. {
  1386. handlePtr->uiIdle();
  1387. }
  1388. static void _activate(NativePluginHandle handle)
  1389. {
  1390. handlePtr->activate();
  1391. }
  1392. static void _deactivate(NativePluginHandle handle)
  1393. {
  1394. handlePtr->deactivate();
  1395. }
  1396. static void _process(NativePluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  1397. {
  1398. handlePtr->process(inBuffer, outBuffer, frames, midiEvents, midiEventCount);
  1399. }
  1400. static char* _get_state(NativePluginHandle handle)
  1401. {
  1402. return handlePtr->getState();
  1403. }
  1404. static void _set_state(NativePluginHandle handle, const char* data)
  1405. {
  1406. handlePtr->setState(data);
  1407. }
  1408. static intptr_t _dispatcher(NativePluginHandle handle, NativePluginDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1409. {
  1410. switch(opcode)
  1411. {
  1412. case NATIVE_PLUGIN_OPCODE_NULL:
  1413. return 0;
  1414. case NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED:
  1415. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  1416. handlePtr->bufferSizeChanged(static_cast<uint32_t>(value));
  1417. return 0;
  1418. case NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED:
  1419. CARLA_SAFE_ASSERT_RETURN(opt > 0.0, 0);
  1420. handlePtr->sampleRateChanged(static_cast<double>(opt));
  1421. return 0;
  1422. case NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED:
  1423. handlePtr->offlineModeChanged(value != 0);
  1424. return 0;
  1425. case NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED:
  1426. //handlePtr->uiNameChanged(static_cast<const char*>(ptr));
  1427. return 0;
  1428. }
  1429. return 0;
  1430. // unused
  1431. (void)index;
  1432. (void)ptr;
  1433. }
  1434. // -------------------------------------------------------------------
  1435. static void _ui_server_callback(void* handle, EngineCallbackOpcode action, uint pluginId, int value1, int value2, float value3, const char* valueStr)
  1436. {
  1437. handlePtr->uiServerCallback(action, pluginId, value1, value2, value3, valueStr);
  1438. }
  1439. // -------------------------------------------------------------------
  1440. #undef handlePtr
  1441. private:
  1442. const NativeHostDescriptor* const pHost;
  1443. const bool kIsPatchbay; // rack if false
  1444. bool fIsActive, fIsRunning;
  1445. CarlaEngineNativeUI fUiServer;
  1446. bool fOptionsForced;
  1447. char fTmpBuf[STR_MAX+1];
  1448. CarlaPlugin* _getFirstPlugin() const noexcept
  1449. {
  1450. if (pData->curPluginCount == 0 || pData->plugins == nullptr)
  1451. return nullptr;
  1452. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  1453. if (plugin == nullptr || ! plugin->isEnabled())
  1454. return nullptr;
  1455. return pData->plugins[0].plugin;
  1456. }
  1457. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineNative)
  1458. };
  1459. // -----------------------------------------------------------------------
  1460. static const NativePluginDescriptor carlaRackDesc = {
  1461. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1462. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1463. |NATIVE_PLUGIN_HAS_UI
  1464. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1465. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1466. |NATIVE_PLUGIN_USES_STATE
  1467. |NATIVE_PLUGIN_USES_TIME),
  1468. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1469. /* audioIns */ 2,
  1470. /* audioOuts */ 2,
  1471. /* midiIns */ 1,
  1472. /* midiOuts */ 1,
  1473. /* paramIns */ 0,
  1474. /* paramOuts */ 0,
  1475. /* name */ "Carla-Rack",
  1476. /* label */ "carlarack",
  1477. /* maker */ "falkTX",
  1478. /* copyright */ "GNU GPL v2+",
  1479. CarlaEngineNative::_instantiateRack,
  1480. CarlaEngineNative::_cleanup,
  1481. CarlaEngineNative::_get_parameter_count,
  1482. CarlaEngineNative::_get_parameter_info,
  1483. CarlaEngineNative::_get_parameter_value,
  1484. CarlaEngineNative::_get_parameter_text,
  1485. CarlaEngineNative::_get_midi_program_count,
  1486. CarlaEngineNative::_get_midi_program_info,
  1487. CarlaEngineNative::_set_parameter_value,
  1488. CarlaEngineNative::_set_midi_program,
  1489. /* _set_custom_data */ nullptr,
  1490. CarlaEngineNative::_ui_show,
  1491. CarlaEngineNative::_ui_idle,
  1492. /* _ui_set_parameter_value */ nullptr,
  1493. /* _ui_set_midi_program */ nullptr,
  1494. /* _ui_set_custom_data */ nullptr,
  1495. CarlaEngineNative::_activate,
  1496. CarlaEngineNative::_deactivate,
  1497. CarlaEngineNative::_process,
  1498. CarlaEngineNative::_get_state,
  1499. CarlaEngineNative::_set_state,
  1500. CarlaEngineNative::_dispatcher
  1501. };
  1502. static const NativePluginDescriptor carlaPatchbayDesc = {
  1503. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1504. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1505. |NATIVE_PLUGIN_HAS_UI
  1506. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1507. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1508. |NATIVE_PLUGIN_USES_STATE
  1509. |NATIVE_PLUGIN_USES_TIME),
  1510. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1511. /* audioIns */ 2,
  1512. /* audioOuts */ 2,
  1513. /* midiIns */ 1,
  1514. /* midiOuts */ 1,
  1515. /* paramIns */ 0,
  1516. /* paramOuts */ 0,
  1517. /* name */ "Carla-Patchbay",
  1518. /* label */ "carlapatchbay",
  1519. /* maker */ "falkTX",
  1520. /* copyright */ "GNU GPL v2+",
  1521. CarlaEngineNative::_instantiatePatchbay,
  1522. CarlaEngineNative::_cleanup,
  1523. CarlaEngineNative::_get_parameter_count,
  1524. CarlaEngineNative::_get_parameter_info,
  1525. CarlaEngineNative::_get_parameter_value,
  1526. CarlaEngineNative::_get_parameter_text,
  1527. CarlaEngineNative::_get_midi_program_count,
  1528. CarlaEngineNative::_get_midi_program_info,
  1529. CarlaEngineNative::_set_parameter_value,
  1530. CarlaEngineNative::_set_midi_program,
  1531. /* _set_custom_data */ nullptr,
  1532. CarlaEngineNative::_ui_show,
  1533. CarlaEngineNative::_ui_idle,
  1534. /* _ui_set_parameter_value */ nullptr,
  1535. /* _ui_set_midi_program */ nullptr,
  1536. /* _ui_set_custom_data */ nullptr,
  1537. CarlaEngineNative::_activate,
  1538. CarlaEngineNative::_deactivate,
  1539. CarlaEngineNative::_process,
  1540. CarlaEngineNative::_get_state,
  1541. CarlaEngineNative::_set_state,
  1542. CarlaEngineNative::_dispatcher
  1543. };
  1544. static const NativePluginDescriptor carlaPatchbay3sDesc = {
  1545. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1546. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1547. |NATIVE_PLUGIN_HAS_UI
  1548. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1549. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1550. |NATIVE_PLUGIN_USES_STATE
  1551. |NATIVE_PLUGIN_USES_TIME),
  1552. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1553. /* audioIns */ 3,
  1554. /* audioOuts */ 2,
  1555. /* midiIns */ 1,
  1556. /* midiOuts */ 1,
  1557. /* paramIns */ 0,
  1558. /* paramOuts */ 0,
  1559. /* name */ "Carla-Patchbay (sidechain)",
  1560. /* label */ "carlapatchbay3s",
  1561. /* maker */ "falkTX",
  1562. /* copyright */ "GNU GPL v2+",
  1563. CarlaEngineNative::_instantiatePatchbay3s,
  1564. CarlaEngineNative::_cleanup,
  1565. CarlaEngineNative::_get_parameter_count,
  1566. CarlaEngineNative::_get_parameter_info,
  1567. CarlaEngineNative::_get_parameter_value,
  1568. CarlaEngineNative::_get_parameter_text,
  1569. CarlaEngineNative::_get_midi_program_count,
  1570. CarlaEngineNative::_get_midi_program_info,
  1571. CarlaEngineNative::_set_parameter_value,
  1572. CarlaEngineNative::_set_midi_program,
  1573. /* _set_custom_data */ nullptr,
  1574. CarlaEngineNative::_ui_show,
  1575. CarlaEngineNative::_ui_idle,
  1576. /* _ui_set_parameter_value */ nullptr,
  1577. /* _ui_set_midi_program */ nullptr,
  1578. /* _ui_set_custom_data */ nullptr,
  1579. CarlaEngineNative::_activate,
  1580. CarlaEngineNative::_deactivate,
  1581. CarlaEngineNative::_process,
  1582. CarlaEngineNative::_get_state,
  1583. CarlaEngineNative::_set_state,
  1584. CarlaEngineNative::_dispatcher
  1585. };
  1586. static const NativePluginDescriptor carlaPatchbay16Desc = {
  1587. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1588. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1589. |NATIVE_PLUGIN_HAS_UI
  1590. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1591. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1592. |NATIVE_PLUGIN_USES_STATE
  1593. |NATIVE_PLUGIN_USES_TIME),
  1594. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1595. /* audioIns */ 16,
  1596. /* audioOuts */ 16,
  1597. /* midiIns */ 1,
  1598. /* midiOuts */ 1,
  1599. /* paramIns */ 0,
  1600. /* paramOuts */ 0,
  1601. /* name */ "Carla-Patchbay (16chan)",
  1602. /* label */ "carlapatchbay16",
  1603. /* maker */ "falkTX",
  1604. /* copyright */ "GNU GPL v2+",
  1605. CarlaEngineNative::_instantiatePatchbay16,
  1606. CarlaEngineNative::_cleanup,
  1607. CarlaEngineNative::_get_parameter_count,
  1608. CarlaEngineNative::_get_parameter_info,
  1609. CarlaEngineNative::_get_parameter_value,
  1610. CarlaEngineNative::_get_parameter_text,
  1611. CarlaEngineNative::_get_midi_program_count,
  1612. CarlaEngineNative::_get_midi_program_info,
  1613. CarlaEngineNative::_set_parameter_value,
  1614. CarlaEngineNative::_set_midi_program,
  1615. /* _set_custom_data */ nullptr,
  1616. CarlaEngineNative::_ui_show,
  1617. CarlaEngineNative::_ui_idle,
  1618. /* _ui_set_parameter_value */ nullptr,
  1619. /* _ui_set_midi_program */ nullptr,
  1620. /* _ui_set_custom_data */ nullptr,
  1621. CarlaEngineNative::_activate,
  1622. CarlaEngineNative::_deactivate,
  1623. CarlaEngineNative::_process,
  1624. CarlaEngineNative::_get_state,
  1625. CarlaEngineNative::_set_state,
  1626. CarlaEngineNative::_dispatcher
  1627. };
  1628. static const NativePluginDescriptor carlaPatchbay32Desc = {
  1629. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1630. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1631. |NATIVE_PLUGIN_HAS_UI
  1632. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1633. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1634. |NATIVE_PLUGIN_USES_STATE
  1635. |NATIVE_PLUGIN_USES_TIME),
  1636. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1637. /* audioIns */ 32,
  1638. /* audioOuts */ 32,
  1639. /* midiIns */ 1,
  1640. /* midiOuts */ 1,
  1641. /* paramIns */ 0,
  1642. /* paramOuts */ 0,
  1643. /* name */ "Carla-Patchbay (32chan)",
  1644. /* label */ "carlapatchbay32",
  1645. /* maker */ "falkTX",
  1646. /* copyright */ "GNU GPL v2+",
  1647. CarlaEngineNative::_instantiatePatchbay32,
  1648. CarlaEngineNative::_cleanup,
  1649. CarlaEngineNative::_get_parameter_count,
  1650. CarlaEngineNative::_get_parameter_info,
  1651. CarlaEngineNative::_get_parameter_value,
  1652. CarlaEngineNative::_get_parameter_text,
  1653. CarlaEngineNative::_get_midi_program_count,
  1654. CarlaEngineNative::_get_midi_program_info,
  1655. CarlaEngineNative::_set_parameter_value,
  1656. CarlaEngineNative::_set_midi_program,
  1657. /* _set_custom_data */ nullptr,
  1658. CarlaEngineNative::_ui_show,
  1659. CarlaEngineNative::_ui_idle,
  1660. /* _ui_set_parameter_value */ nullptr,
  1661. /* _ui_set_midi_program */ nullptr,
  1662. /* _ui_set_custom_data */ nullptr,
  1663. CarlaEngineNative::_activate,
  1664. CarlaEngineNative::_deactivate,
  1665. CarlaEngineNative::_process,
  1666. CarlaEngineNative::_get_state,
  1667. CarlaEngineNative::_set_state,
  1668. CarlaEngineNative::_dispatcher
  1669. };
  1670. CARLA_BACKEND_END_NAMESPACE
  1671. // -----------------------------------------------------------------------
  1672. CARLA_EXPORT
  1673. void carla_register_native_plugin_carla();
  1674. void carla_register_native_plugin_carla()
  1675. {
  1676. CARLA_BACKEND_USE_NAMESPACE;
  1677. carla_register_native_plugin(&carlaRackDesc);
  1678. carla_register_native_plugin(&carlaPatchbayDesc);
  1679. carla_register_native_plugin(&carlaPatchbay3sDesc);
  1680. carla_register_native_plugin(&carlaPatchbay16Desc);
  1681. carla_register_native_plugin(&carlaPatchbay32Desc);
  1682. }
  1683. // -----------------------------------------------------------------------
  1684. CARLA_EXPORT
  1685. const NativePluginDescriptor* carla_get_native_rack_plugin();
  1686. const NativePluginDescriptor* carla_get_native_rack_plugin()
  1687. {
  1688. // if this is called then we're running as special plugin
  1689. gNeedsJuceHandling = true;
  1690. CARLA_BACKEND_USE_NAMESPACE;
  1691. return &carlaRackDesc;
  1692. }
  1693. CARLA_EXPORT
  1694. const NativePluginDescriptor* carla_get_native_patchbay_plugin();
  1695. const NativePluginDescriptor* carla_get_native_patchbay_plugin()
  1696. {
  1697. // if this is called then we're running as special plugin
  1698. gNeedsJuceHandling = true;
  1699. CARLA_BACKEND_USE_NAMESPACE;
  1700. return &carlaPatchbayDesc;
  1701. }
  1702. // -----------------------------------------------------------------------
  1703. // Extra stuff for linking purposes
  1704. #ifdef CARLA_PLUGIN_EXPORT
  1705. CARLA_BACKEND_START_NAMESPACE
  1706. CarlaEngine* CarlaEngine::newJack() { return nullptr; }
  1707. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  1708. CarlaEngine* CarlaEngine::newJuce(const AudioApi) { return nullptr; }
  1709. uint CarlaEngine::getJuceApiCount() { return 0; }
  1710. const char* CarlaEngine::getJuceApiName(const uint) { return nullptr; }
  1711. const char* const* CarlaEngine::getJuceApiDeviceNames(const uint) { return nullptr; }
  1712. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const uint, const char* const) { return nullptr; }
  1713. # else
  1714. CarlaEngine* CarlaEngine::newRtAudio(const AudioApi) { return nullptr; }
  1715. uint CarlaEngine::getRtAudioApiCount() { return 0; }
  1716. const char* CarlaEngine::getRtAudioApiName(const uint) { return nullptr; }
  1717. const char* const* CarlaEngine::getRtAudioApiDeviceNames(const uint) { return nullptr; }
  1718. const EngineDriverDeviceInfo* CarlaEngine::getRtAudioDeviceInfo(const uint, const char* const) { return nullptr; }
  1719. # endif
  1720. CARLA_BACKEND_END_NAMESPACE
  1721. #include "CarlaHostCommon.cpp"
  1722. #include "CarlaPluginUI.cpp"
  1723. #include "CarlaDssiUtils.cpp"
  1724. #include "CarlaPatchbayUtils.cpp"
  1725. #include "CarlaPipeUtils.cpp"
  1726. #include "CarlaStateUtils.cpp"
  1727. #include "CarlaJuceEvents.cpp"
  1728. #endif
  1729. // -----------------------------------------------------------------------