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.

2469 lines
83KB

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