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.

3101 lines
107KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2022 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 "CarlaScopeUtils.hpp"
  22. #include "CarlaMIDI.h"
  23. using water::jmax;
  24. using water::jmin;
  25. using water::AudioProcessor;
  26. using water::MidiBuffer;
  27. using water::String;
  28. using water::StringArray;
  29. CARLA_BACKEND_START_NAMESPACE
  30. // -----------------------------------------------------------------------
  31. // Fallback data
  32. static const PortNameToId kPortNameToIdFallback = { 0, 0, { '\0' }, { '\0' } };
  33. static /* */ PortNameToId kPortNameToIdFallbackNC = { 0, 0, { '\0' }, { '\0' } };
  34. // -----------------------------------------------------------------------
  35. // External Graph stuff
  36. static inline
  37. uint getExternalGraphPortIdFromName(const char* const shortname) noexcept
  38. {
  39. if (std::strcmp(shortname, "AudioIn1") == 0 || std::strcmp(shortname, "audio-in1") == 0)
  40. return kExternalGraphCarlaPortAudioIn1;
  41. if (std::strcmp(shortname, "AudioIn2") == 0 || std::strcmp(shortname, "audio-in2") == 0)
  42. return kExternalGraphCarlaPortAudioIn2;
  43. if (std::strcmp(shortname, "AudioOut1") == 0 || std::strcmp(shortname, "audio-out1") == 0)
  44. return kExternalGraphCarlaPortAudioOut1;
  45. if (std::strcmp(shortname, "AudioOut2") == 0 || std::strcmp(shortname, "audio-out2") == 0)
  46. return kExternalGraphCarlaPortAudioOut2;
  47. if (std::strcmp(shortname, "MidiIn") == 0 || std::strcmp(shortname, "midi-in") == 0)
  48. return kExternalGraphCarlaPortMidiIn;
  49. if (std::strcmp(shortname, "MidiOut") == 0 || std::strcmp(shortname, "midi-out") == 0)
  50. return kExternalGraphCarlaPortMidiOut;
  51. carla_stderr("CarlaBackend::getExternalGraphPortIdFromName(%s) - invalid short name", shortname);
  52. return kExternalGraphCarlaPortNull;
  53. }
  54. static inline
  55. const char* getExternalGraphFullPortNameFromId(const /*RackGraphCarlaPortIds*/ uint portId)
  56. {
  57. switch (portId)
  58. {
  59. case kExternalGraphCarlaPortAudioIn1:
  60. return "Carla:AudioIn1";
  61. case kExternalGraphCarlaPortAudioIn2:
  62. return "Carla:AudioIn2";
  63. case kExternalGraphCarlaPortAudioOut1:
  64. return "Carla:AudioOut1";
  65. case kExternalGraphCarlaPortAudioOut2:
  66. return "Carla:AudioOut2";
  67. case kExternalGraphCarlaPortMidiIn:
  68. return "Carla:MidiIn";
  69. case kExternalGraphCarlaPortMidiOut:
  70. return "Carla:MidiOut";
  71. //case kExternalGraphCarlaPortNull:
  72. //case kExternalGraphCarlaPortMax:
  73. // break;
  74. }
  75. carla_stderr("CarlaBackend::getExternalGraphFullPortNameFromId(%i) - invalid port id", portId);
  76. return nullptr;
  77. }
  78. // -----------------------------------------------------------------------
  79. ExternalGraphPorts::ExternalGraphPorts() noexcept
  80. : ins(),
  81. outs() {}
  82. const char* ExternalGraphPorts::getName(const bool isInput, const uint portId) const noexcept
  83. {
  84. for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin2() : outs.begin2(); it.valid(); it.next())
  85. {
  86. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  87. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  88. if (portNameToId.port == portId)
  89. return portNameToId.name;
  90. }
  91. return nullptr;
  92. }
  93. uint ExternalGraphPorts::getPortId(const bool isInput, const char portName[], bool* const ok) const noexcept
  94. {
  95. for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin2() : outs.begin2(); it.valid(); it.next())
  96. {
  97. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  98. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  99. if (std::strncmp(portNameToId.name, portName, STR_MAX) == 0)
  100. {
  101. if (ok != nullptr)
  102. *ok = true;
  103. return portNameToId.port;
  104. }
  105. }
  106. if (ok != nullptr)
  107. *ok = false;
  108. return 0;
  109. }
  110. // -----------------------------------------------------------------------
  111. ExternalGraph::ExternalGraph(CarlaEngine* const engine) noexcept
  112. : connections(),
  113. audioPorts(),
  114. midiPorts(),
  115. positions(),
  116. retCon(),
  117. kEngine(engine)
  118. {
  119. carla_zeroStruct(positions);
  120. }
  121. void ExternalGraph::clear() noexcept
  122. {
  123. connections.clear();
  124. audioPorts.ins.clear();
  125. audioPorts.outs.clear();
  126. midiPorts.ins.clear();
  127. midiPorts.outs.clear();
  128. }
  129. bool ExternalGraph::connect(const bool sendHost, const bool sendOSC,
  130. const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  131. {
  132. uint otherGroup, otherPort, carlaPort;
  133. if (groupA == kExternalGraphGroupCarla)
  134. {
  135. CARLA_SAFE_ASSERT_RETURN(groupB != kExternalGraphGroupCarla, false);
  136. carlaPort = portA;
  137. otherGroup = groupB;
  138. otherPort = portB;
  139. }
  140. else
  141. {
  142. CARLA_SAFE_ASSERT_RETURN(groupB == kExternalGraphGroupCarla, false);
  143. carlaPort = portB;
  144. otherGroup = groupA;
  145. otherPort = portA;
  146. }
  147. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  148. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  149. bool makeConnection = false;
  150. switch (carlaPort)
  151. {
  152. case kExternalGraphCarlaPortAudioIn1:
  153. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  154. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  155. break;
  156. case kExternalGraphCarlaPortAudioIn2:
  157. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  158. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  159. break;
  160. case kExternalGraphCarlaPortAudioOut1:
  161. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  162. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  163. break;
  164. case kExternalGraphCarlaPortAudioOut2:
  165. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  166. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  167. break;
  168. case kExternalGraphCarlaPortMidiIn:
  169. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiIn, false);
  170. if (const char* const portName = midiPorts.getName(true, otherPort))
  171. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  172. break;
  173. case kExternalGraphCarlaPortMidiOut:
  174. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiOut, false);
  175. if (const char* const portName = midiPorts.getName(false, otherPort))
  176. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  177. break;
  178. }
  179. if (! makeConnection)
  180. {
  181. kEngine->setLastError("Invalid rack connection");
  182. return false;
  183. }
  184. ConnectionToId connectionToId;
  185. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  186. char strBuf[STR_MAX+1];
  187. strBuf[STR_MAX] = '\0';
  188. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  189. kEngine->callback(sendHost, sendOSC,
  190. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0, 0.0f, strBuf);
  191. connections.list.append(connectionToId);
  192. return true;
  193. }
  194. bool ExternalGraph::disconnect(const bool sendHost, const bool sendOSC,
  195. const uint connectionId) noexcept
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(connections.list.count() > 0, false);
  198. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  199. {
  200. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  201. const ConnectionToId& connectionToId(it.getValue(fallback));
  202. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  203. if (connectionToId.id != connectionId)
  204. continue;
  205. uint otherGroup, otherPort, carlaPort;
  206. if (connectionToId.groupA == kExternalGraphGroupCarla)
  207. {
  208. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB != kExternalGraphGroupCarla, false);
  209. carlaPort = connectionToId.portA;
  210. otherGroup = connectionToId.groupB;
  211. otherPort = connectionToId.portB;
  212. }
  213. else
  214. {
  215. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB == kExternalGraphGroupCarla, false);
  216. carlaPort = connectionToId.portB;
  217. otherGroup = connectionToId.groupA;
  218. otherPort = connectionToId.portA;
  219. }
  220. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  221. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  222. bool makeDisconnection = false;
  223. switch (carlaPort)
  224. {
  225. case kExternalGraphCarlaPortAudioIn1:
  226. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  227. break;
  228. case kExternalGraphCarlaPortAudioIn2:
  229. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  230. break;
  231. case kExternalGraphCarlaPortAudioOut1:
  232. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  233. break;
  234. case kExternalGraphCarlaPortAudioOut2:
  235. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  236. break;
  237. case kExternalGraphCarlaPortMidiIn:
  238. if (const char* const portName = midiPorts.getName(true, otherPort))
  239. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  240. break;
  241. case kExternalGraphCarlaPortMidiOut:
  242. if (const char* const portName = midiPorts.getName(false, otherPort))
  243. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  244. break;
  245. }
  246. if (! makeDisconnection)
  247. {
  248. kEngine->setLastError("Invalid rack connection");
  249. return false;
  250. }
  251. kEngine->callback(sendHost, sendOSC,
  252. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0, 0.0f, nullptr);
  253. connections.list.remove(it);
  254. return true;
  255. }
  256. kEngine->setLastError("Failed to find connection");
  257. return false;
  258. }
  259. void ExternalGraph::setGroupPos(const bool sendHost, const bool sendOSC,
  260. const uint groupId, const int x1, const int y1, const int x2, const int y2)
  261. {
  262. CARLA_SAFE_ASSERT_UINT_RETURN(groupId >= kExternalGraphGroupCarla && groupId < kExternalGraphGroupMax, groupId,);
  263. const PatchbayPosition ppos = { true, x1, y1, x2, y2 };
  264. positions[groupId] = ppos;
  265. kEngine->callback(sendHost, sendOSC,
  266. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  267. groupId, x1, y1, x2, static_cast<float>(y2),
  268. nullptr);
  269. }
  270. void ExternalGraph::refresh(const bool sendHost, const bool sendOSC, const char* const deviceName)
  271. {
  272. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  273. const bool isRack = kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK;
  274. // Main
  275. {
  276. kEngine->callback(sendHost, sendOSC,
  277. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  278. kExternalGraphGroupCarla,
  279. PATCHBAY_ICON_CARLA,
  280. MAIN_CARLA_PLUGIN_ID,
  281. 0, 0.0f,
  282. kEngine->getName());
  283. if (isRack)
  284. {
  285. kEngine->callback(sendHost, sendOSC,
  286. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  287. kExternalGraphGroupCarla,
  288. kExternalGraphCarlaPortAudioIn1,
  289. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT,
  290. 0, 0.0f,
  291. "audio-in1");
  292. kEngine->callback(sendHost, sendOSC,
  293. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  294. kExternalGraphGroupCarla,
  295. kExternalGraphCarlaPortAudioIn2,
  296. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT,
  297. 0, 0.0f,
  298. "audio-in2");
  299. kEngine->callback(sendHost, sendOSC,
  300. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  301. kExternalGraphGroupCarla,
  302. kExternalGraphCarlaPortAudioOut1,
  303. PATCHBAY_PORT_TYPE_AUDIO,
  304. 0, 0.0f,
  305. "audio-out1");
  306. kEngine->callback(sendHost, sendOSC,
  307. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  308. kExternalGraphGroupCarla,
  309. kExternalGraphCarlaPortAudioOut2,
  310. PATCHBAY_PORT_TYPE_AUDIO,
  311. 0, 0.0f,
  312. "audio-out2");
  313. }
  314. kEngine->callback(sendHost, sendOSC,
  315. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  316. kExternalGraphGroupCarla,
  317. kExternalGraphCarlaPortMidiIn,
  318. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT,
  319. 0, 0.0f,
  320. "midi-in");
  321. kEngine->callback(sendHost, sendOSC,
  322. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  323. kExternalGraphGroupCarla,
  324. kExternalGraphCarlaPortMidiOut,
  325. PATCHBAY_PORT_TYPE_MIDI,
  326. 0, 0.0f,
  327. "midi-out");
  328. }
  329. char strBuf[STR_MAX+1];
  330. strBuf[STR_MAX] = '\0';
  331. if (isRack)
  332. {
  333. // Audio In
  334. if (deviceName[0] != '\0')
  335. std::snprintf(strBuf, STR_MAX, "Capture (%s)", deviceName);
  336. else
  337. std::strncpy(strBuf, "Capture", STR_MAX);
  338. kEngine->callback(sendHost, sendOSC,
  339. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  340. kExternalGraphGroupAudioIn,
  341. PATCHBAY_ICON_HARDWARE,
  342. -1,
  343. 0, 0.0f,
  344. strBuf);
  345. const CarlaString groupNameIn(strBuf);
  346. int h = 0;
  347. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.ins.begin2(); it.valid(); it.next())
  348. {
  349. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  350. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  351. portNameToId.setFullName(groupNameIn + portNameToId.name);
  352. kEngine->callback(sendHost, sendOSC,
  353. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  354. kExternalGraphGroupAudioIn,
  355. ++h,
  356. PATCHBAY_PORT_TYPE_AUDIO,
  357. 0, 0.0f,
  358. portNameToId.name);
  359. }
  360. // Audio Out
  361. if (deviceName[0] != '\0')
  362. std::snprintf(strBuf, STR_MAX, "Playback (%s)", deviceName);
  363. else
  364. std::strncpy(strBuf, "Playback", STR_MAX);
  365. kEngine->callback(sendHost, sendOSC,
  366. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  367. kExternalGraphGroupAudioOut,
  368. PATCHBAY_ICON_HARDWARE,
  369. -1,
  370. 0, 0.0f,
  371. strBuf);
  372. const CarlaString groupNameOut(strBuf);
  373. h = 0;
  374. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.outs.begin2(); it.valid(); it.next())
  375. {
  376. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  377. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  378. portNameToId.setFullName(groupNameOut + portNameToId.name);
  379. kEngine->callback(sendHost, sendOSC,
  380. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  381. kExternalGraphGroupAudioOut,
  382. ++h,
  383. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT,
  384. 0, 0.0f,
  385. portNameToId.name);
  386. }
  387. }
  388. // MIDI In
  389. {
  390. kEngine->callback(sendHost, sendOSC,
  391. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  392. kExternalGraphGroupMidiIn,
  393. PATCHBAY_ICON_HARDWARE,
  394. -1,
  395. 0, 0.0f,
  396. "Readable MIDI ports");
  397. const CarlaString groupNamePlus("Readable MIDI ports:");
  398. int h = 0;
  399. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.ins.begin2(); it.valid(); it.next())
  400. {
  401. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  402. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  403. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  404. kEngine->callback(sendHost, sendOSC,
  405. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  406. kExternalGraphGroupMidiIn,
  407. ++h,
  408. PATCHBAY_PORT_TYPE_MIDI,
  409. 0, 0.0f,
  410. portNameToId.name);
  411. }
  412. }
  413. // MIDI Out
  414. {
  415. kEngine->callback(sendHost, sendOSC,
  416. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  417. kExternalGraphGroupMidiOut,
  418. PATCHBAY_ICON_HARDWARE,
  419. -1,
  420. 0, 0.0f,
  421. "Writable MIDI ports");
  422. const CarlaString groupNamePlus("Writable MIDI ports:");
  423. int h = 0;
  424. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.outs.begin2(); it.valid(); it.next())
  425. {
  426. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  427. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  428. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  429. kEngine->callback(sendHost, sendOSC,
  430. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  431. kExternalGraphGroupMidiOut,
  432. ++h,
  433. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT,
  434. 0, 0.0f,
  435. portNameToId.name);
  436. }
  437. }
  438. }
  439. const char* const* ExternalGraph::getConnections() const noexcept
  440. {
  441. if (connections.list.count() == 0)
  442. return nullptr;
  443. CarlaStringList connList;
  444. char strBuf[STR_MAX+1];
  445. strBuf[STR_MAX] = '\0';
  446. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  447. {
  448. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  449. const ConnectionToId& connectionToId(it.getValue(fallback));
  450. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  451. uint otherGroup, otherPort, carlaPort;
  452. if (connectionToId.groupA == kExternalGraphGroupCarla)
  453. {
  454. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB != kExternalGraphGroupCarla);
  455. carlaPort = connectionToId.portA;
  456. otherGroup = connectionToId.groupB;
  457. otherPort = connectionToId.portB;
  458. }
  459. else
  460. {
  461. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB == kExternalGraphGroupCarla);
  462. carlaPort = connectionToId.portB;
  463. otherGroup = connectionToId.groupA;
  464. otherPort = connectionToId.portA;
  465. }
  466. CARLA_SAFE_ASSERT_CONTINUE(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax);
  467. CARLA_SAFE_ASSERT_CONTINUE(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax);
  468. switch (carlaPort)
  469. {
  470. case kExternalGraphCarlaPortAudioIn1:
  471. case kExternalGraphCarlaPortAudioIn2:
  472. std::snprintf(strBuf, STR_MAX, "AudioIn:%s", audioPorts.getName(true, otherPort));
  473. connList.append(strBuf);
  474. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  475. break;
  476. case kExternalGraphCarlaPortAudioOut1:
  477. case kExternalGraphCarlaPortAudioOut2:
  478. std::snprintf(strBuf, STR_MAX, "AudioOut:%s", audioPorts.getName(false, otherPort));
  479. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  480. connList.append(strBuf);
  481. break;
  482. case kExternalGraphCarlaPortMidiIn:
  483. std::snprintf(strBuf, STR_MAX, "MidiIn:%s", midiPorts.getName(true, otherPort));
  484. connList.append(strBuf);
  485. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  486. break;
  487. case kExternalGraphCarlaPortMidiOut:
  488. std::snprintf(strBuf, STR_MAX, "MidiOut:%s", midiPorts.getName(false, otherPort));
  489. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  490. connList.append(strBuf);
  491. break;
  492. }
  493. }
  494. if (connList.count() == 0)
  495. return nullptr;
  496. retCon = connList.toCharStringListPtr();
  497. return retCon;
  498. }
  499. bool ExternalGraph::getGroupFromName(const char* groupName, uint& groupId) const noexcept
  500. {
  501. CARLA_SAFE_ASSERT_RETURN(groupName != nullptr && groupName[0] != '\0', false);
  502. if (std::strcmp(groupName, "Carla") == 0)
  503. {
  504. groupId = kExternalGraphGroupCarla;
  505. return true;
  506. }
  507. if (std::strcmp(groupName, "AudioIn") == 0)
  508. {
  509. groupId = kExternalGraphGroupAudioIn;
  510. return true;
  511. }
  512. if (std::strcmp(groupName, "AudioOut") == 0)
  513. {
  514. groupId = kExternalGraphGroupAudioOut;
  515. return true;
  516. }
  517. if (std::strcmp(groupName, "MidiIn") == 0)
  518. {
  519. groupId = kExternalGraphGroupMidiIn;
  520. return true;
  521. }
  522. if (std::strcmp(groupName, "MidiOut") == 0)
  523. {
  524. groupId = kExternalGraphGroupMidiOut;
  525. return true;
  526. }
  527. return false;
  528. }
  529. bool ExternalGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  530. {
  531. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', false);
  532. if (std::strncmp(fullPortName, "Carla:", 6) == 0)
  533. {
  534. groupId = kExternalGraphGroupCarla;
  535. portId = getExternalGraphPortIdFromName(fullPortName+6);
  536. if (portId > kExternalGraphCarlaPortNull && portId < kExternalGraphCarlaPortMax)
  537. return true;
  538. }
  539. else if (std::strncmp(fullPortName, "AudioIn:", 8) == 0)
  540. {
  541. groupId = kExternalGraphGroupAudioIn;
  542. if (const char* const portName = fullPortName+8)
  543. {
  544. bool ok;
  545. portId = audioPorts.getPortId(true, portName, &ok);
  546. return ok;
  547. }
  548. }
  549. else if (std::strncmp(fullPortName, "AudioOut:", 9) == 0)
  550. {
  551. groupId = kExternalGraphGroupAudioOut;
  552. if (const char* const portName = fullPortName+9)
  553. {
  554. bool ok;
  555. portId = audioPorts.getPortId(false, portName, &ok);
  556. return ok;
  557. }
  558. }
  559. else if (std::strncmp(fullPortName, "MidiIn:", 7) == 0)
  560. {
  561. groupId = kExternalGraphGroupMidiIn;
  562. if (const char* const portName = fullPortName+7)
  563. {
  564. bool ok;
  565. portId = midiPorts.getPortId(true, portName, &ok);
  566. return ok;
  567. }
  568. }
  569. else if (std::strncmp(fullPortName, "MidiOut:", 8) == 0)
  570. {
  571. groupId = kExternalGraphGroupMidiOut;
  572. if (const char* const portName = fullPortName+8)
  573. {
  574. bool ok;
  575. portId = midiPorts.getPortId(false, portName, &ok);
  576. return ok;
  577. }
  578. }
  579. return false;
  580. }
  581. // -----------------------------------------------------------------------
  582. // RackGraph Buffers
  583. RackGraph::Buffers::Buffers() noexcept
  584. : mutex(),
  585. connectedIn1(),
  586. connectedIn2(),
  587. connectedOut1(),
  588. connectedOut2(),
  589. #ifdef CARLA_PROPER_CPP11_SUPPORT
  590. inBuf{nullptr, nullptr},
  591. inBufTmp{nullptr, nullptr},
  592. outBuf{nullptr, nullptr},
  593. #endif
  594. unusedBuf(nullptr)
  595. {
  596. #ifndef CARLA_PROPER_CPP11_SUPPORT
  597. inBuf[0] = inBuf[1] = nullptr;
  598. inBufTmp[0] = inBufTmp[1] = nullptr;
  599. outBuf[0] = outBuf[1] = nullptr;
  600. #endif
  601. }
  602. RackGraph::Buffers::~Buffers() noexcept
  603. {
  604. const CarlaRecursiveMutexLocker cml(mutex);
  605. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  606. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  607. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  608. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  609. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  610. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  611. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  612. connectedIn1.clear();
  613. connectedIn2.clear();
  614. connectedOut1.clear();
  615. connectedOut2.clear();
  616. }
  617. void RackGraph::Buffers::setBufferSize(const uint32_t bufferSize, const bool createBuffers) noexcept
  618. {
  619. const CarlaRecursiveMutexLocker cml(mutex);
  620. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  621. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  622. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  623. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  624. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  625. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  626. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  627. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  628. try {
  629. inBufTmp[0] = new float[bufferSize];
  630. inBufTmp[1] = new float[bufferSize];
  631. unusedBuf = new float[bufferSize];
  632. if (createBuffers)
  633. {
  634. inBuf[0] = new float[bufferSize];
  635. inBuf[1] = new float[bufferSize];
  636. outBuf[0] = new float[bufferSize];
  637. outBuf[1] = new float[bufferSize];
  638. }
  639. }
  640. catch(...) {
  641. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  642. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  643. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  644. if (createBuffers)
  645. {
  646. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  647. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  648. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  649. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  650. }
  651. return;
  652. }
  653. carla_zeroFloats(inBufTmp[0], bufferSize);
  654. carla_zeroFloats(inBufTmp[1], bufferSize);
  655. if (createBuffers)
  656. {
  657. carla_zeroFloats(inBuf[0], bufferSize);
  658. carla_zeroFloats(inBuf[1], bufferSize);
  659. carla_zeroFloats(outBuf[0], bufferSize);
  660. carla_zeroFloats(outBuf[1], bufferSize);
  661. }
  662. }
  663. // -----------------------------------------------------------------------
  664. // RackGraph
  665. RackGraph::RackGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs) noexcept
  666. : extGraph(engine),
  667. inputs(ins),
  668. outputs(outs),
  669. isOffline(false),
  670. audioBuffers(),
  671. kEngine(engine)
  672. {
  673. setBufferSize(engine->getBufferSize());
  674. }
  675. RackGraph::~RackGraph() noexcept
  676. {
  677. extGraph.clear();
  678. }
  679. void RackGraph::setBufferSize(const uint32_t bufferSize) noexcept
  680. {
  681. audioBuffers.setBufferSize(bufferSize, (inputs > 0 || outputs > 0));
  682. }
  683. void RackGraph::setOffline(const bool offline) noexcept
  684. {
  685. isOffline = offline;
  686. }
  687. bool RackGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  688. {
  689. return extGraph.connect(true, true, groupA, portA, groupB, portB);
  690. }
  691. bool RackGraph::disconnect(const uint connectionId) noexcept
  692. {
  693. return extGraph.disconnect(true, true, connectionId);
  694. }
  695. void RackGraph::refresh(const bool sendHost, const bool sendOSC, const bool, const char* const deviceName)
  696. {
  697. extGraph.refresh(sendHost, sendOSC, deviceName);
  698. char strBuf[STR_MAX+1];
  699. strBuf[STR_MAX] = '\0';
  700. // Connections
  701. const CarlaRecursiveMutexLocker cml(audioBuffers.mutex);
  702. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  703. {
  704. const uint& portId(it.getValue(0));
  705. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  706. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  707. ConnectionToId connectionToId;
  708. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1);
  709. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  710. kEngine->callback(sendHost, sendOSC,
  711. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  712. connectionToId.id,
  713. 0, 0, 0, 0.0f,
  714. strBuf);
  715. extGraph.connections.list.append(connectionToId);
  716. }
  717. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  718. {
  719. const uint& portId(it.getValue(0));
  720. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  721. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  722. ConnectionToId connectionToId;
  723. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2);
  724. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  725. kEngine->callback(sendHost, sendOSC,
  726. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  727. connectionToId.id,
  728. 0, 0, 0, 0.0f,
  729. strBuf);
  730. extGraph.connections.list.append(connectionToId);
  731. }
  732. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  733. {
  734. const uint& portId(it.getValue(0));
  735. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  736. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  737. ConnectionToId connectionToId;
  738. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, kExternalGraphGroupAudioOut, portId);
  739. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  740. kEngine->callback(sendHost, sendOSC,
  741. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  742. connectionToId.id,
  743. 0, 0, 0, 0.0f,
  744. strBuf);
  745. extGraph.connections.list.append(connectionToId);
  746. }
  747. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  748. {
  749. const uint& portId(it.getValue(0));
  750. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  751. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  752. ConnectionToId connectionToId;
  753. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, kExternalGraphGroupAudioOut, portId);
  754. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  755. kEngine->callback(sendHost, sendOSC,
  756. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  757. connectionToId.id,
  758. 0, 0, 0, 0.0f,
  759. strBuf);
  760. extGraph.connections.list.append(connectionToId);
  761. }
  762. }
  763. const char* const* RackGraph::getConnections() const noexcept
  764. {
  765. return extGraph.getConnections();
  766. }
  767. bool RackGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  768. {
  769. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  770. }
  771. void RackGraph::process(CarlaEngine::ProtectedData* const data, const float* inBufReal[2], float* outBufReal[2], const uint32_t frames)
  772. {
  773. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  774. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  775. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  776. // safe copy
  777. float* const dummyBuf = audioBuffers.unusedBuf;
  778. float* const inBuf0 = audioBuffers.inBufTmp[0];
  779. float* const inBuf1 = audioBuffers.inBufTmp[1];
  780. // initialize audio inputs
  781. carla_copyFloats(inBuf0, inBufReal[0], frames);
  782. carla_copyFloats(inBuf1, inBufReal[1], frames);
  783. // initialize audio outputs (zero)
  784. carla_zeroFloats(outBufReal[0], frames);
  785. carla_zeroFloats(outBufReal[1], frames);
  786. // initialize event outputs (zero)
  787. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  788. uint32_t oldAudioInCount = 0;
  789. uint32_t oldAudioOutCount = 0;
  790. uint32_t oldMidiOutCount = 0;
  791. bool processed = false;
  792. // process plugins
  793. for (uint i=0; i < data->curPluginCount; ++i)
  794. {
  795. const CarlaPluginPtr plugin = data->plugins[i].plugin;
  796. if (plugin.get() == nullptr || ! plugin->isEnabled() || ! plugin->tryLock(isOffline))
  797. continue;
  798. if (processed)
  799. {
  800. // initialize audio inputs (from previous outputs)
  801. carla_copyFloats(inBuf0, outBufReal[0], frames);
  802. carla_copyFloats(inBuf1, outBufReal[1], frames);
  803. // initialize audio outputs (zero)
  804. carla_zeroFloats(outBufReal[0], frames);
  805. carla_zeroFloats(outBufReal[1], frames);
  806. // if plugin has no midi out, add previous events
  807. if (oldMidiOutCount == 0 && data->events.in[0].type != kEngineEventTypeNull)
  808. {
  809. if (data->events.out[0].type != kEngineEventTypeNull)
  810. {
  811. // TODO: carefully add to input, sorted events
  812. //carla_stderr("TODO midi event mixing here %s", plugin->getName());
  813. }
  814. // else nothing needed
  815. }
  816. else
  817. {
  818. // initialize event inputs from previous outputs
  819. carla_copyStructs(data->events.in, data->events.out, kMaxEngineEventInternalCount);
  820. // initialize event outputs (zero)
  821. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  822. }
  823. }
  824. oldAudioInCount = plugin->getAudioInCount();
  825. oldAudioOutCount = plugin->getAudioOutCount();
  826. oldMidiOutCount = plugin->getMidiOutCount();
  827. const uint32_t numInBufs = std::max(oldAudioInCount, 2U);
  828. const uint32_t numOutBufs = std::max(oldAudioOutCount, 2U);
  829. const uint32_t numCvBufs = std::max(plugin->getCVInCount(), plugin->getCVOutCount());
  830. const float* inBuf[numInBufs];
  831. inBuf[0] = inBuf0;
  832. inBuf[1] = inBuf1;
  833. float* outBuf[numOutBufs];
  834. outBuf[0] = outBufReal[0];
  835. outBuf[1] = outBufReal[1];
  836. float* cvBuf[numCvBufs];
  837. for (uint32_t j=0; j<numCvBufs; ++j)
  838. cvBuf[j] = dummyBuf;
  839. if (numInBufs > 2 || numOutBufs > 2 || numCvBufs != 0)
  840. {
  841. carla_zeroFloats(dummyBuf, frames);
  842. for (uint32_t j=2; j<numInBufs; ++j)
  843. inBuf[j] = dummyBuf;
  844. for (uint32_t j=2; j<numOutBufs; ++j)
  845. outBuf[j] = dummyBuf;
  846. }
  847. // process
  848. plugin->initBuffers();
  849. plugin->process(inBuf, outBuf, cvBuf, cvBuf, frames);
  850. plugin->unlock();
  851. // if plugin has no audio inputs, add input buffer
  852. if (oldAudioInCount == 0)
  853. {
  854. carla_addFloats(outBufReal[0], inBuf0, frames);
  855. carla_addFloats(outBufReal[1], inBuf1, frames);
  856. }
  857. // if plugin only has 1 output, copy it to the 2nd
  858. if (oldAudioOutCount == 1)
  859. {
  860. carla_copyFloats(outBufReal[1], outBufReal[0], frames);
  861. }
  862. // set peaks
  863. {
  864. EnginePluginData& pluginData(data->plugins[i]);
  865. if (oldAudioInCount > 0)
  866. {
  867. pluginData.peaks[0] = carla_findMaxNormalizedFloat(inBuf0, frames);
  868. pluginData.peaks[1] = carla_findMaxNormalizedFloat(inBuf1, frames);
  869. }
  870. else
  871. {
  872. pluginData.peaks[0] = 0.0f;
  873. pluginData.peaks[1] = 0.0f;
  874. }
  875. if (oldAudioOutCount > 0)
  876. {
  877. pluginData.peaks[2] = carla_findMaxNormalizedFloat(outBufReal[0], frames);
  878. pluginData.peaks[3] = carla_findMaxNormalizedFloat(outBufReal[1], frames);
  879. }
  880. else
  881. {
  882. pluginData.peaks[2] = 0.0f;
  883. pluginData.peaks[3] = 0.0f;
  884. }
  885. }
  886. processed = true;
  887. }
  888. }
  889. void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  890. {
  891. CARLA_SAFE_ASSERT_RETURN(audioBuffers.outBuf[1] != nullptr,);
  892. const CarlaRecursiveMutexLocker _cml(audioBuffers.mutex);
  893. if (inBuf != nullptr && inputs > 0)
  894. {
  895. bool noConnections = true;
  896. // connect input buffers
  897. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  898. {
  899. const uint& port(it.getValue(0));
  900. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  901. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  902. if (noConnections)
  903. {
  904. carla_copyFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  905. noConnections = false;
  906. }
  907. else
  908. {
  909. carla_addFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  910. }
  911. }
  912. if (noConnections)
  913. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  914. noConnections = true;
  915. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  916. {
  917. const uint& port(it.getValue(0));
  918. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  919. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  920. if (noConnections)
  921. {
  922. carla_copyFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  923. noConnections = false;
  924. }
  925. else
  926. {
  927. carla_addFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  928. }
  929. }
  930. if (noConnections)
  931. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  932. }
  933. else
  934. {
  935. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  936. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  937. }
  938. carla_zeroFloats(audioBuffers.outBuf[0], frames);
  939. carla_zeroFloats(audioBuffers.outBuf[1], frames);
  940. // process
  941. process(data, const_cast<const float**>(audioBuffers.inBuf), audioBuffers.outBuf, frames);
  942. // connect output buffers
  943. if (audioBuffers.connectedOut1.count() != 0)
  944. {
  945. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  946. {
  947. const uint& port(it.getValue(0));
  948. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  949. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  950. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[0], frames);
  951. }
  952. }
  953. if (audioBuffers.connectedOut2.count() != 0)
  954. {
  955. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  956. {
  957. const uint& port(it.getValue(0));
  958. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  959. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  960. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[1], frames);
  961. }
  962. }
  963. }
  964. // -----------------------------------------------------------------------
  965. // Patchbay Graph stuff
  966. static const uint32_t kMaxPortsPerPlugin = 255;
  967. static const uint32_t kAudioInputPortOffset = kMaxPortsPerPlugin*1;
  968. static const uint32_t kAudioOutputPortOffset = kMaxPortsPerPlugin*2;
  969. static const uint32_t kCVInputPortOffset = kMaxPortsPerPlugin*3;
  970. static const uint32_t kCVOutputPortOffset = kMaxPortsPerPlugin*4;
  971. static const uint32_t kMidiInputPortOffset = kMaxPortsPerPlugin*5;
  972. static const uint32_t kMidiOutputPortOffset = kMaxPortsPerPlugin*6;
  973. static const uint32_t kMaxPortOffset = kMaxPortsPerPlugin*7;
  974. static inline
  975. bool adjustPatchbayPortIdForWater(AudioProcessor::ChannelType& channelType, uint& portId)
  976. {
  977. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, false);
  978. CARLA_SAFE_ASSERT_RETURN(portId < kMaxPortOffset, false);
  979. if (portId >= kMidiOutputPortOffset)
  980. {
  981. portId -= kMidiOutputPortOffset;
  982. channelType = AudioProcessor::ChannelTypeMIDI;
  983. return true;
  984. }
  985. if (portId >= kMidiInputPortOffset)
  986. {
  987. portId -= kMidiInputPortOffset;
  988. channelType = AudioProcessor::ChannelTypeMIDI;
  989. return true;
  990. }
  991. if (portId >= kCVOutputPortOffset)
  992. {
  993. portId -= kCVOutputPortOffset;
  994. channelType = AudioProcessor::ChannelTypeCV;
  995. return true;
  996. }
  997. if (portId >= kCVInputPortOffset)
  998. {
  999. portId -= kCVInputPortOffset;
  1000. channelType = AudioProcessor::ChannelTypeCV;
  1001. return true;
  1002. }
  1003. if (portId >= kAudioOutputPortOffset)
  1004. {
  1005. portId -= kAudioOutputPortOffset;
  1006. channelType = AudioProcessor::ChannelTypeAudio;
  1007. return true;
  1008. }
  1009. if (portId >= kAudioInputPortOffset)
  1010. {
  1011. portId -= kAudioInputPortOffset;
  1012. channelType = AudioProcessor::ChannelTypeAudio;
  1013. return true;
  1014. }
  1015. return false;
  1016. }
  1017. static inline
  1018. const String getProcessorFullPortName(AudioProcessor* const proc, const uint32_t portId)
  1019. {
  1020. CARLA_SAFE_ASSERT_RETURN(proc != nullptr, String());
  1021. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, String());
  1022. CARLA_SAFE_ASSERT_RETURN(portId < kMaxPortOffset, String());
  1023. String fullPortName(proc->getName());
  1024. /**/ if (portId >= kMidiOutputPortOffset)
  1025. {
  1026. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI) > 0, String());
  1027. fullPortName += ":" + proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI,
  1028. portId-kMidiOutputPortOffset);
  1029. }
  1030. else if (portId >= kMidiInputPortOffset)
  1031. {
  1032. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI) > 0, String());
  1033. fullPortName += ":" + proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI,
  1034. portId-kMidiInputPortOffset);
  1035. }
  1036. else if (portId >= kCVOutputPortOffset)
  1037. {
  1038. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV) > 0, String());
  1039. fullPortName += ":" + proc->getOutputChannelName(AudioProcessor::ChannelTypeCV,
  1040. portId-kCVOutputPortOffset);
  1041. }
  1042. else if (portId >= kCVInputPortOffset)
  1043. {
  1044. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV) > 0, String());
  1045. fullPortName += ":" + proc->getInputChannelName(AudioProcessor::ChannelTypeCV,
  1046. portId-kCVInputPortOffset);
  1047. }
  1048. else if (portId >= kAudioOutputPortOffset)
  1049. {
  1050. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio) > 0, String());
  1051. fullPortName += ":" + proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio,
  1052. portId-kAudioOutputPortOffset);
  1053. }
  1054. else if (portId >= kAudioInputPortOffset)
  1055. {
  1056. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio) > 0, String());
  1057. fullPortName += ":" + proc->getInputChannelName(AudioProcessor::ChannelTypeAudio,
  1058. portId-kAudioInputPortOffset);
  1059. }
  1060. else
  1061. {
  1062. return String();
  1063. }
  1064. return fullPortName;
  1065. }
  1066. static inline
  1067. void addNodeToPatchbay(const bool sendHost, const bool sendOSC, CarlaEngine* const engine,
  1068. AudioProcessorGraph::Node* const node, const int pluginId, const AudioProcessor* const proc)
  1069. {
  1070. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1071. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1072. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1073. const uint groupId = node->nodeId;
  1074. engine->callback(sendHost, sendOSC,
  1075. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  1076. groupId,
  1077. pluginId >= 0 ? PATCHBAY_ICON_PLUGIN : PATCHBAY_ICON_HARDWARE,
  1078. pluginId,
  1079. 0, 0.0f,
  1080. proc->getName().toRawUTF8());
  1081. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); i<numInputs; ++i)
  1082. {
  1083. engine->callback(sendHost, sendOSC,
  1084. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1085. groupId,
  1086. static_cast<int>(kAudioInputPortOffset+i),
  1087. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT,
  1088. 0, 0.0f,
  1089. proc->getInputChannelName(AudioProcessor::ChannelTypeAudio, i).toRawUTF8());
  1090. }
  1091. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); i<numOutputs; ++i)
  1092. {
  1093. engine->callback(sendHost, sendOSC,
  1094. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1095. groupId,
  1096. static_cast<int>(kAudioOutputPortOffset+i),
  1097. PATCHBAY_PORT_TYPE_AUDIO,
  1098. 0, 0.0f,
  1099. proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio, i).toRawUTF8());
  1100. }
  1101. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); i<numInputs; ++i)
  1102. {
  1103. engine->callback(sendHost, sendOSC,
  1104. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1105. groupId,
  1106. static_cast<int>(kCVInputPortOffset+i),
  1107. PATCHBAY_PORT_TYPE_CV|PATCHBAY_PORT_IS_INPUT,
  1108. 0, 0.0f,
  1109. proc->getInputChannelName(AudioProcessor::ChannelTypeCV, i).toRawUTF8());
  1110. }
  1111. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); i<numOutputs; ++i)
  1112. {
  1113. engine->callback(sendHost, sendOSC,
  1114. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1115. groupId,
  1116. static_cast<int>(kCVOutputPortOffset+i),
  1117. PATCHBAY_PORT_TYPE_CV,
  1118. 0, 0.0f,
  1119. proc->getOutputChannelName(AudioProcessor::ChannelTypeCV, i).toRawUTF8());
  1120. }
  1121. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); i<numInputs; ++i)
  1122. {
  1123. engine->callback(sendHost, sendOSC,
  1124. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1125. groupId,
  1126. static_cast<int>(kMidiInputPortOffset+i),
  1127. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT,
  1128. 0, 0.0f,
  1129. proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI, i).toRawUTF8());
  1130. }
  1131. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); i<numOutputs; ++i)
  1132. {
  1133. engine->callback(sendHost, sendOSC,
  1134. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1135. groupId,
  1136. static_cast<int>(kMidiOutputPortOffset+i),
  1137. PATCHBAY_PORT_TYPE_MIDI,
  1138. 0, 0.0f,
  1139. proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI, i).toRawUTF8());
  1140. }
  1141. if (node->properties.position.valid)
  1142. {
  1143. engine->callback(sendHost, sendOSC,
  1144. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1145. groupId,
  1146. node->properties.position.x1,
  1147. node->properties.position.y1,
  1148. node->properties.position.x2,
  1149. static_cast<float>(node->properties.position.y2),
  1150. nullptr);
  1151. }
  1152. }
  1153. static inline
  1154. void removeNodeFromPatchbay(const bool sendHost, const bool sendOSC, CarlaEngine* const engine,
  1155. const uint32_t groupId, const AudioProcessor* const proc)
  1156. {
  1157. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1158. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1159. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); i<numInputs; ++i)
  1160. {
  1161. engine->callback(sendHost, sendOSC,
  1162. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1163. groupId,
  1164. static_cast<int>(kAudioInputPortOffset+i),
  1165. 0, 0, 0.0f, nullptr);
  1166. }
  1167. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); i<numOutputs; ++i)
  1168. {
  1169. engine->callback(sendHost, sendOSC,
  1170. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1171. groupId,
  1172. static_cast<int>(kAudioOutputPortOffset+i),
  1173. 0, 0, 0.0f,
  1174. nullptr);
  1175. }
  1176. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); i<numInputs; ++i)
  1177. {
  1178. engine->callback(sendHost, sendOSC,
  1179. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1180. groupId,
  1181. static_cast<int>(kCVInputPortOffset+i),
  1182. 0, 0, 0.0f, nullptr);
  1183. }
  1184. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); i<numOutputs; ++i)
  1185. {
  1186. engine->callback(sendHost, sendOSC,
  1187. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1188. groupId,
  1189. static_cast<int>(kCVOutputPortOffset+i),
  1190. 0, 0, 0.0f,
  1191. nullptr);
  1192. }
  1193. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); i<numInputs; ++i)
  1194. {
  1195. engine->callback(sendHost, sendOSC,
  1196. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1197. groupId,
  1198. static_cast<int>(kMidiInputPortOffset+i),
  1199. 0, 0, 0.0f, nullptr);
  1200. }
  1201. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); i<numOutputs; ++i)
  1202. {
  1203. engine->callback(sendHost, sendOSC,
  1204. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1205. groupId,
  1206. static_cast<int>(kMidiOutputPortOffset+i),
  1207. 0, 0, 0.0f, nullptr);
  1208. }
  1209. engine->callback(sendHost, sendOSC,
  1210. ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED,
  1211. groupId,
  1212. 0, 0, 0, 0.0f, nullptr);
  1213. }
  1214. // -----------------------------------------------------------------------
  1215. class CarlaPluginInstance : public AudioProcessor
  1216. {
  1217. public:
  1218. CarlaPluginInstance(CarlaEngine* const engine, const CarlaPluginPtr plugin)
  1219. : kEngine(engine),
  1220. fPlugin(plugin)
  1221. {
  1222. CarlaEngineClient* const client = plugin->getEngineClient();
  1223. setPlayConfigDetails(client->getPortCount(kEnginePortTypeAudio, true),
  1224. client->getPortCount(kEnginePortTypeAudio, false),
  1225. client->getPortCount(kEnginePortTypeCV, true),
  1226. client->getPortCount(kEnginePortTypeCV, false),
  1227. client->getPortCount(kEnginePortTypeEvent, true),
  1228. client->getPortCount(kEnginePortTypeEvent, false),
  1229. getSampleRate(), getBlockSize());
  1230. }
  1231. ~CarlaPluginInstance() override
  1232. {
  1233. }
  1234. void reconfigure() override
  1235. {
  1236. const CarlaPluginPtr plugin = fPlugin;
  1237. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1238. CarlaEngineClient* const client = plugin->getEngineClient();
  1239. CARLA_SAFE_ASSERT_RETURN(client != nullptr,);
  1240. carla_stdout("reconfigure called");
  1241. setPlayConfigDetails(client->getPortCount(kEnginePortTypeAudio, true),
  1242. client->getPortCount(kEnginePortTypeAudio, false),
  1243. client->getPortCount(kEnginePortTypeCV, true),
  1244. client->getPortCount(kEnginePortTypeCV, false),
  1245. client->getPortCount(kEnginePortTypeEvent, true),
  1246. client->getPortCount(kEnginePortTypeEvent, false),
  1247. getSampleRate(), getBlockSize());
  1248. }
  1249. void invalidatePlugin() noexcept
  1250. {
  1251. fPlugin.reset();
  1252. }
  1253. // -------------------------------------------------------------------
  1254. const String getName() const override
  1255. {
  1256. const CarlaPluginPtr plugin = fPlugin;
  1257. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr, String());
  1258. return plugin->getName();
  1259. }
  1260. void processBlockWithCV(AudioSampleBuffer& audio,
  1261. const AudioSampleBuffer& cvIn,
  1262. AudioSampleBuffer& cvOut,
  1263. MidiBuffer& midi) override
  1264. {
  1265. const CarlaPluginPtr plugin = fPlugin;
  1266. if (plugin.get() == nullptr || !plugin->isEnabled() || !plugin->tryLock(kEngine->isOffline()))
  1267. {
  1268. audio.clear();
  1269. cvOut.clear();
  1270. midi.clear();
  1271. return;
  1272. }
  1273. if (CarlaEngineEventPort* const port = plugin->getDefaultEventInPort())
  1274. {
  1275. EngineEvent* const engineEvents(port->fBuffer);
  1276. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1277. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1278. fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
  1279. }
  1280. midi.clear();
  1281. plugin->initBuffers();
  1282. const uint32_t numSamples = audio.getNumSamples();
  1283. const uint32_t numAudioChan = audio.getNumChannels();
  1284. const uint32_t numCVInChan = cvIn.getNumChannels();
  1285. const uint32_t numCVOutChan = cvOut.getNumChannels();
  1286. if (numAudioChan+numCVInChan+numCVOutChan == 0)
  1287. {
  1288. // nothing to process
  1289. plugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
  1290. }
  1291. else if (numAudioChan != 0)
  1292. {
  1293. // processing audio, include code for peaks
  1294. const uint32_t numChan2 = jmin(numAudioChan, 2U);
  1295. if (plugin->getAudioInCount() == 0)
  1296. audio.clear();
  1297. float* audioBuffers[numAudioChan];
  1298. float* cvOutBuffers[numCVOutChan];
  1299. const float* cvInBuffers[numCVInChan];
  1300. for (uint32_t i=0; i<numAudioChan; ++i)
  1301. audioBuffers[i] = audio.getWritePointer(i);
  1302. for (uint32_t i=0; i<numCVOutChan; ++i)
  1303. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1304. for (uint32_t i=0; i<numCVInChan; ++i)
  1305. cvInBuffers[i] = cvIn.getReadPointer(i);
  1306. float inPeaks[2] = { 0.0f };
  1307. float outPeaks[2] = { 0.0f };
  1308. for (uint32_t i=0, count=jmin(plugin->getAudioInCount(), numChan2); i<count; ++i)
  1309. inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1310. plugin->process(const_cast<const float**>(audioBuffers), audioBuffers,
  1311. cvInBuffers, cvOutBuffers,
  1312. numSamples);
  1313. for (uint32_t i=0, count=jmin(plugin->getAudioOutCount(), numChan2); i<count; ++i)
  1314. outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1315. kEngine->setPluginPeaksRT(plugin->getId(), inPeaks, outPeaks);
  1316. }
  1317. else
  1318. {
  1319. // processing CV only, skip audiopeaks
  1320. float* cvOutBuffers[numCVOutChan];
  1321. const float* cvInBuffers[numCVInChan];
  1322. for (uint32_t i=0; i<numCVOutChan; ++i)
  1323. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1324. for (uint32_t i=0; i<numCVInChan; ++i)
  1325. cvInBuffers[i] = cvIn.getReadPointer(i);
  1326. plugin->process(nullptr, nullptr,
  1327. cvInBuffers, cvOutBuffers,
  1328. numSamples);
  1329. }
  1330. midi.clear();
  1331. if (CarlaEngineEventPort* const port = plugin->getDefaultEventOutPort())
  1332. {
  1333. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  1334. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1335. fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
  1336. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1337. }
  1338. plugin->unlock();
  1339. }
  1340. const String getInputChannelName(ChannelType t, uint i) const override
  1341. {
  1342. const CarlaPluginPtr plugin = fPlugin;
  1343. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr, String());
  1344. CarlaEngineClient* const client = plugin->getEngineClient();
  1345. switch (t)
  1346. {
  1347. case ChannelTypeAudio:
  1348. return client->getAudioPortName(true, i);
  1349. case ChannelTypeCV:
  1350. return client->getCVPortName(true, i);
  1351. case ChannelTypeMIDI:
  1352. return client->getEventPortName(true, i);
  1353. }
  1354. return String();
  1355. }
  1356. const String getOutputChannelName(ChannelType t, uint i) const override
  1357. {
  1358. const CarlaPluginPtr plugin = fPlugin;
  1359. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr, String());
  1360. CarlaEngineClient* const client(plugin->getEngineClient());
  1361. switch (t)
  1362. {
  1363. case ChannelTypeAudio:
  1364. return client->getAudioPortName(false, i);
  1365. case ChannelTypeCV:
  1366. return client->getCVPortName(false, i);
  1367. case ChannelTypeMIDI:
  1368. return client->getEventPortName(false, i);
  1369. }
  1370. return String();
  1371. }
  1372. void prepareToPlay(double, int) override {}
  1373. void releaseResources() override {}
  1374. bool acceptsMidi() const override
  1375. {
  1376. const CarlaPluginPtr plugin = fPlugin;
  1377. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr, false);
  1378. return plugin->getDefaultEventInPort() != nullptr;
  1379. }
  1380. bool producesMidi() const override
  1381. {
  1382. const CarlaPluginPtr plugin = fPlugin;
  1383. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr, false);
  1384. return plugin->getDefaultEventOutPort() != nullptr;
  1385. }
  1386. private:
  1387. CarlaEngine* const kEngine;
  1388. CarlaPluginPtr fPlugin;
  1389. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1390. };
  1391. // -----------------------------------------------------------------------
  1392. // Patchbay Graph
  1393. class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
  1394. {
  1395. public:
  1396. NamedAudioGraphIOProcessor(const IODeviceType iotype)
  1397. : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
  1398. inputNames(),
  1399. outputNames() {}
  1400. const String getInputChannelName (ChannelType, uint _index) const override
  1401. {
  1402. const int index = static_cast<int>(_index); // FIXME
  1403. if (index < inputNames.size())
  1404. return inputNames[index];
  1405. return String("Playback ") + String(index+1);
  1406. }
  1407. const String getOutputChannelName (ChannelType, uint _index) const override
  1408. {
  1409. const int index = static_cast<int>(_index); // FIXME
  1410. if (index < outputNames.size())
  1411. return outputNames[index];
  1412. return String("Capture ") + String(index+1);
  1413. }
  1414. void setNames(const bool setInputNames, const StringArray& names)
  1415. {
  1416. if (setInputNames)
  1417. inputNames = names;
  1418. else
  1419. outputNames = names;
  1420. }
  1421. private:
  1422. StringArray inputNames;
  1423. StringArray outputNames;
  1424. };
  1425. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine,
  1426. const uint32_t audioIns, const uint32_t audioOuts,
  1427. const uint32_t cvIns, const uint32_t cvOuts)
  1428. : CarlaRunner("PatchbayReorderRunner"),
  1429. connections(),
  1430. graph(),
  1431. audioBuffer(),
  1432. cvInBuffer(),
  1433. cvOutBuffer(),
  1434. midiBuffer(),
  1435. numAudioIns(carla_fixedValue(0U, 64U, audioIns)),
  1436. numAudioOuts(carla_fixedValue(0U, 64U, audioOuts)),
  1437. numCVIns(carla_fixedValue(0U, 32U, cvIns)),
  1438. numCVOuts(carla_fixedValue(0U, 32U, cvOuts)),
  1439. retCon(),
  1440. usingExternalHost(false),
  1441. usingExternalOSC(false),
  1442. extGraph(engine),
  1443. kEngine(engine)
  1444. {
  1445. const uint32_t bufferSize(engine->getBufferSize());
  1446. const double sampleRate(engine->getSampleRate());
  1447. graph.setPlayConfigDetails(numAudioIns, numAudioOuts,
  1448. numCVIns, numCVOuts,
  1449. 1, 1,
  1450. sampleRate, static_cast<int>(bufferSize));
  1451. graph.prepareToPlay(sampleRate, static_cast<int>(bufferSize));
  1452. audioBuffer.setSize(jmax(numAudioIns, numAudioOuts), bufferSize);
  1453. cvInBuffer.setSize(numCVIns, bufferSize);
  1454. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1455. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1456. midiBuffer.clear();
  1457. StringArray channelNames;
  1458. switch (numAudioIns)
  1459. {
  1460. case 2:
  1461. channelNames.add("Left");
  1462. channelNames.add("Right");
  1463. break;
  1464. case 3:
  1465. channelNames.add("Left");
  1466. channelNames.add("Right");
  1467. channelNames.add("Sidechain");
  1468. break;
  1469. }
  1470. if (numAudioIns != 0)
  1471. {
  1472. NamedAudioGraphIOProcessor* const proc(
  1473. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
  1474. proc->setNames(false, channelNames);
  1475. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1476. node->properties.isPlugin = false;
  1477. node->properties.isOutput = false;
  1478. node->properties.isAudio = true;
  1479. node->properties.isCV = false;
  1480. node->properties.isMIDI = false;
  1481. node->properties.isOSC = false;
  1482. }
  1483. if (numAudioOuts != 0)
  1484. {
  1485. NamedAudioGraphIOProcessor* const proc(
  1486. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
  1487. proc->setNames(true, channelNames);
  1488. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1489. node->properties.isPlugin = false;
  1490. node->properties.isOutput = false;
  1491. node->properties.isAudio = true;
  1492. node->properties.isCV = false;
  1493. node->properties.isMIDI = false;
  1494. node->properties.isOSC = false;
  1495. }
  1496. if (numCVIns != 0)
  1497. {
  1498. NamedAudioGraphIOProcessor* const proc(
  1499. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvInputNode));
  1500. // proc->setNames(false, channelNames);
  1501. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1502. node->properties.isPlugin = false;
  1503. node->properties.isOutput = false;
  1504. node->properties.isAudio = false;
  1505. node->properties.isCV = true;
  1506. node->properties.isMIDI = false;
  1507. node->properties.isOSC = false;
  1508. }
  1509. if (numCVOuts != 0)
  1510. {
  1511. NamedAudioGraphIOProcessor* const proc(
  1512. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvOutputNode));
  1513. // proc->setNames(true, channelNames);
  1514. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1515. node->properties.isPlugin = false;
  1516. node->properties.isOutput = false;
  1517. node->properties.isAudio = false;
  1518. node->properties.isCV = true;
  1519. node->properties.isMIDI = false;
  1520. node->properties.isOSC = false;
  1521. }
  1522. {
  1523. NamedAudioGraphIOProcessor* const proc(
  1524. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
  1525. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1526. node->properties.isPlugin = false;
  1527. node->properties.isOutput = false;
  1528. node->properties.isAudio = false;
  1529. node->properties.isCV = false;
  1530. node->properties.isMIDI = true;
  1531. node->properties.isOSC = false;
  1532. }
  1533. {
  1534. NamedAudioGraphIOProcessor* const proc(
  1535. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
  1536. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1537. node->properties.isPlugin = false;
  1538. node->properties.isOutput = true;
  1539. node->properties.isAudio = false;
  1540. node->properties.isCV = false;
  1541. node->properties.isMIDI = true;
  1542. node->properties.isOSC = false;
  1543. }
  1544. startRunner(100);
  1545. }
  1546. PatchbayGraph::~PatchbayGraph()
  1547. {
  1548. stopRunner();
  1549. connections.clear();
  1550. extGraph.clear();
  1551. graph.releaseResources();
  1552. graph.clear();
  1553. audioBuffer.clear();
  1554. cvInBuffer.clear();
  1555. cvOutBuffer.clear();
  1556. }
  1557. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1558. {
  1559. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1560. graph.releaseResources();
  1561. graph.prepareToPlay(kEngine->getSampleRate(), static_cast<int>(bufferSize));
  1562. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSize);
  1563. cvInBuffer.setSize(numCVIns, bufferSize);
  1564. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1565. }
  1566. void PatchbayGraph::setSampleRate(const double sampleRate)
  1567. {
  1568. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1569. graph.releaseResources();
  1570. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1571. }
  1572. void PatchbayGraph::setOffline(const bool offline)
  1573. {
  1574. graph.setNonRealtime(offline);
  1575. }
  1576. void PatchbayGraph::addPlugin(const CarlaPluginPtr plugin)
  1577. {
  1578. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1579. carla_debug("PatchbayGraph::addPlugin(%p)", plugin.get());
  1580. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1581. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1582. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1583. const bool sendHost = !usingExternalHost;
  1584. const bool sendOSC = !usingExternalOSC;
  1585. plugin->setPatchbayNodeId(node->nodeId);
  1586. node->properties.isPlugin = true;
  1587. node->properties.pluginId = plugin->getId();
  1588. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, static_cast<int>(plugin->getId()), instance);
  1589. }
  1590. void PatchbayGraph::replacePlugin(const CarlaPluginPtr oldPlugin, const CarlaPluginPtr newPlugin)
  1591. {
  1592. CARLA_SAFE_ASSERT_RETURN(oldPlugin.get() != nullptr,);
  1593. CARLA_SAFE_ASSERT_RETURN(newPlugin.get() != nullptr,);
  1594. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1595. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1596. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1597. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1598. const bool sendHost = !usingExternalHost;
  1599. const bool sendOSC = !usingExternalOSC;
  1600. disconnectInternalGroup(oldNode->nodeId);
  1601. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, oldNode->nodeId, oldNode->getProcessor());
  1602. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1603. graph.removeNode(oldNode->nodeId);
  1604. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1605. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1606. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1607. newPlugin->setPatchbayNodeId(node->nodeId);
  1608. node->properties.isPlugin = true;
  1609. node->properties.pluginId = newPlugin->getId();
  1610. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, static_cast<int>(newPlugin->getId()), instance);
  1611. }
  1612. void PatchbayGraph::renamePlugin(const CarlaPluginPtr plugin, const char* const newName)
  1613. {
  1614. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1615. carla_debug("PatchbayGraph::renamePlugin(%p)", plugin.get(), newName);
  1616. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1617. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1618. const bool sendHost = !usingExternalHost;
  1619. const bool sendOSC = !usingExternalOSC;
  1620. kEngine->callback(sendHost, sendOSC,
  1621. ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED,
  1622. node->nodeId,
  1623. 0, 0, 0, 0.0f,
  1624. newName);
  1625. }
  1626. void PatchbayGraph::switchPlugins(CarlaPluginPtr pluginA, CarlaPluginPtr pluginB)
  1627. {
  1628. CARLA_SAFE_ASSERT_RETURN(pluginA.get() != nullptr,);
  1629. CARLA_SAFE_ASSERT_RETURN(pluginB.get() != nullptr,);
  1630. CARLA_SAFE_ASSERT_RETURN(pluginA != pluginB,);
  1631. CARLA_SAFE_ASSERT_RETURN(pluginA->getId() != pluginB->getId(),);
  1632. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(pluginA->getPatchbayNodeId()));
  1633. CARLA_SAFE_ASSERT_RETURN(nodeA != nullptr,);
  1634. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(pluginB->getPatchbayNodeId()));
  1635. CARLA_SAFE_ASSERT_RETURN(nodeB != nullptr,);
  1636. nodeA->properties.pluginId = pluginB->getId();
  1637. nodeB->properties.pluginId = pluginA->getId();
  1638. }
  1639. void PatchbayGraph::reconfigureForCV(const CarlaPluginPtr plugin, const uint portIndex, bool added)
  1640. {
  1641. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1642. carla_debug("PatchbayGraph::reconfigureForCV(%p, %u, %s)", plugin.get(), portIndex, bool2str(added));
  1643. AudioProcessorGraph::Node* const node = graph.getNodeForId(plugin->getPatchbayNodeId());
  1644. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1645. CarlaPluginInstance* const proc = dynamic_cast<CarlaPluginInstance*>(node->getProcessor());
  1646. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1647. const bool sendHost = !usingExternalHost;
  1648. const bool sendOSC = !usingExternalOSC;
  1649. const uint oldCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1650. // const uint oldCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1651. {
  1652. const CarlaRecursiveMutexLocker crml(graph.getCallbackLock());
  1653. proc->reconfigure();
  1654. graph.buildRenderingSequence();
  1655. }
  1656. const uint newCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1657. // const uint newCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1658. if (added)
  1659. {
  1660. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn > oldCvIn, newCvIn, oldCvIn,);
  1661. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut >= oldCvOut, newCvOut, oldCvOut,);
  1662. kEngine->callback(sendHost, sendOSC,
  1663. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1664. node->nodeId,
  1665. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1666. PATCHBAY_PORT_TYPE_CV|PATCHBAY_PORT_IS_INPUT,
  1667. 0, 0.0f,
  1668. proc->getInputChannelName(AudioProcessor::ChannelTypeCV, portIndex).toRawUTF8());
  1669. }
  1670. else
  1671. {
  1672. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn < oldCvIn, newCvIn, oldCvIn,);
  1673. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut <= oldCvOut, newCvOut, oldCvOut,);
  1674. kEngine->callback(sendHost, sendOSC,
  1675. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1676. node->nodeId,
  1677. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1678. 0, 0, 0.0f, nullptr);
  1679. }
  1680. }
  1681. void PatchbayGraph::removePlugin(const CarlaPluginPtr plugin)
  1682. {
  1683. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1684. carla_debug("PatchbayGraph::removePlugin(%p)", plugin.get());
  1685. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1686. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1687. const bool sendHost = !usingExternalHost;
  1688. const bool sendOSC = !usingExternalOSC;
  1689. disconnectInternalGroup(node->nodeId);
  1690. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1691. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1692. // Fix plugin Ids properties
  1693. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1694. {
  1695. const CarlaPluginPtr plugin2 = kEngine->getPlugin(i);
  1696. CARLA_SAFE_ASSERT_BREAK(plugin2.get() != nullptr);
  1697. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1698. {
  1699. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.isPlugin);
  1700. node2->properties.pluginId = i - 1;
  1701. }
  1702. }
  1703. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1704. }
  1705. void PatchbayGraph::removeAllPlugins(const bool aboutToClose)
  1706. {
  1707. carla_debug("PatchbayGraph::removeAllPlugins()");
  1708. stopRunner();
  1709. const bool sendHost = !usingExternalHost;
  1710. const bool sendOSC = !usingExternalOSC;
  1711. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1712. {
  1713. const CarlaPluginPtr plugin = kEngine->getPlugin(i);
  1714. CARLA_SAFE_ASSERT_CONTINUE(plugin.get() != nullptr);
  1715. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1716. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1717. disconnectInternalGroup(node->nodeId);
  1718. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1719. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1720. graph.removeNode(node->nodeId);
  1721. }
  1722. if (!aboutToClose)
  1723. startRunner(100);
  1724. }
  1725. bool PatchbayGraph::connect(const bool external,
  1726. const uint groupA, const uint portA, const uint groupB, const uint portB)
  1727. {
  1728. if (external)
  1729. return extGraph.connect(usingExternalHost, usingExternalOSC, groupA, portA, groupB, portB);
  1730. uint adjustedPortA = portA;
  1731. uint adjustedPortB = portB;
  1732. AudioProcessor::ChannelType channelType;
  1733. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1734. return false;
  1735. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1736. return false;
  1737. if (! graph.addConnection(channelType, groupA, adjustedPortA, groupB, adjustedPortB))
  1738. {
  1739. kEngine->setLastError("Failed from water");
  1740. return false;
  1741. }
  1742. ConnectionToId connectionToId;
  1743. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1744. char strBuf[STR_MAX+1];
  1745. strBuf[STR_MAX] = '\0';
  1746. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1747. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1748. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1749. connectionToId.id,
  1750. 0, 0, 0, 0.0f,
  1751. strBuf);
  1752. connections.list.append(connectionToId);
  1753. return true;
  1754. }
  1755. bool PatchbayGraph::disconnect(const bool external, const uint connectionId)
  1756. {
  1757. if (external)
  1758. return extGraph.disconnect(usingExternalHost, usingExternalOSC, connectionId);
  1759. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1760. {
  1761. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1762. const ConnectionToId& connectionToId(it.getValue(fallback));
  1763. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1764. if (connectionToId.id != connectionId)
  1765. continue;
  1766. uint adjustedPortA = connectionToId.portA;
  1767. uint adjustedPortB = connectionToId.portB;
  1768. AudioProcessor::ChannelType channelType;
  1769. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1770. return false;
  1771. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1772. return false;
  1773. if (! graph.removeConnection(channelType,
  1774. connectionToId.groupA, adjustedPortA,
  1775. connectionToId.groupB, adjustedPortB))
  1776. return false;
  1777. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1778. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1779. connectionToId.id,
  1780. 0, 0, 0, 0.0f,
  1781. nullptr);
  1782. connections.list.remove(it);
  1783. return true;
  1784. }
  1785. kEngine->setLastError("Failed to find connection");
  1786. return false;
  1787. }
  1788. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1789. {
  1790. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1791. {
  1792. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1793. const ConnectionToId& connectionToId(it.getValue(fallback));
  1794. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1795. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1796. continue;
  1797. /*
  1798. uint adjustedPortA = connectionToId.portA;
  1799. uint adjustedPortB = connectionToId.portB;
  1800. AudioProcessor::ChannelType channelType;
  1801. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1802. return false;
  1803. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1804. return false;
  1805. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1806. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1807. */
  1808. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1809. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1810. connectionToId.id,
  1811. 0, 0, 0, 0.0f,
  1812. nullptr);
  1813. connections.list.remove(it);
  1814. }
  1815. }
  1816. void PatchbayGraph::setGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  1817. uint groupId, int x1, int y1, int x2, int y2)
  1818. {
  1819. if (external)
  1820. return extGraph.setGroupPos(sendHost, sendOSC, groupId, x1, y1, x2, y2);
  1821. AudioProcessorGraph::Node* const node(graph.getNodeForId(groupId));
  1822. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1823. node->properties.position.x1 = x1;
  1824. node->properties.position.y1 = y1;
  1825. node->properties.position.x2 = x2;
  1826. node->properties.position.y2 = y2;
  1827. node->properties.position.valid = true;
  1828. kEngine->callback(sendHost, sendOSC,
  1829. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1830. groupId, x1, y1, x2, static_cast<float>(y2),
  1831. nullptr);
  1832. }
  1833. void PatchbayGraph::refresh(const bool sendHost, const bool sendOSC, const bool external, const char* const deviceName)
  1834. {
  1835. if (external)
  1836. return extGraph.refresh(sendHost, sendOSC, deviceName);
  1837. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1838. connections.clear();
  1839. graph.removeIllegalConnections();
  1840. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1841. {
  1842. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1843. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1844. AudioProcessor* const proc(node->getProcessor());
  1845. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1846. int pluginId = -1;
  1847. // plugin node
  1848. if (node->properties.isPlugin)
  1849. pluginId = static_cast<int>(node->properties.pluginId);
  1850. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, pluginId, proc);
  1851. }
  1852. char strBuf[STR_MAX+1];
  1853. strBuf[STR_MAX] = '\0';
  1854. for (size_t i=0, count=graph.getNumConnections(); i<count; ++i)
  1855. {
  1856. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1857. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1858. const uint groupA = conn->sourceNodeId;
  1859. const uint groupB = conn->destNodeId;
  1860. uint portA = conn->sourceChannelIndex;
  1861. uint portB = conn->destChannelIndex;
  1862. switch (conn->channelType)
  1863. {
  1864. case AudioProcessor::ChannelTypeAudio:
  1865. portA += kAudioOutputPortOffset;
  1866. portB += kAudioInputPortOffset;
  1867. break;
  1868. case AudioProcessor::ChannelTypeCV:
  1869. portA += kCVOutputPortOffset;
  1870. portB += kCVInputPortOffset;
  1871. break;
  1872. case AudioProcessor::ChannelTypeMIDI:
  1873. portA += kMidiOutputPortOffset;
  1874. portB += kMidiInputPortOffset;
  1875. break;
  1876. }
  1877. ConnectionToId connectionToId;
  1878. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1879. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1880. kEngine->callback(sendHost, sendOSC,
  1881. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1882. connectionToId.id,
  1883. 0, 0, 0, 0.0f,
  1884. strBuf);
  1885. connections.list.append(connectionToId);
  1886. }
  1887. }
  1888. const char* const* PatchbayGraph::getConnections(const bool external) const
  1889. {
  1890. if (external)
  1891. return extGraph.getConnections();
  1892. if (connections.list.count() == 0)
  1893. return nullptr;
  1894. CarlaStringList connList;
  1895. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1896. {
  1897. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1898. const ConnectionToId& connectionToId(it.getValue(fallback));
  1899. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1900. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1901. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1902. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1903. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1904. AudioProcessor* const procA(nodeA->getProcessor());
  1905. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1906. AudioProcessor* const procB(nodeB->getProcessor());
  1907. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1908. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1909. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1910. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1911. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1912. connList.append(fullPortNameA.toRawUTF8());
  1913. connList.append(fullPortNameB.toRawUTF8());
  1914. }
  1915. if (connList.count() == 0)
  1916. return nullptr;
  1917. retCon = connList.toCharStringListPtr();
  1918. return retCon;
  1919. }
  1920. const CarlaEngine::PatchbayPosition* PatchbayGraph::getPositions(bool external, uint& count) const
  1921. {
  1922. CarlaEngine::PatchbayPosition* ret;
  1923. if (external)
  1924. {
  1925. try {
  1926. ret = new CarlaEngine::PatchbayPosition[kExternalGraphGroupMax];
  1927. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  1928. count = 0;
  1929. for (uint i=kExternalGraphGroupCarla; i<kExternalGraphGroupMax; ++i)
  1930. {
  1931. const PatchbayPosition& eppos(extGraph.positions[i]);
  1932. if (! eppos.active)
  1933. continue;
  1934. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  1935. switch (i)
  1936. {
  1937. case kExternalGraphGroupCarla:
  1938. ppos.name = "Carla";
  1939. break;
  1940. case kExternalGraphGroupAudioIn:
  1941. ppos.name = "AudioIn";
  1942. break;
  1943. case kExternalGraphGroupAudioOut:
  1944. ppos.name = "AudioOut";
  1945. break;
  1946. case kExternalGraphGroupMidiIn:
  1947. ppos.name = "MidiIn";
  1948. break;
  1949. case kExternalGraphGroupMidiOut:
  1950. ppos.name = "MidiOut";
  1951. break;
  1952. }
  1953. ppos.dealloc = false;
  1954. ppos.pluginId = -1;
  1955. ppos.x1 = eppos.x1;
  1956. ppos.y1 = eppos.y1;
  1957. ppos.x2 = eppos.x2;
  1958. ppos.y2 = eppos.y2;
  1959. }
  1960. return ret;
  1961. }
  1962. else
  1963. {
  1964. const int numNodes = graph.getNumNodes();
  1965. CARLA_SAFE_ASSERT_RETURN(numNodes > 0, nullptr);
  1966. try {
  1967. ret = new CarlaEngine::PatchbayPosition[numNodes];
  1968. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  1969. count = 0;
  1970. for (int i=numNodes; --i >= 0;)
  1971. {
  1972. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1973. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1974. if (! node->properties.position.valid)
  1975. continue;
  1976. AudioProcessor* const proc(node->getProcessor());
  1977. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1978. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  1979. ppos.name = carla_strdup(proc->getName().toRawUTF8());
  1980. ppos.dealloc = true;
  1981. ppos.pluginId = node->properties.isPlugin ? static_cast<int>(node->properties.pluginId) : -1;
  1982. ppos.x1 = node->properties.position.x1;
  1983. ppos.y1 = node->properties.position.y1;
  1984. ppos.x2 = node->properties.position.x2;
  1985. ppos.y2 = node->properties.position.y2;
  1986. }
  1987. return ret;
  1988. }
  1989. }
  1990. bool PatchbayGraph::getGroupFromName(bool external, const char* groupName, uint& groupId) const
  1991. {
  1992. if (external)
  1993. return extGraph.getGroupFromName(groupName, groupId);
  1994. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1995. {
  1996. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1997. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1998. AudioProcessor* const proc(node->getProcessor());
  1999. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  2000. if (proc->getName() != groupName)
  2001. continue;
  2002. groupId = node->nodeId;
  2003. return true;
  2004. }
  2005. return false;
  2006. }
  2007. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  2008. {
  2009. if (external)
  2010. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  2011. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  2012. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  2013. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  2014. {
  2015. AudioProcessorGraph::Node* const node(graph.getNode(i));
  2016. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  2017. AudioProcessor* const proc(node->getProcessor());
  2018. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  2019. if (proc->getName() != groupName)
  2020. continue;
  2021. groupId = node->nodeId;
  2022. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); j<numInputs; ++j)
  2023. {
  2024. if (proc->getInputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  2025. continue;
  2026. portId = kAudioInputPortOffset+j;
  2027. return true;
  2028. }
  2029. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); j<numOutputs; ++j)
  2030. {
  2031. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  2032. continue;
  2033. portId = kAudioOutputPortOffset+j;
  2034. return true;
  2035. }
  2036. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); j<numInputs; ++j)
  2037. {
  2038. if (proc->getInputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  2039. continue;
  2040. portId = kCVInputPortOffset+j;
  2041. return true;
  2042. }
  2043. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); j<numOutputs; ++j)
  2044. {
  2045. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  2046. continue;
  2047. portId = kCVOutputPortOffset+j;
  2048. return true;
  2049. }
  2050. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); j<numInputs; ++j)
  2051. {
  2052. if (proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  2053. continue;
  2054. portId = kMidiInputPortOffset+j;
  2055. return true;
  2056. }
  2057. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); j<numOutputs; ++j)
  2058. {
  2059. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  2060. continue;
  2061. portId = kMidiOutputPortOffset+j;
  2062. return true;
  2063. }
  2064. }
  2065. return false;
  2066. }
  2067. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data,
  2068. const float* const* const inBuf,
  2069. float* const* const outBuf,
  2070. const uint32_t frames)
  2071. {
  2072. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  2073. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  2074. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  2075. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  2076. // put events in water buffer
  2077. {
  2078. midiBuffer.clear();
  2079. fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  2080. }
  2081. // set audio and cv buffer size, needed for water internals
  2082. if (! audioBuffer.setSizeRT(frames))
  2083. return;
  2084. if (! cvInBuffer.setSizeRT(frames))
  2085. return;
  2086. if (! cvOutBuffer.setSizeRT(frames))
  2087. return;
  2088. // put carla audio and cv in water buffer
  2089. {
  2090. uint32_t i=0;
  2091. for (; i < numAudioIns; ++i) {
  2092. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  2093. audioBuffer.copyFrom(i, 0, inBuf[i], frames);
  2094. }
  2095. for (uint32_t j=0; j < numCVIns; ++j, ++i) {
  2096. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  2097. cvInBuffer.copyFrom(j, 0, inBuf[i], frames);
  2098. }
  2099. // clear remaining channels
  2100. for (uint32_t j=numAudioIns, count=audioBuffer.getNumChannels(); j < count; ++j)
  2101. audioBuffer.clear(j, 0, frames);
  2102. for (uint32_t j=0; j < numCVOuts; ++j)
  2103. cvOutBuffer.clear(j, 0, frames);
  2104. }
  2105. // ready to go!
  2106. graph.processBlockWithCV(audioBuffer, cvInBuffer, cvOutBuffer, midiBuffer);
  2107. // put water audio and cv in carla buffer
  2108. {
  2109. uint32_t i=0;
  2110. for (; i < numAudioOuts; ++i)
  2111. carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
  2112. for (uint32_t j=0; j < numCVOuts; ++j, ++i)
  2113. carla_copyFloats(outBuf[i], cvOutBuffer.getReadPointer(j), frames);
  2114. }
  2115. // put water events in carla buffer
  2116. {
  2117. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  2118. fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
  2119. midiBuffer.clear();
  2120. }
  2121. }
  2122. bool PatchbayGraph::run()
  2123. {
  2124. graph.reorderNowIfNeeded();
  2125. return true;
  2126. }
  2127. // -----------------------------------------------------------------------
  2128. // InternalGraph
  2129. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  2130. : fIsRack(false),
  2131. fNumAudioOuts(0),
  2132. fIsReady(false),
  2133. fRack(nullptr),
  2134. kEngine(engine) {}
  2135. EngineInternalGraph::~EngineInternalGraph() noexcept
  2136. {
  2137. CARLA_SAFE_ASSERT(! fIsReady);
  2138. CARLA_SAFE_ASSERT(fRack == nullptr);
  2139. }
  2140. void EngineInternalGraph::create(const uint32_t audioIns, const uint32_t audioOuts,
  2141. const uint32_t cvIns, const uint32_t cvOuts)
  2142. {
  2143. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  2144. if (fIsRack)
  2145. {
  2146. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  2147. fRack = new RackGraph(kEngine, audioIns, audioOuts);
  2148. }
  2149. else
  2150. {
  2151. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  2152. fPatchbay = new PatchbayGraph(kEngine, audioIns, audioOuts, cvIns, cvOuts);
  2153. }
  2154. fNumAudioOuts = audioOuts;
  2155. fIsReady = true;
  2156. }
  2157. void EngineInternalGraph::destroy() noexcept
  2158. {
  2159. if (! fIsReady)
  2160. {
  2161. CARLA_SAFE_ASSERT(fRack == nullptr);
  2162. return;
  2163. }
  2164. if (fIsRack)
  2165. {
  2166. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2167. delete fRack;
  2168. fRack = nullptr;
  2169. }
  2170. else
  2171. {
  2172. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2173. delete fPatchbay;
  2174. fPatchbay = nullptr;
  2175. }
  2176. fIsReady = false;
  2177. fNumAudioOuts = 0;
  2178. }
  2179. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  2180. {
  2181. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2182. if (fIsRack)
  2183. {
  2184. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2185. fRack->setBufferSize(bufferSize);
  2186. }
  2187. else
  2188. {
  2189. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2190. fPatchbay->setBufferSize(bufferSize);
  2191. }
  2192. }
  2193. void EngineInternalGraph::setSampleRate(const double sampleRate)
  2194. {
  2195. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2196. if (fIsRack)
  2197. {
  2198. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2199. }
  2200. else
  2201. {
  2202. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2203. fPatchbay->setSampleRate(sampleRate);
  2204. }
  2205. }
  2206. void EngineInternalGraph::setOffline(const bool offline)
  2207. {
  2208. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2209. if (fIsRack)
  2210. {
  2211. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2212. fRack->setOffline(offline);
  2213. }
  2214. else
  2215. {
  2216. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2217. fPatchbay->setOffline(offline);
  2218. }
  2219. }
  2220. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  2221. {
  2222. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  2223. return fRack;
  2224. }
  2225. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  2226. {
  2227. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  2228. return fPatchbay;
  2229. }
  2230. PatchbayGraph* EngineInternalGraph::getPatchbayGraphOrNull() const noexcept
  2231. {
  2232. return fIsRack ? nullptr : fPatchbay;
  2233. }
  2234. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  2235. {
  2236. if (fIsRack)
  2237. {
  2238. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2239. fRack->processHelper(data, inBuf, outBuf, frames);
  2240. }
  2241. else
  2242. {
  2243. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2244. fPatchbay->process(data, inBuf, outBuf, frames);
  2245. }
  2246. }
  2247. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  2248. {
  2249. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  2250. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2251. fRack->process(data, inBuf, outBuf, frames);
  2252. }
  2253. // -----------------------------------------------------------------------
  2254. // used for internal patchbay mode
  2255. void EngineInternalGraph::addPlugin(const CarlaPluginPtr plugin)
  2256. {
  2257. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2258. fPatchbay->addPlugin(plugin);
  2259. }
  2260. void EngineInternalGraph::replacePlugin(const CarlaPluginPtr oldPlugin, const CarlaPluginPtr newPlugin)
  2261. {
  2262. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2263. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  2264. }
  2265. void EngineInternalGraph::renamePlugin(const CarlaPluginPtr plugin, const char* const newName)
  2266. {
  2267. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2268. fPatchbay->renamePlugin(plugin, newName);
  2269. }
  2270. void EngineInternalGraph::switchPlugins(CarlaPluginPtr pluginA, CarlaPluginPtr pluginB)
  2271. {
  2272. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2273. fPatchbay->switchPlugins(pluginA, pluginB);
  2274. }
  2275. void EngineInternalGraph::removePlugin(const CarlaPluginPtr plugin)
  2276. {
  2277. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2278. fPatchbay->removePlugin(plugin);
  2279. }
  2280. void EngineInternalGraph::removeAllPlugins(const bool aboutToClose)
  2281. {
  2282. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2283. fPatchbay->removeAllPlugins(aboutToClose);
  2284. }
  2285. bool EngineInternalGraph::isUsingExternalHost() const noexcept
  2286. {
  2287. if (fIsRack)
  2288. return true;
  2289. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2290. return fPatchbay->usingExternalHost;
  2291. }
  2292. bool EngineInternalGraph::isUsingExternalOSC() const noexcept
  2293. {
  2294. if (fIsRack)
  2295. return true;
  2296. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2297. return fPatchbay->usingExternalOSC;
  2298. }
  2299. void EngineInternalGraph::setUsingExternalHost(const bool usingExternal) noexcept
  2300. {
  2301. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2302. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2303. fPatchbay->usingExternalHost = usingExternal;
  2304. }
  2305. void EngineInternalGraph::setUsingExternalOSC(const bool usingExternal) noexcept
  2306. {
  2307. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2308. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2309. fPatchbay->usingExternalOSC = usingExternal;
  2310. }
  2311. // -----------------------------------------------------------------------
  2312. // CarlaEngine Patchbay stuff
  2313. bool CarlaEngine::patchbayConnect(const bool external,
  2314. const uint groupA, const uint portA,
  2315. const uint groupB, const uint portB)
  2316. {
  2317. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2318. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2319. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  2320. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2321. {
  2322. RackGraph* const graph = pData->graph.getRackGraph();
  2323. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2324. return graph->connect(groupA, portA, groupB, portB);
  2325. }
  2326. else
  2327. {
  2328. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2329. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2330. return graph->connect(external, groupA, portA, groupB, portB);
  2331. }
  2332. }
  2333. bool CarlaEngine::patchbayDisconnect(const bool external, const uint connectionId)
  2334. {
  2335. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2336. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2337. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  2338. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2339. {
  2340. RackGraph* const graph = pData->graph.getRackGraph();
  2341. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2342. return graph->disconnect(connectionId);
  2343. }
  2344. else
  2345. {
  2346. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2347. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2348. return graph->disconnect(external, connectionId);
  2349. }
  2350. }
  2351. bool CarlaEngine::patchbaySetGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  2352. const uint groupId, const int x1, const int y1, const int x2, const int y2)
  2353. {
  2354. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  2355. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2356. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2357. carla_debug("CarlaEngine::patchbaySetGroupPos(%u, %i, %i, %i, %i)", groupId, x1, y1, x2, y2);
  2358. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2359. {
  2360. // we don't bother to save position in this case, there is only midi in/out
  2361. return true;
  2362. }
  2363. else
  2364. {
  2365. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2366. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2367. graph->setGroupPos(sendHost, sendOSC, external, groupId, x1, y1, x2, y2);
  2368. return true;
  2369. }
  2370. }
  2371. bool CarlaEngine::patchbayRefresh(const bool sendHost, const bool sendOSC, const bool external)
  2372. {
  2373. // subclasses should handle this
  2374. CARLA_SAFE_ASSERT_RETURN(! external, false);
  2375. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2376. {
  2377. // This is implemented in engine subclasses
  2378. setLastError("Unsupported operation");
  2379. return false;
  2380. }
  2381. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2382. {
  2383. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2384. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2385. graph->refresh(sendHost, sendOSC, external, "");
  2386. return true;
  2387. }
  2388. setLastError("Unsupported operation");
  2389. return false;
  2390. }
  2391. // -----------------------------------------------------------------------
  2392. // Patchbay stuff
  2393. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  2394. {
  2395. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  2396. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  2397. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2398. {
  2399. RackGraph* const graph = pData->graph.getRackGraph();
  2400. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2401. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  2402. return graph->getConnections();
  2403. }
  2404. else
  2405. {
  2406. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2407. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2408. return graph->getConnections(external);
  2409. }
  2410. return nullptr;
  2411. }
  2412. const CarlaEngine::PatchbayPosition* CarlaEngine::getPatchbayPositions(bool external, uint& count) const
  2413. {
  2414. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  2415. carla_debug("CarlaEngine::getPatchbayPositions(%s)", bool2str(external));
  2416. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2417. {
  2418. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2419. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2420. return graph->getPositions(external, count);
  2421. }
  2422. return nullptr;
  2423. }
  2424. void CarlaEngine::restorePatchbayConnection(const bool external,
  2425. const char* const sourcePort,
  2426. const char* const targetPort)
  2427. {
  2428. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  2429. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  2430. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  2431. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  2432. uint groupA, portA;
  2433. uint groupB, portB;
  2434. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2435. {
  2436. RackGraph* const graph = pData->graph.getRackGraph();
  2437. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2438. CARLA_SAFE_ASSERT_RETURN(external,);
  2439. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  2440. return;
  2441. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  2442. return;
  2443. graph->connect(groupA, portA, groupB, portB);
  2444. }
  2445. else
  2446. {
  2447. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2448. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2449. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  2450. return;
  2451. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  2452. return;
  2453. graph->connect(external, groupA, portA, groupB, portB);
  2454. }
  2455. }
  2456. bool CarlaEngine::restorePatchbayGroupPosition(const bool external, PatchbayPosition& ppos)
  2457. {
  2458. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2459. CARLA_SAFE_ASSERT_RETURN(ppos.name != nullptr && ppos.name[0] != '\0', false);
  2460. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2461. {
  2462. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2463. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2464. const char* const orig_name = ppos.name;
  2465. // strip client name prefix if present
  2466. if (ppos.pluginId >= 0)
  2467. {
  2468. if (const char* const rname2 = std::strstr(ppos.name, "."))
  2469. if (const char* const rname3 = std::strstr(rname2 + 1, "/"))
  2470. ppos.name = rname3 + 1;
  2471. }
  2472. uint groupId;
  2473. CARLA_SAFE_ASSERT_INT_RETURN(graph->getGroupFromName(external, ppos.name, groupId), external, false);
  2474. graph->setGroupPos(true, true, external, groupId, ppos.x1, ppos.y1, ppos.x2, ppos.y2);
  2475. return ppos.name != orig_name;
  2476. }
  2477. return false;
  2478. }
  2479. // -----------------------------------------------------------------------
  2480. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2481. {
  2482. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2483. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2484. RackGraph* const graph(pData->graph.getRackGraph());
  2485. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2486. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2487. switch (connectionType)
  2488. {
  2489. case kExternalGraphConnectionAudioIn1:
  2490. return graph->audioBuffers.connectedIn1.append(portId);
  2491. case kExternalGraphConnectionAudioIn2:
  2492. return graph->audioBuffers.connectedIn2.append(portId);
  2493. case kExternalGraphConnectionAudioOut1:
  2494. return graph->audioBuffers.connectedOut1.append(portId);
  2495. case kExternalGraphConnectionAudioOut2:
  2496. return graph->audioBuffers.connectedOut2.append(portId);
  2497. }
  2498. return false;
  2499. }
  2500. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2501. {
  2502. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2503. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2504. RackGraph* const graph(pData->graph.getRackGraph());
  2505. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2506. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2507. switch (connectionType)
  2508. {
  2509. case kExternalGraphConnectionAudioIn1:
  2510. return graph->audioBuffers.connectedIn1.removeOne(portId);
  2511. case kExternalGraphConnectionAudioIn2:
  2512. return graph->audioBuffers.connectedIn2.removeOne(portId);
  2513. case kExternalGraphConnectionAudioOut1:
  2514. return graph->audioBuffers.connectedOut1.removeOne(portId);
  2515. case kExternalGraphConnectionAudioOut2:
  2516. return graph->audioBuffers.connectedOut2.removeOne(portId);
  2517. }
  2518. return false;
  2519. }
  2520. // -----------------------------------------------------------------------
  2521. CARLA_BACKEND_END_NAMESPACE