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

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  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. #ifdef CARLA_OS_WIN
  22. # error This file should not be compiled for Windows
  23. #endif
  24. #include "CarlaEngineInternal.hpp"
  25. #include "CarlaPlugin.hpp"
  26. #include "CarlaBackendUtils.hpp"
  27. #include "CarlaMathUtils.hpp"
  28. #include "CarlaPipeUtils.hpp"
  29. #include "CarlaStateUtils.hpp"
  30. #include "CarlaNative.hpp"
  31. #include <QtCore/QTextStream>
  32. #include <QtXml/QDomNode>
  33. CARLA_BACKEND_START_NAMESPACE
  34. // -----------------------------------------------------------------------
  35. class CarlaEngineNativeUI : public CarlaPipeServer
  36. {
  37. public:
  38. enum UiState {
  39. UiNone = 0,
  40. UiHide,
  41. UiShow,
  42. UiCrashed
  43. };
  44. CarlaEngineNativeUI(CarlaEngine* const engine)
  45. : fEngine(engine),
  46. fUiState(UiNone)
  47. {
  48. carla_debug("CarlaEngineNativeUI::CarlaEngineNativeUI(%p)", engine);
  49. }
  50. ~CarlaEngineNativeUI() override
  51. {
  52. CARLA_ASSERT_INT(fUiState == UiNone, fUiState);
  53. carla_debug("CarlaEngineNativeUI::~CarlaEngineNativeUI()");
  54. }
  55. void setData(const char* const filename, const double sampleRate, const char* const uiTitle)
  56. {
  57. fFilename = filename;
  58. fSampleRate = CarlaString(sampleRate);
  59. fUiTitle = uiTitle;
  60. }
  61. UiState getAndResetUiState() noexcept
  62. {
  63. const UiState uiState(fUiState);
  64. fUiState = UiNone;
  65. return uiState;
  66. }
  67. void start()
  68. {
  69. CarlaPipeServer::start(fFilename, fSampleRate, fUiTitle);
  70. writeMsg("show\n", 5);
  71. }
  72. protected:
  73. void msgReceived(const char* const msg) override
  74. {
  75. if (std::strcmp(msg, "exiting") == 0)
  76. {
  77. waitChildClose();
  78. fUiState = UiHide;
  79. }
  80. else if (std::strcmp(msg, "set_engine_option") == 0)
  81. {
  82. int option, value;
  83. const char* valueStr;
  84. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(option),);
  85. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(value),);
  86. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(valueStr),);
  87. fEngine->setOption((EngineOption)option, value, valueStr);
  88. delete[] valueStr;
  89. }
  90. else if (std::strcmp(msg, "load_file") == 0)
  91. {
  92. const char* filename;
  93. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename),);
  94. fEngine->loadFile(filename);
  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),);
  101. fEngine->loadProject(filename);
  102. delete[] filename;
  103. }
  104. else if (std::strcmp(msg, "save_project") == 0)
  105. {
  106. const char* filename;
  107. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename),);
  108. fEngine->saveProject(filename);
  109. delete[] filename;
  110. }
  111. else if (std::strcmp(msg, "patchbay_connect") == 0)
  112. {
  113. int portA, portB;
  114. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(portA),);
  115. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(portB),);
  116. fEngine->patchbayConnect(portA, portB);
  117. }
  118. else if (std::strcmp(msg, "patchbay_disconnect") == 0)
  119. {
  120. uint connectionId;
  121. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(connectionId),);
  122. fEngine->patchbayDisconnect(connectionId);
  123. }
  124. else if (std::strcmp(msg, "patchbay_refresh") == 0)
  125. {
  126. fEngine->patchbayRefresh();
  127. }
  128. else if (std::strcmp(msg, "transport_play") == 0)
  129. {
  130. fEngine->transportPlay();
  131. }
  132. else if (std::strcmp(msg, "transport_pause") == 0)
  133. {
  134. fEngine->transportPause();
  135. }
  136. else if (std::strcmp(msg, "transport_relocate") == 0)
  137. {
  138. long frame;
  139. CARLA_SAFE_ASSERT_RETURN(readNextLineAsLong(frame),);
  140. fEngine->transportRelocate((uint64_t)frame);
  141. }
  142. else if (std::strcmp(msg, "add_plugin") == 0)
  143. {
  144. int btype, ptype;
  145. const char* filename = nullptr;
  146. const char* name;
  147. const char* label;
  148. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(btype),);
  149. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(ptype),);
  150. readNextLineAsString(filename); // can be null
  151. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(name),);
  152. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(label),);
  153. if (filename != nullptr && std::strcmp(filename, "(null)") == 0)
  154. {
  155. delete[] filename;
  156. filename = nullptr;
  157. }
  158. if (std::strcmp(name, "(null)") == 0)
  159. {
  160. delete[] name;
  161. name = nullptr;
  162. }
  163. fEngine->addPlugin((BinaryType)btype, (PluginType)ptype, filename, name, label);
  164. if (filename != nullptr)
  165. delete[] filename;
  166. if (name != nullptr)
  167. delete[] name;
  168. delete[] label;
  169. }
  170. else if (std::strcmp(msg, "remove_plugin") == 0)
  171. {
  172. uint pluginId;
  173. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  174. fEngine->removePlugin(pluginId);
  175. }
  176. else if (std::strcmp(msg, "remove_all_plugins") == 0)
  177. {
  178. fEngine->removeAllPlugins();
  179. }
  180. else if (std::strcmp(msg, "rename_plugin") == 0)
  181. {
  182. uint pluginId;
  183. const char* newName;
  184. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  185. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(newName),);
  186. /*const char* name =*/ fEngine->renamePlugin(pluginId, newName);
  187. delete[] newName;
  188. }
  189. else if (std::strcmp(msg, "clone_plugin") == 0)
  190. {
  191. uint pluginId;
  192. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  193. fEngine->clonePlugin(pluginId);
  194. }
  195. else if (std::strcmp(msg, "replace_plugin") == 0)
  196. {
  197. uint pluginId;
  198. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  199. fEngine->replacePlugin(pluginId);
  200. }
  201. else if (std::strcmp(msg, "switch_plugins") == 0)
  202. {
  203. uint pluginIdA, pluginIdB;
  204. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginIdA),);
  205. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginIdB),);
  206. fEngine->switchPlugins(pluginIdA, pluginIdB);
  207. }
  208. else if (std::strcmp(msg, "load_plugin_state") == 0)
  209. {
  210. uint pluginId;
  211. const char* filename;
  212. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  213. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename),);
  214. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  215. plugin->loadStateFromFile(filename);
  216. delete[] filename;
  217. }
  218. else if (std::strcmp(msg, "save_plugin_state") == 0)
  219. {
  220. uint pluginId;
  221. const char* filename;
  222. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  223. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename),);
  224. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  225. plugin->saveStateToFile(filename);
  226. delete[] filename;
  227. }
  228. else if (std::strcmp(msg, "set_option") == 0)
  229. {
  230. uint pluginId;
  231. uint option;
  232. bool yesNo;
  233. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  234. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(option),);
  235. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(yesNo),);
  236. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  237. plugin->setOption(option, yesNo);
  238. }
  239. else if (std::strcmp(msg, "set_active") == 0)
  240. {
  241. uint pluginId;
  242. bool onOff;
  243. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  244. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff),);
  245. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  246. plugin->setActive(onOff, true, false);
  247. }
  248. else if (std::strcmp(msg, "set_drywet") == 0)
  249. {
  250. uint pluginId;
  251. float value;
  252. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  253. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value),);
  254. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  255. plugin->setDryWet(value, true, false);
  256. }
  257. else if (std::strcmp(msg, "set_volume") == 0)
  258. {
  259. uint pluginId;
  260. float value;
  261. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  262. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value),);
  263. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  264. plugin->setVolume(value, true, false);
  265. }
  266. else if (std::strcmp(msg, "set_balance_left") == 0)
  267. {
  268. uint pluginId;
  269. float value;
  270. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  271. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value),);
  272. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  273. plugin->setBalanceLeft(value, true, false);
  274. }
  275. else if (std::strcmp(msg, "set_balance_right") == 0)
  276. {
  277. uint pluginId;
  278. float value;
  279. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  280. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value),);
  281. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  282. plugin->setBalanceRight(value, true, false);
  283. }
  284. else if (std::strcmp(msg, "set_panning") == 0)
  285. {
  286. uint pluginId;
  287. float value;
  288. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  289. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value),);
  290. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  291. plugin->setPanning(value, true, false);
  292. }
  293. else if (std::strcmp(msg, "set_ctrl_channel") == 0)
  294. {
  295. uint pluginId;
  296. int channel;
  297. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  298. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(channel),);
  299. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS,);
  300. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  301. plugin->setCtrlChannel(int8_t(channel), true, false);
  302. }
  303. else if (std::strcmp(msg, "set_parameter_value") == 0)
  304. {
  305. uint pluginId;
  306. uint parameterId;
  307. float value;
  308. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  309. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(parameterId),);
  310. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value),);
  311. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  312. plugin->setParameterValue(parameterId, value, true, true, false);
  313. }
  314. else if (std::strcmp(msg, "set_parameter_midi_channel") == 0)
  315. {
  316. uint pluginId;
  317. uint parameterId;
  318. int channel;
  319. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  320. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(parameterId),);
  321. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(channel),);
  322. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS,);
  323. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  324. plugin->setParameterMidiChannel(parameterId, uint8_t(channel), true, false);
  325. }
  326. else if (std::strcmp(msg, "set_parameter_midi_cc") == 0)
  327. {
  328. uint pluginId;
  329. uint parameterId;
  330. int cc;
  331. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  332. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(parameterId),);
  333. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(cc),);
  334. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < 0x5F,);
  335. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  336. plugin->setParameterMidiCC(parameterId, int16_t(cc), true, false);
  337. }
  338. else if (std::strcmp(msg, "set_program") == 0)
  339. {
  340. uint pluginId;
  341. int index;
  342. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  343. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(index),);
  344. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  345. plugin->setProgram(index, true, true, false);
  346. }
  347. else if (std::strcmp(msg, "set_midi_program") == 0)
  348. {
  349. uint pluginId;
  350. int index;
  351. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  352. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(index),);
  353. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  354. plugin->setMidiProgram(index, true, true, false);
  355. }
  356. else if (std::strcmp(msg, "set_custom_data") == 0)
  357. {
  358. uint pluginId;
  359. const char* type;
  360. const char* key;
  361. const char* value;
  362. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  363. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(type),);
  364. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key),);
  365. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value),);
  366. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  367. plugin->setCustomData(type, key, value, true);
  368. }
  369. else if (std::strcmp(msg, "set_chunk_data") == 0)
  370. {
  371. uint pluginId;
  372. const char* cdata;
  373. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  374. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(cdata),);
  375. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  376. plugin->setChunkData(cdata);
  377. }
  378. else if (std::strcmp(msg, "prepare_for_save") == 0)
  379. {
  380. uint pluginId;
  381. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  382. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  383. plugin->prepareForSave();
  384. }
  385. else if (std::strcmp(msg, "send_midi_note") == 0)
  386. {
  387. uint pluginId;
  388. int channel, note, velocity;
  389. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  390. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(channel),);
  391. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(note),);
  392. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(velocity),);
  393. CARLA_SAFE_ASSERT_RETURN(channel >= 0 && channel < MAX_MIDI_CHANNELS,);
  394. CARLA_SAFE_ASSERT_RETURN(note >= 0 && channel < MAX_MIDI_VALUE,);
  395. CARLA_SAFE_ASSERT_RETURN(velocity >= 0 && channel < MAX_MIDI_VALUE,);
  396. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  397. plugin->sendMidiSingleNote(uint8_t(channel), uint8_t(note), uint8_t(velocity), true, true, false);
  398. }
  399. else if (std::strcmp(msg, "show_custom_ui") == 0)
  400. {
  401. uint pluginId;
  402. bool yesNo;
  403. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId),);
  404. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(yesNo),);
  405. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  406. plugin->showCustomUI(yesNo);
  407. }
  408. else
  409. {
  410. carla_stderr("msgReceived : %s", msg);
  411. }
  412. }
  413. private:
  414. CarlaEngine* const fEngine;
  415. CarlaString fFilename;
  416. CarlaString fSampleRate;
  417. CarlaString fUiTitle;
  418. UiState fUiState;
  419. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineNativeUI)
  420. };
  421. // -----------------------------------------------------------------------
  422. class CarlaEngineNative : public CarlaEngine
  423. {
  424. public:
  425. CarlaEngineNative(const NativeHostDescriptor* const host, const bool isPatchbay)
  426. : CarlaEngine(),
  427. pHost(host),
  428. fIsPatchbay(isPatchbay),
  429. fIsActive(false),
  430. fIsRunning(false),
  431. fUiServer(this)
  432. {
  433. carla_debug("CarlaEngineNative::CarlaEngineNative()");
  434. fTmpBuf[STR_MAX] = '\0';
  435. // set-up engine
  436. if (fIsPatchbay)
  437. {
  438. pData->options.processMode = ENGINE_PROCESS_MODE_PATCHBAY;
  439. pData->options.transportMode = ENGINE_TRANSPORT_MODE_PLUGIN;
  440. pData->options.forceStereo = false;
  441. pData->options.preferPluginBridges = false;
  442. pData->options.preferUiBridges = false;
  443. init("Carla-Patchbay");
  444. }
  445. else
  446. {
  447. pData->options.processMode = ENGINE_PROCESS_MODE_CONTINUOUS_RACK;
  448. pData->options.transportMode = ENGINE_TRANSPORT_MODE_PLUGIN;
  449. pData->options.forceStereo = true;
  450. pData->options.preferPluginBridges = false;
  451. pData->options.preferUiBridges = false;
  452. init("Carla-Rack");
  453. }
  454. setCallback(_ui_server_callback, this);
  455. }
  456. ~CarlaEngineNative() override
  457. {
  458. CARLA_ASSERT(! fIsActive);
  459. carla_debug("CarlaEngineNative::~CarlaEngineNative() - START");
  460. pData->aboutToClose = true;
  461. fIsRunning = false;
  462. removeAllPlugins();
  463. runPendingRtEvents();
  464. close();
  465. carla_debug("CarlaEngineNative::~CarlaEngineNative() - END");
  466. }
  467. protected:
  468. // -------------------------------------
  469. // CarlaEngine virtual calls
  470. bool init(const char* const clientName) override
  471. {
  472. carla_debug("CarlaEngineNative::init(\"%s\")", clientName);
  473. pData->bufferSize = pHost->get_buffer_size(pHost->handle);
  474. pData->sampleRate = pHost->get_sample_rate(pHost->handle);
  475. fIsRunning = true;
  476. CarlaEngine::init(clientName);
  477. return true;
  478. }
  479. bool isRunning() const noexcept override
  480. {
  481. return fIsRunning;
  482. }
  483. bool isOffline() const noexcept override
  484. {
  485. return pHost->is_offline(pHost->handle);
  486. }
  487. EngineType getType() const noexcept override
  488. {
  489. return kEngineTypePlugin;
  490. }
  491. const char* getCurrentDriverName() const noexcept override
  492. {
  493. return "Plugin";
  494. }
  495. // -------------------------------------------------------------------
  496. const char* renamePlugin(const unsigned int id, const char* const newName) override
  497. {
  498. if (const char* const retName = CarlaEngine::renamePlugin(id, newName))
  499. {
  500. uiServerCallback(ENGINE_CALLBACK_PLUGIN_RENAMED, id, 0, 0, 0.0f, retName);
  501. return retName;
  502. }
  503. return nullptr;
  504. }
  505. // -------------------------------------------------------------------
  506. void bufferSizeChanged(const uint32_t newBufferSize)
  507. {
  508. pData->bufferSize = newBufferSize;
  509. CarlaEngine::bufferSizeChanged(newBufferSize);
  510. }
  511. void sampleRateChanged(const double newSampleRate)
  512. {
  513. pData->sampleRate = newSampleRate;
  514. CarlaEngine::sampleRateChanged(newSampleRate);
  515. }
  516. // -------------------------------------------------------------------
  517. void uiServerSendPluginInfo(CarlaPlugin* const plugin)
  518. {
  519. const uint pluginId(plugin->getId());
  520. std::sprintf(fTmpBuf, "PLUGIN_INFO_%i\n", pluginId);
  521. fUiServer.writeMsg(fTmpBuf);
  522. std::sprintf(fTmpBuf, "%i:%i:%i:%li:%i:%i\n", plugin->getType(), plugin->getCategory(), plugin->getHints(), plugin->getUniqueId(), plugin->getOptionsAvailable(), plugin->getOptionsEnabled());
  523. fUiServer.writeMsg(fTmpBuf);
  524. if (const char* const filename = plugin->getFilename())
  525. {
  526. std::sprintf(fTmpBuf, "%s", filename);
  527. fUiServer.writeAndFixMsg(fTmpBuf);
  528. }
  529. else
  530. fUiServer.writeMsg("\n");
  531. if (const char* const name = plugin->getName())
  532. {
  533. std::sprintf(fTmpBuf, "%s", name);
  534. fUiServer.writeAndFixMsg(fTmpBuf);
  535. }
  536. else
  537. fUiServer.writeMsg("\n");
  538. if (const char* const iconName = plugin->getIconName())
  539. {
  540. std::sprintf(fTmpBuf, "%s", iconName);
  541. fUiServer.writeAndFixMsg(fTmpBuf);
  542. }
  543. else
  544. fUiServer.writeMsg("\n");
  545. plugin->getRealName(fTmpBuf);
  546. fUiServer.writeAndFixMsg(fTmpBuf);
  547. plugin->getLabel(fTmpBuf);
  548. fUiServer.writeAndFixMsg(fTmpBuf);
  549. plugin->getMaker(fTmpBuf);
  550. fUiServer.writeAndFixMsg(fTmpBuf);
  551. plugin->getCopyright(fTmpBuf);
  552. fUiServer.writeAndFixMsg(fTmpBuf);
  553. std::sprintf(fTmpBuf, "AUDIO_COUNT_%i:%i:%i\n", pluginId, plugin->getAudioInCount(), plugin->getAudioOutCount());
  554. fUiServer.writeMsg(fTmpBuf);
  555. std::sprintf(fTmpBuf, "MIDI_COUNT_%i:%i:%i\n", pluginId, plugin->getMidiInCount(), plugin->getMidiOutCount());
  556. fUiServer.writeMsg(fTmpBuf);
  557. }
  558. void uiServerSendPluginParameters(CarlaPlugin* const plugin)
  559. {
  560. const uint pluginId(plugin->getId());
  561. uint32_t ins, outs, count;
  562. plugin->getParameterCountInfo(ins, outs);
  563. count = plugin->getParameterCount();
  564. std::sprintf(fTmpBuf, "PARAMETER_COUNT_%i:%i:%i:%i\n", pluginId, ins, outs, count);
  565. fUiServer.writeMsg(fTmpBuf);
  566. for (uint32_t i=0; i<count; ++i)
  567. {
  568. const ParameterData& paramData(plugin->getParameterData(i));
  569. const ParameterRanges& paramRanges(plugin->getParameterRanges(i));
  570. std::sprintf(fTmpBuf, "PARAMETER_DATA_%i:%i\n", pluginId, i);
  571. fUiServer.writeMsg(fTmpBuf);
  572. std::sprintf(fTmpBuf, "%i:%i:%i:%i\n", paramData.type, paramData.hints, paramData.midiChannel, paramData.midiCC);
  573. fUiServer.writeMsg(fTmpBuf);
  574. plugin->getParameterName(i, fTmpBuf);
  575. fUiServer.writeAndFixMsg(fTmpBuf);
  576. plugin->getParameterUnit(i, fTmpBuf);
  577. fUiServer.writeAndFixMsg(fTmpBuf);
  578. std::sprintf(fTmpBuf, "PARAMETER_RANGES_%i:%i\n", pluginId, i);
  579. fUiServer.writeMsg(fTmpBuf);
  580. std::sprintf(fTmpBuf, "%f:%f:%f:%f:%f:%f\n", paramRanges.def, paramRanges.min, paramRanges.max, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  581. fUiServer.writeMsg(fTmpBuf);
  582. std::sprintf(fTmpBuf, "PARAMVAL_%i:%i\n", pluginId, i);
  583. fUiServer.writeMsg(fTmpBuf);
  584. std::sprintf(fTmpBuf, "%f\n", plugin->getParameterValue(i));
  585. fUiServer.writeMsg(fTmpBuf);
  586. }
  587. }
  588. void uiServerSendPluginPrograms(CarlaPlugin* const plugin)
  589. {
  590. const uint pluginId(plugin->getId());
  591. uint32_t count = plugin->getProgramCount();
  592. std::sprintf(fTmpBuf, "PROGRAM_COUNT_%i:%i:%i\n", pluginId, count, plugin->getCurrentProgram());
  593. fUiServer.writeMsg(fTmpBuf);
  594. for (uint32_t i=0; i<count; ++i)
  595. {
  596. std::sprintf(fTmpBuf, "PROGRAM_NAME_%i:%i\n", pluginId, i);
  597. fUiServer.writeMsg(fTmpBuf);
  598. plugin->getProgramName(i, fTmpBuf);
  599. fUiServer.writeAndFixMsg(fTmpBuf);
  600. }
  601. count = plugin->getMidiProgramCount();
  602. std::sprintf(fTmpBuf, "MIDI_PROGRAM_COUNT_%i:%i:%i\n", pluginId, count, plugin->getCurrentMidiProgram());
  603. fUiServer.writeMsg(fTmpBuf);
  604. for (uint32_t i=0; i<count; ++i)
  605. {
  606. std::sprintf(fTmpBuf, "MIDI_PROGRAM_DATA_%i:%i\n", pluginId, i);
  607. fUiServer.writeMsg(fTmpBuf);
  608. const MidiProgramData& mpData(plugin->getMidiProgramData(i));
  609. std::sprintf(fTmpBuf, "%i:%i\n", mpData.bank, mpData.program);
  610. fUiServer.writeMsg(fTmpBuf);
  611. std::sprintf(fTmpBuf, "%s", mpData.name);
  612. fUiServer.writeAndFixMsg(fTmpBuf);
  613. }
  614. }
  615. void uiServerCallback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr)
  616. {
  617. if (! fIsRunning)
  618. return;
  619. if (! fUiServer.isOk())
  620. return;
  621. CarlaPlugin* plugin;
  622. switch (action)
  623. {
  624. case ENGINE_CALLBACK_RELOAD_INFO:
  625. plugin = getPlugin(pluginId);
  626. if (plugin != nullptr && plugin->isEnabled())
  627. {
  628. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  629. uiServerSendPluginInfo(plugin);
  630. }
  631. break;
  632. case ENGINE_CALLBACK_RELOAD_PARAMETERS:
  633. plugin = getPlugin(pluginId);
  634. if (plugin != nullptr && plugin->isEnabled())
  635. {
  636. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  637. uiServerSendPluginParameters(plugin);
  638. }
  639. break;
  640. case ENGINE_CALLBACK_RELOAD_PROGRAMS:
  641. plugin = getPlugin(pluginId);
  642. if (plugin != nullptr && plugin->isEnabled())
  643. {
  644. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  645. uiServerSendPluginPrograms(plugin);
  646. }
  647. break;
  648. case ENGINE_CALLBACK_RELOAD_ALL:
  649. case ENGINE_CALLBACK_PLUGIN_ADDED:
  650. plugin = getPlugin(pluginId);
  651. if (plugin != nullptr && plugin->isEnabled())
  652. {
  653. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  654. uiServerSendPluginInfo(plugin);
  655. uiServerSendPluginParameters(plugin);
  656. uiServerSendPluginPrograms(plugin);
  657. }
  658. break;
  659. default:
  660. break;
  661. }
  662. std::sprintf(fTmpBuf, "ENGINE_CALLBACK_%i\n", int(action));
  663. fUiServer.writeMsg(fTmpBuf);
  664. std::sprintf(fTmpBuf, "%u\n", pluginId);
  665. fUiServer.writeMsg(fTmpBuf);
  666. std::sprintf(fTmpBuf, "%i\n", value1);
  667. fUiServer.writeMsg(fTmpBuf);
  668. std::sprintf(fTmpBuf, "%i\n", value2);
  669. fUiServer.writeMsg(fTmpBuf);
  670. std::sprintf(fTmpBuf, "%f\n", value3);
  671. fUiServer.writeMsg(fTmpBuf);
  672. fUiServer.writeAndFixMsg(valueStr);
  673. }
  674. // -------------------------------------------------------------------
  675. // Plugin parameter calls
  676. uint32_t getParameterCount() const
  677. {
  678. if (CarlaPlugin* const plugin = _getFirstPlugin())
  679. return plugin->getParameterCount();
  680. return 0;
  681. }
  682. const NativeParameter* getParameterInfo(const uint32_t index) const
  683. {
  684. if (CarlaPlugin* const plugin = _getFirstPlugin())
  685. {
  686. if (index < plugin->getParameterCount())
  687. {
  688. static NativeParameter param;
  689. static char strBufName[STR_MAX+1];
  690. static char strBufUnit[STR_MAX+1];
  691. const ParameterData& paramData(plugin->getParameterData(index));
  692. const ParameterRanges& paramRanges(plugin->getParameterRanges(index));
  693. plugin->getParameterName(index, strBufName);
  694. plugin->getParameterUnit(index, strBufUnit);
  695. unsigned int hints = 0x0;
  696. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  697. hints |= ::PARAMETER_IS_BOOLEAN;
  698. if (paramData.hints & PARAMETER_IS_INTEGER)
  699. hints |= ::PARAMETER_IS_INTEGER;
  700. if (paramData.hints & PARAMETER_IS_LOGARITHMIC)
  701. hints |= ::PARAMETER_IS_LOGARITHMIC;
  702. if (paramData.hints & PARAMETER_IS_AUTOMABLE)
  703. hints |= ::PARAMETER_IS_AUTOMABLE;
  704. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  705. hints |= ::PARAMETER_USES_SAMPLE_RATE;
  706. if (paramData.hints & PARAMETER_USES_SCALEPOINTS)
  707. hints |= ::PARAMETER_USES_SCALEPOINTS;
  708. if (paramData.hints & PARAMETER_USES_CUSTOM_TEXT)
  709. hints |= ::PARAMETER_USES_CUSTOM_TEXT;
  710. if (paramData.type == PARAMETER_INPUT || paramData.type == PARAMETER_OUTPUT)
  711. {
  712. if (paramData.hints & PARAMETER_IS_ENABLED)
  713. hints |= ::PARAMETER_IS_ENABLED;
  714. if (paramData.type == PARAMETER_OUTPUT)
  715. hints |= ::PARAMETER_IS_OUTPUT;
  716. }
  717. param.hints = static_cast<NativeParameterHints>(hints);
  718. param.name = strBufName;
  719. param.unit = strBufUnit;
  720. param.ranges.def = paramRanges.def;
  721. param.ranges.min = paramRanges.min;
  722. param.ranges.max = paramRanges.max;
  723. param.ranges.step = paramRanges.step;
  724. param.ranges.stepSmall = paramRanges.stepSmall;
  725. param.ranges.stepLarge = paramRanges.stepLarge;
  726. param.scalePointCount = 0; // TODO
  727. param.scalePoints = nullptr;
  728. return &param;
  729. }
  730. }
  731. return nullptr;
  732. }
  733. float getParameterValue(const uint32_t index) const
  734. {
  735. if (CarlaPlugin* const plugin = _getFirstPlugin())
  736. {
  737. if (index < plugin->getParameterCount())
  738. return plugin->getParameterValue(index);
  739. }
  740. return 0.0f;
  741. }
  742. const char* getParameterText(const uint32_t index, const float value) const
  743. {
  744. if (CarlaPlugin* const plugin = _getFirstPlugin())
  745. {
  746. if (index < plugin->getParameterCount())
  747. {
  748. static char strBuf[STR_MAX+1];
  749. carla_zeroChar(strBuf, STR_MAX+1);
  750. plugin->getParameterText(index, value, strBuf);
  751. return strBuf;
  752. }
  753. }
  754. return nullptr;
  755. }
  756. // -------------------------------------------------------------------
  757. // Plugin midi-program calls
  758. uint32_t getMidiProgramCount() const
  759. {
  760. if (CarlaPlugin* const plugin = _getFirstPlugin())
  761. return plugin->getMidiProgramCount();
  762. return 0;
  763. }
  764. const NativeMidiProgram* getMidiProgramInfo(const uint32_t index) const
  765. {
  766. if (CarlaPlugin* const plugin = _getFirstPlugin())
  767. {
  768. if (index < plugin->getMidiProgramCount())
  769. {
  770. static NativeMidiProgram midiProg;
  771. {
  772. const MidiProgramData& midiProgData(plugin->getMidiProgramData(index));
  773. midiProg.bank = midiProgData.bank;
  774. midiProg.program = midiProgData.program;
  775. midiProg.name = midiProgData.name;
  776. }
  777. return &midiProg;
  778. }
  779. }
  780. return nullptr;
  781. }
  782. // -------------------------------------------------------------------
  783. // Plugin state calls
  784. void setParameterValue(const uint32_t index, const float value)
  785. {
  786. if (CarlaPlugin* const plugin = _getFirstPlugin())
  787. {
  788. if (index < plugin->getParameterCount())
  789. plugin->setParameterValue(index, value, false, false, false);
  790. }
  791. }
  792. void setMidiProgram(const uint8_t, const uint32_t bank, const uint32_t program)
  793. {
  794. if (CarlaPlugin* const plugin = _getFirstPlugin())
  795. plugin->setMidiProgramById(bank, program, false, false, false);
  796. }
  797. // -------------------------------------------------------------------
  798. // Plugin process calls
  799. void activate()
  800. {
  801. #if 0
  802. for (uint32_t i=0; i < pData->curPluginCount; ++i)
  803. {
  804. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  805. if (plugin == nullptr || ! plugin->isEnabled())
  806. continue;
  807. plugin->setActive(true, true, false);
  808. }
  809. #endif
  810. fIsActive = true;
  811. }
  812. void deactivate()
  813. {
  814. fIsActive = false;
  815. #if 0
  816. for (uint32_t i=0; i < pData->curPluginCount; ++i)
  817. {
  818. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  819. if (plugin == nullptr || ! plugin->isEnabled())
  820. continue;
  821. plugin->setActive(false, true, false);
  822. }
  823. #endif
  824. // just in case
  825. runPendingRtEvents();
  826. }
  827. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount)
  828. {
  829. if (pData->curPluginCount == 0 && ! fIsPatchbay)
  830. {
  831. FLOAT_COPY(outBuffer[0], inBuffer[0], frames);
  832. FLOAT_COPY(outBuffer[1], inBuffer[1], frames);
  833. return runPendingRtEvents();;
  834. }
  835. // ---------------------------------------------------------------
  836. // Time Info
  837. const NativeTimeInfo* const timeInfo(pHost->get_time_info(pHost->handle));
  838. pData->timeInfo.playing = timeInfo->playing;
  839. pData->timeInfo.frame = timeInfo->frame;
  840. pData->timeInfo.usecs = timeInfo->usecs;
  841. pData->timeInfo.valid = 0x0;
  842. if (timeInfo->bbt.valid)
  843. {
  844. pData->timeInfo.valid |= EngineTimeInfo::kValidBBT;
  845. pData->timeInfo.bbt.bar = timeInfo->bbt.bar;
  846. pData->timeInfo.bbt.beat = timeInfo->bbt.beat;
  847. pData->timeInfo.bbt.tick = timeInfo->bbt.tick;
  848. pData->timeInfo.bbt.barStartTick = timeInfo->bbt.barStartTick;
  849. pData->timeInfo.bbt.beatsPerBar = timeInfo->bbt.beatsPerBar;
  850. pData->timeInfo.bbt.beatType = timeInfo->bbt.beatType;
  851. pData->timeInfo.bbt.ticksPerBeat = timeInfo->bbt.ticksPerBeat;
  852. pData->timeInfo.bbt.beatsPerMinute = timeInfo->bbt.beatsPerMinute;
  853. }
  854. // ---------------------------------------------------------------
  855. // initialize events
  856. carla_zeroStruct<EngineEvent>(pData->bufEvents.in, kMaxEngineEventInternalCount);
  857. carla_zeroStruct<EngineEvent>(pData->bufEvents.out, kMaxEngineEventInternalCount);
  858. // ---------------------------------------------------------------
  859. // events input (before processing)
  860. {
  861. uint32_t engineEventIndex = 0;
  862. for (uint32_t i=0; i < midiEventCount && engineEventIndex < kMaxEngineEventInternalCount; ++i)
  863. {
  864. const NativeMidiEvent& midiEvent(midiEvents[i]);
  865. EngineEvent& engineEvent(pData->bufEvents.in[engineEventIndex++]);
  866. engineEvent.time = midiEvent.time;
  867. engineEvent.fillFromMidiData(midiEvent.size, midiEvent.data);
  868. if (engineEventIndex >= kMaxEngineEventInternalCount)
  869. break;
  870. }
  871. }
  872. if (fIsPatchbay)
  873. {
  874. // -----------------------------------------------------------
  875. // create audio buffers
  876. //float* inBuf[2] = { inBuffer[0], inBuffer[1] };
  877. //float* outBuf[2] = { outBuffer[0], outBuffer[1] };
  878. //uint32_t bufCount[2] = { 2, 2 };
  879. // -----------------------------------------------------------
  880. // process
  881. //pData->processPatchbay(inBuf, outBuf, bufCount, frames, isOffline());
  882. }
  883. else
  884. {
  885. // -----------------------------------------------------------
  886. // create audio buffers
  887. float* inBuf[2] = { inBuffer[0], inBuffer[1] };
  888. float* outBuf[2] = { outBuffer[0], outBuffer[1] };
  889. // -----------------------------------------------------------
  890. // process
  891. pData->processRack(inBuf, outBuf, frames, isOffline());
  892. }
  893. // ---------------------------------------------------------------
  894. // events output (after processing)
  895. carla_zeroStruct<EngineEvent>(pData->bufEvents.in, kMaxEngineEventInternalCount);
  896. {
  897. NativeMidiEvent midiEvent;
  898. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  899. {
  900. const EngineEvent& engineEvent(pData->bufEvents.out[i]);
  901. if (engineEvent.type == kEngineEventTypeNull)
  902. break;
  903. midiEvent.time = engineEvent.time;
  904. if (engineEvent.type == CarlaBackend::kEngineEventTypeControl)
  905. {
  906. midiEvent.port = 0;
  907. engineEvent.ctrl.dumpToMidiData(engineEvent.channel, midiEvent.size, midiEvent.data);
  908. }
  909. else if (engineEvent.type == kEngineEventTypeMidi)
  910. {
  911. if (engineEvent.midi.size > 4 || engineEvent.midi.dataExt != nullptr)
  912. continue;
  913. midiEvent.port = engineEvent.midi.port;
  914. midiEvent.size = engineEvent.midi.size;
  915. midiEvent.data[0] = static_cast<uint8_t>(engineEvent.midi.data[0] + engineEvent.channel);
  916. for (uint8_t j=1; j < midiEvent.size; ++j)
  917. midiEvent.data[j] = engineEvent.midi.data[j];
  918. }
  919. else
  920. {
  921. carla_stderr("Unknown event type...");
  922. continue;
  923. }
  924. pHost->write_midi_event(pHost->handle, &midiEvent);
  925. }
  926. }
  927. runPendingRtEvents();
  928. }
  929. // -------------------------------------------------------------------
  930. // Plugin UI calls
  931. void uiShow(const bool show)
  932. {
  933. if (show)
  934. {
  935. CarlaString path(pHost->resourceDir);
  936. path += "/carla-plugin";
  937. carla_stdout("Trying to start carla-plugin using \"%s\"", path.getBuffer());
  938. fUiServer.setData(path.getBuffer(), pData->sampleRate, pHost->uiName);
  939. fUiServer.start();
  940. for (uint i=0; i < pData->curPluginCount; ++i)
  941. {
  942. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  943. if (plugin != nullptr && plugin->isEnabled())
  944. {
  945. uiServerCallback(ENGINE_CALLBACK_PLUGIN_ADDED, i, 0, 0, 0.0f, plugin->getName());
  946. }
  947. }
  948. }
  949. else
  950. {
  951. fUiServer.stop();
  952. }
  953. }
  954. void uiIdle()
  955. {
  956. CarlaEngine::idle();
  957. fUiServer.idle();
  958. if (! fUiServer.isOk())
  959. return;
  960. for (uint i=0; i < pData->curPluginCount; ++i)
  961. {
  962. const EnginePluginData& plugData(pData->plugins[i]);
  963. const CarlaPlugin* const plugin(pData->plugins[i].plugin);
  964. std::sprintf(fTmpBuf, "PEAKS_%i\n", i);
  965. fUiServer.writeMsg(fTmpBuf);
  966. std::sprintf(fTmpBuf, "%f:%f:%f:%f\n", plugData.insPeak[0], plugData.insPeak[1], plugData.outsPeak[0], plugData.outsPeak[1]);
  967. fUiServer.writeMsg(fTmpBuf);
  968. for (uint32_t j=0, count=plugin->getParameterCount(); j < count; ++j)
  969. {
  970. if (plugin->isParameterOutput(j))
  971. continue;
  972. std::sprintf(fTmpBuf, "PARAMVAL_%i:%i\n", i, j);
  973. fUiServer.writeMsg(fTmpBuf);
  974. std::sprintf(fTmpBuf, "%f\n", plugin->getParameterValue(j));
  975. fUiServer.writeMsg(fTmpBuf);
  976. }
  977. }
  978. switch (fUiServer.getAndResetUiState())
  979. {
  980. case CarlaEngineNativeUI::UiNone:
  981. case CarlaEngineNativeUI::UiShow:
  982. break;
  983. case CarlaEngineNativeUI::UiCrashed:
  984. pHost->dispatcher(pHost->handle, HOST_OPCODE_UI_UNAVAILABLE, 0, 0, nullptr, 0.0f);
  985. break;
  986. case CarlaEngineNativeUI::UiHide:
  987. pHost->ui_closed(pHost->handle);
  988. break;
  989. }
  990. }
  991. // -------------------------------------------------------------------
  992. // Plugin state calls
  993. char* getState() const
  994. {
  995. QString string;
  996. QTextStream out(&string);
  997. out << "<?xml version='1.0' encoding='UTF-8'?>\n";
  998. out << "<!DOCTYPE CARLA-PROJECT>\n";
  999. out << "<CARLA-PROJECT VERSION='2.0'>\n";
  1000. bool firstPlugin = true;
  1001. char strBuf[STR_MAX+1];
  1002. for (unsigned int i=0; i < pData->curPluginCount; ++i)
  1003. {
  1004. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1005. if (plugin != nullptr && plugin->isEnabled())
  1006. {
  1007. if (! firstPlugin)
  1008. out << "\n";
  1009. strBuf[0] = '\0';
  1010. plugin->getRealName(strBuf);
  1011. //if (strBuf[0] != '\0')
  1012. // out << QString(" <!-- %1 -->\n").arg(xmlSafeString(strBuf, true));
  1013. QString content;
  1014. fillXmlStringFromSaveState(content, plugin->getSaveState());
  1015. out << " <Plugin>\n";
  1016. out << content;
  1017. out << " </Plugin>\n";
  1018. firstPlugin = false;
  1019. }
  1020. }
  1021. out << "</CARLA-PROJECT>\n";
  1022. return strdup(string.toUtf8().constData());
  1023. }
  1024. void setState(const char* const data)
  1025. {
  1026. QDomDocument xml;
  1027. xml.setContent(QString(data));
  1028. QDomNode xmlNode(xml.documentElement());
  1029. if (xmlNode.toElement().tagName().compare("carla-project", Qt::CaseInsensitive) != 0)
  1030. {
  1031. carla_stderr2("Not a valid Carla project");
  1032. return;
  1033. }
  1034. //bool pluginsAdded = false;
  1035. for (QDomNode node = xmlNode.firstChild(); ! node.isNull(); node = node.nextSibling())
  1036. {
  1037. if (node.toElement().tagName().compare("plugin", Qt::CaseInsensitive) == 0)
  1038. {
  1039. SaveState saveState;
  1040. fillSaveStateFromXmlNode(saveState, node);
  1041. CARLA_SAFE_ASSERT_CONTINUE(saveState.type != nullptr)
  1042. const void* extraStuff = nullptr;
  1043. // check if using GIG, SF2 or SFZ 16outs
  1044. static const char kUse16OutsSuffix[] = " (16 outs)";
  1045. if (CarlaString(saveState.label).endsWith(kUse16OutsSuffix))
  1046. {
  1047. if (std::strcmp(saveState.type, "GIG") == 0 || std::strcmp(saveState.type, "SF2") == 0)
  1048. extraStuff = "true";
  1049. }
  1050. // TODO - proper find&load plugins
  1051. if (addPlugin(getPluginTypeFromString(saveState.type), saveState.binary, saveState.name, saveState.label, extraStuff))
  1052. {
  1053. if (CarlaPlugin* const plugin = getPlugin(pData->curPluginCount-1))
  1054. plugin->loadSaveState(saveState);
  1055. }
  1056. //pluginsAdded = true;
  1057. }
  1058. }
  1059. //if (pluginsAdded)
  1060. // pHost->dispatcher(pHost->handle, HOST_OPCODE_RELOAD_ALL, 0, 0, nullptr, 0.0f);
  1061. }
  1062. // -------------------------------------------------------------------
  1063. public:
  1064. #define handlePtr ((CarlaEngineNative*)handle)
  1065. static NativePluginHandle _instantiateRack(const NativeHostDescriptor* host)
  1066. {
  1067. return new CarlaEngineNative(host, false);
  1068. }
  1069. #ifdef HAVE_JUCE
  1070. static NativePluginHandle _instantiatePatchbay(const NativeHostDescriptor* host)
  1071. {
  1072. return new CarlaEngineNative(host, true);
  1073. }
  1074. #endif
  1075. static void _cleanup(NativePluginHandle handle)
  1076. {
  1077. delete handlePtr;
  1078. }
  1079. static uint32_t _get_parameter_count(NativePluginHandle handle)
  1080. {
  1081. return handlePtr->getParameterCount();
  1082. }
  1083. static const NativeParameter* _get_parameter_info(NativePluginHandle handle, uint32_t index)
  1084. {
  1085. return handlePtr->getParameterInfo(index);
  1086. }
  1087. static float _get_parameter_value(NativePluginHandle handle, uint32_t index)
  1088. {
  1089. return handlePtr->getParameterValue(index);
  1090. }
  1091. static const char* _get_parameter_text(NativePluginHandle handle, uint32_t index, float value)
  1092. {
  1093. return handlePtr->getParameterText(index, value);
  1094. }
  1095. static uint32_t _get_midi_program_count(NativePluginHandle handle)
  1096. {
  1097. return handlePtr->getMidiProgramCount();
  1098. }
  1099. static const NativeMidiProgram* _get_midi_program_info(NativePluginHandle handle, uint32_t index)
  1100. {
  1101. return handlePtr->getMidiProgramInfo(index);
  1102. }
  1103. static void _set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  1104. {
  1105. handlePtr->setParameterValue(index, value);
  1106. }
  1107. static void _set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  1108. {
  1109. handlePtr->setMidiProgram(channel, bank, program);
  1110. }
  1111. static void _ui_show(NativePluginHandle handle, bool show)
  1112. {
  1113. handlePtr->uiShow(show);
  1114. }
  1115. static void _ui_idle(NativePluginHandle handle)
  1116. {
  1117. handlePtr->uiIdle();
  1118. }
  1119. static void _activate(NativePluginHandle handle)
  1120. {
  1121. handlePtr->activate();
  1122. }
  1123. static void _deactivate(NativePluginHandle handle)
  1124. {
  1125. handlePtr->deactivate();
  1126. }
  1127. static void _process(NativePluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  1128. {
  1129. handlePtr->process(inBuffer, outBuffer, frames, midiEvents, midiEventCount);
  1130. }
  1131. static char* _get_state(NativePluginHandle handle)
  1132. {
  1133. return handlePtr->getState();
  1134. }
  1135. static void _set_state(NativePluginHandle handle, const char* data)
  1136. {
  1137. handlePtr->setState(data);
  1138. }
  1139. static intptr_t _dispatcher(NativePluginHandle handle, NativePluginDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1140. {
  1141. switch(opcode)
  1142. {
  1143. case PLUGIN_OPCODE_NULL:
  1144. return 0;
  1145. case PLUGIN_OPCODE_BUFFER_SIZE_CHANGED:
  1146. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  1147. handlePtr->bufferSizeChanged(static_cast<uint32_t>(value));
  1148. return 0;
  1149. case PLUGIN_OPCODE_SAMPLE_RATE_CHANGED:
  1150. handlePtr->sampleRateChanged(static_cast<double>(opt));
  1151. return 0;
  1152. case PLUGIN_OPCODE_OFFLINE_CHANGED:
  1153. handlePtr->offlineModeChanged(value != 0);
  1154. return 0;
  1155. case PLUGIN_OPCODE_UI_NAME_CHANGED:
  1156. //handlePtr->uiNameChanged(static_cast<const char*>(ptr));
  1157. return 0;
  1158. }
  1159. return 0;
  1160. // unused
  1161. (void)index;
  1162. (void)ptr;
  1163. }
  1164. // -------------------------------------------------------------------
  1165. static void _ui_server_callback(void* handle, EngineCallbackOpcode action, uint pluginId, int value1, int value2, float value3, const char* valueStr)
  1166. {
  1167. handlePtr->uiServerCallback(action, pluginId, value1, value2, value3, valueStr);
  1168. }
  1169. // -------------------------------------------------------------------
  1170. #undef handlePtr
  1171. private:
  1172. const NativeHostDescriptor* const pHost;
  1173. const bool fIsPatchbay; // rack if false
  1174. bool fIsActive, fIsRunning;
  1175. CarlaEngineNativeUI fUiServer;
  1176. char fTmpBuf[STR_MAX+1];
  1177. CarlaPlugin* _getFirstPlugin() const noexcept
  1178. {
  1179. if (pData->curPluginCount == 0 || pData->plugins == nullptr)
  1180. return nullptr;
  1181. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  1182. if (plugin == nullptr || ! plugin->isEnabled())
  1183. return nullptr;
  1184. return pData->plugins[0].plugin;
  1185. }
  1186. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineNative)
  1187. };
  1188. // -----------------------------------------------------------------------
  1189. static const NativePluginDescriptor carlaRackDesc = {
  1190. /* category */ ::PLUGIN_CATEGORY_OTHER,
  1191. /* hints */ static_cast<NativePluginHints>(::PLUGIN_IS_SYNTH|::PLUGIN_HAS_UI|::PLUGIN_NEEDS_FIXED_BUFFERS|::PLUGIN_NEEDS_SINGLE_THREAD|::PLUGIN_USES_STATE|::PLUGIN_USES_TIME),
  1192. /* supports */ static_cast<NativePluginSupports>(::PLUGIN_SUPPORTS_EVERYTHING),
  1193. /* audioIns */ 2,
  1194. /* audioOuts */ 2,
  1195. /* midiIns */ 1,
  1196. /* midiOuts */ 1,
  1197. /* paramIns */ 0,
  1198. /* paramOuts */ 0,
  1199. /* name */ "Carla-Rack",
  1200. /* label */ "carla-rack",
  1201. /* maker */ "falkTX",
  1202. /* copyright */ "GNU GPL v2+",
  1203. CarlaEngineNative::_instantiateRack,
  1204. CarlaEngineNative::_cleanup,
  1205. CarlaEngineNative::_get_parameter_count,
  1206. CarlaEngineNative::_get_parameter_info,
  1207. CarlaEngineNative::_get_parameter_value,
  1208. CarlaEngineNative::_get_parameter_text,
  1209. CarlaEngineNative::_get_midi_program_count,
  1210. CarlaEngineNative::_get_midi_program_info,
  1211. CarlaEngineNative::_set_parameter_value,
  1212. CarlaEngineNative::_set_midi_program,
  1213. /* _set_custom_data */ nullptr,
  1214. CarlaEngineNative::_ui_show,
  1215. CarlaEngineNative::_ui_idle,
  1216. /* _ui_set_parameter_value */ nullptr,
  1217. /* _ui_set_midi_program */ nullptr,
  1218. /* _ui_set_custom_data */ nullptr,
  1219. CarlaEngineNative::_activate,
  1220. CarlaEngineNative::_deactivate,
  1221. CarlaEngineNative::_process,
  1222. CarlaEngineNative::_get_state,
  1223. CarlaEngineNative::_set_state,
  1224. CarlaEngineNative::_dispatcher
  1225. };
  1226. #if 0 //def HAVE_JUCE
  1227. static const NativePluginDescriptor carlaPatchbayDesc = {
  1228. /* category */ ::PLUGIN_CATEGORY_OTHER,
  1229. /* hints */ static_cast<NativePluginHints>(::PLUGIN_IS_SYNTH|::PLUGIN_HAS_UI|::PLUGIN_NEEDS_FIXED_BUFFERS|::PLUGIN_NEEDS_SINGLE_THREAD|::PLUGIN_USES_STATE|::PLUGIN_USES_TIME),
  1230. /* supports */ static_cast<NativePluginSupports>(::PLUGIN_SUPPORTS_EVERYTHING),
  1231. /* audioIns */ 2,
  1232. /* audioOuts */ 2,
  1233. /* midiIns */ 1,
  1234. /* midiOuts */ 1,
  1235. /* paramIns */ 0,
  1236. /* paramOuts */ 0,
  1237. /* name */ "Carla-Patchbay",
  1238. /* label */ "carla-patchbay",
  1239. /* maker */ "falkTX",
  1240. /* copyright */ "GNU GPL v2+",
  1241. CarlaEngineNative::_instantiatePatchbay,
  1242. CarlaEngineNative::_cleanup,
  1243. CarlaEngineNative::_get_parameter_count,
  1244. CarlaEngineNative::_get_parameter_info,
  1245. CarlaEngineNative::_get_parameter_value,
  1246. CarlaEngineNative::_get_parameter_text,
  1247. CarlaEngineNative::_get_midi_program_count,
  1248. CarlaEngineNative::_get_midi_program_info,
  1249. CarlaEngineNative::_set_parameter_value,
  1250. CarlaEngineNative::_set_midi_program,
  1251. /* _set_custom_data */ nullptr,
  1252. CarlaEngineNative::_ui_show,
  1253. CarlaEngineNative::_ui_idle,
  1254. /* _ui_set_parameter_value */ nullptr,
  1255. /* _ui_set_midi_program */ nullptr,
  1256. /* _ui_set_custom_data */ nullptr,
  1257. CarlaEngineNative::_activate,
  1258. CarlaEngineNative::_deactivate,
  1259. CarlaEngineNative::_process,
  1260. CarlaEngineNative::_get_state,
  1261. CarlaEngineNative::_set_state,
  1262. CarlaEngineNative::_dispatcher
  1263. };
  1264. #endif
  1265. // -----------------------------------------------------------------------
  1266. CARLA_BACKEND_END_NAMESPACE
  1267. CARLA_EXPORT
  1268. void carla_register_native_plugin_carla();
  1269. void carla_register_native_plugin_carla()
  1270. {
  1271. CARLA_BACKEND_USE_NAMESPACE
  1272. carla_register_native_plugin(&carlaRackDesc);
  1273. #if 0 //def HAVE_JUCE
  1274. carla_register_native_plugin(&carlaPatchbayDesc);
  1275. #endif
  1276. }
  1277. // -----------------------------------------------------------------------
  1278. // Extra stuff for linking purposes
  1279. #ifdef CARLA_PLUGIN_EXPORT
  1280. CARLA_BACKEND_START_NAMESPACE
  1281. CarlaEngine* CarlaEngine::newJack() { return nullptr; }
  1282. CarlaEngine* CarlaEngine::newRtAudio(const AudioApi) { return nullptr; }
  1283. unsigned int CarlaEngine::getRtAudioApiCount() { return 0; }
  1284. const char* CarlaEngine::getRtAudioApiName(const unsigned int) { return nullptr; }
  1285. const char* const* CarlaEngine::getRtAudioApiDeviceNames(const unsigned int) { return nullptr; }
  1286. const EngineDriverDeviceInfo* CarlaEngine::getRtAudioDeviceInfo(const unsigned int, const char* const) { return nullptr; }
  1287. # ifdef HAVE_JUCE
  1288. CarlaEngine* CarlaEngine::newJuce(const AudioApi) { return nullptr; }
  1289. unsigned int CarlaEngine::getJuceApiCount() { return 0; }
  1290. const char* CarlaEngine::getJuceApiName(const unsigned int) { return nullptr; }
  1291. const char* const* CarlaEngine::getJuceApiDeviceNames(const unsigned int) { return nullptr; }
  1292. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const unsigned int, const char* const) { return nullptr; }
  1293. # endif
  1294. CARLA_BACKEND_END_NAMESPACE
  1295. #ifdef WANT_DSSI
  1296. # include "CarlaDssiUtils.cpp"
  1297. #endif
  1298. #include "CarlaStateUtils.cpp"
  1299. #endif
  1300. // -----------------------------------------------------------------------