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.

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