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.

1656 lines
54KB

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