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.

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