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.

1762 lines
58KB

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