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

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