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.

3073 lines
106KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 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.contains("x1"))
  1142. {
  1143. engine->callback(sendHost, sendOSC,
  1144. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1145. groupId,
  1146. node->properties.getWithDefault("x1", 0),
  1147. node->properties.getWithDefault("y1", 0),
  1148. node->properties.getWithDefault("x2", 0),
  1149. static_cast<float>(node->properties.getWithDefault("y2", 0)),
  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. CARLA_SAFE_ASSERT_RETURN(fPlugin.get() != nullptr,);
  1237. CarlaEngineClient* const client = fPlugin->getEngineClient();
  1238. CARLA_SAFE_ASSERT_RETURN(client != nullptr,);
  1239. carla_stdout("reconfigure called");
  1240. setPlayConfigDetails(client->getPortCount(kEnginePortTypeAudio, true),
  1241. client->getPortCount(kEnginePortTypeAudio, false),
  1242. client->getPortCount(kEnginePortTypeCV, true),
  1243. client->getPortCount(kEnginePortTypeCV, false),
  1244. client->getPortCount(kEnginePortTypeEvent, true),
  1245. client->getPortCount(kEnginePortTypeEvent, false),
  1246. getSampleRate(), getBlockSize());
  1247. }
  1248. void invalidatePlugin() noexcept
  1249. {
  1250. fPlugin.reset();
  1251. }
  1252. // -------------------------------------------------------------------
  1253. const String getName() const override
  1254. {
  1255. return fPlugin->getName();
  1256. }
  1257. void processBlockWithCV(AudioSampleBuffer& audio,
  1258. const AudioSampleBuffer& cvIn,
  1259. AudioSampleBuffer& cvOut,
  1260. MidiBuffer& midi) override
  1261. {
  1262. if (fPlugin.get() == nullptr || ! fPlugin->isEnabled() || ! fPlugin->tryLock(kEngine->isOffline()))
  1263. {
  1264. audio.clear();
  1265. cvOut.clear();
  1266. midi.clear();
  1267. return;
  1268. }
  1269. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  1270. {
  1271. EngineEvent* const engineEvents(port->fBuffer);
  1272. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1273. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1274. fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
  1275. }
  1276. midi.clear();
  1277. fPlugin->initBuffers();
  1278. const uint32_t numSamples = audio.getNumSamples();
  1279. const uint32_t numAudioChan = audio.getNumChannels();
  1280. const uint32_t numCVInChan = cvIn.getNumChannels();
  1281. const uint32_t numCVOutChan = cvOut.getNumChannels();
  1282. if (numAudioChan+numCVInChan+numCVOutChan == 0)
  1283. {
  1284. // nothing to process
  1285. fPlugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
  1286. }
  1287. else if (numAudioChan != 0)
  1288. {
  1289. // processing audio, include code for peaks
  1290. const uint32_t numChan2 = jmin(numAudioChan, 2U);
  1291. if (fPlugin->getAudioInCount() == 0)
  1292. audio.clear();
  1293. float* audioBuffers[numAudioChan];
  1294. float* cvOutBuffers[numCVOutChan];
  1295. const float* cvInBuffers[numCVInChan];
  1296. for (uint32_t i=0; i<numAudioChan; ++i)
  1297. audioBuffers[i] = audio.getWritePointer(i);
  1298. for (uint32_t i=0; i<numCVOutChan; ++i)
  1299. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1300. for (uint32_t i=0; i<numCVInChan; ++i)
  1301. cvInBuffers[i] = cvIn.getReadPointer(i);
  1302. float inPeaks[2] = { 0.0f };
  1303. float outPeaks[2] = { 0.0f };
  1304. for (uint32_t i=0, count=jmin(fPlugin->getAudioInCount(), numChan2); i<count; ++i)
  1305. inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1306. fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers,
  1307. cvInBuffers, cvOutBuffers,
  1308. numSamples);
  1309. for (uint32_t i=0, count=jmin(fPlugin->getAudioOutCount(), numChan2); i<count; ++i)
  1310. outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1311. kEngine->setPluginPeaksRT(fPlugin->getId(), inPeaks, outPeaks);
  1312. }
  1313. else
  1314. {
  1315. // processing CV only, skip audiopeaks
  1316. float* cvOutBuffers[numCVOutChan];
  1317. const float* cvInBuffers[numCVInChan];
  1318. for (uint32_t i=0; i<numCVOutChan; ++i)
  1319. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1320. for (uint32_t i=0; i<numCVInChan; ++i)
  1321. cvInBuffers[i] = cvIn.getReadPointer(i);
  1322. fPlugin->process(nullptr, nullptr,
  1323. cvInBuffers, cvOutBuffers,
  1324. numSamples);
  1325. }
  1326. midi.clear();
  1327. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  1328. {
  1329. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  1330. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1331. fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
  1332. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1333. }
  1334. fPlugin->unlock();
  1335. }
  1336. const String getInputChannelName(ChannelType t, uint i) const override
  1337. {
  1338. CarlaEngineClient* const client = fPlugin->getEngineClient();
  1339. switch (t)
  1340. {
  1341. case ChannelTypeAudio:
  1342. return client->getAudioPortName(true, i);
  1343. case ChannelTypeCV:
  1344. return client->getCVPortName(true, i);
  1345. case ChannelTypeMIDI:
  1346. return client->getEventPortName(true, i);
  1347. }
  1348. return String();
  1349. }
  1350. const String getOutputChannelName(ChannelType t, uint i) const override
  1351. {
  1352. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1353. switch (t)
  1354. {
  1355. case ChannelTypeAudio:
  1356. return client->getAudioPortName(false, i);
  1357. case ChannelTypeCV:
  1358. return client->getCVPortName(false, i);
  1359. case ChannelTypeMIDI:
  1360. return client->getEventPortName(false, i);
  1361. }
  1362. return String();
  1363. }
  1364. void prepareToPlay(double, int) override {}
  1365. void releaseResources() override {}
  1366. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  1367. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  1368. private:
  1369. CarlaEngine* const kEngine;
  1370. CarlaPluginPtr fPlugin;
  1371. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1372. };
  1373. // -----------------------------------------------------------------------
  1374. // Patchbay Graph
  1375. class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
  1376. {
  1377. public:
  1378. NamedAudioGraphIOProcessor(const IODeviceType iotype)
  1379. : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
  1380. inputNames(),
  1381. outputNames() {}
  1382. const String getInputChannelName (ChannelType, uint _index) const override
  1383. {
  1384. const int index = static_cast<int>(_index); // FIXME
  1385. if (index < inputNames.size())
  1386. return inputNames[index];
  1387. return String("Playback ") + String(index+1);
  1388. }
  1389. const String getOutputChannelName (ChannelType, uint _index) const override
  1390. {
  1391. const int index = static_cast<int>(_index); // FIXME
  1392. if (index < outputNames.size())
  1393. return outputNames[index];
  1394. return String("Capture ") + String(index+1);
  1395. }
  1396. void setNames(const bool setInputNames, const StringArray& names)
  1397. {
  1398. if (setInputNames)
  1399. inputNames = names;
  1400. else
  1401. outputNames = names;
  1402. }
  1403. private:
  1404. StringArray inputNames;
  1405. StringArray outputNames;
  1406. };
  1407. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine,
  1408. const uint32_t audioIns, const uint32_t audioOuts,
  1409. const uint32_t cvIns, const uint32_t cvOuts)
  1410. : CarlaThread("PatchbayReorderThread"),
  1411. connections(),
  1412. graph(),
  1413. audioBuffer(),
  1414. cvInBuffer(),
  1415. cvOutBuffer(),
  1416. midiBuffer(),
  1417. numAudioIns(carla_fixedValue(0U, 64U, audioIns)),
  1418. numAudioOuts(carla_fixedValue(0U, 64U, audioOuts)),
  1419. numCVIns(carla_fixedValue(0U, 32U, cvIns)),
  1420. numCVOuts(carla_fixedValue(0U, 32U, cvOuts)),
  1421. retCon(),
  1422. usingExternalHost(false),
  1423. usingExternalOSC(false),
  1424. extGraph(engine),
  1425. kEngine(engine)
  1426. {
  1427. const uint32_t bufferSize(engine->getBufferSize());
  1428. const double sampleRate(engine->getSampleRate());
  1429. graph.setPlayConfigDetails(numAudioIns, numAudioOuts,
  1430. numCVIns, numCVOuts,
  1431. 1, 1,
  1432. sampleRate, static_cast<int>(bufferSize));
  1433. graph.prepareToPlay(sampleRate, static_cast<int>(bufferSize));
  1434. audioBuffer.setSize(jmax(numAudioIns, numAudioOuts), bufferSize);
  1435. cvInBuffer.setSize(numCVIns, bufferSize);
  1436. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1437. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1438. midiBuffer.clear();
  1439. StringArray channelNames;
  1440. switch (numAudioIns)
  1441. {
  1442. case 2:
  1443. channelNames.add("Left");
  1444. channelNames.add("Right");
  1445. break;
  1446. case 3:
  1447. channelNames.add("Left");
  1448. channelNames.add("Right");
  1449. channelNames.add("Sidechain");
  1450. break;
  1451. }
  1452. if (numAudioIns != 0)
  1453. {
  1454. NamedAudioGraphIOProcessor* const proc(
  1455. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
  1456. proc->setNames(false, channelNames);
  1457. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1458. node->properties.set("isPlugin", false);
  1459. node->properties.set("isOutput", false);
  1460. node->properties.set("isAudio", true);
  1461. node->properties.set("isCV", false);
  1462. node->properties.set("isMIDI", false);
  1463. node->properties.set("isOSC", false);
  1464. }
  1465. if (numAudioOuts != 0)
  1466. {
  1467. NamedAudioGraphIOProcessor* const proc(
  1468. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
  1469. proc->setNames(true, channelNames);
  1470. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1471. node->properties.set("isPlugin", false);
  1472. node->properties.set("isOutput", false);
  1473. node->properties.set("isAudio", true);
  1474. node->properties.set("isCV", false);
  1475. node->properties.set("isMIDI", false);
  1476. node->properties.set("isOSC", false);
  1477. }
  1478. if (numCVIns != 0)
  1479. {
  1480. NamedAudioGraphIOProcessor* const proc(
  1481. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvInputNode));
  1482. // proc->setNames(false, channelNames);
  1483. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1484. node->properties.set("isPlugin", false);
  1485. node->properties.set("isOutput", false);
  1486. node->properties.set("isAudio", false);
  1487. node->properties.set("isCV", true);
  1488. node->properties.set("isMIDI", false);
  1489. node->properties.set("isOSC", false);
  1490. }
  1491. if (numCVOuts != 0)
  1492. {
  1493. NamedAudioGraphIOProcessor* const proc(
  1494. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvOutputNode));
  1495. // proc->setNames(true, channelNames);
  1496. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1497. node->properties.set("isPlugin", false);
  1498. node->properties.set("isOutput", false);
  1499. node->properties.set("isAudio", false);
  1500. node->properties.set("isCV", true);
  1501. node->properties.set("isMIDI", false);
  1502. node->properties.set("isOSC", false);
  1503. }
  1504. {
  1505. NamedAudioGraphIOProcessor* const proc(
  1506. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
  1507. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1508. node->properties.set("isPlugin", false);
  1509. node->properties.set("isOutput", false);
  1510. node->properties.set("isAudio", false);
  1511. node->properties.set("isCV", false);
  1512. node->properties.set("isMIDI", true);
  1513. node->properties.set("isOSC", false);
  1514. }
  1515. {
  1516. NamedAudioGraphIOProcessor* const proc(
  1517. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
  1518. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1519. node->properties.set("isPlugin", false);
  1520. node->properties.set("isOutput", true);
  1521. node->properties.set("isAudio", false);
  1522. node->properties.set("isCV", false);
  1523. node->properties.set("isMIDI", true);
  1524. node->properties.set("isOSC", false);
  1525. }
  1526. startThread();
  1527. }
  1528. PatchbayGraph::~PatchbayGraph()
  1529. {
  1530. stopThread(-1);
  1531. connections.clear();
  1532. extGraph.clear();
  1533. graph.releaseResources();
  1534. graph.clear();
  1535. audioBuffer.clear();
  1536. cvInBuffer.clear();
  1537. cvOutBuffer.clear();
  1538. }
  1539. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1540. {
  1541. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1542. graph.releaseResources();
  1543. graph.prepareToPlay(kEngine->getSampleRate(), static_cast<int>(bufferSize));
  1544. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSize);
  1545. cvInBuffer.setSize(numCVIns, bufferSize);
  1546. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1547. }
  1548. void PatchbayGraph::setSampleRate(const double sampleRate)
  1549. {
  1550. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1551. graph.releaseResources();
  1552. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1553. }
  1554. void PatchbayGraph::setOffline(const bool offline)
  1555. {
  1556. graph.setNonRealtime(offline);
  1557. }
  1558. void PatchbayGraph::addPlugin(const CarlaPluginPtr plugin)
  1559. {
  1560. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1561. carla_debug("PatchbayGraph::addPlugin(%p)", plugin.get());
  1562. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1563. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1564. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1565. const bool sendHost = !usingExternalHost;
  1566. const bool sendOSC = !usingExternalOSC;
  1567. plugin->setPatchbayNodeId(node->nodeId);
  1568. node->properties.set("isPlugin", true);
  1569. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  1570. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, static_cast<int>(plugin->getId()), instance);
  1571. }
  1572. void PatchbayGraph::replacePlugin(const CarlaPluginPtr oldPlugin, const CarlaPluginPtr newPlugin)
  1573. {
  1574. CARLA_SAFE_ASSERT_RETURN(oldPlugin.get() != nullptr,);
  1575. CARLA_SAFE_ASSERT_RETURN(newPlugin.get() != nullptr,);
  1576. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1577. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1578. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1579. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1580. const bool sendHost = !usingExternalHost;
  1581. const bool sendOSC = !usingExternalOSC;
  1582. disconnectInternalGroup(oldNode->nodeId);
  1583. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, oldNode->nodeId, oldNode->getProcessor());
  1584. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1585. graph.removeNode(oldNode->nodeId);
  1586. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1587. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1588. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1589. newPlugin->setPatchbayNodeId(node->nodeId);
  1590. node->properties.set("isPlugin", true);
  1591. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  1592. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, static_cast<int>(newPlugin->getId()), instance);
  1593. }
  1594. void PatchbayGraph::renamePlugin(const CarlaPluginPtr plugin, const char* const newName)
  1595. {
  1596. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1597. carla_debug("PatchbayGraph::renamePlugin(%p)", plugin.get(), newName);
  1598. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1599. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1600. const bool sendHost = !usingExternalHost;
  1601. const bool sendOSC = !usingExternalOSC;
  1602. kEngine->callback(sendHost, sendOSC,
  1603. ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED,
  1604. node->nodeId,
  1605. 0, 0, 0, 0.0f,
  1606. newName);
  1607. }
  1608. void PatchbayGraph::switchPlugins(CarlaPluginPtr pluginA, CarlaPluginPtr pluginB)
  1609. {
  1610. CARLA_SAFE_ASSERT_RETURN(pluginA.get() != nullptr,);
  1611. CARLA_SAFE_ASSERT_RETURN(pluginB.get() != nullptr,);
  1612. CARLA_SAFE_ASSERT_RETURN(pluginA != pluginB,);
  1613. CARLA_SAFE_ASSERT_RETURN(pluginA->getId() != pluginB->getId(),);
  1614. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(pluginA->getPatchbayNodeId()));
  1615. CARLA_SAFE_ASSERT_RETURN(nodeA != nullptr,);
  1616. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(pluginB->getPatchbayNodeId()));
  1617. CARLA_SAFE_ASSERT_RETURN(nodeB != nullptr,);
  1618. nodeA->properties.set("pluginId", static_cast<int>(pluginB->getId()));
  1619. nodeB->properties.set("pluginId", static_cast<int>(pluginA->getId()));
  1620. }
  1621. void PatchbayGraph::reconfigureForCV(const CarlaPluginPtr plugin, const uint portIndex, bool added)
  1622. {
  1623. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1624. carla_debug("PatchbayGraph::reconfigureForCV(%p, %u, %s)", plugin.get(), portIndex, bool2str(added));
  1625. AudioProcessorGraph::Node* const node = graph.getNodeForId(plugin->getPatchbayNodeId());
  1626. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1627. CarlaPluginInstance* const proc = dynamic_cast<CarlaPluginInstance*>(node->getProcessor());
  1628. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1629. const bool sendHost = !usingExternalHost;
  1630. const bool sendOSC = !usingExternalOSC;
  1631. const uint oldCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1632. // const uint oldCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1633. {
  1634. const CarlaRecursiveMutexLocker crml(graph.getCallbackLock());
  1635. proc->reconfigure();
  1636. graph.buildRenderingSequence();
  1637. }
  1638. const uint newCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1639. // const uint newCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1640. if (added)
  1641. {
  1642. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn > oldCvIn, newCvIn, oldCvIn,);
  1643. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut >= oldCvOut, newCvOut, oldCvOut,);
  1644. kEngine->callback(sendHost, sendOSC,
  1645. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1646. node->nodeId,
  1647. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1648. PATCHBAY_PORT_TYPE_CV|PATCHBAY_PORT_IS_INPUT,
  1649. 0, 0.0f,
  1650. proc->getInputChannelName(AudioProcessor::ChannelTypeCV, portIndex).toRawUTF8());
  1651. }
  1652. else
  1653. {
  1654. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn < oldCvIn, newCvIn, oldCvIn,);
  1655. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut <= oldCvOut, newCvOut, oldCvOut,);
  1656. kEngine->callback(sendHost, sendOSC,
  1657. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1658. node->nodeId,
  1659. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1660. 0, 0, 0.0f, nullptr);
  1661. }
  1662. }
  1663. void PatchbayGraph::removePlugin(const CarlaPluginPtr plugin)
  1664. {
  1665. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1666. carla_debug("PatchbayGraph::removePlugin(%p)", plugin.get());
  1667. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1668. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1669. const bool sendHost = !usingExternalHost;
  1670. const bool sendOSC = !usingExternalOSC;
  1671. disconnectInternalGroup(node->nodeId);
  1672. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1673. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1674. // Fix plugin Ids properties
  1675. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1676. {
  1677. const CarlaPluginPtr plugin2 = kEngine->getPlugin(i);
  1678. CARLA_SAFE_ASSERT_BREAK(plugin2.get() != nullptr);
  1679. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1680. {
  1681. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != water::var(-1));
  1682. node2->properties.set("pluginId", static_cast<int>(i-1));
  1683. }
  1684. }
  1685. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1686. }
  1687. void PatchbayGraph::removeAllPlugins()
  1688. {
  1689. carla_debug("PatchbayGraph::removeAllPlugins()");
  1690. const bool sendHost = !usingExternalHost;
  1691. const bool sendOSC = !usingExternalOSC;
  1692. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1693. {
  1694. const CarlaPluginPtr plugin = kEngine->getPlugin(i);
  1695. CARLA_SAFE_ASSERT_CONTINUE(plugin.get() != nullptr);
  1696. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1697. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1698. disconnectInternalGroup(node->nodeId);
  1699. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1700. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1701. graph.removeNode(node->nodeId);
  1702. }
  1703. }
  1704. bool PatchbayGraph::connect(const bool external,
  1705. const uint groupA, const uint portA, const uint groupB, const uint portB)
  1706. {
  1707. if (external)
  1708. return extGraph.connect(usingExternalHost, usingExternalOSC, groupA, portA, groupB, portB);
  1709. uint adjustedPortA = portA;
  1710. uint adjustedPortB = portB;
  1711. AudioProcessor::ChannelType channelType;
  1712. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1713. return false;
  1714. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1715. return false;
  1716. if (! graph.addConnection(channelType, groupA, adjustedPortA, groupB, adjustedPortB))
  1717. {
  1718. kEngine->setLastError("Failed from water");
  1719. return false;
  1720. }
  1721. ConnectionToId connectionToId;
  1722. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1723. char strBuf[STR_MAX+1];
  1724. strBuf[STR_MAX] = '\0';
  1725. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1726. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1727. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1728. connectionToId.id,
  1729. 0, 0, 0, 0.0f,
  1730. strBuf);
  1731. connections.list.append(connectionToId);
  1732. return true;
  1733. }
  1734. bool PatchbayGraph::disconnect(const bool external, const uint connectionId)
  1735. {
  1736. if (external)
  1737. return extGraph.disconnect(usingExternalHost, usingExternalOSC, connectionId);
  1738. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1739. {
  1740. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1741. const ConnectionToId& connectionToId(it.getValue(fallback));
  1742. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1743. if (connectionToId.id != connectionId)
  1744. continue;
  1745. uint adjustedPortA = connectionToId.portA;
  1746. uint adjustedPortB = connectionToId.portB;
  1747. AudioProcessor::ChannelType channelType;
  1748. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1749. return false;
  1750. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1751. return false;
  1752. if (! graph.removeConnection(channelType,
  1753. connectionToId.groupA, adjustedPortA,
  1754. connectionToId.groupB, adjustedPortB))
  1755. return false;
  1756. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1757. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1758. connectionToId.id,
  1759. 0, 0, 0, 0.0f,
  1760. nullptr);
  1761. connections.list.remove(it);
  1762. return true;
  1763. }
  1764. kEngine->setLastError("Failed to find connection");
  1765. return false;
  1766. }
  1767. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1768. {
  1769. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1770. {
  1771. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1772. const ConnectionToId& connectionToId(it.getValue(fallback));
  1773. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1774. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1775. continue;
  1776. /*
  1777. uint adjustedPortA = connectionToId.portA;
  1778. uint adjustedPortB = connectionToId.portB;
  1779. AudioProcessor::ChannelType channelType;
  1780. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1781. return false;
  1782. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1783. return false;
  1784. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1785. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1786. */
  1787. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1788. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1789. connectionToId.id,
  1790. 0, 0, 0, 0.0f,
  1791. nullptr);
  1792. connections.list.remove(it);
  1793. }
  1794. }
  1795. void PatchbayGraph::setGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  1796. uint groupId, int x1, int y1, int x2, int y2)
  1797. {
  1798. if (external)
  1799. return extGraph.setGroupPos(sendHost, sendOSC, groupId, x1, y1, x2, y2);
  1800. AudioProcessorGraph::Node* const node(graph.getNodeForId(groupId));
  1801. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1802. node->properties.set("x1", x1);
  1803. node->properties.set("y1", y1);
  1804. node->properties.set("x2", x2);
  1805. node->properties.set("y2", y2);
  1806. kEngine->callback(sendHost, sendOSC,
  1807. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1808. groupId, x1, y1, x2, static_cast<float>(y2),
  1809. nullptr);
  1810. }
  1811. void PatchbayGraph::refresh(const bool sendHost, const bool sendOSC, const bool external, const char* const deviceName)
  1812. {
  1813. if (external)
  1814. return extGraph.refresh(sendHost, sendOSC, deviceName);
  1815. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1816. connections.clear();
  1817. graph.removeIllegalConnections();
  1818. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1819. {
  1820. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1821. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1822. AudioProcessor* const proc(node->getProcessor());
  1823. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1824. int pluginId = -1;
  1825. // plugin node
  1826. if (node->properties.getWithDefault("isPlugin", false) == water::var(true))
  1827. pluginId = node->properties.getWithDefault("pluginId", -1);
  1828. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, pluginId, proc);
  1829. }
  1830. char strBuf[STR_MAX+1];
  1831. strBuf[STR_MAX] = '\0';
  1832. for (size_t i=0, count=graph.getNumConnections(); i<count; ++i)
  1833. {
  1834. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1835. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1836. const uint groupA = conn->sourceNodeId;
  1837. const uint groupB = conn->destNodeId;
  1838. uint portA = conn->sourceChannelIndex;
  1839. uint portB = conn->destChannelIndex;
  1840. switch (conn->channelType)
  1841. {
  1842. case AudioProcessor::ChannelTypeAudio:
  1843. portA += kAudioOutputPortOffset;
  1844. portB += kAudioInputPortOffset;
  1845. break;
  1846. case AudioProcessor::ChannelTypeCV:
  1847. portA += kCVOutputPortOffset;
  1848. portB += kCVInputPortOffset;
  1849. break;
  1850. case AudioProcessor::ChannelTypeMIDI:
  1851. portA += kMidiOutputPortOffset;
  1852. portB += kMidiInputPortOffset;
  1853. break;
  1854. }
  1855. ConnectionToId connectionToId;
  1856. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1857. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1858. kEngine->callback(sendHost, sendOSC,
  1859. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1860. connectionToId.id,
  1861. 0, 0, 0, 0.0f,
  1862. strBuf);
  1863. connections.list.append(connectionToId);
  1864. }
  1865. }
  1866. const char* const* PatchbayGraph::getConnections(const bool external) const
  1867. {
  1868. if (external)
  1869. return extGraph.getConnections();
  1870. if (connections.list.count() == 0)
  1871. return nullptr;
  1872. CarlaStringList connList;
  1873. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1874. {
  1875. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1876. const ConnectionToId& connectionToId(it.getValue(fallback));
  1877. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1878. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1879. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1880. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1881. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1882. AudioProcessor* const procA(nodeA->getProcessor());
  1883. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1884. AudioProcessor* const procB(nodeB->getProcessor());
  1885. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1886. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1887. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1888. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1889. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1890. connList.append(fullPortNameA.toRawUTF8());
  1891. connList.append(fullPortNameB.toRawUTF8());
  1892. }
  1893. if (connList.count() == 0)
  1894. return nullptr;
  1895. retCon = connList.toCharStringListPtr();
  1896. return retCon;
  1897. }
  1898. const CarlaEngine::PatchbayPosition* PatchbayGraph::getPositions(bool external, uint& count) const
  1899. {
  1900. CarlaEngine::PatchbayPosition* ret;
  1901. if (external)
  1902. {
  1903. try {
  1904. ret = new CarlaEngine::PatchbayPosition[kExternalGraphGroupMax];
  1905. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  1906. count = 0;
  1907. for (uint i=kExternalGraphGroupCarla; i<kExternalGraphGroupMax; ++i)
  1908. {
  1909. const PatchbayPosition& eppos(extGraph.positions[i]);
  1910. if (! eppos.active)
  1911. continue;
  1912. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  1913. switch (i)
  1914. {
  1915. case kExternalGraphGroupCarla:
  1916. ppos.name = "Carla";
  1917. break;
  1918. case kExternalGraphGroupAudioIn:
  1919. ppos.name = "AudioIn";
  1920. break;
  1921. case kExternalGraphGroupAudioOut:
  1922. ppos.name = "AudioOut";
  1923. break;
  1924. case kExternalGraphGroupMidiIn:
  1925. ppos.name = "MidiIn";
  1926. break;
  1927. case kExternalGraphGroupMidiOut:
  1928. ppos.name = "MidiOut";
  1929. break;
  1930. }
  1931. ppos.dealloc = false;
  1932. ppos.pluginId = -1;
  1933. ppos.x1 = eppos.x1;
  1934. ppos.y1 = eppos.y1;
  1935. ppos.x2 = eppos.x2;
  1936. ppos.y2 = eppos.y2;
  1937. }
  1938. return ret;
  1939. }
  1940. else
  1941. {
  1942. const int numNodes = graph.getNumNodes();
  1943. CARLA_SAFE_ASSERT_RETURN(numNodes > 0, nullptr);
  1944. try {
  1945. ret = new CarlaEngine::PatchbayPosition[numNodes];
  1946. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  1947. count = 0;
  1948. for (int i=numNodes; --i >= 0;)
  1949. {
  1950. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1951. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1952. if (! node->properties.contains("x1"))
  1953. continue;
  1954. AudioProcessor* const proc(node->getProcessor());
  1955. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1956. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  1957. ppos.name = carla_strdup(proc->getName().toRawUTF8());
  1958. ppos.dealloc = true;
  1959. ppos.pluginId = node->properties.getWithDefault("pluginId", -1);
  1960. ppos.x1 = node->properties.getWithDefault("x1", 0);
  1961. ppos.y1 = node->properties.getWithDefault("y1", 0);
  1962. ppos.x2 = node->properties.getWithDefault("x2", 0);
  1963. ppos.y2 = node->properties.getWithDefault("y2", 0);
  1964. }
  1965. return ret;
  1966. }
  1967. }
  1968. bool PatchbayGraph::getGroupFromName(bool external, const char* groupName, uint& groupId) const
  1969. {
  1970. if (external)
  1971. return extGraph.getGroupFromName(groupName, groupId);
  1972. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1973. {
  1974. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1975. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1976. AudioProcessor* const proc(node->getProcessor());
  1977. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1978. if (proc->getName() != groupName)
  1979. continue;
  1980. groupId = node->nodeId;
  1981. return true;
  1982. }
  1983. return false;
  1984. }
  1985. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  1986. {
  1987. if (external)
  1988. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  1989. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1990. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1991. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1992. {
  1993. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1994. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1995. AudioProcessor* const proc(node->getProcessor());
  1996. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1997. if (proc->getName() != groupName)
  1998. continue;
  1999. groupId = node->nodeId;
  2000. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); j<numInputs; ++j)
  2001. {
  2002. if (proc->getInputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  2003. continue;
  2004. portId = kAudioInputPortOffset+j;
  2005. return true;
  2006. }
  2007. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); j<numOutputs; ++j)
  2008. {
  2009. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  2010. continue;
  2011. portId = kAudioOutputPortOffset+j;
  2012. return true;
  2013. }
  2014. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); j<numInputs; ++j)
  2015. {
  2016. if (proc->getInputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  2017. continue;
  2018. portId = kCVInputPortOffset+j;
  2019. return true;
  2020. }
  2021. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); j<numOutputs; ++j)
  2022. {
  2023. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  2024. continue;
  2025. portId = kCVOutputPortOffset+j;
  2026. return true;
  2027. }
  2028. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); j<numInputs; ++j)
  2029. {
  2030. if (proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  2031. continue;
  2032. portId = kMidiInputPortOffset+j;
  2033. return true;
  2034. }
  2035. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); j<numOutputs; ++j)
  2036. {
  2037. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  2038. continue;
  2039. portId = kMidiOutputPortOffset+j;
  2040. return true;
  2041. }
  2042. }
  2043. return false;
  2044. }
  2045. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data,
  2046. const float* const* const inBuf,
  2047. float* const* const outBuf,
  2048. const uint32_t frames)
  2049. {
  2050. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  2051. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  2052. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  2053. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  2054. // put events in water buffer
  2055. {
  2056. midiBuffer.clear();
  2057. fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  2058. }
  2059. // set audio and cv buffer size, needed for water internals
  2060. if (! audioBuffer.setSizeRT(frames))
  2061. return;
  2062. if (! cvInBuffer.setSizeRT(frames))
  2063. return;
  2064. if (! cvOutBuffer.setSizeRT(frames))
  2065. return;
  2066. // put carla audio and cv in water buffer
  2067. {
  2068. uint32_t i=0;
  2069. for (; i < numAudioIns; ++i) {
  2070. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  2071. audioBuffer.copyFrom(i, 0, inBuf[i], frames);
  2072. }
  2073. for (uint32_t j=0; j < numCVIns; ++j, ++i) {
  2074. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  2075. cvInBuffer.copyFrom(j, 0, inBuf[i], frames);
  2076. }
  2077. // clear remaining channels
  2078. for (uint32_t j=numAudioIns, count=audioBuffer.getNumChannels(); j < count; ++j)
  2079. audioBuffer.clear(j, 0, frames);
  2080. for (uint32_t j=0; j < numCVOuts; ++j)
  2081. cvOutBuffer.clear(j, 0, frames);
  2082. }
  2083. // ready to go!
  2084. graph.processBlockWithCV(audioBuffer, cvInBuffer, cvOutBuffer, midiBuffer);
  2085. // put water audio and cv in carla buffer
  2086. {
  2087. uint32_t i=0;
  2088. for (; i < numAudioOuts; ++i)
  2089. carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
  2090. for (uint32_t j=0; j < numCVOuts; ++j, ++i)
  2091. carla_copyFloats(outBuf[i], cvOutBuffer.getReadPointer(j), frames);
  2092. }
  2093. // put water events in carla buffer
  2094. {
  2095. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  2096. fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
  2097. midiBuffer.clear();
  2098. }
  2099. }
  2100. void PatchbayGraph::run()
  2101. {
  2102. while (! shouldThreadExit())
  2103. {
  2104. carla_msleep(100);
  2105. graph.reorderNowIfNeeded();
  2106. }
  2107. }
  2108. // -----------------------------------------------------------------------
  2109. // InternalGraph
  2110. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  2111. : fIsRack(false),
  2112. fNumAudioOuts(0),
  2113. fIsReady(false),
  2114. fRack(nullptr),
  2115. kEngine(engine) {}
  2116. EngineInternalGraph::~EngineInternalGraph() noexcept
  2117. {
  2118. CARLA_SAFE_ASSERT(! fIsReady);
  2119. CARLA_SAFE_ASSERT(fRack == nullptr);
  2120. }
  2121. void EngineInternalGraph::create(const uint32_t audioIns, const uint32_t audioOuts,
  2122. const uint32_t cvIns, const uint32_t cvOuts)
  2123. {
  2124. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  2125. if (fIsRack)
  2126. {
  2127. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  2128. fRack = new RackGraph(kEngine, audioIns, audioOuts);
  2129. }
  2130. else
  2131. {
  2132. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  2133. fPatchbay = new PatchbayGraph(kEngine, audioIns, audioOuts, cvIns, cvOuts);
  2134. }
  2135. fNumAudioOuts = audioOuts;
  2136. fIsReady = true;
  2137. }
  2138. void EngineInternalGraph::destroy() noexcept
  2139. {
  2140. if (! fIsReady)
  2141. {
  2142. CARLA_SAFE_ASSERT(fRack == nullptr);
  2143. return;
  2144. }
  2145. if (fIsRack)
  2146. {
  2147. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2148. delete fRack;
  2149. fRack = nullptr;
  2150. }
  2151. else
  2152. {
  2153. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2154. delete fPatchbay;
  2155. fPatchbay = nullptr;
  2156. }
  2157. fIsReady = false;
  2158. fNumAudioOuts = 0;
  2159. }
  2160. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  2161. {
  2162. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2163. if (fIsRack)
  2164. {
  2165. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2166. fRack->setBufferSize(bufferSize);
  2167. }
  2168. else
  2169. {
  2170. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2171. fPatchbay->setBufferSize(bufferSize);
  2172. }
  2173. }
  2174. void EngineInternalGraph::setSampleRate(const double sampleRate)
  2175. {
  2176. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2177. if (fIsRack)
  2178. {
  2179. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2180. }
  2181. else
  2182. {
  2183. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2184. fPatchbay->setSampleRate(sampleRate);
  2185. }
  2186. }
  2187. void EngineInternalGraph::setOffline(const bool offline)
  2188. {
  2189. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2190. if (fIsRack)
  2191. {
  2192. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2193. fRack->setOffline(offline);
  2194. }
  2195. else
  2196. {
  2197. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2198. fPatchbay->setOffline(offline);
  2199. }
  2200. }
  2201. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  2202. {
  2203. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  2204. return fRack;
  2205. }
  2206. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  2207. {
  2208. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  2209. return fPatchbay;
  2210. }
  2211. PatchbayGraph* EngineInternalGraph::getPatchbayGraphOrNull() const noexcept
  2212. {
  2213. return fIsRack ? nullptr : fPatchbay;
  2214. }
  2215. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  2216. {
  2217. if (fIsRack)
  2218. {
  2219. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2220. fRack->processHelper(data, inBuf, outBuf, frames);
  2221. }
  2222. else
  2223. {
  2224. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2225. fPatchbay->process(data, inBuf, outBuf, frames);
  2226. }
  2227. }
  2228. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  2229. {
  2230. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  2231. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2232. fRack->process(data, inBuf, outBuf, frames);
  2233. }
  2234. // -----------------------------------------------------------------------
  2235. // used for internal patchbay mode
  2236. void EngineInternalGraph::addPlugin(const CarlaPluginPtr plugin)
  2237. {
  2238. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2239. fPatchbay->addPlugin(plugin);
  2240. }
  2241. void EngineInternalGraph::replacePlugin(const CarlaPluginPtr oldPlugin, const CarlaPluginPtr newPlugin)
  2242. {
  2243. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2244. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  2245. }
  2246. void EngineInternalGraph::renamePlugin(const CarlaPluginPtr plugin, const char* const newName)
  2247. {
  2248. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2249. fPatchbay->renamePlugin(plugin, newName);
  2250. }
  2251. void EngineInternalGraph::switchPlugins(CarlaPluginPtr pluginA, CarlaPluginPtr pluginB)
  2252. {
  2253. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2254. fPatchbay->switchPlugins(pluginA, pluginB);
  2255. }
  2256. void EngineInternalGraph::removePlugin(const CarlaPluginPtr plugin)
  2257. {
  2258. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2259. fPatchbay->removePlugin(plugin);
  2260. }
  2261. void EngineInternalGraph::removeAllPlugins()
  2262. {
  2263. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2264. fPatchbay->removeAllPlugins();
  2265. }
  2266. bool EngineInternalGraph::isUsingExternalHost() const noexcept
  2267. {
  2268. if (fIsRack)
  2269. return true;
  2270. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2271. return fPatchbay->usingExternalHost;
  2272. }
  2273. bool EngineInternalGraph::isUsingExternalOSC() const noexcept
  2274. {
  2275. if (fIsRack)
  2276. return true;
  2277. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2278. return fPatchbay->usingExternalOSC;
  2279. }
  2280. void EngineInternalGraph::setUsingExternalHost(const bool usingExternal) noexcept
  2281. {
  2282. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2283. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2284. fPatchbay->usingExternalHost = usingExternal;
  2285. }
  2286. void EngineInternalGraph::setUsingExternalOSC(const bool usingExternal) noexcept
  2287. {
  2288. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2289. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2290. fPatchbay->usingExternalOSC = usingExternal;
  2291. }
  2292. // -----------------------------------------------------------------------
  2293. // CarlaEngine Patchbay stuff
  2294. bool CarlaEngine::patchbayConnect(const bool external,
  2295. const uint groupA, const uint portA,
  2296. const uint groupB, const uint portB)
  2297. {
  2298. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2299. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2300. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  2301. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2302. {
  2303. RackGraph* const graph = pData->graph.getRackGraph();
  2304. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2305. return graph->connect(groupA, portA, groupB, portB);
  2306. }
  2307. else
  2308. {
  2309. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2310. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2311. return graph->connect(external, groupA, portA, groupB, portB);
  2312. }
  2313. }
  2314. bool CarlaEngine::patchbayDisconnect(const bool external, const uint connectionId)
  2315. {
  2316. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2317. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2318. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  2319. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2320. {
  2321. RackGraph* const graph = pData->graph.getRackGraph();
  2322. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2323. return graph->disconnect(connectionId);
  2324. }
  2325. else
  2326. {
  2327. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2328. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2329. return graph->disconnect(external, connectionId);
  2330. }
  2331. }
  2332. bool CarlaEngine::patchbaySetGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  2333. const uint groupId, const int x1, const int y1, const int x2, const int y2)
  2334. {
  2335. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  2336. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2337. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2338. carla_debug("CarlaEngine::patchbaySetGroupPos(%u, %i, %i, %i, %i)", groupId, x1, y1, x2, y2);
  2339. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2340. {
  2341. // we don't bother to save position in this case, there is only midi in/out
  2342. return true;
  2343. }
  2344. else
  2345. {
  2346. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2347. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2348. graph->setGroupPos(sendHost, sendOSC, external, groupId, x1, y1, x2, y2);
  2349. return true;
  2350. }
  2351. }
  2352. bool CarlaEngine::patchbayRefresh(const bool sendHost, const bool sendOSC, const bool external)
  2353. {
  2354. // subclasses should handle this
  2355. CARLA_SAFE_ASSERT_RETURN(! external, false);
  2356. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2357. {
  2358. // This is implemented in engine subclasses
  2359. setLastError("Unsupported operation");
  2360. return false;
  2361. }
  2362. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2363. {
  2364. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2365. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2366. graph->refresh(sendHost, sendOSC, external, "");
  2367. return true;
  2368. }
  2369. setLastError("Unsupported operation");
  2370. return false;
  2371. }
  2372. // -----------------------------------------------------------------------
  2373. // Patchbay stuff
  2374. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  2375. {
  2376. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  2377. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  2378. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2379. {
  2380. RackGraph* const graph = pData->graph.getRackGraph();
  2381. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2382. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  2383. return graph->getConnections();
  2384. }
  2385. else
  2386. {
  2387. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2388. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2389. return graph->getConnections(external);
  2390. }
  2391. return nullptr;
  2392. }
  2393. const CarlaEngine::PatchbayPosition* CarlaEngine::getPatchbayPositions(bool external, uint& count) const
  2394. {
  2395. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  2396. carla_debug("CarlaEngine::getPatchbayPositions(%s)", bool2str(external));
  2397. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2398. {
  2399. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2400. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2401. return graph->getPositions(external, count);
  2402. }
  2403. return nullptr;
  2404. }
  2405. void CarlaEngine::restorePatchbayConnection(const bool external,
  2406. const char* const sourcePort,
  2407. const char* const targetPort)
  2408. {
  2409. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  2410. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  2411. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  2412. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  2413. uint groupA, portA;
  2414. uint groupB, portB;
  2415. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2416. {
  2417. RackGraph* const graph = pData->graph.getRackGraph();
  2418. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2419. CARLA_SAFE_ASSERT_RETURN(external,);
  2420. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  2421. return;
  2422. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  2423. return;
  2424. graph->connect(groupA, portA, groupB, portB);
  2425. }
  2426. else
  2427. {
  2428. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2429. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2430. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  2431. return;
  2432. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  2433. return;
  2434. graph->connect(external, groupA, portA, groupB, portB);
  2435. }
  2436. }
  2437. bool CarlaEngine::restorePatchbayGroupPosition(const bool external, PatchbayPosition& ppos)
  2438. {
  2439. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2440. CARLA_SAFE_ASSERT_RETURN(ppos.name != nullptr && ppos.name[0] != '\0', false);
  2441. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2442. {
  2443. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2444. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2445. const char* const orig_name = ppos.name;
  2446. // strip client name prefix if present
  2447. if (ppos.pluginId >= 0)
  2448. {
  2449. if (const char* const rname2 = std::strstr(ppos.name, "."))
  2450. if (const char* const rname3 = std::strstr(rname2 + 1, "/"))
  2451. ppos.name = rname3 + 1;
  2452. }
  2453. uint groupId;
  2454. CARLA_SAFE_ASSERT_INT_RETURN(graph->getGroupFromName(external, ppos.name, groupId), external, false);
  2455. graph->setGroupPos(true, true, external, groupId, ppos.x1, ppos.y1, ppos.x2, ppos.y2);
  2456. return ppos.name != orig_name;
  2457. }
  2458. return false;
  2459. }
  2460. // -----------------------------------------------------------------------
  2461. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2462. {
  2463. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2464. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2465. RackGraph* const graph(pData->graph.getRackGraph());
  2466. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2467. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2468. switch (connectionType)
  2469. {
  2470. case kExternalGraphConnectionAudioIn1:
  2471. return graph->audioBuffers.connectedIn1.append(portId);
  2472. case kExternalGraphConnectionAudioIn2:
  2473. return graph->audioBuffers.connectedIn2.append(portId);
  2474. case kExternalGraphConnectionAudioOut1:
  2475. return graph->audioBuffers.connectedOut1.append(portId);
  2476. case kExternalGraphConnectionAudioOut2:
  2477. return graph->audioBuffers.connectedOut2.append(portId);
  2478. }
  2479. return false;
  2480. }
  2481. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2482. {
  2483. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2484. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2485. RackGraph* const graph(pData->graph.getRackGraph());
  2486. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2487. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2488. switch (connectionType)
  2489. {
  2490. case kExternalGraphConnectionAudioIn1:
  2491. return graph->audioBuffers.connectedIn1.removeOne(portId);
  2492. case kExternalGraphConnectionAudioIn2:
  2493. return graph->audioBuffers.connectedIn2.removeOne(portId);
  2494. case kExternalGraphConnectionAudioOut1:
  2495. return graph->audioBuffers.connectedOut1.removeOne(portId);
  2496. case kExternalGraphConnectionAudioOut2:
  2497. return graph->audioBuffers.connectedOut2.removeOne(portId);
  2498. }
  2499. return false;
  2500. }
  2501. // -----------------------------------------------------------------------
  2502. CARLA_BACKEND_END_NAMESPACE