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.

2359 lines
78KB

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