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.

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