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.

1623 lines
52KB

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