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