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.

1676 lines
55KB

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