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.

1618 lines
53KB

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