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.

2340 lines
79KB

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