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.

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