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.

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