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.

2213 lines
75KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2017 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. #include "CarlaEngineInternal.hpp"
  22. #include "CarlaPlugin.hpp"
  23. #include "CarlaBackendUtils.hpp"
  24. #include "CarlaBase64Utils.hpp"
  25. #include "CarlaBinaryUtils.hpp"
  26. #include "CarlaMathUtils.hpp"
  27. #include "CarlaStateUtils.hpp"
  28. #include "CarlaExternalUI.hpp"
  29. #include "CarlaHost.h"
  30. #include "CarlaNative.hpp"
  31. #include "juce_audio_basics/juce_audio_basics.h"
  32. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  33. # include "juce_gui_basics/juce_gui_basics.h"
  34. #else
  35. # include "juce_events/juce_events.h"
  36. #endif
  37. using juce::File;
  38. using juce::FloatVectorOperations;
  39. using juce::MemoryOutputStream;
  40. using juce::MessageManager;
  41. using juce::ScopedJuceInitialiser_GUI;
  42. using juce::ScopedPointer;
  43. using juce::SharedResourcePointer;
  44. using juce::String;
  45. using juce::XmlDocument;
  46. using juce::XmlElement;
  47. CARLA_BACKEND_START_NAMESPACE
  48. #if ! (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  49. static int numScopedInitInstances = 0;
  50. class SharedMessageThread : public juce::Thread
  51. {
  52. public:
  53. SharedMessageThread()
  54. : juce::Thread ("SharedMessageThread"),
  55. initialised (false) {}
  56. ~SharedMessageThread()
  57. {
  58. CARLA_SAFE_ASSERT(numScopedInitInstances == 0);
  59. // in case something fails
  60. MessageManager::getInstance()->stopDispatchLoop();
  61. waitForThreadToExit (5000);
  62. }
  63. void incRef()
  64. {
  65. if (numScopedInitInstances++ == 0)
  66. {
  67. startThread (7);
  68. while (! initialised)
  69. sleep (1);
  70. }
  71. }
  72. void decRef()
  73. {
  74. if (--numScopedInitInstances == 0)
  75. {
  76. MessageManager::getInstance()->stopDispatchLoop();
  77. waitForThreadToExit (5000);
  78. }
  79. }
  80. protected:
  81. void run() override
  82. {
  83. const ScopedJuceInitialiser_GUI juceInitialiser;
  84. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  85. initialised = true;
  86. MessageManager::getInstance()->runDispatchLoop();
  87. }
  88. private:
  89. volatile bool initialised;
  90. };
  91. #endif
  92. // -----------------------------------------------------------------------
  93. class CarlaEngineNativeUI : public CarlaExternalUI
  94. {
  95. public:
  96. CarlaEngineNativeUI(CarlaEngine* const engine)
  97. : fEngine(engine),
  98. fRemoteWinId(0)
  99. {
  100. carla_debug("CarlaEngineNativeUI::CarlaEngineNativeUI(%p)", engine);
  101. }
  102. ~CarlaEngineNativeUI() noexcept override
  103. {
  104. carla_debug("CarlaEngineNativeUI::~CarlaEngineNativeUI()");
  105. }
  106. bool isReady() const noexcept
  107. {
  108. return fRemoteWinId != 0;
  109. }
  110. intptr_t getRemoteWinId() const noexcept
  111. {
  112. return fRemoteWinId;
  113. }
  114. protected:
  115. bool msgReceived(const char* const msg) noexcept override
  116. {
  117. if (CarlaExternalUI::msgReceived(msg))
  118. return true;
  119. bool ok = true;
  120. if (std::strcmp(msg, "set_engine_option") == 0)
  121. {
  122. uint32_t option;
  123. int32_t value;
  124. const char* valueStr = nullptr;
  125. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(option), true);
  126. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(value), true);
  127. readNextLineAsString(valueStr); // can be null
  128. try {
  129. fEngine->setOption(static_cast<EngineOption>(option), value, valueStr);
  130. } CARLA_SAFE_EXCEPTION("setOption");
  131. if (valueStr != nullptr)
  132. delete[] valueStr;
  133. }
  134. else if (std::strcmp(msg, "load_file") == 0)
  135. {
  136. const char* filename;
  137. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  138. try {
  139. ok = fEngine->loadFile(filename);
  140. } CARLA_SAFE_EXCEPTION("loadFile");
  141. delete[] filename;
  142. }
  143. else if (std::strcmp(msg, "load_project") == 0)
  144. {
  145. const char* filename;
  146. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  147. try {
  148. ok = fEngine->loadProject(filename);
  149. } CARLA_SAFE_EXCEPTION("loadProject");
  150. delete[] filename;
  151. }
  152. else if (std::strcmp(msg, "save_project") == 0)
  153. {
  154. const char* filename;
  155. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  156. try {
  157. ok = fEngine->saveProject(filename);
  158. } CARLA_SAFE_EXCEPTION("saveProject");
  159. delete[] filename;
  160. }
  161. else if (std::strcmp(msg, "patchbay_connect") == 0)
  162. {
  163. uint32_t groupA, portA, groupB, portB;
  164. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(groupA), true);
  165. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(portA), true);
  166. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(groupB), true);
  167. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(portB), true);
  168. try {
  169. ok = fEngine->patchbayConnect(groupA, portA, groupB, portB);
  170. } CARLA_SAFE_EXCEPTION("patchbayConnect");
  171. }
  172. else if (std::strcmp(msg, "patchbay_disconnect") == 0)
  173. {
  174. uint32_t connectionId;
  175. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(connectionId), true);
  176. try {
  177. ok = fEngine->patchbayDisconnect(connectionId);
  178. } CARLA_SAFE_EXCEPTION("patchbayDisconnect");
  179. }
  180. else if (std::strcmp(msg, "patchbay_refresh") == 0)
  181. {
  182. try {
  183. ok = fEngine->patchbayRefresh(false);
  184. } CARLA_SAFE_EXCEPTION("patchbayRefresh");
  185. }
  186. else if (std::strcmp(msg, "transport_play") == 0)
  187. {
  188. fEngine->transportPlay();
  189. }
  190. else if (std::strcmp(msg, "transport_pause") == 0)
  191. {
  192. fEngine->transportPause();
  193. }
  194. else if (std::strcmp(msg, "transport_relocate") == 0)
  195. {
  196. uint64_t frame;
  197. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(frame), true);
  198. fEngine->transportRelocate(frame);
  199. }
  200. else if (std::strcmp(msg, "add_plugin") == 0)
  201. {
  202. uint32_t btype, ptype;
  203. const char* filename = nullptr;
  204. const char* name;
  205. const char* label;
  206. int64_t uniqueId;
  207. uint options;
  208. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(btype), true);
  209. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(ptype), true);
  210. readNextLineAsString(filename); // can be null
  211. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(name), true);
  212. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(label), true);
  213. CARLA_SAFE_ASSERT_RETURN(readNextLineAsLong(uniqueId), true);
  214. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(options), true);
  215. if (filename != nullptr && std::strcmp(filename, "(null)") == 0)
  216. {
  217. delete[] filename;
  218. filename = nullptr;
  219. }
  220. if (std::strcmp(name, "(null)") == 0)
  221. {
  222. delete[] name;
  223. name = nullptr;
  224. }
  225. ok = fEngine->addPlugin(static_cast<BinaryType>(btype), static_cast<PluginType>(ptype),
  226. filename, name, label, uniqueId, nullptr, options);
  227. if (filename != nullptr)
  228. delete[] filename;
  229. if (name != nullptr)
  230. delete[] name;
  231. delete[] label;
  232. }
  233. else if (std::strcmp(msg, "remove_plugin") == 0)
  234. {
  235. uint32_t pluginId;
  236. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  237. ok = fEngine->removePlugin(pluginId);
  238. }
  239. else if (std::strcmp(msg, "remove_all_plugins") == 0)
  240. {
  241. ok = fEngine->removeAllPlugins();
  242. }
  243. else if (std::strcmp(msg, "rename_plugin") == 0)
  244. {
  245. uint32_t pluginId;
  246. const char* newName;
  247. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  248. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(newName), true);
  249. // TODO
  250. /*const char* name =*/ fEngine->renamePlugin(pluginId, newName);
  251. delete[] newName;
  252. }
  253. else if (std::strcmp(msg, "clone_plugin") == 0)
  254. {
  255. uint32_t pluginId;
  256. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  257. ok = fEngine->clonePlugin(pluginId);
  258. }
  259. else if (std::strcmp(msg, "replace_plugin") == 0)
  260. {
  261. uint32_t pluginId;
  262. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  263. ok = fEngine->replacePlugin(pluginId);
  264. }
  265. else if (std::strcmp(msg, "switch_plugins") == 0)
  266. {
  267. uint32_t pluginIdA, pluginIdB;
  268. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginIdA), true);
  269. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginIdB), true);
  270. ok = fEngine->switchPlugins(pluginIdA, pluginIdB);
  271. }
  272. else if (std::strcmp(msg, "load_plugin_state") == 0)
  273. {
  274. uint32_t pluginId;
  275. const char* filename;
  276. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  277. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  278. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  279. plugin->loadStateFromFile(filename);
  280. delete[] filename;
  281. }
  282. else if (std::strcmp(msg, "save_plugin_state") == 0)
  283. {
  284. uint32_t pluginId;
  285. const char* filename;
  286. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  287. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(filename), true);
  288. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  289. plugin->saveStateToFile(filename);
  290. delete[] filename;
  291. }
  292. else if (std::strcmp(msg, "set_option") == 0)
  293. {
  294. uint32_t pluginId, option;
  295. bool yesNo;
  296. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  297. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(option), true);
  298. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(yesNo), true);
  299. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  300. plugin->setOption(option, yesNo, false);
  301. }
  302. else if (std::strcmp(msg, "set_active") == 0)
  303. {
  304. uint32_t pluginId;
  305. bool onOff;
  306. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  307. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  308. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  309. plugin->setActive(onOff, true, false);
  310. }
  311. else if (std::strcmp(msg, "set_drywet") == 0)
  312. {
  313. uint32_t pluginId;
  314. float value;
  315. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  316. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  317. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  318. plugin->setDryWet(value, true, false);
  319. }
  320. else if (std::strcmp(msg, "set_volume") == 0)
  321. {
  322. uint32_t pluginId;
  323. float value;
  324. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  325. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  326. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  327. plugin->setVolume(value, true, false);
  328. }
  329. else if (std::strcmp(msg, "set_balance_left") == 0)
  330. {
  331. uint32_t pluginId;
  332. float value;
  333. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  334. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  335. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  336. plugin->setBalanceLeft(value, true, false);
  337. }
  338. else if (std::strcmp(msg, "set_balance_right") == 0)
  339. {
  340. uint32_t pluginId;
  341. float value;
  342. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  343. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  344. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  345. plugin->setBalanceRight(value, true, false);
  346. }
  347. else if (std::strcmp(msg, "set_panning") == 0)
  348. {
  349. uint32_t pluginId;
  350. float value;
  351. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  352. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  353. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  354. plugin->setPanning(value, true, false);
  355. }
  356. else if (std::strcmp(msg, "set_ctrl_channel") == 0)
  357. {
  358. uint32_t pluginId;
  359. int32_t channel;
  360. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  361. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(channel), true);
  362. CARLA_SAFE_ASSERT_RETURN(channel >= -1 && channel < MAX_MIDI_CHANNELS, true);
  363. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  364. plugin->setCtrlChannel(int8_t(channel), true, false);
  365. }
  366. else if (std::strcmp(msg, "set_parameter_value") == 0)
  367. {
  368. uint32_t pluginId, parameterId;
  369. float value;
  370. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  371. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(parameterId), true);
  372. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  373. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  374. plugin->setParameterValue(parameterId, value, true, true, false);
  375. }
  376. else if (std::strcmp(msg, "set_parameter_midi_channel") == 0)
  377. {
  378. uint32_t pluginId, parameterId, channel;
  379. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  380. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(parameterId), true);
  381. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  382. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  383. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  384. plugin->setParameterMidiChannel(parameterId, static_cast<uint8_t>(channel), true, false);
  385. }
  386. else if (std::strcmp(msg, "set_parameter_midi_cc") == 0)
  387. {
  388. uint32_t pluginId, parameterId;
  389. int32_t cc;
  390. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  391. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(parameterId), true);
  392. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(cc), true);
  393. CARLA_SAFE_ASSERT_RETURN(cc >= -1 && cc < MAX_MIDI_CONTROL, true);
  394. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  395. plugin->setParameterMidiCC(parameterId, static_cast<int16_t>(cc), true, false);
  396. }
  397. else if (std::strcmp(msg, "set_program") == 0)
  398. {
  399. uint32_t pluginId;
  400. int32_t index;
  401. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  402. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(index), true);
  403. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  404. {
  405. plugin->setProgram(index, true, true, false);
  406. _updateParamValues(plugin, pluginId);
  407. }
  408. }
  409. else if (std::strcmp(msg, "set_midi_program") == 0)
  410. {
  411. uint32_t pluginId;
  412. int32_t index;
  413. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  414. CARLA_SAFE_ASSERT_RETURN(readNextLineAsInt(index), true);
  415. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  416. {
  417. plugin->setMidiProgram(index, true, true, false);
  418. _updateParamValues(plugin, pluginId);
  419. }
  420. }
  421. else if (std::strcmp(msg, "set_custom_data") == 0)
  422. {
  423. uint32_t pluginId;
  424. const char* type;
  425. const char* key;
  426. const char* value;
  427. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  428. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(type), true);
  429. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  430. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  431. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  432. plugin->setCustomData(type, key, value, true);
  433. delete[] type;
  434. delete[] key;
  435. delete[] value;
  436. }
  437. else if (std::strcmp(msg, "set_chunk_data") == 0)
  438. {
  439. uint32_t pluginId;
  440. const char* cdata;
  441. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  442. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(cdata), true);
  443. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  444. {
  445. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(cdata));
  446. plugin->setChunkData(chunk.data(), chunk.size());
  447. }
  448. delete[] cdata;
  449. }
  450. else if (std::strcmp(msg, "prepare_for_save") == 0)
  451. {
  452. uint32_t pluginId;
  453. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  454. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  455. plugin->prepareForSave();
  456. }
  457. else if (std::strcmp(msg, "reset_parameters") == 0)
  458. {
  459. uint32_t pluginId;
  460. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  461. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  462. plugin->resetParameters();
  463. }
  464. else if (std::strcmp(msg, "randomize_parameters") == 0)
  465. {
  466. uint32_t pluginId;
  467. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  468. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  469. plugin->randomizeParameters();
  470. }
  471. else if (std::strcmp(msg, "send_midi_note") == 0)
  472. {
  473. uint32_t pluginId, channel, note, velocity;
  474. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  475. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(channel), true);
  476. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(note), true);
  477. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(velocity), true);
  478. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, true);
  479. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_VALUE, true);
  480. CARLA_SAFE_ASSERT_RETURN(velocity < MAX_MIDI_VALUE, true);
  481. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  482. plugin->sendMidiSingleNote(static_cast<uint8_t>(channel), static_cast<uint8_t>(note), static_cast<uint8_t>(velocity), true, true, false);
  483. }
  484. else if (std::strcmp(msg, "show_custom_ui") == 0)
  485. {
  486. uint32_t pluginId;
  487. bool yesNo;
  488. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(pluginId), true);
  489. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(yesNo), true);
  490. if (CarlaPlugin* const plugin = fEngine->getPlugin(pluginId))
  491. plugin->showCustomUI(yesNo);
  492. }
  493. else if (std::strcmp(msg, "ready") == 0)
  494. {
  495. uint64_t winId;
  496. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(winId), true);
  497. fRemoteWinId = static_cast<intptr_t>(winId);
  498. }
  499. else
  500. {
  501. carla_stderr("CarlaEngineNativeUI::msgReceived : %s", msg);
  502. return false;
  503. }
  504. if (! ok)
  505. {
  506. const CarlaMutexLocker cml(getPipeLock());
  507. writeMessage("error\n", 6);
  508. writeAndFixMessage(fEngine->getLastError());
  509. flushMessages();
  510. }
  511. return true;
  512. }
  513. private:
  514. CarlaEngine* const fEngine;
  515. intptr_t fRemoteWinId;
  516. void _updateParamValues(CarlaPlugin* const plugin, const uint32_t pluginId) const noexcept
  517. {
  518. for (uint32_t i=0, count=plugin->getParameterCount(); i<count; ++i)
  519. fEngine->callback(ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED,
  520. pluginId, static_cast<int>(i), 0, plugin->getParameterValue(i), nullptr);
  521. }
  522. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineNativeUI)
  523. };
  524. // -----------------------------------------------------------------------
  525. class CarlaEngineNative : public CarlaEngine
  526. {
  527. public:
  528. CarlaEngineNative(const NativeHostDescriptor* const host, const bool isPatchbay, const uint32_t inChan = 2, uint32_t outChan = 0)
  529. : CarlaEngine(),
  530. pHost(host),
  531. #if ! (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  532. kNeedsJuceMsgThread(host->dispatcher(pHost->handle,
  533. NATIVE_HOST_OPCODE_INTERNAL_PLUGIN, 0, 0, nullptr, 0.0f) == 0),
  534. #endif
  535. kIsPatchbay(isPatchbay),
  536. fIsActive(false),
  537. fIsRunning(false),
  538. fUiServer(this),
  539. fOptionsForced(false),
  540. fWaitForReadyMsg(false)
  541. {
  542. carla_debug("CarlaEngineNative::CarlaEngineNative()");
  543. carla_zeroChars(fTmpBuf, STR_MAX+1);
  544. pData->bufferSize = pHost->get_buffer_size(pHost->handle);
  545. pData->sampleRate = pHost->get_sample_rate(pHost->handle);
  546. pData->initTime(nullptr);
  547. if (outChan == 0)
  548. outChan = inChan;
  549. #if ! (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  550. if (kNeedsJuceMsgThread)
  551. fJuceMsgThread->incRef();
  552. #endif
  553. // set-up engine
  554. if (kIsPatchbay)
  555. {
  556. pData->options.processMode = ENGINE_PROCESS_MODE_PATCHBAY;
  557. pData->options.transportMode = ENGINE_TRANSPORT_MODE_PLUGIN;
  558. pData->options.forceStereo = false;
  559. pData->options.preferPluginBridges = false;
  560. pData->options.preferUiBridges = false;
  561. init("Carla-Patchbay");
  562. pData->graph.create(inChan, outChan);
  563. }
  564. else
  565. {
  566. CARLA_SAFE_ASSERT(inChan == 2);
  567. CARLA_SAFE_ASSERT(outChan == 2);
  568. pData->options.processMode = ENGINE_PROCESS_MODE_CONTINUOUS_RACK;
  569. pData->options.transportMode = ENGINE_TRANSPORT_MODE_PLUGIN;
  570. pData->options.forceStereo = true;
  571. pData->options.preferPluginBridges = false;
  572. pData->options.preferUiBridges = false;
  573. init("Carla-Rack");
  574. pData->graph.create(0, 0);
  575. }
  576. if (pData->options.resourceDir != nullptr)
  577. delete[] pData->options.resourceDir;
  578. if (pData->options.binaryDir != nullptr)
  579. delete[] pData->options.binaryDir;
  580. pData->options.resourceDir = carla_strdup(pHost->resourceDir);
  581. pData->options.binaryDir = carla_strdup(carla_get_library_folder());
  582. setCallback(_ui_server_callback, this);
  583. }
  584. ~CarlaEngineNative() override
  585. {
  586. CARLA_SAFE_ASSERT(! fIsActive);
  587. carla_debug("CarlaEngineNative::~CarlaEngineNative() - START");
  588. pData->aboutToClose = true;
  589. fIsRunning = false;
  590. removeAllPlugins();
  591. //runPendingRtEvents();
  592. close();
  593. pData->graph.destroy();
  594. #if ! (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  595. if (kNeedsJuceMsgThread)
  596. fJuceMsgThread->decRef();
  597. #endif
  598. carla_debug("CarlaEngineNative::~CarlaEngineNative() - END");
  599. }
  600. protected:
  601. // -------------------------------------
  602. // CarlaEngine virtual calls
  603. bool init(const char* const clientName) override
  604. {
  605. carla_debug("CarlaEngineNative::init(\"%s\")", clientName);
  606. fIsRunning = true;
  607. if (! pData->init(clientName))
  608. {
  609. close();
  610. setLastError("Failed to init internal data");
  611. return false;
  612. }
  613. pData->bufferSize = pHost->get_buffer_size(pHost->handle);
  614. pData->sampleRate = pHost->get_sample_rate(pHost->handle);
  615. return true;
  616. }
  617. bool close() override
  618. {
  619. fIsRunning = false;
  620. CarlaEngine::close();
  621. return true;
  622. }
  623. bool isRunning() const noexcept override
  624. {
  625. return fIsRunning;
  626. }
  627. bool isOffline() const noexcept override
  628. {
  629. return pHost->is_offline(pHost->handle);
  630. }
  631. bool usesConstantBufferSize() const noexcept override
  632. {
  633. // TODO
  634. return true;
  635. }
  636. EngineType getType() const noexcept override
  637. {
  638. return kEngineTypePlugin;
  639. }
  640. const char* getCurrentDriverName() const noexcept override
  641. {
  642. return "Plugin";
  643. }
  644. void callback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr) noexcept override
  645. {
  646. CarlaEngine::callback(action, pluginId, value1, value2, value3, valueStr);
  647. if (action == ENGINE_CALLBACK_IDLE && ! pData->aboutToClose)
  648. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_HOST_IDLE, 0, 0, nullptr, 0.0f);
  649. }
  650. // -------------------------------------------------------------------
  651. const char* renamePlugin(const uint id, const char* const newName) override
  652. {
  653. if (const char* const retName = CarlaEngine::renamePlugin(id, newName))
  654. {
  655. uiServerCallback(ENGINE_CALLBACK_PLUGIN_RENAMED, id, 0, 0, 0.0f, retName);
  656. return retName;
  657. }
  658. return nullptr;
  659. }
  660. // -------------------------------------------------------------------
  661. void bufferSizeChanged(const uint32_t newBufferSize)
  662. {
  663. if (pData->bufferSize == newBufferSize)
  664. return;
  665. {
  666. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  667. fUiServer.writeAndFixMessage("buffer-size");
  668. std::sprintf(fTmpBuf, "%i\n", newBufferSize);
  669. fUiServer.writeMessage(fTmpBuf);
  670. fUiServer.flushMessages();
  671. }
  672. pData->bufferSize = newBufferSize;
  673. CarlaEngine::bufferSizeChanged(newBufferSize);
  674. }
  675. void sampleRateChanged(const double newSampleRate)
  676. {
  677. if (carla_isEqual(pData->sampleRate, newSampleRate))
  678. return;
  679. {
  680. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  681. const ScopedLocale csl;
  682. fUiServer.writeAndFixMessage("sample-rate");
  683. std::sprintf(fTmpBuf, "%f\n", newSampleRate);
  684. fUiServer.writeMessage(fTmpBuf);
  685. fUiServer.flushMessages();
  686. }
  687. pData->sampleRate = newSampleRate;
  688. CarlaEngine::sampleRateChanged(newSampleRate);
  689. }
  690. // -------------------------------------------------------------------
  691. void uiServerSendPluginInfo(CarlaPlugin* const plugin)
  692. {
  693. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  694. const uint pluginId(plugin->getId());
  695. std::sprintf(fTmpBuf, "PLUGIN_INFO_%i\n", pluginId);
  696. fUiServer.writeMessage(fTmpBuf);
  697. std::sprintf(fTmpBuf, "%i:%i:%i:" P_INT64 ":%i:%i\n", plugin->getType(), plugin->getCategory(), plugin->getHints(), plugin->getUniqueId(), plugin->getOptionsAvailable(), plugin->getOptionsEnabled());
  698. fUiServer.writeMessage(fTmpBuf);
  699. if (const char* const filename = plugin->getFilename())
  700. {
  701. std::sprintf(fTmpBuf, "%s", filename);
  702. fUiServer.writeAndFixMessage(fTmpBuf);
  703. }
  704. else
  705. fUiServer.writeMessage("\n");
  706. if (const char* const name = plugin->getName())
  707. {
  708. std::sprintf(fTmpBuf, "%s", name);
  709. fUiServer.writeAndFixMessage(fTmpBuf);
  710. }
  711. else
  712. fUiServer.writeMessage("\n");
  713. if (const char* const iconName = plugin->getIconName())
  714. {
  715. std::sprintf(fTmpBuf, "%s", iconName);
  716. fUiServer.writeAndFixMessage(fTmpBuf);
  717. }
  718. else
  719. fUiServer.writeMessage("\n");
  720. plugin->getRealName(fTmpBuf);
  721. fUiServer.writeAndFixMessage(fTmpBuf);
  722. plugin->getLabel(fTmpBuf);
  723. fUiServer.writeAndFixMessage(fTmpBuf);
  724. plugin->getMaker(fTmpBuf);
  725. fUiServer.writeAndFixMessage(fTmpBuf);
  726. plugin->getCopyright(fTmpBuf);
  727. fUiServer.writeAndFixMessage(fTmpBuf);
  728. std::sprintf(fTmpBuf, "AUDIO_COUNT_%i:%i:%i\n", pluginId, plugin->getAudioInCount(), plugin->getAudioOutCount());
  729. fUiServer.writeMessage(fTmpBuf);
  730. std::sprintf(fTmpBuf, "MIDI_COUNT_%i:%i:%i\n", pluginId, plugin->getMidiInCount(), plugin->getMidiOutCount());
  731. fUiServer.writeMessage(fTmpBuf);
  732. fUiServer.flushMessages();
  733. }
  734. void uiServerSendPluginParameters(CarlaPlugin* const plugin)
  735. {
  736. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  737. const ScopedLocale csl;
  738. const uint pluginId(plugin->getId());
  739. for (int32_t i=PARAMETER_ACTIVE; i>PARAMETER_MAX; --i)
  740. {
  741. std::sprintf(fTmpBuf, "PARAMVAL_%i:%i\n", pluginId, i);
  742. fUiServer.writeMessage(fTmpBuf);
  743. std::sprintf(fTmpBuf, "%f\n", plugin->getInternalParameterValue(i));
  744. fUiServer.writeMessage(fTmpBuf);
  745. fUiServer.flushMessages();
  746. }
  747. uint32_t ins, outs, count;
  748. plugin->getParameterCountInfo(ins, outs);
  749. count = plugin->getParameterCount();
  750. std::sprintf(fTmpBuf, "PARAMETER_COUNT_%i:%i:%i:%i\n", pluginId, ins, outs, count);
  751. fUiServer.writeMessage(fTmpBuf);
  752. for (uint32_t i=0; i<count; ++i)
  753. {
  754. const ParameterData& paramData(plugin->getParameterData(i));
  755. const ParameterRanges& paramRanges(plugin->getParameterRanges(i));
  756. std::sprintf(fTmpBuf, "PARAMETER_DATA_%i:%i\n", pluginId, i);
  757. fUiServer.writeMessage(fTmpBuf);
  758. std::sprintf(fTmpBuf, "%i:%i:%i:%i\n", paramData.type, paramData.hints, paramData.midiChannel, paramData.midiCC);
  759. fUiServer.writeMessage(fTmpBuf);
  760. plugin->getParameterName(i, fTmpBuf);
  761. fUiServer.writeAndFixMessage(fTmpBuf);
  762. plugin->getParameterUnit(i, fTmpBuf);
  763. fUiServer.writeAndFixMessage(fTmpBuf);
  764. std::sprintf(fTmpBuf, "PARAMETER_RANGES_%i:%i\n", pluginId, i);
  765. fUiServer.writeMessage(fTmpBuf);
  766. std::sprintf(fTmpBuf, "%f:%f:%f:%f:%f:%f\n", paramRanges.def, paramRanges.min, paramRanges.max, paramRanges.step, paramRanges.stepSmall, paramRanges.stepLarge);
  767. fUiServer.writeMessage(fTmpBuf);
  768. std::sprintf(fTmpBuf, "PARAMVAL_%i:%i\n", pluginId, i);
  769. fUiServer.writeMessage(fTmpBuf);
  770. std::sprintf(fTmpBuf, "%f\n", plugin->getParameterValue(i));
  771. fUiServer.writeMessage(fTmpBuf);
  772. }
  773. fUiServer.flushMessages();
  774. }
  775. void uiServerSendPluginPrograms(CarlaPlugin* const plugin)
  776. {
  777. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  778. const uint pluginId(plugin->getId());
  779. uint32_t count = plugin->getProgramCount();
  780. std::sprintf(fTmpBuf, "PROGRAM_COUNT_%i:%i:%i\n", pluginId, count, plugin->getCurrentProgram());
  781. fUiServer.writeMessage(fTmpBuf);
  782. for (uint32_t i=0; i<count; ++i)
  783. {
  784. std::sprintf(fTmpBuf, "PROGRAM_NAME_%i:%i\n", pluginId, i);
  785. fUiServer.writeMessage(fTmpBuf);
  786. plugin->getProgramName(i, fTmpBuf);
  787. fUiServer.writeAndFixMessage(fTmpBuf);
  788. }
  789. fUiServer.flushMessages();
  790. count = plugin->getMidiProgramCount();
  791. std::sprintf(fTmpBuf, "MIDI_PROGRAM_COUNT_%i:%i:%i\n", pluginId, count, plugin->getCurrentMidiProgram());
  792. fUiServer.writeMessage(fTmpBuf);
  793. for (uint32_t i=0; i<count; ++i)
  794. {
  795. std::sprintf(fTmpBuf, "MIDI_PROGRAM_DATA_%i:%i\n", pluginId, i);
  796. fUiServer.writeMessage(fTmpBuf);
  797. const MidiProgramData& mpData(plugin->getMidiProgramData(i));
  798. std::sprintf(fTmpBuf, "%i:%i\n", mpData.bank, mpData.program);
  799. fUiServer.writeMessage(fTmpBuf);
  800. std::sprintf(fTmpBuf, "%s", mpData.name);
  801. fUiServer.writeAndFixMessage(fTmpBuf);
  802. }
  803. fUiServer.flushMessages();
  804. }
  805. void uiServerSendPluginProperties(CarlaPlugin* const plugin)
  806. {
  807. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  808. const uint pluginId(plugin->getId());
  809. uint32_t count = plugin->getCustomDataCount();
  810. std::sprintf(fTmpBuf, "CUSTOM_DATA_COUNT_%i:%i\n", pluginId, count);
  811. fUiServer.writeMessage(fTmpBuf);
  812. for (uint32_t i=0; i<count; ++i)
  813. {
  814. const CustomData& customData(plugin->getCustomData(i));
  815. CARLA_SAFE_ASSERT_CONTINUE(customData.isValid());
  816. if (std::strcmp(customData.type, CUSTOM_DATA_TYPE_PROPERTY) != 0)
  817. continue;
  818. std::sprintf(fTmpBuf, "CUSTOM_DATA_%i:%i\n", pluginId, i);
  819. fUiServer.writeMessage(fTmpBuf);
  820. fUiServer.writeAndFixMessage(customData.type);
  821. fUiServer.writeAndFixMessage(customData.key);
  822. fUiServer.writeAndFixMessage(customData.value);
  823. }
  824. fUiServer.flushMessages();
  825. }
  826. void uiServerCallback(const EngineCallbackOpcode action, const uint pluginId, const int value1, const int value2, const float value3, const char* const valueStr)
  827. {
  828. if (! fIsRunning)
  829. return;
  830. if (! fUiServer.isPipeRunning())
  831. return;
  832. CarlaPlugin* plugin;
  833. switch (action)
  834. {
  835. case ENGINE_CALLBACK_UPDATE:
  836. plugin = getPlugin(pluginId);
  837. if (plugin != nullptr && plugin->isEnabled())
  838. {
  839. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  840. uiServerSendPluginProperties(plugin);
  841. }
  842. break;
  843. case ENGINE_CALLBACK_RELOAD_INFO:
  844. plugin = getPlugin(pluginId);
  845. if (plugin != nullptr && plugin->isEnabled())
  846. {
  847. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  848. uiServerSendPluginInfo(plugin);
  849. }
  850. break;
  851. case ENGINE_CALLBACK_RELOAD_PARAMETERS:
  852. plugin = getPlugin(pluginId);
  853. if (plugin != nullptr && plugin->isEnabled())
  854. {
  855. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  856. uiServerSendPluginParameters(plugin);
  857. }
  858. break;
  859. case ENGINE_CALLBACK_RELOAD_PROGRAMS:
  860. plugin = getPlugin(pluginId);
  861. if (plugin != nullptr && plugin->isEnabled())
  862. {
  863. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  864. uiServerSendPluginPrograms(plugin);
  865. }
  866. break;
  867. case ENGINE_CALLBACK_RELOAD_ALL:
  868. case ENGINE_CALLBACK_PLUGIN_ADDED:
  869. plugin = getPlugin(pluginId);
  870. if (plugin != nullptr && plugin->isEnabled())
  871. {
  872. CARLA_SAFE_ASSERT_BREAK(plugin->getId() == pluginId);
  873. uiServerSendPluginInfo(plugin);
  874. uiServerSendPluginParameters(plugin);
  875. uiServerSendPluginPrograms(plugin);
  876. uiServerSendPluginProperties(plugin);
  877. }
  878. break;
  879. default:
  880. break;
  881. }
  882. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  883. const ScopedLocale csl;
  884. std::sprintf(fTmpBuf, "ENGINE_CALLBACK_%i\n", int(action));
  885. fUiServer.writeMessage(fTmpBuf);
  886. std::sprintf(fTmpBuf, "%u\n", pluginId);
  887. fUiServer.writeMessage(fTmpBuf);
  888. std::sprintf(fTmpBuf, "%i\n", value1);
  889. fUiServer.writeMessage(fTmpBuf);
  890. std::sprintf(fTmpBuf, "%i\n", value2);
  891. fUiServer.writeMessage(fTmpBuf);
  892. std::sprintf(fTmpBuf, "%f\n", value3);
  893. fUiServer.writeMessage(fTmpBuf);
  894. fUiServer.writeAndFixMessage(valueStr != nullptr ? valueStr : "");
  895. fUiServer.flushMessages();
  896. }
  897. void uiServerInfo()
  898. {
  899. CARLA_SAFE_ASSERT_RETURN(fIsRunning,);
  900. CARLA_SAFE_ASSERT_RETURN(fUiServer.isPipeRunning(),);
  901. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  902. #ifdef HAVE_LIBLO
  903. fUiServer.writeAndFixMessage("osc-urls");
  904. fUiServer.writeAndFixMessage(pData->osc.getServerPathTCP());
  905. fUiServer.writeAndFixMessage(pData->osc.getServerPathUDP());
  906. #endif
  907. fUiServer.writeAndFixMessage("max-plugin-number");
  908. std::sprintf(fTmpBuf, "%i\n", pData->maxPluginNumber);
  909. fUiServer.writeMessage(fTmpBuf);
  910. fUiServer.writeAndFixMessage("buffer-size");
  911. std::sprintf(fTmpBuf, "%i\n", pData->bufferSize);
  912. fUiServer.writeMessage(fTmpBuf);
  913. const ScopedLocale csl;
  914. fUiServer.writeAndFixMessage("sample-rate");
  915. std::sprintf(fTmpBuf, "%f\n", pData->sampleRate);
  916. fUiServer.writeMessage(fTmpBuf);
  917. fUiServer.flushMessages();
  918. }
  919. void uiServerOptions()
  920. {
  921. CARLA_SAFE_ASSERT_RETURN(fIsRunning,);
  922. CARLA_SAFE_ASSERT_RETURN(fUiServer.isPipeRunning(),);
  923. const EngineOptions& options(pData->options);
  924. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  925. const char* const optionsForcedStr(fOptionsForced ? "true\n" : "false\n");
  926. const std::size_t optionsForcedStrSize(fOptionsForced ? 5 : 6);
  927. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PROCESS_MODE);
  928. fUiServer.writeMessage(fTmpBuf);
  929. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  930. std::sprintf(fTmpBuf, "%i\n", options.processMode);
  931. fUiServer.writeMessage(fTmpBuf);
  932. fUiServer.flushMessages();
  933. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_TRANSPORT_MODE);
  934. fUiServer.writeMessage(fTmpBuf);
  935. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  936. std::sprintf(fTmpBuf, "%i\n", options.transportMode);
  937. fUiServer.writeMessage(fTmpBuf);
  938. fUiServer.flushMessages();
  939. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_FORCE_STEREO);
  940. fUiServer.writeMessage(fTmpBuf);
  941. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  942. fUiServer.writeMessage(options.forceStereo ? "true\n" : "false\n");
  943. fUiServer.flushMessages();
  944. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PREFER_PLUGIN_BRIDGES);
  945. fUiServer.writeMessage(fTmpBuf);
  946. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  947. fUiServer.writeMessage(options.preferPluginBridges ? "true\n" : "false\n");
  948. fUiServer.flushMessages();
  949. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PREFER_UI_BRIDGES);
  950. fUiServer.writeMessage(fTmpBuf);
  951. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  952. fUiServer.writeMessage(options.preferUiBridges ? "true\n" : "false\n");
  953. fUiServer.flushMessages();
  954. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_UIS_ALWAYS_ON_TOP);
  955. fUiServer.writeMessage(fTmpBuf);
  956. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  957. fUiServer.writeMessage(options.uisAlwaysOnTop ? "true\n" : "false\n");
  958. fUiServer.flushMessages();
  959. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_MAX_PARAMETERS);
  960. fUiServer.writeMessage(fTmpBuf);
  961. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  962. std::sprintf(fTmpBuf, "%i\n", options.maxParameters);
  963. fUiServer.writeMessage(fTmpBuf);
  964. fUiServer.flushMessages();
  965. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_UI_BRIDGES_TIMEOUT);
  966. fUiServer.writeMessage(fTmpBuf);
  967. fUiServer.writeMessage(optionsForcedStr, optionsForcedStrSize);
  968. std::sprintf(fTmpBuf, "%i\n", options.uiBridgesTimeout);
  969. fUiServer.writeMessage(fTmpBuf);
  970. fUiServer.flushMessages();
  971. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PATH_BINARIES);
  972. fUiServer.writeMessage(fTmpBuf);
  973. fUiServer.writeMessage("true\n", 5);
  974. std::sprintf(fTmpBuf, "%s\n", options.binaryDir);
  975. fUiServer.writeMessage(fTmpBuf);
  976. fUiServer.flushMessages();
  977. std::sprintf(fTmpBuf, "ENGINE_OPTION_%i\n", ENGINE_OPTION_PATH_RESOURCES);
  978. fUiServer.writeMessage(fTmpBuf);
  979. fUiServer.writeMessage("true\n", 5);
  980. std::sprintf(fTmpBuf, "%s\n", options.resourceDir);
  981. fUiServer.writeMessage(fTmpBuf);
  982. fUiServer.flushMessages();
  983. }
  984. // -------------------------------------------------------------------
  985. // Plugin parameter calls
  986. uint32_t getParameterCount() const
  987. {
  988. if (CarlaPlugin* const plugin = _getFirstPlugin())
  989. return plugin->getParameterCount();
  990. return 0;
  991. }
  992. const NativeParameter* getParameterInfo(const uint32_t index) const
  993. {
  994. if (CarlaPlugin* const plugin = _getFirstPlugin())
  995. {
  996. if (index < plugin->getParameterCount())
  997. {
  998. static NativeParameter param;
  999. static char strBufName[STR_MAX+1];
  1000. static char strBufUnit[STR_MAX+1];
  1001. const ParameterData& paramData(plugin->getParameterData(index));
  1002. const ParameterRanges& paramRanges(plugin->getParameterRanges(index));
  1003. plugin->getParameterName(index, strBufName);
  1004. plugin->getParameterUnit(index, strBufUnit);
  1005. uint hints = 0x0;
  1006. if (paramData.hints & PARAMETER_IS_BOOLEAN)
  1007. hints |= NATIVE_PARAMETER_IS_BOOLEAN;
  1008. if (paramData.hints & PARAMETER_IS_INTEGER)
  1009. hints |= NATIVE_PARAMETER_IS_INTEGER;
  1010. if (paramData.hints & PARAMETER_IS_LOGARITHMIC)
  1011. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  1012. if (paramData.hints & PARAMETER_IS_AUTOMABLE)
  1013. hints |= NATIVE_PARAMETER_IS_AUTOMABLE;
  1014. if (paramData.hints & PARAMETER_USES_SAMPLERATE)
  1015. hints |= NATIVE_PARAMETER_USES_SAMPLE_RATE;
  1016. if (paramData.hints & PARAMETER_USES_SCALEPOINTS)
  1017. hints |= NATIVE_PARAMETER_USES_SCALEPOINTS;
  1018. if (paramData.type == PARAMETER_INPUT || paramData.type == PARAMETER_OUTPUT)
  1019. {
  1020. if (paramData.hints & PARAMETER_IS_ENABLED)
  1021. hints |= NATIVE_PARAMETER_IS_ENABLED;
  1022. if (paramData.type == PARAMETER_OUTPUT)
  1023. hints |= NATIVE_PARAMETER_IS_OUTPUT;
  1024. }
  1025. param.hints = static_cast<NativeParameterHints>(hints);
  1026. param.name = strBufName;
  1027. param.unit = strBufUnit;
  1028. param.ranges.def = paramRanges.def;
  1029. param.ranges.min = paramRanges.min;
  1030. param.ranges.max = paramRanges.max;
  1031. param.ranges.step = paramRanges.step;
  1032. param.ranges.stepSmall = paramRanges.stepSmall;
  1033. param.ranges.stepLarge = paramRanges.stepLarge;
  1034. param.scalePointCount = 0; // TODO
  1035. param.scalePoints = nullptr;
  1036. return &param;
  1037. }
  1038. }
  1039. return nullptr;
  1040. }
  1041. float getParameterValue(const uint32_t index) const
  1042. {
  1043. if (CarlaPlugin* const plugin = _getFirstPlugin())
  1044. {
  1045. if (index < plugin->getParameterCount())
  1046. return plugin->getParameterValue(index);
  1047. }
  1048. return 0.0f;
  1049. }
  1050. // -------------------------------------------------------------------
  1051. // Plugin midi-program calls
  1052. uint32_t getMidiProgramCount() const
  1053. {
  1054. if (CarlaPlugin* const plugin = _getFirstPlugin())
  1055. return plugin->getMidiProgramCount();
  1056. return 0;
  1057. }
  1058. const NativeMidiProgram* getMidiProgramInfo(const uint32_t index) const
  1059. {
  1060. if (CarlaPlugin* const plugin = _getFirstPlugin())
  1061. {
  1062. if (index < plugin->getMidiProgramCount())
  1063. {
  1064. static NativeMidiProgram midiProg;
  1065. {
  1066. const MidiProgramData& midiProgData(plugin->getMidiProgramData(index));
  1067. midiProg.bank = midiProgData.bank;
  1068. midiProg.program = midiProgData.program;
  1069. midiProg.name = midiProgData.name;
  1070. }
  1071. return &midiProg;
  1072. }
  1073. }
  1074. return nullptr;
  1075. }
  1076. // -------------------------------------------------------------------
  1077. // Plugin state calls
  1078. void setParameterValue(const uint32_t index, const float value)
  1079. {
  1080. if (CarlaPlugin* const plugin = _getFirstPlugin())
  1081. {
  1082. if (index < plugin->getParameterCount())
  1083. plugin->setParameterValue(index, value, false, false, false);
  1084. }
  1085. }
  1086. void setMidiProgram(const uint8_t, const uint32_t bank, const uint32_t program)
  1087. {
  1088. if (CarlaPlugin* const plugin = _getFirstPlugin())
  1089. plugin->setMidiProgramById(bank, program, false, false, false);
  1090. }
  1091. // -------------------------------------------------------------------
  1092. // Plugin process calls
  1093. void activate()
  1094. {
  1095. #if 0
  1096. for (uint i=0; i < pData->curPluginCount; ++i)
  1097. {
  1098. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1099. if (plugin == nullptr || ! plugin->isEnabled())
  1100. continue;
  1101. plugin->setActive(true, true, false);
  1102. }
  1103. #endif
  1104. fIsActive = true;
  1105. }
  1106. void deactivate()
  1107. {
  1108. fIsActive = false;
  1109. #if 0
  1110. for (uint i=0; i < pData->curPluginCount; ++i)
  1111. {
  1112. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1113. if (plugin == nullptr || ! plugin->isEnabled())
  1114. continue;
  1115. plugin->setActive(false, true, false);
  1116. }
  1117. #endif
  1118. // just in case
  1119. //runPendingRtEvents();
  1120. }
  1121. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames,
  1122. const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount)
  1123. {
  1124. const PendingRtEventsRunner prt(this, frames);
  1125. // ---------------------------------------------------------------
  1126. // Time Info
  1127. const NativeTimeInfo* const timeInfo(pHost->get_time_info(pHost->handle));
  1128. pData->timeInfo.playing = timeInfo->playing;
  1129. pData->timeInfo.frame = timeInfo->frame;
  1130. pData->timeInfo.usecs = timeInfo->usecs;
  1131. pData->timeInfo.valid = 0x0;
  1132. if (timeInfo->bbt.valid)
  1133. {
  1134. pData->timeInfo.valid |= EngineTimeInfo::kValidBBT;
  1135. pData->timeInfo.bbt.bar = timeInfo->bbt.bar;
  1136. pData->timeInfo.bbt.beat = timeInfo->bbt.beat;
  1137. pData->timeInfo.bbt.tick = timeInfo->bbt.tick;
  1138. pData->timeInfo.bbt.barStartTick = timeInfo->bbt.barStartTick;
  1139. pData->timeInfo.bbt.beatsPerBar = timeInfo->bbt.beatsPerBar;
  1140. pData->timeInfo.bbt.beatType = timeInfo->bbt.beatType;
  1141. pData->timeInfo.bbt.ticksPerBeat = timeInfo->bbt.ticksPerBeat;
  1142. pData->timeInfo.bbt.beatsPerMinute = timeInfo->bbt.beatsPerMinute;
  1143. }
  1144. // ---------------------------------------------------------------
  1145. // Do nothing if no plugins and rack mode
  1146. if (pData->curPluginCount == 0 && ! kIsPatchbay)
  1147. {
  1148. if (outBuffer[0] != inBuffer[0])
  1149. FloatVectorOperations::copy(outBuffer[0], inBuffer[0], static_cast<int>(frames));
  1150. if (outBuffer[1] != inBuffer[1])
  1151. FloatVectorOperations::copy(outBuffer[1], inBuffer[1], static_cast<int>(frames));
  1152. for (uint32_t i=0; i < midiEventCount; ++i)
  1153. {
  1154. if (! pHost->write_midi_event(pHost->handle, &midiEvents[i]))
  1155. break;
  1156. }
  1157. return;
  1158. }
  1159. // ---------------------------------------------------------------
  1160. // initialize events
  1161. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  1162. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  1163. // ---------------------------------------------------------------
  1164. // events input (before processing)
  1165. {
  1166. uint32_t engineEventIndex = 0;
  1167. for (uint32_t i=0; i < midiEventCount && engineEventIndex < kMaxEngineEventInternalCount; ++i)
  1168. {
  1169. const NativeMidiEvent& midiEvent(midiEvents[i]);
  1170. EngineEvent& engineEvent(pData->events.in[engineEventIndex++]);
  1171. engineEvent.time = midiEvent.time;
  1172. engineEvent.fillFromMidiData(midiEvent.size, midiEvent.data, 0);
  1173. if (engineEventIndex >= kMaxEngineEventInternalCount)
  1174. break;
  1175. }
  1176. }
  1177. if (kIsPatchbay)
  1178. {
  1179. // -----------------------------------------------------------
  1180. // process
  1181. pData->graph.process(pData, inBuffer, outBuffer, frames);
  1182. }
  1183. else
  1184. {
  1185. // -----------------------------------------------------------
  1186. // create audio buffers
  1187. const float* inBuf[2] = { inBuffer[0], inBuffer[1] };
  1188. /* */ float* outBuf[2] = { outBuffer[0], outBuffer[1] };
  1189. // -----------------------------------------------------------
  1190. // process
  1191. pData->graph.processRack(pData, inBuf, outBuf, frames);
  1192. }
  1193. // ---------------------------------------------------------------
  1194. // events output (after processing)
  1195. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  1196. {
  1197. NativeMidiEvent midiEvent;
  1198. for (uint32_t i=0; i < kMaxEngineEventInternalCount; ++i)
  1199. {
  1200. const EngineEvent& engineEvent(pData->events.out[i]);
  1201. if (engineEvent.type == kEngineEventTypeNull)
  1202. break;
  1203. midiEvent.time = engineEvent.time;
  1204. if (engineEvent.type == CarlaBackend::kEngineEventTypeControl)
  1205. {
  1206. midiEvent.port = 0;
  1207. engineEvent.ctrl.convertToMidiData(engineEvent.channel, midiEvent.size, midiEvent.data);
  1208. }
  1209. else if (engineEvent.type == kEngineEventTypeMidi)
  1210. {
  1211. if (engineEvent.midi.size > 4 || engineEvent.midi.dataExt != nullptr)
  1212. continue;
  1213. midiEvent.port = engineEvent.midi.port;
  1214. midiEvent.size = engineEvent.midi.size;
  1215. midiEvent.data[0] = static_cast<uint8_t>(engineEvent.midi.data[0] + engineEvent.channel);
  1216. for (uint8_t j=1; j < midiEvent.size; ++j)
  1217. midiEvent.data[j] = engineEvent.midi.data[j];
  1218. }
  1219. else
  1220. {
  1221. carla_stderr("Unknown event type...");
  1222. continue;
  1223. }
  1224. pHost->write_midi_event(pHost->handle, &midiEvent);
  1225. }
  1226. }
  1227. }
  1228. // -------------------------------------------------------------------
  1229. // Plugin UI calls
  1230. void uiShow(const bool show)
  1231. {
  1232. if (show)
  1233. {
  1234. if (fUiServer.isPipeRunning())
  1235. {
  1236. fUiServer.writeFocusMessage();
  1237. return;
  1238. }
  1239. CarlaString path(pHost->resourceDir);
  1240. if (kIsPatchbay)
  1241. path += CARLA_OS_SEP_STR "carla-plugin-patchbay";
  1242. else
  1243. path += CARLA_OS_SEP_STR "carla-plugin";
  1244. #ifdef CARLA_OS_WIN
  1245. path += ".exe";
  1246. #endif
  1247. carla_stdout("Trying to start carla-plugin using \"%s\"", path.buffer());
  1248. fUiServer.setData(path, pData->sampleRate, pHost->uiName);
  1249. if (! fUiServer.startPipeServer(false))
  1250. {
  1251. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UI_UNAVAILABLE, 0, 0, nullptr, 0.0f);
  1252. return;
  1253. }
  1254. uiServerInfo();
  1255. uiServerOptions();
  1256. uiServerCallback(ENGINE_CALLBACK_ENGINE_STARTED, 0, pData->options.processMode, pData->options.transportMode, 0.0f, "Plugin");
  1257. fUiServer.writeShowMessage();
  1258. for (uint i=0; i < pData->curPluginCount; ++i)
  1259. {
  1260. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1261. if (plugin != nullptr && plugin->isEnabled())
  1262. {
  1263. uiServerCallback(ENGINE_CALLBACK_PLUGIN_ADDED, i, 0, 0, 0.0f, plugin->getName());
  1264. }
  1265. }
  1266. if (kIsPatchbay)
  1267. patchbayRefresh(false);
  1268. if (fWaitForReadyMsg)
  1269. {
  1270. carla_stdout("Using Carla plugin embedded, waiting for it to be ready...");
  1271. for (; fUiServer.isPipeRunning() && ! fUiServer.isReady();)
  1272. {
  1273. carla_msleep(25);
  1274. fUiServer.idlePipe();
  1275. }
  1276. carla_stdout("Done!");
  1277. }
  1278. }
  1279. else
  1280. {
  1281. fUiServer.stopPipeServer(2000);
  1282. // hide all custom uis
  1283. for (uint i=0; i < pData->curPluginCount; ++i)
  1284. {
  1285. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1286. if (plugin != nullptr && plugin->isEnabled())
  1287. {
  1288. if (plugin->getHints() & PLUGIN_HAS_CUSTOM_UI)
  1289. {
  1290. try {
  1291. plugin->showCustomUI(false);
  1292. } CARLA_SAFE_EXCEPTION_CONTINUE("Plugin showCustomUI (hide)");
  1293. }
  1294. }
  1295. }
  1296. }
  1297. }
  1298. void uiIdle()
  1299. {
  1300. for (uint i=0; i < pData->curPluginCount; ++i)
  1301. {
  1302. CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1303. if (plugin != nullptr && plugin->isEnabled())
  1304. {
  1305. const uint hints(plugin->getHints());
  1306. if ((hints & PLUGIN_HAS_CUSTOM_UI) != 0 && (hints & PLUGIN_NEEDS_UI_MAIN_THREAD) != 0)
  1307. {
  1308. try {
  1309. plugin->uiIdle();
  1310. } CARLA_SAFE_EXCEPTION_CONTINUE("Plugin uiIdle");
  1311. }
  1312. }
  1313. }
  1314. if (fUiServer.isPipeRunning())
  1315. {
  1316. fUiServer.idlePipe();
  1317. const CarlaMutexLocker cml(fUiServer.getPipeLock());
  1318. #ifndef CARLA_OS_WIN
  1319. const EngineTimeInfo& timeInfo(pData->timeInfo);
  1320. const ScopedLocale csl;
  1321. // send transport
  1322. fUiServer.writeAndFixMessage("transport");
  1323. fUiServer.writeMessage(timeInfo.playing ? "true\n" : "false\n");
  1324. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  1325. {
  1326. std::sprintf(fTmpBuf, P_UINT64 ":%i:%i:%i\n", timeInfo.frame, timeInfo.bbt.bar, timeInfo.bbt.beat, timeInfo.bbt.tick);
  1327. fUiServer.writeMessage(fTmpBuf);
  1328. std::sprintf(fTmpBuf, "%f\n", timeInfo.bbt.beatsPerMinute);
  1329. fUiServer.writeMessage(fTmpBuf);
  1330. }
  1331. else
  1332. {
  1333. std::sprintf(fTmpBuf, P_UINT64 ":0:0:0\n", timeInfo.frame);
  1334. fUiServer.writeMessage(fTmpBuf);
  1335. fUiServer.writeMessage("0.0\n");
  1336. }
  1337. fUiServer.flushMessages();
  1338. #endif
  1339. // send peaks and param outputs for all plugins
  1340. for (uint i=0; i < pData->curPluginCount; ++i)
  1341. {
  1342. const EnginePluginData& plugData(pData->plugins[i]);
  1343. const CarlaPlugin* const plugin(pData->plugins[i].plugin);
  1344. std::sprintf(fTmpBuf, "PEAKS_%i\n", i);
  1345. fUiServer.writeMessage(fTmpBuf);
  1346. std::sprintf(fTmpBuf, "%f:%f:%f:%f\n", plugData.insPeak[0], plugData.insPeak[1], plugData.outsPeak[0], plugData.outsPeak[1]);
  1347. fUiServer.writeMessage(fTmpBuf);
  1348. fUiServer.flushMessages();
  1349. for (uint32_t j=0, count=plugin->getParameterCount(); j < count; ++j)
  1350. {
  1351. if (! plugin->isParameterOutput(j))
  1352. continue;
  1353. std::sprintf(fTmpBuf, "PARAMVAL_%i:%i\n", i, j);
  1354. fUiServer.writeMessage(fTmpBuf);
  1355. std::sprintf(fTmpBuf, "%f\n", plugin->getParameterValue(j));
  1356. fUiServer.writeMessage(fTmpBuf);
  1357. fUiServer.flushMessages();
  1358. }
  1359. }
  1360. }
  1361. switch (fUiServer.getAndResetUiState())
  1362. {
  1363. case CarlaExternalUI::UiNone:
  1364. case CarlaExternalUI::UiShow:
  1365. break;
  1366. case CarlaExternalUI::UiCrashed:
  1367. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UI_UNAVAILABLE, 0, 0, nullptr, 0.0f);
  1368. break;
  1369. case CarlaExternalUI::UiHide:
  1370. pHost->ui_closed(pHost->handle);
  1371. fUiServer.stopPipeServer(1000);
  1372. break;
  1373. }
  1374. }
  1375. // -------------------------------------------------------------------
  1376. // Plugin state calls
  1377. char* getState() const
  1378. {
  1379. MemoryOutputStream out;
  1380. saveProjectInternal(out);
  1381. return strdup(out.toString().toRawUTF8());
  1382. }
  1383. void setState(const char* const data)
  1384. {
  1385. // remove all plugins from UI side
  1386. for (uint i=0, count=pData->curPluginCount; i < count; ++i)
  1387. CarlaEngine::callback(ENGINE_CALLBACK_PLUGIN_REMOVED, count-i-1, 0, 0, 0.0f, nullptr);
  1388. // remove all plugins from backend, no lock
  1389. fIsRunning = false;
  1390. removeAllPlugins();
  1391. fIsRunning = true;
  1392. // stopped during removeAllPlugins()
  1393. if (! pData->thread.isThreadRunning())
  1394. pData->thread.startThread();
  1395. fOptionsForced = true;
  1396. const String state(data);
  1397. XmlDocument xml(state);
  1398. loadProjectInternal(xml);
  1399. }
  1400. // -------------------------------------------------------------------
  1401. public:
  1402. #define handlePtr ((CarlaEngineNative*)handle)
  1403. static NativePluginHandle _instantiateRack(const NativeHostDescriptor* host)
  1404. {
  1405. return new CarlaEngineNative(host, false);
  1406. }
  1407. static NativePluginHandle _instantiatePatchbay(const NativeHostDescriptor* host)
  1408. {
  1409. return new CarlaEngineNative(host, true);
  1410. }
  1411. static NativePluginHandle _instantiatePatchbay3s(const NativeHostDescriptor* host)
  1412. {
  1413. return new CarlaEngineNative(host, true, 3, 2);
  1414. }
  1415. static NativePluginHandle _instantiatePatchbay16(const NativeHostDescriptor* host)
  1416. {
  1417. return new CarlaEngineNative(host, true, 16);
  1418. }
  1419. static NativePluginHandle _instantiatePatchbay32(const NativeHostDescriptor* host)
  1420. {
  1421. return new CarlaEngineNative(host, true, 32);
  1422. }
  1423. static void _cleanup(NativePluginHandle handle)
  1424. {
  1425. delete handlePtr;
  1426. }
  1427. static uint32_t _get_parameter_count(NativePluginHandle handle)
  1428. {
  1429. return handlePtr->getParameterCount();
  1430. }
  1431. static const NativeParameter* _get_parameter_info(NativePluginHandle handle, uint32_t index)
  1432. {
  1433. return handlePtr->getParameterInfo(index);
  1434. }
  1435. static float _get_parameter_value(NativePluginHandle handle, uint32_t index)
  1436. {
  1437. return handlePtr->getParameterValue(index);
  1438. }
  1439. static uint32_t _get_midi_program_count(NativePluginHandle handle)
  1440. {
  1441. return handlePtr->getMidiProgramCount();
  1442. }
  1443. static const NativeMidiProgram* _get_midi_program_info(NativePluginHandle handle, uint32_t index)
  1444. {
  1445. return handlePtr->getMidiProgramInfo(index);
  1446. }
  1447. static void _set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  1448. {
  1449. handlePtr->setParameterValue(index, value);
  1450. }
  1451. static void _set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  1452. {
  1453. handlePtr->setMidiProgram(channel, bank, program);
  1454. }
  1455. static void _ui_show(NativePluginHandle handle, bool show)
  1456. {
  1457. handlePtr->uiShow(show);
  1458. }
  1459. static void _ui_idle(NativePluginHandle handle)
  1460. {
  1461. handlePtr->uiIdle();
  1462. }
  1463. static void _activate(NativePluginHandle handle)
  1464. {
  1465. handlePtr->activate();
  1466. }
  1467. static void _deactivate(NativePluginHandle handle)
  1468. {
  1469. handlePtr->deactivate();
  1470. }
  1471. static void _process(NativePluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  1472. {
  1473. handlePtr->process(inBuffer, outBuffer, frames, midiEvents, midiEventCount);
  1474. }
  1475. static char* _get_state(NativePluginHandle handle)
  1476. {
  1477. return handlePtr->getState();
  1478. }
  1479. static void _set_state(NativePluginHandle handle, const char* data)
  1480. {
  1481. handlePtr->setState(data);
  1482. }
  1483. static intptr_t _dispatcher(NativePluginHandle handle, NativePluginDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1484. {
  1485. switch(opcode)
  1486. {
  1487. case NATIVE_PLUGIN_OPCODE_NULL:
  1488. if (static_cast<uint32_t>(index) == 0xDEADF00D && static_cast<uintptr_t>(value) == 0xC0C0B00B)
  1489. {
  1490. handlePtr->fWaitForReadyMsg = true;
  1491. return handlePtr->fUiServer.getRemoteWinId();
  1492. }
  1493. return 0;
  1494. case NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED:
  1495. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  1496. handlePtr->bufferSizeChanged(static_cast<uint32_t>(value));
  1497. return 0;
  1498. case NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED:
  1499. CARLA_SAFE_ASSERT_RETURN(opt > 0.0, 0);
  1500. handlePtr->sampleRateChanged(static_cast<double>(opt));
  1501. return 0;
  1502. case NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED:
  1503. handlePtr->offlineModeChanged(value != 0);
  1504. return 0;
  1505. case NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED:
  1506. //handlePtr->uiNameChanged(static_cast<const char*>(ptr));
  1507. return 0;
  1508. }
  1509. return 0;
  1510. // unused
  1511. (void)index;
  1512. (void)ptr;
  1513. }
  1514. // -------------------------------------------------------------------
  1515. static void _ui_server_callback(void* handle, EngineCallbackOpcode action, uint pluginId, int value1, int value2, float value3, const char* valueStr)
  1516. {
  1517. handlePtr->uiServerCallback(action, pluginId, value1, value2, value3, valueStr);
  1518. }
  1519. // -------------------------------------------------------------------
  1520. #undef handlePtr
  1521. private:
  1522. const NativeHostDescriptor* const pHost;
  1523. #if ! (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  1524. const bool kNeedsJuceMsgThread;
  1525. const SharedResourcePointer<SharedMessageThread> fJuceMsgThread;
  1526. #endif
  1527. const bool kIsPatchbay; // rack if false
  1528. bool fIsActive, fIsRunning;
  1529. CarlaEngineNativeUI fUiServer;
  1530. bool fOptionsForced;
  1531. bool fWaitForReadyMsg;
  1532. char fTmpBuf[STR_MAX+1];
  1533. CarlaPlugin* _getFirstPlugin() const noexcept
  1534. {
  1535. if (pData->curPluginCount == 0 || pData->plugins == nullptr)
  1536. return nullptr;
  1537. CarlaPlugin* const plugin(pData->plugins[0].plugin);
  1538. if (plugin == nullptr || ! plugin->isEnabled())
  1539. return nullptr;
  1540. return pData->plugins[0].plugin;
  1541. }
  1542. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineNative)
  1543. };
  1544. // -----------------------------------------------------------------------
  1545. static const NativePluginDescriptor carlaRackDesc = {
  1546. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1547. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1548. |NATIVE_PLUGIN_HAS_UI
  1549. //|NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1550. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1551. |NATIVE_PLUGIN_USES_STATE
  1552. |NATIVE_PLUGIN_USES_TIME),
  1553. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1554. /* audioIns */ 2,
  1555. /* audioOuts */ 2,
  1556. /* midiIns */ 1,
  1557. /* midiOuts */ 1,
  1558. /* paramIns */ 0,
  1559. /* paramOuts */ 0,
  1560. /* name */ "Carla-Rack",
  1561. /* label */ "carlarack",
  1562. /* maker */ "falkTX",
  1563. /* copyright */ "GNU GPL v2+",
  1564. CarlaEngineNative::_instantiateRack,
  1565. CarlaEngineNative::_cleanup,
  1566. CarlaEngineNative::_get_parameter_count,
  1567. CarlaEngineNative::_get_parameter_info,
  1568. CarlaEngineNative::_get_parameter_value,
  1569. CarlaEngineNative::_get_midi_program_count,
  1570. CarlaEngineNative::_get_midi_program_info,
  1571. CarlaEngineNative::_set_parameter_value,
  1572. CarlaEngineNative::_set_midi_program,
  1573. /* _set_custom_data */ nullptr,
  1574. CarlaEngineNative::_ui_show,
  1575. CarlaEngineNative::_ui_idle,
  1576. /* _ui_set_parameter_value */ nullptr,
  1577. /* _ui_set_midi_program */ nullptr,
  1578. /* _ui_set_custom_data */ nullptr,
  1579. CarlaEngineNative::_activate,
  1580. CarlaEngineNative::_deactivate,
  1581. CarlaEngineNative::_process,
  1582. CarlaEngineNative::_get_state,
  1583. CarlaEngineNative::_set_state,
  1584. CarlaEngineNative::_dispatcher
  1585. };
  1586. static const NativePluginDescriptor carlaPatchbayDesc = {
  1587. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1588. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1589. |NATIVE_PLUGIN_HAS_UI
  1590. //|NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1591. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1592. |NATIVE_PLUGIN_USES_STATE
  1593. |NATIVE_PLUGIN_USES_TIME),
  1594. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1595. /* audioIns */ 2,
  1596. /* audioOuts */ 2,
  1597. /* midiIns */ 1,
  1598. /* midiOuts */ 1,
  1599. /* paramIns */ 0,
  1600. /* paramOuts */ 0,
  1601. /* name */ "Carla-Patchbay",
  1602. /* label */ "carlapatchbay",
  1603. /* maker */ "falkTX",
  1604. /* copyright */ "GNU GPL v2+",
  1605. CarlaEngineNative::_instantiatePatchbay,
  1606. CarlaEngineNative::_cleanup,
  1607. CarlaEngineNative::_get_parameter_count,
  1608. CarlaEngineNative::_get_parameter_info,
  1609. CarlaEngineNative::_get_parameter_value,
  1610. CarlaEngineNative::_get_midi_program_count,
  1611. CarlaEngineNative::_get_midi_program_info,
  1612. CarlaEngineNative::_set_parameter_value,
  1613. CarlaEngineNative::_set_midi_program,
  1614. /* _set_custom_data */ nullptr,
  1615. CarlaEngineNative::_ui_show,
  1616. CarlaEngineNative::_ui_idle,
  1617. /* _ui_set_parameter_value */ nullptr,
  1618. /* _ui_set_midi_program */ nullptr,
  1619. /* _ui_set_custom_data */ nullptr,
  1620. CarlaEngineNative::_activate,
  1621. CarlaEngineNative::_deactivate,
  1622. CarlaEngineNative::_process,
  1623. CarlaEngineNative::_get_state,
  1624. CarlaEngineNative::_set_state,
  1625. CarlaEngineNative::_dispatcher
  1626. };
  1627. static const NativePluginDescriptor carlaPatchbay3sDesc = {
  1628. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1629. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1630. |NATIVE_PLUGIN_HAS_UI
  1631. //|NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1632. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1633. |NATIVE_PLUGIN_USES_STATE
  1634. |NATIVE_PLUGIN_USES_TIME),
  1635. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1636. /* audioIns */ 3,
  1637. /* audioOuts */ 2,
  1638. /* midiIns */ 1,
  1639. /* midiOuts */ 1,
  1640. /* paramIns */ 0,
  1641. /* paramOuts */ 0,
  1642. /* name */ "Carla-Patchbay (sidechain)",
  1643. /* label */ "carlapatchbay3s",
  1644. /* maker */ "falkTX",
  1645. /* copyright */ "GNU GPL v2+",
  1646. CarlaEngineNative::_instantiatePatchbay3s,
  1647. CarlaEngineNative::_cleanup,
  1648. CarlaEngineNative::_get_parameter_count,
  1649. CarlaEngineNative::_get_parameter_info,
  1650. CarlaEngineNative::_get_parameter_value,
  1651. CarlaEngineNative::_get_midi_program_count,
  1652. CarlaEngineNative::_get_midi_program_info,
  1653. CarlaEngineNative::_set_parameter_value,
  1654. CarlaEngineNative::_set_midi_program,
  1655. /* _set_custom_data */ nullptr,
  1656. CarlaEngineNative::_ui_show,
  1657. CarlaEngineNative::_ui_idle,
  1658. /* _ui_set_parameter_value */ nullptr,
  1659. /* _ui_set_midi_program */ nullptr,
  1660. /* _ui_set_custom_data */ nullptr,
  1661. CarlaEngineNative::_activate,
  1662. CarlaEngineNative::_deactivate,
  1663. CarlaEngineNative::_process,
  1664. CarlaEngineNative::_get_state,
  1665. CarlaEngineNative::_set_state,
  1666. CarlaEngineNative::_dispatcher
  1667. };
  1668. static const NativePluginDescriptor carlaPatchbay16Desc = {
  1669. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1670. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1671. |NATIVE_PLUGIN_HAS_UI
  1672. //|NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1673. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1674. |NATIVE_PLUGIN_USES_STATE
  1675. |NATIVE_PLUGIN_USES_TIME),
  1676. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1677. /* audioIns */ 16,
  1678. /* audioOuts */ 16,
  1679. /* midiIns */ 1,
  1680. /* midiOuts */ 1,
  1681. /* paramIns */ 0,
  1682. /* paramOuts */ 0,
  1683. /* name */ "Carla-Patchbay (16chan)",
  1684. /* label */ "carlapatchbay16",
  1685. /* maker */ "falkTX",
  1686. /* copyright */ "GNU GPL v2+",
  1687. CarlaEngineNative::_instantiatePatchbay16,
  1688. CarlaEngineNative::_cleanup,
  1689. CarlaEngineNative::_get_parameter_count,
  1690. CarlaEngineNative::_get_parameter_info,
  1691. CarlaEngineNative::_get_parameter_value,
  1692. CarlaEngineNative::_get_midi_program_count,
  1693. CarlaEngineNative::_get_midi_program_info,
  1694. CarlaEngineNative::_set_parameter_value,
  1695. CarlaEngineNative::_set_midi_program,
  1696. /* _set_custom_data */ nullptr,
  1697. CarlaEngineNative::_ui_show,
  1698. CarlaEngineNative::_ui_idle,
  1699. /* _ui_set_parameter_value */ nullptr,
  1700. /* _ui_set_midi_program */ nullptr,
  1701. /* _ui_set_custom_data */ nullptr,
  1702. CarlaEngineNative::_activate,
  1703. CarlaEngineNative::_deactivate,
  1704. CarlaEngineNative::_process,
  1705. CarlaEngineNative::_get_state,
  1706. CarlaEngineNative::_set_state,
  1707. CarlaEngineNative::_dispatcher
  1708. };
  1709. static const NativePluginDescriptor carlaPatchbay32Desc = {
  1710. /* category */ NATIVE_PLUGIN_CATEGORY_OTHER,
  1711. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  1712. |NATIVE_PLUGIN_HAS_UI
  1713. //|NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  1714. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  1715. |NATIVE_PLUGIN_USES_STATE
  1716. |NATIVE_PLUGIN_USES_TIME),
  1717. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_EVERYTHING),
  1718. /* audioIns */ 32,
  1719. /* audioOuts */ 32,
  1720. /* midiIns */ 1,
  1721. /* midiOuts */ 1,
  1722. /* paramIns */ 0,
  1723. /* paramOuts */ 0,
  1724. /* name */ "Carla-Patchbay (32chan)",
  1725. /* label */ "carlapatchbay32",
  1726. /* maker */ "falkTX",
  1727. /* copyright */ "GNU GPL v2+",
  1728. CarlaEngineNative::_instantiatePatchbay32,
  1729. CarlaEngineNative::_cleanup,
  1730. CarlaEngineNative::_get_parameter_count,
  1731. CarlaEngineNative::_get_parameter_info,
  1732. CarlaEngineNative::_get_parameter_value,
  1733. CarlaEngineNative::_get_midi_program_count,
  1734. CarlaEngineNative::_get_midi_program_info,
  1735. CarlaEngineNative::_set_parameter_value,
  1736. CarlaEngineNative::_set_midi_program,
  1737. /* _set_custom_data */ nullptr,
  1738. CarlaEngineNative::_ui_show,
  1739. CarlaEngineNative::_ui_idle,
  1740. /* _ui_set_parameter_value */ nullptr,
  1741. /* _ui_set_midi_program */ nullptr,
  1742. /* _ui_set_custom_data */ nullptr,
  1743. CarlaEngineNative::_activate,
  1744. CarlaEngineNative::_deactivate,
  1745. CarlaEngineNative::_process,
  1746. CarlaEngineNative::_get_state,
  1747. CarlaEngineNative::_set_state,
  1748. CarlaEngineNative::_dispatcher
  1749. };
  1750. CARLA_BACKEND_END_NAMESPACE
  1751. // -----------------------------------------------------------------------
  1752. CARLA_EXPORT
  1753. void carla_register_native_plugin_carla();
  1754. void carla_register_native_plugin_carla()
  1755. {
  1756. CARLA_BACKEND_USE_NAMESPACE;
  1757. carla_register_native_plugin(&carlaRackDesc);
  1758. carla_register_native_plugin(&carlaPatchbayDesc);
  1759. carla_register_native_plugin(&carlaPatchbay3sDesc);
  1760. carla_register_native_plugin(&carlaPatchbay16Desc);
  1761. carla_register_native_plugin(&carlaPatchbay32Desc);
  1762. }
  1763. // -----------------------------------------------------------------------
  1764. CARLA_EXPORT
  1765. const NativePluginDescriptor* carla_get_native_rack_plugin();
  1766. const NativePluginDescriptor* carla_get_native_rack_plugin()
  1767. {
  1768. CARLA_BACKEND_USE_NAMESPACE;
  1769. return &carlaRackDesc;
  1770. }
  1771. CARLA_EXPORT
  1772. const NativePluginDescriptor* carla_get_native_patchbay_plugin();
  1773. const NativePluginDescriptor* carla_get_native_patchbay_plugin()
  1774. {
  1775. CARLA_BACKEND_USE_NAMESPACE;
  1776. return &carlaPatchbayDesc;
  1777. }
  1778. // -----------------------------------------------------------------------
  1779. // Extra stuff for linking purposes
  1780. #ifdef CARLA_PLUGIN_EXPORT
  1781. CARLA_BACKEND_START_NAMESPACE
  1782. CarlaEngine* CarlaEngine::newJack() { return nullptr; }
  1783. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  1784. CarlaEngine* CarlaEngine::newJuce(const AudioApi) { return nullptr; }
  1785. uint CarlaEngine::getJuceApiCount() { return 0; }
  1786. const char* CarlaEngine::getJuceApiName(const uint) { return nullptr; }
  1787. const char* const* CarlaEngine::getJuceApiDeviceNames(const uint) { return nullptr; }
  1788. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const uint, const char* const) { return nullptr; }
  1789. # else
  1790. CarlaEngine* CarlaEngine::newRtAudio(const AudioApi) { return nullptr; }
  1791. uint CarlaEngine::getRtAudioApiCount() { return 0; }
  1792. const char* CarlaEngine::getRtAudioApiName(const uint) { return nullptr; }
  1793. const char* const* CarlaEngine::getRtAudioApiDeviceNames(const uint) { return nullptr; }
  1794. const EngineDriverDeviceInfo* CarlaEngine::getRtAudioDeviceInfo(const uint, const char* const) { return nullptr; }
  1795. # endif
  1796. CARLA_BACKEND_END_NAMESPACE
  1797. #include "CarlaHostCommon.cpp"
  1798. #include "CarlaPluginUI.cpp"
  1799. #include "CarlaDssiUtils.cpp"
  1800. #include "CarlaPatchbayUtils.cpp"
  1801. #include "CarlaPipeUtils.cpp"
  1802. #include "CarlaStateUtils.cpp"
  1803. #include "CarlaJuceAudioProcessors.cpp"
  1804. #endif
  1805. // -----------------------------------------------------------------------