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.

1763 lines
59KB

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