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.

2277 lines
76KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 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 "CarlaEngineGraph.hpp"
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaPlugin.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaJuceUtils.hpp"
  22. #include "CarlaMIDI.h"
  23. using water::jmax;
  24. using water::jmin;
  25. using water::AudioProcessor;
  26. using water::MidiBuffer;
  27. using water::String;
  28. using water::StringArray;
  29. CARLA_BACKEND_START_NAMESPACE
  30. // -----------------------------------------------------------------------
  31. // Fallback data
  32. static const PortNameToId kPortNameToIdFallback = { 0, 0, { '\0' }, { '\0' } };
  33. static /* */ PortNameToId kPortNameToIdFallbackNC = { 0, 0, { '\0' }, { '\0' } };
  34. // -----------------------------------------------------------------------
  35. // External Graph stuff
  36. static inline
  37. uint getExternalGraphPortIdFromName(const char* const shortname) noexcept
  38. {
  39. if (std::strcmp(shortname, "AudioIn1") == 0 || std::strcmp(shortname, "audio-in1") == 0)
  40. return kExternalGraphCarlaPortAudioIn1;
  41. if (std::strcmp(shortname, "AudioIn2") == 0 || std::strcmp(shortname, "audio-in2") == 0)
  42. return kExternalGraphCarlaPortAudioIn2;
  43. if (std::strcmp(shortname, "AudioOut1") == 0 || std::strcmp(shortname, "audio-out1") == 0)
  44. return kExternalGraphCarlaPortAudioOut1;
  45. if (std::strcmp(shortname, "AudioOut2") == 0 || std::strcmp(shortname, "audio-out2") == 0)
  46. return kExternalGraphCarlaPortAudioOut2;
  47. if (std::strcmp(shortname, "MidiIn") == 0 || std::strcmp(shortname, "midi-in") == 0)
  48. return kExternalGraphCarlaPortMidiIn;
  49. if (std::strcmp(shortname, "MidiOut") == 0 || std::strcmp(shortname, "midi-out") == 0)
  50. return kExternalGraphCarlaPortMidiOut;
  51. carla_stderr("CarlaBackend::getExternalGraphPortIdFromName(%s) - invalid short name", shortname);
  52. return kExternalGraphCarlaPortNull;
  53. }
  54. static inline
  55. const char* getExternalGraphFullPortNameFromId(const /*RackGraphCarlaPortIds*/ uint portId)
  56. {
  57. switch (portId)
  58. {
  59. case kExternalGraphCarlaPortAudioIn1:
  60. return "Carla:AudioIn1";
  61. case kExternalGraphCarlaPortAudioIn2:
  62. return "Carla:AudioIn2";
  63. case kExternalGraphCarlaPortAudioOut1:
  64. return "Carla:AudioOut1";
  65. case kExternalGraphCarlaPortAudioOut2:
  66. return "Carla:AudioOut2";
  67. case kExternalGraphCarlaPortMidiIn:
  68. return "Carla:MidiIn";
  69. case kExternalGraphCarlaPortMidiOut:
  70. return "Carla:MidiOut";
  71. //case kExternalGraphCarlaPortNull:
  72. //case kExternalGraphCarlaPortMax:
  73. // break;
  74. }
  75. carla_stderr("CarlaBackend::getExternalGraphFullPortNameFromId(%i) - invalid port id", portId);
  76. return nullptr;
  77. }
  78. // -----------------------------------------------------------------------
  79. ExternalGraphPorts::ExternalGraphPorts() noexcept
  80. : ins(),
  81. outs() {}
  82. const char* ExternalGraphPorts::getName(const bool isInput, const uint portId) const noexcept
  83. {
  84. for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin2() : outs.begin2(); it.valid(); it.next())
  85. {
  86. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  87. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  88. if (portNameToId.port == portId)
  89. return portNameToId.name;
  90. }
  91. return nullptr;
  92. }
  93. uint ExternalGraphPorts::getPortId(const bool isInput, const char portName[], bool* const ok) const noexcept
  94. {
  95. for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin2() : outs.begin2(); it.valid(); it.next())
  96. {
  97. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  98. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  99. if (std::strncmp(portNameToId.name, portName, STR_MAX) == 0)
  100. {
  101. if (ok != nullptr)
  102. *ok = true;
  103. return portNameToId.port;
  104. }
  105. }
  106. if (ok != nullptr)
  107. *ok = false;
  108. return 0;
  109. }
  110. // -----------------------------------------------------------------------
  111. ExternalGraph::ExternalGraph(CarlaEngine* const engine) noexcept
  112. : connections(),
  113. audioPorts(),
  114. midiPorts(),
  115. retCon(),
  116. kEngine(engine) {}
  117. void ExternalGraph::clear() noexcept
  118. {
  119. connections.clear();
  120. audioPorts.ins.clear();
  121. audioPorts.outs.clear();
  122. midiPorts.ins.clear();
  123. midiPorts.outs.clear();
  124. }
  125. bool ExternalGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback) noexcept
  126. {
  127. uint otherGroup, otherPort, carlaPort;
  128. if (groupA == kExternalGraphGroupCarla)
  129. {
  130. CARLA_SAFE_ASSERT_RETURN(groupB != kExternalGraphGroupCarla, false);
  131. carlaPort = portA;
  132. otherGroup = groupB;
  133. otherPort = portB;
  134. }
  135. else
  136. {
  137. CARLA_SAFE_ASSERT_RETURN(groupB == kExternalGraphGroupCarla, false);
  138. carlaPort = portB;
  139. otherGroup = groupA;
  140. otherPort = portA;
  141. }
  142. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  143. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  144. bool makeConnection = false;
  145. switch (carlaPort)
  146. {
  147. case kExternalGraphCarlaPortAudioIn1:
  148. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  149. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  150. break;
  151. case kExternalGraphCarlaPortAudioIn2:
  152. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  153. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  154. break;
  155. case kExternalGraphCarlaPortAudioOut1:
  156. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  157. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  158. break;
  159. case kExternalGraphCarlaPortAudioOut2:
  160. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  161. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  162. break;
  163. case kExternalGraphCarlaPortMidiIn:
  164. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiIn, false);
  165. if (const char* const portName = midiPorts.getName(true, otherPort))
  166. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  167. break;
  168. case kExternalGraphCarlaPortMidiOut:
  169. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiOut, false);
  170. if (const char* const portName = midiPorts.getName(false, otherPort))
  171. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  172. break;
  173. }
  174. if (! makeConnection)
  175. {
  176. kEngine->setLastError("Invalid rack connection");
  177. return false;
  178. }
  179. ConnectionToId connectionToId;
  180. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  181. char strBuf[STR_MAX+1];
  182. strBuf[STR_MAX] = '\0';
  183. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  184. if (sendCallback)
  185. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  186. connections.list.append(connectionToId);
  187. return true;
  188. }
  189. bool ExternalGraph::disconnect(const uint connectionId) noexcept
  190. {
  191. CARLA_SAFE_ASSERT_RETURN(connections.list.count() > 0, false);
  192. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  193. {
  194. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  195. const ConnectionToId& connectionToId(it.getValue(fallback));
  196. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  197. if (connectionToId.id != connectionId)
  198. continue;
  199. uint otherGroup, otherPort, carlaPort;
  200. if (connectionToId.groupA == kExternalGraphGroupCarla)
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB != kExternalGraphGroupCarla, false);
  203. carlaPort = connectionToId.portA;
  204. otherGroup = connectionToId.groupB;
  205. otherPort = connectionToId.portB;
  206. }
  207. else
  208. {
  209. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB == kExternalGraphGroupCarla, false);
  210. carlaPort = connectionToId.portB;
  211. otherGroup = connectionToId.groupA;
  212. otherPort = connectionToId.portA;
  213. }
  214. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  215. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  216. bool makeDisconnection = false;
  217. switch (carlaPort)
  218. {
  219. case kExternalGraphCarlaPortAudioIn1:
  220. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  221. break;
  222. case kExternalGraphCarlaPortAudioIn2:
  223. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  224. break;
  225. case kExternalGraphCarlaPortAudioOut1:
  226. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  227. break;
  228. case kExternalGraphCarlaPortAudioOut2:
  229. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  230. break;
  231. case kExternalGraphCarlaPortMidiIn:
  232. if (const char* const portName = midiPorts.getName(true, otherPort))
  233. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  234. break;
  235. case kExternalGraphCarlaPortMidiOut:
  236. if (const char* const portName = midiPorts.getName(false, otherPort))
  237. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  238. break;
  239. }
  240. if (! makeDisconnection)
  241. {
  242. kEngine->setLastError("Invalid rack connection");
  243. return false;
  244. }
  245. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  246. connections.list.remove(it);
  247. return true;
  248. }
  249. kEngine->setLastError("Failed to find connection");
  250. return false;
  251. }
  252. void ExternalGraph::refresh(const char* const deviceName)
  253. {
  254. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  255. const bool isRack(kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  256. // Main
  257. {
  258. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupCarla, PATCHBAY_ICON_CARLA, MAIN_CARLA_PLUGIN_ID, 0.0f, kEngine->getName());
  259. if (isRack)
  260. {
  261. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in1");
  262. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in2");
  263. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out1");
  264. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out2");
  265. }
  266. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiIn, PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "midi-in");
  267. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiOut, PATCHBAY_PORT_TYPE_MIDI, 0.0f, "midi-out");
  268. }
  269. char strBuf[STR_MAX+1];
  270. strBuf[STR_MAX] = '\0';
  271. if (isRack)
  272. {
  273. // Audio In
  274. if (deviceName[0] != '\0')
  275. std::snprintf(strBuf, STR_MAX, "Capture (%s)", deviceName);
  276. else
  277. std::strncpy(strBuf, "Capture", STR_MAX);
  278. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupAudioIn, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  279. const CarlaString groupNameIn(strBuf);
  280. int h = 0;
  281. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.ins.begin2(); it.valid(); it.next())
  282. {
  283. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  284. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  285. portNameToId.setFullName(groupNameIn + portNameToId.name);
  286. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupAudioIn, ++h,
  287. PATCHBAY_PORT_TYPE_AUDIO, 0.0f, portNameToId.name);
  288. }
  289. // Audio Out
  290. if (deviceName[0] != '\0')
  291. std::snprintf(strBuf, STR_MAX, "Playback (%s)", deviceName);
  292. else
  293. std::strncpy(strBuf, "Playback", STR_MAX);
  294. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupAudioOut, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  295. const CarlaString groupNameOut(strBuf);
  296. h = 0;
  297. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.outs.begin2(); it.valid(); it.next())
  298. {
  299. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  300. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  301. portNameToId.setFullName(groupNameOut + portNameToId.name);
  302. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupAudioOut, ++h,
  303. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, portNameToId.name);
  304. }
  305. }
  306. // MIDI In
  307. {
  308. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupMidiIn, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Readable MIDI ports");
  309. const CarlaString groupNamePlus("Readable MIDI ports:");
  310. int h = 0;
  311. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.ins.begin2(); it.valid(); it.next())
  312. {
  313. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  314. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  315. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  316. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupMidiIn, ++h,
  317. PATCHBAY_PORT_TYPE_MIDI, 0.0f, portNameToId.name);
  318. }
  319. }
  320. // MIDI Out
  321. {
  322. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupMidiOut, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Writable MIDI ports");
  323. const CarlaString groupNamePlus("Writable MIDI ports:");
  324. int h = 0;
  325. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.outs.begin2(); it.valid(); it.next())
  326. {
  327. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  328. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  329. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  330. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupMidiOut, ++h,
  331. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, portNameToId.name);
  332. }
  333. }
  334. }
  335. const char* const* ExternalGraph::getConnections() const noexcept
  336. {
  337. if (connections.list.count() == 0)
  338. return nullptr;
  339. CarlaStringList connList;
  340. char strBuf[STR_MAX+1];
  341. strBuf[STR_MAX] = '\0';
  342. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  343. {
  344. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  345. const ConnectionToId& connectionToId(it.getValue(fallback));
  346. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  347. uint otherGroup, otherPort, carlaPort;
  348. if (connectionToId.groupA == kExternalGraphGroupCarla)
  349. {
  350. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB != kExternalGraphGroupCarla);
  351. carlaPort = connectionToId.portA;
  352. otherGroup = connectionToId.groupB;
  353. otherPort = connectionToId.portB;
  354. }
  355. else
  356. {
  357. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB == kExternalGraphGroupCarla);
  358. carlaPort = connectionToId.portB;
  359. otherGroup = connectionToId.groupA;
  360. otherPort = connectionToId.portA;
  361. }
  362. CARLA_SAFE_ASSERT_CONTINUE(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax);
  363. CARLA_SAFE_ASSERT_CONTINUE(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax);
  364. switch (carlaPort)
  365. {
  366. case kExternalGraphCarlaPortAudioIn1:
  367. case kExternalGraphCarlaPortAudioIn2:
  368. std::snprintf(strBuf, STR_MAX, "AudioIn:%s", audioPorts.getName(true, otherPort));
  369. connList.append(strBuf);
  370. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  371. break;
  372. case kExternalGraphCarlaPortAudioOut1:
  373. case kExternalGraphCarlaPortAudioOut2:
  374. std::snprintf(strBuf, STR_MAX, "AudioOut:%s", audioPorts.getName(false, otherPort));
  375. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  376. connList.append(strBuf);
  377. break;
  378. case kExternalGraphCarlaPortMidiIn:
  379. std::snprintf(strBuf, STR_MAX, "MidiIn:%s", midiPorts.getName(true, otherPort));
  380. connList.append(strBuf);
  381. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  382. break;
  383. case kExternalGraphCarlaPortMidiOut:
  384. std::snprintf(strBuf, STR_MAX, "MidiOut:%s", midiPorts.getName(false, otherPort));
  385. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  386. connList.append(strBuf);
  387. break;
  388. }
  389. }
  390. if (connList.count() == 0)
  391. return nullptr;
  392. retCon = connList.toCharStringListPtr();
  393. return retCon;
  394. }
  395. bool ExternalGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  396. {
  397. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', false);
  398. if (std::strncmp(fullPortName, "Carla:", 6) == 0)
  399. {
  400. groupId = kExternalGraphGroupCarla;
  401. portId = getExternalGraphPortIdFromName(fullPortName+6);
  402. if (portId > kExternalGraphCarlaPortNull && portId < kExternalGraphCarlaPortMax)
  403. return true;
  404. }
  405. else if (std::strncmp(fullPortName, "AudioIn:", 8) == 0)
  406. {
  407. groupId = kExternalGraphGroupAudioIn;
  408. if (const char* const portName = fullPortName+8)
  409. {
  410. bool ok;
  411. portId = audioPorts.getPortId(true, portName, &ok);
  412. return ok;
  413. }
  414. }
  415. else if (std::strncmp(fullPortName, "AudioOut:", 9) == 0)
  416. {
  417. groupId = kExternalGraphGroupAudioOut;
  418. if (const char* const portName = fullPortName+9)
  419. {
  420. bool ok;
  421. portId = audioPorts.getPortId(false, portName, &ok);
  422. return ok;
  423. }
  424. }
  425. else if (std::strncmp(fullPortName, "MidiIn:", 7) == 0)
  426. {
  427. groupId = kExternalGraphGroupMidiIn;
  428. if (const char* const portName = fullPortName+7)
  429. {
  430. bool ok;
  431. portId = midiPorts.getPortId(true, portName, &ok);
  432. return ok;
  433. }
  434. }
  435. else if (std::strncmp(fullPortName, "MidiOut:", 8) == 0)
  436. {
  437. groupId = kExternalGraphGroupMidiOut;
  438. if (const char* const portName = fullPortName+8)
  439. {
  440. bool ok;
  441. portId = midiPorts.getPortId(false, portName, &ok);
  442. return ok;
  443. }
  444. }
  445. return false;
  446. }
  447. // -----------------------------------------------------------------------
  448. // RackGraph Buffers
  449. RackGraph::Buffers::Buffers() noexcept
  450. : mutex(),
  451. connectedIn1(),
  452. connectedIn2(),
  453. connectedOut1(),
  454. connectedOut2(),
  455. #ifdef CARLA_PROPER_CPP11_SUPPORT
  456. inBuf{nullptr, nullptr},
  457. inBufTmp{nullptr, nullptr},
  458. outBuf{nullptr, nullptr},
  459. #endif
  460. unusedBuf(nullptr)
  461. {
  462. #ifndef CARLA_PROPER_CPP11_SUPPORT
  463. inBuf[0] = inBuf[1] = nullptr;
  464. inBufTmp[0] = inBufTmp[1] = nullptr;
  465. outBuf[0] = outBuf[1] = nullptr;
  466. #endif
  467. }
  468. RackGraph::Buffers::~Buffers() noexcept
  469. {
  470. const CarlaRecursiveMutexLocker cml(mutex);
  471. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  472. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  473. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  474. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  475. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  476. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  477. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  478. connectedIn1.clear();
  479. connectedIn2.clear();
  480. connectedOut1.clear();
  481. connectedOut2.clear();
  482. }
  483. void RackGraph::Buffers::setBufferSize(const uint32_t bufferSize, const bool createBuffers) noexcept
  484. {
  485. const CarlaRecursiveMutexLocker cml(mutex);
  486. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  487. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  488. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  489. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  490. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  491. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  492. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  493. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  494. try {
  495. inBufTmp[0] = new float[bufferSize];
  496. inBufTmp[1] = new float[bufferSize];
  497. unusedBuf = new float[bufferSize];
  498. if (createBuffers)
  499. {
  500. inBuf[0] = new float[bufferSize];
  501. inBuf[1] = new float[bufferSize];
  502. outBuf[0] = new float[bufferSize];
  503. outBuf[1] = new float[bufferSize];
  504. }
  505. }
  506. catch(...) {
  507. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  508. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  509. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  510. if (createBuffers)
  511. {
  512. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  513. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  514. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  515. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  516. }
  517. return;
  518. }
  519. carla_zeroFloats(inBufTmp[0], bufferSize);
  520. carla_zeroFloats(inBufTmp[1], bufferSize);
  521. if (createBuffers)
  522. {
  523. carla_zeroFloats(inBuf[0], bufferSize);
  524. carla_zeroFloats(inBuf[1], bufferSize);
  525. carla_zeroFloats(outBuf[0], bufferSize);
  526. carla_zeroFloats(outBuf[1], bufferSize);
  527. }
  528. }
  529. // -----------------------------------------------------------------------
  530. // RackGraph
  531. RackGraph::RackGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs) noexcept
  532. : extGraph(engine),
  533. inputs(ins),
  534. outputs(outs),
  535. isOffline(false),
  536. audioBuffers(),
  537. kEngine(engine)
  538. {
  539. setBufferSize(engine->getBufferSize());
  540. }
  541. RackGraph::~RackGraph() noexcept
  542. {
  543. extGraph.clear();
  544. }
  545. void RackGraph::setBufferSize(const uint32_t bufferSize) noexcept
  546. {
  547. audioBuffers.setBufferSize(bufferSize, (inputs > 0 || outputs > 0));
  548. }
  549. void RackGraph::setOffline(const bool offline) noexcept
  550. {
  551. isOffline = offline;
  552. }
  553. bool RackGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  554. {
  555. return extGraph.connect(groupA, portA, groupB, portB, true);
  556. }
  557. bool RackGraph::disconnect(const uint connectionId) noexcept
  558. {
  559. return extGraph.disconnect(connectionId);
  560. }
  561. void RackGraph::refresh(const char* const deviceName)
  562. {
  563. extGraph.refresh(deviceName);
  564. char strBuf[STR_MAX+1];
  565. strBuf[STR_MAX] = '\0';
  566. // Connections
  567. const CarlaRecursiveMutexLocker cml(audioBuffers.mutex);
  568. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  569. {
  570. const uint& portId(it.getValue(0));
  571. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  572. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  573. ConnectionToId connectionToId;
  574. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1);
  575. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  576. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  577. extGraph.connections.list.append(connectionToId);
  578. }
  579. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  580. {
  581. const uint& portId(it.getValue(0));
  582. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  583. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  584. ConnectionToId connectionToId;
  585. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2);
  586. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  587. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  588. extGraph.connections.list.append(connectionToId);
  589. }
  590. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  591. {
  592. const uint& portId(it.getValue(0));
  593. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  594. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  595. ConnectionToId connectionToId;
  596. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, kExternalGraphGroupAudioOut, portId);
  597. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  598. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  599. extGraph.connections.list.append(connectionToId);
  600. }
  601. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  602. {
  603. const uint& portId(it.getValue(0));
  604. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  605. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  606. ConnectionToId connectionToId;
  607. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, kExternalGraphGroupAudioOut, portId);
  608. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  609. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  610. extGraph.connections.list.append(connectionToId);
  611. }
  612. }
  613. const char* const* RackGraph::getConnections() const noexcept
  614. {
  615. return extGraph.getConnections();
  616. }
  617. bool RackGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  618. {
  619. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  620. }
  621. void RackGraph::process(CarlaEngine::ProtectedData* const data, const float* inBufReal[2], float* outBufReal[2], const uint32_t frames)
  622. {
  623. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  624. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  625. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  626. // safe copy
  627. float* const dummyBuf = audioBuffers.unusedBuf;
  628. float* const inBuf0 = audioBuffers.inBufTmp[0];
  629. float* const inBuf1 = audioBuffers.inBufTmp[1];
  630. // initialize audio inputs
  631. carla_copyFloats(inBuf0, inBufReal[0], frames);
  632. carla_copyFloats(inBuf1, inBufReal[1], frames);
  633. // initialize audio outputs (zero)
  634. carla_zeroFloats(outBufReal[0], frames);
  635. carla_zeroFloats(outBufReal[1], frames);
  636. // initialize event outputs (zero)
  637. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  638. uint32_t oldAudioInCount = 0;
  639. uint32_t oldAudioOutCount = 0;
  640. uint32_t oldMidiOutCount = 0;
  641. bool processed = false;
  642. // process plugins
  643. for (uint i=0; i < data->curPluginCount; ++i)
  644. {
  645. CarlaPlugin* const plugin = data->plugins[i].plugin;
  646. if (plugin == nullptr || ! plugin->isEnabled() || ! plugin->tryLock(isOffline))
  647. continue;
  648. if (processed)
  649. {
  650. // initialize audio inputs (from previous outputs)
  651. carla_copyFloats(inBuf0, outBufReal[0], frames);
  652. carla_copyFloats(inBuf1, outBufReal[1], frames);
  653. // initialize audio outputs (zero)
  654. carla_zeroFloats(outBufReal[0], frames);
  655. carla_zeroFloats(outBufReal[1], frames);
  656. // if plugin has no midi out, add previous events
  657. if (oldMidiOutCount == 0 && data->events.in[0].type != kEngineEventTypeNull)
  658. {
  659. if (data->events.out[0].type != kEngineEventTypeNull)
  660. {
  661. // TODO: carefully add to input, sorted events
  662. //carla_stderr("TODO midi event mixing here %s", plugin->getName());
  663. }
  664. // else nothing needed
  665. }
  666. else
  667. {
  668. // initialize event inputs from previous outputs
  669. carla_copyStructs(data->events.in, data->events.out, kMaxEngineEventInternalCount);
  670. // initialize event outputs (zero)
  671. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  672. }
  673. }
  674. oldAudioInCount = plugin->getAudioInCount();
  675. oldAudioOutCount = plugin->getAudioOutCount();
  676. oldMidiOutCount = plugin->getMidiOutCount();
  677. const uint32_t numInBufs = std::max(oldAudioInCount, 2U);
  678. const uint32_t numOutBufs = std::max(oldAudioOutCount, 2U);
  679. const float* inBuf[numInBufs];
  680. inBuf[0] = inBuf0;
  681. inBuf[1] = inBuf1;
  682. float* outBuf[numOutBufs];
  683. outBuf[0] = outBufReal[0];
  684. outBuf[1] = outBufReal[1];
  685. if (numInBufs > 2 || numOutBufs > 2)
  686. {
  687. carla_zeroFloats(dummyBuf, frames);
  688. for (uint32_t i=2; i<numInBufs; ++i)
  689. inBuf[i] = dummyBuf;
  690. for (uint32_t i=2; i<numOutBufs; ++i)
  691. outBuf[i] = dummyBuf;
  692. }
  693. // process
  694. plugin->initBuffers();
  695. plugin->process(inBuf, outBuf, nullptr, nullptr, frames);
  696. plugin->unlock();
  697. // if plugin has no audio inputs, add input buffer
  698. if (oldAudioInCount == 0)
  699. {
  700. carla_addFloats(outBufReal[0], inBuf0, frames);
  701. carla_addFloats(outBufReal[1], inBuf1, frames);
  702. }
  703. // if plugin only has 1 output, copy it to the 2nd
  704. if (oldAudioOutCount == 1)
  705. {
  706. carla_copyFloats(outBufReal[1], outBufReal[0], frames);
  707. }
  708. // set peaks
  709. {
  710. EnginePluginData& pluginData(data->plugins[i]);
  711. if (oldAudioInCount > 0)
  712. {
  713. pluginData.insPeak[0] = carla_findMaxNormalizedFloat(inBuf0, frames);
  714. pluginData.insPeak[1] = carla_findMaxNormalizedFloat(inBuf1, frames);
  715. }
  716. else
  717. {
  718. pluginData.insPeak[0] = 0.0f;
  719. pluginData.insPeak[1] = 0.0f;
  720. }
  721. if (oldAudioOutCount > 0)
  722. {
  723. pluginData.outsPeak[0] = carla_findMaxNormalizedFloat(outBufReal[0], frames);
  724. pluginData.outsPeak[1] = carla_findMaxNormalizedFloat(outBufReal[1], frames);
  725. }
  726. else
  727. {
  728. pluginData.outsPeak[0] = 0.0f;
  729. pluginData.outsPeak[1] = 0.0f;
  730. }
  731. }
  732. processed = true;
  733. }
  734. }
  735. void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  736. {
  737. CARLA_SAFE_ASSERT_RETURN(audioBuffers.outBuf[1] != nullptr,);
  738. const CarlaRecursiveMutexLocker _cml(audioBuffers.mutex);
  739. if (inBuf != nullptr && inputs > 0)
  740. {
  741. bool noConnections = true;
  742. // connect input buffers
  743. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  744. {
  745. const uint& port(it.getValue(0));
  746. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  747. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  748. if (noConnections)
  749. {
  750. carla_copyFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  751. noConnections = false;
  752. }
  753. else
  754. {
  755. carla_addFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  756. }
  757. }
  758. if (noConnections)
  759. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  760. noConnections = true;
  761. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  762. {
  763. const uint& port(it.getValue(0));
  764. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  765. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  766. if (noConnections)
  767. {
  768. carla_copyFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  769. noConnections = false;
  770. }
  771. else
  772. {
  773. carla_addFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  774. }
  775. }
  776. if (noConnections)
  777. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  778. }
  779. else
  780. {
  781. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  782. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  783. }
  784. carla_zeroFloats(audioBuffers.outBuf[0], frames);
  785. carla_zeroFloats(audioBuffers.outBuf[1], frames);
  786. // process
  787. process(data, const_cast<const float**>(audioBuffers.inBuf), audioBuffers.outBuf, frames);
  788. // connect output buffers
  789. if (audioBuffers.connectedOut1.count() != 0)
  790. {
  791. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  792. {
  793. const uint& port(it.getValue(0));
  794. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  795. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  796. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[0], frames);
  797. }
  798. }
  799. if (audioBuffers.connectedOut2.count() != 0)
  800. {
  801. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  802. {
  803. const uint& port(it.getValue(0));
  804. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  805. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  806. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[1], frames);
  807. }
  808. }
  809. }
  810. // -----------------------------------------------------------------------
  811. // Patchbay Graph stuff
  812. static const uint32_t kAudioInputPortOffset = MAX_PATCHBAY_PLUGINS*1;
  813. static const uint32_t kAudioOutputPortOffset = MAX_PATCHBAY_PLUGINS*2;
  814. static const uint32_t kMidiInputPortOffset = MAX_PATCHBAY_PLUGINS*3;
  815. static const uint32_t kMidiOutputPortOffset = MAX_PATCHBAY_PLUGINS*3+1;
  816. static const uint kMidiChannelIndex = static_cast<uint>(AudioProcessorGraph::midiChannelIndex);
  817. static inline
  818. bool adjustPatchbayPortIdForWater(uint& portId)
  819. {
  820. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, false);
  821. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, false);
  822. if (portId == kMidiInputPortOffset)
  823. {
  824. portId = kMidiChannelIndex;
  825. return true;
  826. }
  827. if (portId == kMidiOutputPortOffset)
  828. {
  829. portId = kMidiChannelIndex;
  830. return true;
  831. }
  832. if (portId >= kAudioOutputPortOffset)
  833. {
  834. portId -= kAudioOutputPortOffset;
  835. return true;
  836. }
  837. if (portId >= kAudioInputPortOffset)
  838. {
  839. portId -= kAudioInputPortOffset;
  840. return true;
  841. }
  842. return false;
  843. }
  844. static inline
  845. const String getProcessorFullPortName(AudioProcessor* const proc, const uint32_t portId)
  846. {
  847. CARLA_SAFE_ASSERT_RETURN(proc != nullptr, String());
  848. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, String());
  849. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, String());
  850. String fullPortName(proc->getName());
  851. if (portId == kMidiOutputPortOffset)
  852. {
  853. fullPortName += ":events-out";
  854. }
  855. else if (portId == kMidiInputPortOffset)
  856. {
  857. fullPortName += ":events-in";
  858. }
  859. else if (portId >= kAudioOutputPortOffset)
  860. {
  861. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels() > 0, String());
  862. fullPortName += ":" + proc->getOutputChannelName(static_cast<int>(portId-kAudioOutputPortOffset));
  863. }
  864. else if (portId >= kAudioInputPortOffset)
  865. {
  866. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels() > 0, String());
  867. fullPortName += ":" + proc->getInputChannelName(static_cast<int>(portId-kAudioInputPortOffset));
  868. }
  869. else
  870. {
  871. return String();
  872. }
  873. return fullPortName;
  874. }
  875. static inline
  876. void addNodeToPatchbay(CarlaEngine* const engine, const uint32_t groupId, const int clientId, const AudioProcessor* const proc)
  877. {
  878. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  879. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  880. const int icon((clientId >= 0) ? PATCHBAY_ICON_PLUGIN : PATCHBAY_ICON_HARDWARE);
  881. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, groupId, icon, clientId, 0.0f, proc->getName().toRawUTF8());
  882. for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
  883. {
  884. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  885. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, proc->getInputChannelName(i).toRawUTF8());
  886. }
  887. for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
  888. {
  889. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  890. PATCHBAY_PORT_TYPE_AUDIO, 0.0f, proc->getOutputChannelName(i).toRawUTF8());
  891. }
  892. if (proc->acceptsMidi())
  893. {
  894. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiInputPortOffset),
  895. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "events-in");
  896. }
  897. if (proc->producesMidi())
  898. {
  899. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiOutputPortOffset),
  900. PATCHBAY_PORT_TYPE_MIDI, 0.0f, "events-out");
  901. }
  902. }
  903. static inline
  904. void removeNodeFromPatchbay(CarlaEngine* const engine, const uint32_t groupId, const AudioProcessor* const proc)
  905. {
  906. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  907. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  908. for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
  909. {
  910. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  911. 0, 0.0f, nullptr);
  912. }
  913. for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
  914. {
  915. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  916. 0, 0.0f, nullptr);
  917. }
  918. if (proc->acceptsMidi())
  919. {
  920. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiInputPortOffset),
  921. 0, 0.0f, nullptr);
  922. }
  923. if (proc->producesMidi())
  924. {
  925. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiOutputPortOffset),
  926. 0, 0.0f, nullptr);
  927. }
  928. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED, groupId, 0, 0, 0.0f, nullptr);
  929. }
  930. // -----------------------------------------------------------------------
  931. class CarlaPluginInstance : public AudioProcessor
  932. {
  933. public:
  934. CarlaPluginInstance(CarlaEngine* const engine, CarlaPlugin* const plugin)
  935. : kEngine(engine),
  936. fPlugin(plugin)
  937. {
  938. setPlayConfigDetails(static_cast<int>(fPlugin->getAudioInCount()),
  939. static_cast<int>(fPlugin->getAudioOutCount()),
  940. getSampleRate(), getBlockSize());
  941. }
  942. ~CarlaPluginInstance() override
  943. {
  944. }
  945. void invalidatePlugin() noexcept
  946. {
  947. fPlugin = nullptr;
  948. }
  949. // -------------------------------------------------------------------
  950. const String getName() const override
  951. {
  952. return fPlugin->getName();
  953. }
  954. void processBlock(AudioSampleBuffer& audio, MidiBuffer& midi) override
  955. {
  956. if (fPlugin == nullptr || ! fPlugin->isEnabled() || ! fPlugin->tryLock(kEngine->isOffline()))
  957. {
  958. audio.clear();
  959. midi.clear();
  960. return;
  961. }
  962. fPlugin->initBuffers();
  963. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  964. {
  965. EngineEvent* const engineEvents(port->fBuffer);
  966. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  967. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  968. fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
  969. }
  970. midi.clear();
  971. // TODO - CV support
  972. const uint32_t numSamples(static_cast<uint32_t>(audio.getNumSamples()));
  973. if (const int numChan = audio.getNumChannels())
  974. {
  975. const uint numChanu = static_cast<uint>(jmin(numChan, 2));
  976. if (fPlugin->getAudioInCount() == 0)
  977. audio.clear();
  978. float* audioBuffers[numChan];
  979. for (int i=0; i<numChan; ++i)
  980. audioBuffers[i] = audio.getWritePointer(i);
  981. float inPeaks[2] = { 0.0f };
  982. float outPeaks[2] = { 0.0f };
  983. for (uint32_t i=0, count=jmin(fPlugin->getAudioInCount(), numChanu); i<count; ++i)
  984. inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  985. fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers, nullptr, nullptr, numSamples);
  986. for (uint32_t i=0, count=jmin(fPlugin->getAudioOutCount(), numChanu); i<count; ++i)
  987. outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  988. kEngine->setPluginPeaks(fPlugin->getId(), inPeaks, outPeaks);
  989. }
  990. else
  991. {
  992. fPlugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
  993. }
  994. midi.clear();
  995. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  996. {
  997. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  998. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  999. fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
  1000. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1001. }
  1002. fPlugin->unlock();
  1003. }
  1004. const String getInputChannelName(int i) const override
  1005. {
  1006. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  1007. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1008. return client->getAudioPortName(true, static_cast<uint>(i));
  1009. }
  1010. const String getOutputChannelName(int i) const override
  1011. {
  1012. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  1013. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1014. return client->getAudioPortName(false, static_cast<uint>(i));
  1015. }
  1016. void prepareToPlay(double, int) override {}
  1017. void releaseResources() override {}
  1018. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  1019. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  1020. private:
  1021. CarlaEngine* const kEngine;
  1022. CarlaPlugin* fPlugin;
  1023. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1024. };
  1025. // -----------------------------------------------------------------------
  1026. // Patchbay Graph
  1027. class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
  1028. {
  1029. public:
  1030. NamedAudioGraphIOProcessor(const IODeviceType iotype)
  1031. : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
  1032. inputNames(),
  1033. outputNames() {}
  1034. const String getInputChannelName (int index) const override
  1035. {
  1036. if (index < inputNames.size())
  1037. return inputNames[index];
  1038. return String("Playback ") + String(index+1);
  1039. }
  1040. const String getOutputChannelName (int index) const override
  1041. {
  1042. if (index < outputNames.size())
  1043. return outputNames[index];
  1044. return String("Capture ") + String(index+1);
  1045. }
  1046. void setNames(const bool setInputNames, const StringArray& names)
  1047. {
  1048. if (setInputNames)
  1049. inputNames = names;
  1050. else
  1051. outputNames = names;
  1052. }
  1053. private:
  1054. StringArray inputNames;
  1055. StringArray outputNames;
  1056. };
  1057. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs)
  1058. : CarlaThread("PatchbayReorderThread"),
  1059. connections(),
  1060. graph(),
  1061. audioBuffer(),
  1062. midiBuffer(),
  1063. inputs(carla_fixedValue(0U, 32U, ins)),
  1064. outputs(carla_fixedValue(0U, 32U, outs)),
  1065. retCon(),
  1066. usingExternal(false),
  1067. extGraph(engine),
  1068. kEngine(engine)
  1069. {
  1070. const int bufferSize(static_cast<int>(engine->getBufferSize()));
  1071. const double sampleRate(engine->getSampleRate());
  1072. graph.setPlayConfigDetails(static_cast<int>(inputs), static_cast<int>(outputs), sampleRate, bufferSize);
  1073. graph.prepareToPlay(sampleRate, bufferSize);
  1074. audioBuffer.setSize(static_cast<int>(jmax(inputs, outputs)), bufferSize);
  1075. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1076. midiBuffer.clear();
  1077. StringArray channelNames;
  1078. switch (inputs)
  1079. {
  1080. case 2:
  1081. channelNames.add("Left");
  1082. channelNames.add("Right");
  1083. break;
  1084. case 3:
  1085. channelNames.add("Left");
  1086. channelNames.add("Right");
  1087. channelNames.add("Sidechain");
  1088. break;
  1089. }
  1090. {
  1091. NamedAudioGraphIOProcessor* const proc(
  1092. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
  1093. proc->setNames(false, channelNames);
  1094. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1095. node->properties.set("isPlugin", false);
  1096. node->properties.set("isOutput", false);
  1097. node->properties.set("isAudio", true);
  1098. node->properties.set("isCV", false);
  1099. node->properties.set("isMIDI", false);
  1100. node->properties.set("isOSC", false);
  1101. }
  1102. {
  1103. NamedAudioGraphIOProcessor* const proc(
  1104. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
  1105. proc->setNames(true, channelNames);
  1106. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1107. node->properties.set("isPlugin", false);
  1108. node->properties.set("isOutput", false);
  1109. node->properties.set("isAudio", true);
  1110. node->properties.set("isCV", false);
  1111. node->properties.set("isMIDI", false);
  1112. node->properties.set("isOSC", false);
  1113. }
  1114. {
  1115. NamedAudioGraphIOProcessor* const proc(
  1116. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
  1117. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1118. node->properties.set("isPlugin", false);
  1119. node->properties.set("isOutput", false);
  1120. node->properties.set("isAudio", false);
  1121. node->properties.set("isCV", false);
  1122. node->properties.set("isMIDI", true);
  1123. node->properties.set("isOSC", false);
  1124. }
  1125. {
  1126. NamedAudioGraphIOProcessor* const proc(
  1127. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
  1128. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1129. node->properties.set("isPlugin", false);
  1130. node->properties.set("isOutput", true);
  1131. node->properties.set("isAudio", false);
  1132. node->properties.set("isCV", false);
  1133. node->properties.set("isMIDI", true);
  1134. node->properties.set("isOSC", false);
  1135. }
  1136. startThread();
  1137. }
  1138. PatchbayGraph::~PatchbayGraph()
  1139. {
  1140. stopThread(-1);
  1141. connections.clear();
  1142. extGraph.clear();
  1143. graph.releaseResources();
  1144. graph.clear();
  1145. audioBuffer.clear();
  1146. }
  1147. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1148. {
  1149. const int bufferSizei(static_cast<int>(bufferSize));
  1150. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1151. graph.releaseResources();
  1152. graph.prepareToPlay(kEngine->getSampleRate(), bufferSizei);
  1153. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSizei);
  1154. }
  1155. void PatchbayGraph::setSampleRate(const double sampleRate)
  1156. {
  1157. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1158. graph.releaseResources();
  1159. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1160. }
  1161. void PatchbayGraph::setOffline(const bool offline)
  1162. {
  1163. graph.setNonRealtime(offline);
  1164. }
  1165. void PatchbayGraph::addPlugin(CarlaPlugin* const plugin)
  1166. {
  1167. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1168. carla_debug("PatchbayGraph::addPlugin(%p)", plugin);
  1169. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1170. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1171. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1172. plugin->setPatchbayNodeId(node->nodeId);
  1173. node->properties.set("isPlugin", true);
  1174. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  1175. if (! usingExternal)
  1176. addNodeToPatchbay(plugin->getEngine(), node->nodeId, static_cast<int>(plugin->getId()), instance);
  1177. }
  1178. void PatchbayGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1179. {
  1180. CARLA_SAFE_ASSERT_RETURN(oldPlugin != nullptr,);
  1181. CARLA_SAFE_ASSERT_RETURN(newPlugin != nullptr,);
  1182. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1183. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1184. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1185. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1186. if (! usingExternal)
  1187. {
  1188. disconnectInternalGroup(oldNode->nodeId);
  1189. removeNodeFromPatchbay(kEngine, oldNode->nodeId, oldNode->getProcessor());
  1190. }
  1191. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1192. graph.removeNode(oldNode->nodeId);
  1193. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1194. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1195. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1196. newPlugin->setPatchbayNodeId(node->nodeId);
  1197. node->properties.set("isPlugin", true);
  1198. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  1199. if (! usingExternal)
  1200. addNodeToPatchbay(newPlugin->getEngine(), node->nodeId, static_cast<int>(newPlugin->getId()), instance);
  1201. }
  1202. void PatchbayGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  1203. {
  1204. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1205. carla_debug("PatchbayGraph::renamePlugin(%p)", plugin, newName);
  1206. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1207. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1208. if (! usingExternal)
  1209. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED, node->nodeId, 0, 0, 0.0f, newName);
  1210. }
  1211. void PatchbayGraph::removePlugin(CarlaPlugin* const plugin)
  1212. {
  1213. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1214. carla_debug("PatchbayGraph::removePlugin(%p)", plugin);
  1215. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1216. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1217. if (! usingExternal)
  1218. {
  1219. disconnectInternalGroup(node->nodeId);
  1220. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1221. }
  1222. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1223. // Fix plugin Ids properties
  1224. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1225. {
  1226. CarlaPlugin* const plugin2(kEngine->getPlugin(i));
  1227. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  1228. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1229. {
  1230. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != water::var(-1));
  1231. node2->properties.set("pluginId", static_cast<int>(i-1));
  1232. }
  1233. }
  1234. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1235. }
  1236. void PatchbayGraph::removeAllPlugins()
  1237. {
  1238. carla_debug("PatchbayGraph::removeAllPlugins()");
  1239. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1240. {
  1241. CarlaPlugin* const plugin(kEngine->getPlugin(i));
  1242. CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
  1243. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1244. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1245. if (! usingExternal)
  1246. {
  1247. disconnectInternalGroup(node->nodeId);
  1248. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1249. }
  1250. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1251. graph.removeNode(node->nodeId);
  1252. }
  1253. }
  1254. bool PatchbayGraph::connect(const bool external, const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback)
  1255. {
  1256. if (external)
  1257. return extGraph.connect(groupA, portA, groupB, portB, sendCallback);
  1258. uint adjustedPortA = portA;
  1259. uint adjustedPortB = portB;
  1260. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1261. return false;
  1262. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1263. return false;
  1264. if (! graph.addConnection(groupA, static_cast<int>(adjustedPortA), groupB, static_cast<int>(adjustedPortB)))
  1265. {
  1266. kEngine->setLastError("Failed from water");
  1267. return false;
  1268. }
  1269. ConnectionToId connectionToId;
  1270. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1271. char strBuf[STR_MAX+1];
  1272. strBuf[STR_MAX] = '\0';
  1273. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1274. if (sendCallback)
  1275. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1276. connections.list.append(connectionToId);
  1277. return true;
  1278. }
  1279. bool PatchbayGraph::disconnect(const uint connectionId)
  1280. {
  1281. if (usingExternal)
  1282. return extGraph.disconnect(connectionId);
  1283. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1284. {
  1285. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1286. const ConnectionToId& connectionToId(it.getValue(fallback));
  1287. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1288. if (connectionToId.id != connectionId)
  1289. continue;
  1290. uint adjustedPortA = connectionToId.portA;
  1291. uint adjustedPortB = connectionToId.portB;
  1292. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1293. return false;
  1294. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1295. return false;
  1296. if (! graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1297. connectionToId.groupB, static_cast<int>(adjustedPortB)))
  1298. return false;
  1299. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1300. connections.list.remove(it);
  1301. return true;
  1302. }
  1303. kEngine->setLastError("Failed to find connection");
  1304. return false;
  1305. }
  1306. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1307. {
  1308. CARLA_SAFE_ASSERT(! usingExternal);
  1309. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1310. {
  1311. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1312. const ConnectionToId& connectionToId(it.getValue(fallback));
  1313. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1314. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1315. continue;
  1316. /*
  1317. uint adjustedPortA = connectionToId.portA;
  1318. uint adjustedPortB = connectionToId.portB;
  1319. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1320. return false;
  1321. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1322. return false;
  1323. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1324. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1325. */
  1326. if (! usingExternal)
  1327. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1328. connections.list.remove(it);
  1329. }
  1330. }
  1331. void PatchbayGraph::refresh(const char* const deviceName)
  1332. {
  1333. if (usingExternal)
  1334. return extGraph.refresh(deviceName);
  1335. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1336. connections.clear();
  1337. graph.removeIllegalConnections();
  1338. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1339. {
  1340. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1341. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1342. AudioProcessor* const proc(node->getProcessor());
  1343. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1344. int clientId = -1;
  1345. // plugin node
  1346. if (node->properties.getWithDefault("isPlugin", false) == water::var(true))
  1347. clientId = node->properties.getWithDefault("pluginId", -1);
  1348. addNodeToPatchbay(kEngine, node->nodeId, clientId, proc);
  1349. }
  1350. char strBuf[STR_MAX+1];
  1351. strBuf[STR_MAX] = '\0';
  1352. for (int i=0, count=graph.getNumConnections(); i<count; ++i)
  1353. {
  1354. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1355. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1356. CARLA_SAFE_ASSERT_CONTINUE(conn->sourceChannelIndex >= 0);
  1357. CARLA_SAFE_ASSERT_CONTINUE(conn->destChannelIndex >= 0);
  1358. const uint groupA = conn->sourceNodeId;
  1359. const uint groupB = conn->destNodeId;
  1360. uint portA = static_cast<uint>(conn->sourceChannelIndex);
  1361. uint portB = static_cast<uint>(conn->destChannelIndex);
  1362. if (portA == kMidiChannelIndex)
  1363. portA = kMidiOutputPortOffset;
  1364. else
  1365. portA += kAudioOutputPortOffset;
  1366. if (portB == kMidiChannelIndex)
  1367. portB = kMidiInputPortOffset;
  1368. else
  1369. portB += kAudioInputPortOffset;
  1370. ConnectionToId connectionToId;
  1371. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1372. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1373. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1374. connections.list.append(connectionToId);
  1375. }
  1376. }
  1377. const char* const* PatchbayGraph::getConnections(const bool external) const
  1378. {
  1379. if (external)
  1380. return extGraph.getConnections();
  1381. if (connections.list.count() == 0)
  1382. return nullptr;
  1383. CarlaStringList connList;
  1384. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1385. {
  1386. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1387. const ConnectionToId& connectionToId(it.getValue(fallback));
  1388. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1389. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1390. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1391. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1392. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1393. AudioProcessor* const procA(nodeA->getProcessor());
  1394. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1395. AudioProcessor* const procB(nodeB->getProcessor());
  1396. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1397. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1398. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1399. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1400. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1401. connList.append(fullPortNameA.toRawUTF8());
  1402. connList.append(fullPortNameB.toRawUTF8());
  1403. }
  1404. if (connList.count() == 0)
  1405. return nullptr;
  1406. retCon = connList.toCharStringListPtr();
  1407. return retCon;
  1408. }
  1409. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  1410. {
  1411. if (external)
  1412. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  1413. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1414. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1415. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1416. {
  1417. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1418. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1419. AudioProcessor* const proc(node->getProcessor());
  1420. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1421. if (proc->getName() != groupName)
  1422. continue;
  1423. groupId = node->nodeId;
  1424. if (portName == "events-in")
  1425. {
  1426. portId = kMidiInputPortOffset;
  1427. return true;
  1428. }
  1429. if (portName == "events-out")
  1430. {
  1431. portId = kMidiOutputPortOffset;
  1432. return true;
  1433. }
  1434. for (int j=0, numInputs=proc->getTotalNumInputChannels(); j<numInputs; ++j)
  1435. {
  1436. if (proc->getInputChannelName(j) != portName)
  1437. continue;
  1438. portId = kAudioInputPortOffset+static_cast<uint>(j);
  1439. return true;
  1440. }
  1441. for (int j=0, numOutputs=proc->getTotalNumOutputChannels(); j<numOutputs; ++j)
  1442. {
  1443. if (proc->getOutputChannelName(j) != portName)
  1444. continue;
  1445. portId = kAudioOutputPortOffset+static_cast<uint>(j);
  1446. return true;
  1447. }
  1448. }
  1449. return false;
  1450. }
  1451. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const int frames)
  1452. {
  1453. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1454. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  1455. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  1456. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  1457. // put events in water buffer
  1458. {
  1459. midiBuffer.clear();
  1460. fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  1461. }
  1462. // set audio buffer size, needed for water internals
  1463. if (! audioBuffer.setSizeRT(frames))
  1464. return;
  1465. // put carla audio in water buffer
  1466. {
  1467. int i=0;
  1468. for (; i < static_cast<int>(inputs); ++i)
  1469. audioBuffer.copyFrom(i, 0, inBuf[i], frames);
  1470. // clear remaining channels
  1471. for (const int count=audioBuffer.getNumChannels(); i<count; ++i)
  1472. audioBuffer.clear(i, 0, frames);
  1473. }
  1474. // ready to go!
  1475. graph.processBlock(audioBuffer, midiBuffer);
  1476. // put water audio in carla buffer
  1477. {
  1478. for (int i=0; i < static_cast<int>(outputs); ++i)
  1479. carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
  1480. }
  1481. // put water events in carla buffer
  1482. {
  1483. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  1484. fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
  1485. midiBuffer.clear();
  1486. }
  1487. }
  1488. void PatchbayGraph::run()
  1489. {
  1490. while (! shouldThreadExit())
  1491. {
  1492. carla_msleep(100);
  1493. graph.reorderNowIfNeeded();
  1494. }
  1495. }
  1496. // -----------------------------------------------------------------------
  1497. // InternalGraph
  1498. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  1499. : fIsReady(false),
  1500. fRack(nullptr),
  1501. kEngine(engine) {}
  1502. EngineInternalGraph::~EngineInternalGraph() noexcept
  1503. {
  1504. CARLA_SAFE_ASSERT(! fIsReady);
  1505. CARLA_SAFE_ASSERT(fRack == nullptr);
  1506. }
  1507. void EngineInternalGraph::create(const uint32_t inputs, const uint32_t outputs)
  1508. {
  1509. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  1510. if (fIsRack)
  1511. {
  1512. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  1513. fRack = new RackGraph(kEngine, inputs, outputs);
  1514. }
  1515. else
  1516. {
  1517. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  1518. fPatchbay = new PatchbayGraph(kEngine, inputs, outputs);
  1519. }
  1520. fIsReady = true;
  1521. }
  1522. void EngineInternalGraph::destroy() noexcept
  1523. {
  1524. if (! fIsReady)
  1525. {
  1526. CARLA_SAFE_ASSERT(fRack == nullptr);
  1527. return;
  1528. }
  1529. if (fIsRack)
  1530. {
  1531. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1532. delete fRack;
  1533. fRack = nullptr;
  1534. }
  1535. else
  1536. {
  1537. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1538. delete fPatchbay;
  1539. fPatchbay = nullptr;
  1540. }
  1541. fIsReady = false;
  1542. }
  1543. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  1544. {
  1545. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1546. if (fIsRack)
  1547. {
  1548. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1549. fRack->setBufferSize(bufferSize);
  1550. }
  1551. else
  1552. {
  1553. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1554. fPatchbay->setBufferSize(bufferSize);
  1555. }
  1556. }
  1557. void EngineInternalGraph::setSampleRate(const double sampleRate)
  1558. {
  1559. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1560. if (fIsRack)
  1561. {
  1562. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1563. }
  1564. else
  1565. {
  1566. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1567. fPatchbay->setSampleRate(sampleRate);
  1568. }
  1569. }
  1570. void EngineInternalGraph::setOffline(const bool offline)
  1571. {
  1572. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1573. if (fIsRack)
  1574. {
  1575. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1576. fRack->setOffline(offline);
  1577. }
  1578. else
  1579. {
  1580. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1581. fPatchbay->setOffline(offline);
  1582. }
  1583. }
  1584. bool EngineInternalGraph::isReady() const noexcept
  1585. {
  1586. return fIsReady;
  1587. }
  1588. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  1589. {
  1590. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  1591. return fRack;
  1592. }
  1593. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  1594. {
  1595. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  1596. return fPatchbay;
  1597. }
  1598. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  1599. {
  1600. if (fIsRack)
  1601. {
  1602. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1603. fRack->processHelper(data, inBuf, outBuf, frames);
  1604. }
  1605. else
  1606. {
  1607. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1608. fPatchbay->process(data, inBuf, outBuf, static_cast<int>(frames));
  1609. }
  1610. }
  1611. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  1612. {
  1613. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  1614. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1615. fRack->process(data, inBuf, outBuf, frames);
  1616. }
  1617. // -----------------------------------------------------------------------
  1618. // used for internal patchbay mode
  1619. void EngineInternalGraph::addPlugin(CarlaPlugin* const plugin)
  1620. {
  1621. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1622. fPatchbay->addPlugin(plugin);
  1623. }
  1624. void EngineInternalGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1625. {
  1626. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1627. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  1628. }
  1629. void EngineInternalGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  1630. {
  1631. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1632. fPatchbay->renamePlugin(plugin, newName);
  1633. }
  1634. void EngineInternalGraph::removePlugin(CarlaPlugin* const plugin)
  1635. {
  1636. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1637. fPatchbay->removePlugin(plugin);
  1638. }
  1639. void EngineInternalGraph::removeAllPlugins()
  1640. {
  1641. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1642. fPatchbay->removeAllPlugins();
  1643. }
  1644. bool EngineInternalGraph::isUsingExternal() const noexcept
  1645. {
  1646. if (fIsRack)
  1647. return true;
  1648. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  1649. return fPatchbay->usingExternal;
  1650. }
  1651. void EngineInternalGraph::setUsingExternal(const bool usingExternal) noexcept
  1652. {
  1653. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  1654. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1655. fPatchbay->usingExternal = usingExternal;
  1656. }
  1657. // -----------------------------------------------------------------------
  1658. // CarlaEngine Patchbay stuff
  1659. bool CarlaEngine::patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB)
  1660. {
  1661. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1662. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1663. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  1664. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1665. {
  1666. RackGraph* const graph = pData->graph.getRackGraph();
  1667. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1668. return graph->connect(groupA, portA, groupB, portB);
  1669. }
  1670. else
  1671. {
  1672. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1673. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1674. return graph->connect(graph->usingExternal, groupA, portA, groupB, portB, true);
  1675. }
  1676. return false;
  1677. }
  1678. bool CarlaEngine::patchbayDisconnect(const uint connectionId)
  1679. {
  1680. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1681. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1682. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  1683. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1684. {
  1685. RackGraph* const graph = pData->graph.getRackGraph();
  1686. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1687. return graph->disconnect(connectionId);
  1688. }
  1689. else
  1690. {
  1691. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1692. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1693. return graph->disconnect(connectionId);
  1694. }
  1695. return false;
  1696. }
  1697. bool CarlaEngine::patchbayRefresh(const bool external)
  1698. {
  1699. // subclasses should handle this
  1700. CARLA_SAFE_ASSERT_RETURN(! external, false);
  1701. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1702. {
  1703. // This is implemented in engine subclasses
  1704. setLastError("Unsupported operation");
  1705. return false;
  1706. }
  1707. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1708. {
  1709. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1710. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1711. graph->refresh("");
  1712. return true;
  1713. }
  1714. setLastError("Unsupported operation");
  1715. return false;
  1716. }
  1717. // -----------------------------------------------------------------------
  1718. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  1719. {
  1720. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  1721. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  1722. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1723. {
  1724. RackGraph* const graph = pData->graph.getRackGraph();
  1725. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1726. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  1727. return graph->getConnections();
  1728. }
  1729. else
  1730. {
  1731. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1732. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1733. return graph->getConnections(external);
  1734. }
  1735. return nullptr;
  1736. }
  1737. void CarlaEngine::restorePatchbayConnection(const bool external, const char* const sourcePort, const char* const targetPort, const bool sendCallback)
  1738. {
  1739. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  1740. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  1741. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  1742. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  1743. uint groupA, portA;
  1744. uint groupB, portB;
  1745. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1746. {
  1747. RackGraph* const graph = pData->graph.getRackGraph();
  1748. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1749. CARLA_SAFE_ASSERT_RETURN(external,);
  1750. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  1751. return;
  1752. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  1753. return;
  1754. graph->connect(groupA, portA, groupB, portB);
  1755. }
  1756. else
  1757. {
  1758. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1759. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1760. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  1761. return;
  1762. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  1763. return;
  1764. graph->connect(external, groupA, portA, groupB, portB, sendCallback);
  1765. }
  1766. }
  1767. // -----------------------------------------------------------------------
  1768. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1769. {
  1770. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1771. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1772. RackGraph* const graph(pData->graph.getRackGraph());
  1773. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1774. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1775. switch (connectionType)
  1776. {
  1777. case kExternalGraphConnectionAudioIn1:
  1778. return graph->audioBuffers.connectedIn1.append(portId);
  1779. case kExternalGraphConnectionAudioIn2:
  1780. return graph->audioBuffers.connectedIn2.append(portId);
  1781. case kExternalGraphConnectionAudioOut1:
  1782. return graph->audioBuffers.connectedOut1.append(portId);
  1783. case kExternalGraphConnectionAudioOut2:
  1784. return graph->audioBuffers.connectedOut2.append(portId);
  1785. }
  1786. return false;
  1787. }
  1788. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1789. {
  1790. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1791. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1792. RackGraph* const graph(pData->graph.getRackGraph());
  1793. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1794. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1795. switch (connectionType)
  1796. {
  1797. case kExternalGraphConnectionAudioIn1:
  1798. return graph->audioBuffers.connectedIn1.removeOne(portId);
  1799. case kExternalGraphConnectionAudioIn2:
  1800. return graph->audioBuffers.connectedIn2.removeOne(portId);
  1801. case kExternalGraphConnectionAudioOut1:
  1802. return graph->audioBuffers.connectedOut1.removeOne(portId);
  1803. case kExternalGraphConnectionAudioOut2:
  1804. return graph->audioBuffers.connectedOut2.removeOne(portId);
  1805. }
  1806. return false;
  1807. }
  1808. // -----------------------------------------------------------------------
  1809. CARLA_BACKEND_END_NAMESPACE