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.

3035 lines
105KB

  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 float* inBuf[numInBufs];
  830. inBuf[0] = inBuf0;
  831. inBuf[1] = inBuf1;
  832. float* outBuf[numOutBufs];
  833. outBuf[0] = outBufReal[0];
  834. outBuf[1] = outBufReal[1];
  835. if (numInBufs > 2 || numOutBufs > 2)
  836. {
  837. carla_zeroFloats(dummyBuf, frames);
  838. for (uint32_t j=2; j<numInBufs; ++j)
  839. inBuf[j] = dummyBuf;
  840. for (uint32_t j=2; j<numOutBufs; ++j)
  841. outBuf[j] = dummyBuf;
  842. }
  843. // process
  844. plugin->initBuffers();
  845. plugin->process(inBuf, outBuf, nullptr, nullptr, frames);
  846. plugin->unlock();
  847. // if plugin has no audio inputs, add input buffer
  848. if (oldAudioInCount == 0)
  849. {
  850. carla_addFloats(outBufReal[0], inBuf0, frames);
  851. carla_addFloats(outBufReal[1], inBuf1, frames);
  852. }
  853. // if plugin only has 1 output, copy it to the 2nd
  854. if (oldAudioOutCount == 1)
  855. {
  856. carla_copyFloats(outBufReal[1], outBufReal[0], frames);
  857. }
  858. // set peaks
  859. {
  860. EnginePluginData& pluginData(data->plugins[i]);
  861. if (oldAudioInCount > 0)
  862. {
  863. pluginData.peaks[0] = carla_findMaxNormalizedFloat(inBuf0, frames);
  864. pluginData.peaks[1] = carla_findMaxNormalizedFloat(inBuf1, frames);
  865. }
  866. else
  867. {
  868. pluginData.peaks[0] = 0.0f;
  869. pluginData.peaks[1] = 0.0f;
  870. }
  871. if (oldAudioOutCount > 0)
  872. {
  873. pluginData.peaks[2] = carla_findMaxNormalizedFloat(outBufReal[0], frames);
  874. pluginData.peaks[3] = carla_findMaxNormalizedFloat(outBufReal[1], frames);
  875. }
  876. else
  877. {
  878. pluginData.peaks[2] = 0.0f;
  879. pluginData.peaks[3] = 0.0f;
  880. }
  881. }
  882. processed = true;
  883. }
  884. }
  885. void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  886. {
  887. CARLA_SAFE_ASSERT_RETURN(audioBuffers.outBuf[1] != nullptr,);
  888. const CarlaRecursiveMutexLocker _cml(audioBuffers.mutex);
  889. if (inBuf != nullptr && inputs > 0)
  890. {
  891. bool noConnections = true;
  892. // connect input buffers
  893. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  894. {
  895. const uint& port(it.getValue(0));
  896. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  897. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  898. if (noConnections)
  899. {
  900. carla_copyFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  901. noConnections = false;
  902. }
  903. else
  904. {
  905. carla_addFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  906. }
  907. }
  908. if (noConnections)
  909. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  910. noConnections = true;
  911. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  912. {
  913. const uint& port(it.getValue(0));
  914. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  915. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  916. if (noConnections)
  917. {
  918. carla_copyFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  919. noConnections = false;
  920. }
  921. else
  922. {
  923. carla_addFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  924. }
  925. }
  926. if (noConnections)
  927. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  928. }
  929. else
  930. {
  931. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  932. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  933. }
  934. carla_zeroFloats(audioBuffers.outBuf[0], frames);
  935. carla_zeroFloats(audioBuffers.outBuf[1], frames);
  936. // process
  937. process(data, const_cast<const float**>(audioBuffers.inBuf), audioBuffers.outBuf, frames);
  938. // connect output buffers
  939. if (audioBuffers.connectedOut1.count() != 0)
  940. {
  941. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  942. {
  943. const uint& port(it.getValue(0));
  944. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  945. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  946. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[0], frames);
  947. }
  948. }
  949. if (audioBuffers.connectedOut2.count() != 0)
  950. {
  951. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  952. {
  953. const uint& port(it.getValue(0));
  954. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  955. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  956. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[1], frames);
  957. }
  958. }
  959. }
  960. // -----------------------------------------------------------------------
  961. // Patchbay Graph stuff
  962. static const uint32_t kMaxPortsPerPlugin = 255;
  963. static const uint32_t kAudioInputPortOffset = kMaxPortsPerPlugin*1;
  964. static const uint32_t kAudioOutputPortOffset = kMaxPortsPerPlugin*2;
  965. static const uint32_t kCVInputPortOffset = kMaxPortsPerPlugin*3;
  966. static const uint32_t kCVOutputPortOffset = kMaxPortsPerPlugin*4;
  967. static const uint32_t kMidiInputPortOffset = kMaxPortsPerPlugin*5;
  968. static const uint32_t kMidiOutputPortOffset = kMaxPortsPerPlugin*6;
  969. static const uint32_t kMaxPortOffset = kMaxPortsPerPlugin*7;
  970. static inline
  971. bool adjustPatchbayPortIdForWater(AudioProcessor::ChannelType& channelType, uint& portId)
  972. {
  973. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, false);
  974. CARLA_SAFE_ASSERT_RETURN(portId < kMaxPortOffset, false);
  975. if (portId >= kMidiOutputPortOffset)
  976. {
  977. portId -= kMidiOutputPortOffset;
  978. channelType = AudioProcessor::ChannelTypeMIDI;
  979. return true;
  980. }
  981. if (portId >= kMidiInputPortOffset)
  982. {
  983. portId -= kMidiInputPortOffset;
  984. channelType = AudioProcessor::ChannelTypeMIDI;
  985. return true;
  986. }
  987. if (portId >= kCVOutputPortOffset)
  988. {
  989. portId -= kCVOutputPortOffset;
  990. channelType = AudioProcessor::ChannelTypeCV;
  991. return true;
  992. }
  993. if (portId >= kCVInputPortOffset)
  994. {
  995. portId -= kCVInputPortOffset;
  996. channelType = AudioProcessor::ChannelTypeCV;
  997. return true;
  998. }
  999. if (portId >= kAudioOutputPortOffset)
  1000. {
  1001. portId -= kAudioOutputPortOffset;
  1002. channelType = AudioProcessor::ChannelTypeAudio;
  1003. return true;
  1004. }
  1005. if (portId >= kAudioInputPortOffset)
  1006. {
  1007. portId -= kAudioInputPortOffset;
  1008. channelType = AudioProcessor::ChannelTypeAudio;
  1009. return true;
  1010. }
  1011. return false;
  1012. }
  1013. static inline
  1014. const String getProcessorFullPortName(AudioProcessor* const proc, const uint32_t portId)
  1015. {
  1016. CARLA_SAFE_ASSERT_RETURN(proc != nullptr, String());
  1017. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, String());
  1018. CARLA_SAFE_ASSERT_RETURN(portId < kMaxPortOffset, String());
  1019. String fullPortName(proc->getName());
  1020. /**/ if (portId >= kMidiOutputPortOffset)
  1021. {
  1022. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI) > 0, String());
  1023. fullPortName += ":" + proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI,
  1024. portId-kMidiOutputPortOffset);
  1025. }
  1026. else if (portId >= kMidiInputPortOffset)
  1027. {
  1028. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI) > 0, String());
  1029. fullPortName += ":" + proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI,
  1030. portId-kMidiInputPortOffset);
  1031. }
  1032. else if (portId >= kCVOutputPortOffset)
  1033. {
  1034. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV) > 0, String());
  1035. fullPortName += ":" + proc->getOutputChannelName(AudioProcessor::ChannelTypeCV,
  1036. portId-kCVOutputPortOffset);
  1037. }
  1038. else if (portId >= kCVInputPortOffset)
  1039. {
  1040. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV) > 0, String());
  1041. fullPortName += ":" + proc->getInputChannelName(AudioProcessor::ChannelTypeCV,
  1042. portId-kCVInputPortOffset);
  1043. }
  1044. else if (portId >= kAudioOutputPortOffset)
  1045. {
  1046. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio) > 0, String());
  1047. fullPortName += ":" + proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio,
  1048. portId-kAudioOutputPortOffset);
  1049. }
  1050. else if (portId >= kAudioInputPortOffset)
  1051. {
  1052. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio) > 0, String());
  1053. fullPortName += ":" + proc->getInputChannelName(AudioProcessor::ChannelTypeAudio,
  1054. portId-kAudioInputPortOffset);
  1055. }
  1056. else
  1057. {
  1058. return String();
  1059. }
  1060. return fullPortName;
  1061. }
  1062. static inline
  1063. void addNodeToPatchbay(const bool sendHost, const bool sendOSC, CarlaEngine* const engine,
  1064. AudioProcessorGraph::Node* const node, const int pluginId, const AudioProcessor* const proc)
  1065. {
  1066. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1067. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1068. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1069. const uint groupId = node->nodeId;
  1070. engine->callback(sendHost, sendOSC,
  1071. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  1072. groupId,
  1073. pluginId >= 0 ? PATCHBAY_ICON_PLUGIN : PATCHBAY_ICON_HARDWARE,
  1074. pluginId,
  1075. 0, 0.0f,
  1076. proc->getName().toRawUTF8());
  1077. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); i<numInputs; ++i)
  1078. {
  1079. engine->callback(sendHost, sendOSC,
  1080. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1081. groupId,
  1082. static_cast<int>(kAudioInputPortOffset+i),
  1083. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT,
  1084. 0, 0.0f,
  1085. proc->getInputChannelName(AudioProcessor::ChannelTypeAudio, i).toRawUTF8());
  1086. }
  1087. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); i<numOutputs; ++i)
  1088. {
  1089. engine->callback(sendHost, sendOSC,
  1090. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1091. groupId,
  1092. static_cast<int>(kAudioOutputPortOffset+i),
  1093. PATCHBAY_PORT_TYPE_AUDIO,
  1094. 0, 0.0f,
  1095. proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio, i).toRawUTF8());
  1096. }
  1097. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); i<numInputs; ++i)
  1098. {
  1099. engine->callback(sendHost, sendOSC,
  1100. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1101. groupId,
  1102. static_cast<int>(kCVInputPortOffset+i),
  1103. PATCHBAY_PORT_TYPE_CV|PATCHBAY_PORT_IS_INPUT,
  1104. 0, 0.0f,
  1105. proc->getInputChannelName(AudioProcessor::ChannelTypeCV, i).toRawUTF8());
  1106. }
  1107. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); i<numOutputs; ++i)
  1108. {
  1109. engine->callback(sendHost, sendOSC,
  1110. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1111. groupId,
  1112. static_cast<int>(kCVOutputPortOffset+i),
  1113. PATCHBAY_PORT_TYPE_CV,
  1114. 0, 0.0f,
  1115. proc->getOutputChannelName(AudioProcessor::ChannelTypeCV, i).toRawUTF8());
  1116. }
  1117. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); i<numInputs; ++i)
  1118. {
  1119. engine->callback(sendHost, sendOSC,
  1120. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1121. groupId,
  1122. static_cast<int>(kMidiInputPortOffset+i),
  1123. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT,
  1124. 0, 0.0f,
  1125. proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI, i).toRawUTF8());
  1126. }
  1127. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); i<numOutputs; ++i)
  1128. {
  1129. engine->callback(sendHost, sendOSC,
  1130. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1131. groupId,
  1132. static_cast<int>(kMidiOutputPortOffset+i),
  1133. PATCHBAY_PORT_TYPE_MIDI,
  1134. 0, 0.0f,
  1135. proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI, i).toRawUTF8());
  1136. }
  1137. if (node->properties.contains("x1"))
  1138. {
  1139. engine->callback(sendHost, sendOSC,
  1140. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1141. groupId,
  1142. node->properties.getWithDefault("x1", 0),
  1143. node->properties.getWithDefault("y1", 0),
  1144. node->properties.getWithDefault("x2", 0),
  1145. static_cast<float>(node->properties.getWithDefault("y2", 0)),
  1146. nullptr);
  1147. }
  1148. }
  1149. static inline
  1150. void removeNodeFromPatchbay(const bool sendHost, const bool sendOSC, CarlaEngine* const engine,
  1151. const uint32_t groupId, const AudioProcessor* const proc)
  1152. {
  1153. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1154. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1155. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); i<numInputs; ++i)
  1156. {
  1157. engine->callback(sendHost, sendOSC,
  1158. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1159. groupId,
  1160. static_cast<int>(kAudioInputPortOffset+i),
  1161. 0, 0, 0.0f, nullptr);
  1162. }
  1163. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); i<numOutputs; ++i)
  1164. {
  1165. engine->callback(sendHost, sendOSC,
  1166. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1167. groupId,
  1168. static_cast<int>(kAudioOutputPortOffset+i),
  1169. 0, 0, 0.0f,
  1170. nullptr);
  1171. }
  1172. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); i<numInputs; ++i)
  1173. {
  1174. engine->callback(sendHost, sendOSC,
  1175. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1176. groupId,
  1177. static_cast<int>(kCVInputPortOffset+i),
  1178. 0, 0, 0.0f, nullptr);
  1179. }
  1180. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); i<numOutputs; ++i)
  1181. {
  1182. engine->callback(sendHost, sendOSC,
  1183. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1184. groupId,
  1185. static_cast<int>(kCVOutputPortOffset+i),
  1186. 0, 0, 0.0f,
  1187. nullptr);
  1188. }
  1189. for (uint i=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); i<numInputs; ++i)
  1190. {
  1191. engine->callback(sendHost, sendOSC,
  1192. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1193. groupId,
  1194. static_cast<int>(kMidiInputPortOffset+i),
  1195. 0, 0, 0.0f, nullptr);
  1196. }
  1197. for (uint i=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); i<numOutputs; ++i)
  1198. {
  1199. engine->callback(sendHost, sendOSC,
  1200. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1201. groupId,
  1202. static_cast<int>(kMidiOutputPortOffset+i),
  1203. 0, 0, 0.0f, nullptr);
  1204. }
  1205. engine->callback(sendHost, sendOSC,
  1206. ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED,
  1207. groupId,
  1208. 0, 0, 0, 0.0f, nullptr);
  1209. }
  1210. // -----------------------------------------------------------------------
  1211. class CarlaPluginInstance : public AudioProcessor
  1212. {
  1213. public:
  1214. CarlaPluginInstance(CarlaEngine* const engine, const CarlaPluginPtr plugin)
  1215. : kEngine(engine),
  1216. fPlugin(plugin)
  1217. {
  1218. CarlaEngineClient* const client = plugin->getEngineClient();
  1219. setPlayConfigDetails(client->getPortCount(kEnginePortTypeAudio, true),
  1220. client->getPortCount(kEnginePortTypeAudio, false),
  1221. client->getPortCount(kEnginePortTypeCV, true),
  1222. client->getPortCount(kEnginePortTypeCV, false),
  1223. client->getPortCount(kEnginePortTypeEvent, true),
  1224. client->getPortCount(kEnginePortTypeEvent, false),
  1225. getSampleRate(), getBlockSize());
  1226. }
  1227. ~CarlaPluginInstance() override
  1228. {
  1229. }
  1230. void reconfigure() override
  1231. {
  1232. CARLA_SAFE_ASSERT_RETURN(fPlugin.get() != nullptr,);
  1233. CarlaEngineClient* const client = fPlugin->getEngineClient();
  1234. CARLA_SAFE_ASSERT_RETURN(client != nullptr,);
  1235. carla_stdout("reconfigure called");
  1236. setPlayConfigDetails(client->getPortCount(kEnginePortTypeAudio, true),
  1237. client->getPortCount(kEnginePortTypeAudio, false),
  1238. client->getPortCount(kEnginePortTypeCV, true),
  1239. client->getPortCount(kEnginePortTypeCV, false),
  1240. client->getPortCount(kEnginePortTypeEvent, true),
  1241. client->getPortCount(kEnginePortTypeEvent, false),
  1242. getSampleRate(), getBlockSize());
  1243. }
  1244. void invalidatePlugin() noexcept
  1245. {
  1246. fPlugin.reset();
  1247. }
  1248. // -------------------------------------------------------------------
  1249. const String getName() const override
  1250. {
  1251. return fPlugin->getName();
  1252. }
  1253. void processBlockWithCV(AudioSampleBuffer& audio,
  1254. const AudioSampleBuffer& cvIn,
  1255. AudioSampleBuffer& cvOut,
  1256. MidiBuffer& midi) override
  1257. {
  1258. if (fPlugin.get() == nullptr || ! fPlugin->isEnabled() || ! fPlugin->tryLock(kEngine->isOffline()))
  1259. {
  1260. audio.clear();
  1261. cvOut.clear();
  1262. midi.clear();
  1263. return;
  1264. }
  1265. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  1266. {
  1267. EngineEvent* const engineEvents(port->fBuffer);
  1268. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1269. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1270. fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
  1271. }
  1272. midi.clear();
  1273. fPlugin->initBuffers();
  1274. const uint32_t numSamples = audio.getNumSamples();
  1275. const uint32_t numAudioChan = audio.getNumChannels();
  1276. const uint32_t numCVInChan = cvIn.getNumChannels();
  1277. const uint32_t numCVOutChan = cvOut.getNumChannels();
  1278. if (numAudioChan+numCVInChan+numCVOutChan == 0)
  1279. {
  1280. // nothing to process
  1281. fPlugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
  1282. }
  1283. else if (numAudioChan != 0)
  1284. {
  1285. // processing audio, include code for peaks
  1286. const uint32_t numChan2 = jmin(numAudioChan, 2U);
  1287. if (fPlugin->getAudioInCount() == 0)
  1288. audio.clear();
  1289. float* audioBuffers[numAudioChan];
  1290. float* cvOutBuffers[numCVOutChan];
  1291. const float* cvInBuffers[numCVInChan];
  1292. for (uint32_t i=0; i<numAudioChan; ++i)
  1293. audioBuffers[i] = audio.getWritePointer(i);
  1294. for (uint32_t i=0; i<numCVOutChan; ++i)
  1295. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1296. for (uint32_t i=0; i<numCVInChan; ++i)
  1297. cvInBuffers[i] = cvIn.getReadPointer(i);
  1298. float inPeaks[2] = { 0.0f };
  1299. float outPeaks[2] = { 0.0f };
  1300. for (uint32_t i=0, count=jmin(fPlugin->getAudioInCount(), numChan2); i<count; ++i)
  1301. inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1302. fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers,
  1303. cvInBuffers, cvOutBuffers,
  1304. numSamples);
  1305. for (uint32_t i=0, count=jmin(fPlugin->getAudioOutCount(), numChan2); i<count; ++i)
  1306. outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1307. kEngine->setPluginPeaksRT(fPlugin->getId(), inPeaks, outPeaks);
  1308. }
  1309. else
  1310. {
  1311. // processing CV only, skip audiopeaks
  1312. float* cvOutBuffers[numCVOutChan];
  1313. const float* cvInBuffers[numCVInChan];
  1314. for (uint32_t i=0; i<numCVOutChan; ++i)
  1315. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1316. for (uint32_t i=0; i<numCVInChan; ++i)
  1317. cvInBuffers[i] = cvIn.getReadPointer(i);
  1318. fPlugin->process(nullptr, nullptr,
  1319. cvInBuffers, cvOutBuffers,
  1320. numSamples);
  1321. }
  1322. midi.clear();
  1323. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  1324. {
  1325. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  1326. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1327. fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
  1328. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1329. }
  1330. fPlugin->unlock();
  1331. }
  1332. const String getInputChannelName(ChannelType t, uint i) const override
  1333. {
  1334. CarlaEngineClient* const client = fPlugin->getEngineClient();
  1335. switch (t)
  1336. {
  1337. case ChannelTypeAudio:
  1338. return client->getAudioPortName(true, i);
  1339. case ChannelTypeCV:
  1340. return client->getCVPortName(true, i);
  1341. case ChannelTypeMIDI:
  1342. return client->getEventPortName(true, i);
  1343. }
  1344. return String();
  1345. }
  1346. const String getOutputChannelName(ChannelType t, uint i) const override
  1347. {
  1348. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1349. switch (t)
  1350. {
  1351. case ChannelTypeAudio:
  1352. return client->getAudioPortName(false, i);
  1353. case ChannelTypeCV:
  1354. return client->getCVPortName(false, i);
  1355. case ChannelTypeMIDI:
  1356. return client->getEventPortName(false, i);
  1357. }
  1358. return String();
  1359. }
  1360. void prepareToPlay(double, int) override {}
  1361. void releaseResources() override {}
  1362. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  1363. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  1364. private:
  1365. CarlaEngine* const kEngine;
  1366. CarlaPluginPtr fPlugin;
  1367. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1368. };
  1369. // -----------------------------------------------------------------------
  1370. // Patchbay Graph
  1371. class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
  1372. {
  1373. public:
  1374. NamedAudioGraphIOProcessor(const IODeviceType iotype)
  1375. : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
  1376. inputNames(),
  1377. outputNames() {}
  1378. const String getInputChannelName (ChannelType, uint _index) const override
  1379. {
  1380. const int index = static_cast<int>(_index); // FIXME
  1381. if (index < inputNames.size())
  1382. return inputNames[index];
  1383. return String("Playback ") + String(index+1);
  1384. }
  1385. const String getOutputChannelName (ChannelType, uint _index) const override
  1386. {
  1387. const int index = static_cast<int>(_index); // FIXME
  1388. if (index < outputNames.size())
  1389. return outputNames[index];
  1390. return String("Capture ") + String(index+1);
  1391. }
  1392. void setNames(const bool setInputNames, const StringArray& names)
  1393. {
  1394. if (setInputNames)
  1395. inputNames = names;
  1396. else
  1397. outputNames = names;
  1398. }
  1399. private:
  1400. StringArray inputNames;
  1401. StringArray outputNames;
  1402. };
  1403. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine,
  1404. const uint32_t audioIns, const uint32_t audioOuts,
  1405. const uint32_t cvIns, const uint32_t cvOuts)
  1406. : CarlaThread("PatchbayReorderThread"),
  1407. connections(),
  1408. graph(),
  1409. audioBuffer(),
  1410. cvInBuffer(),
  1411. cvOutBuffer(),
  1412. midiBuffer(),
  1413. numAudioIns(carla_fixedValue(0U, 64U, audioIns)),
  1414. numAudioOuts(carla_fixedValue(0U, 64U, audioOuts)),
  1415. numCVIns(carla_fixedValue(0U, 8U, cvIns)),
  1416. numCVOuts(carla_fixedValue(0U, 8U, cvOuts)),
  1417. retCon(),
  1418. usingExternalHost(false),
  1419. usingExternalOSC(false),
  1420. extGraph(engine),
  1421. kEngine(engine)
  1422. {
  1423. const uint32_t bufferSize(engine->getBufferSize());
  1424. const double sampleRate(engine->getSampleRate());
  1425. graph.setPlayConfigDetails(numAudioIns, numAudioOuts,
  1426. numCVIns, numCVOuts,
  1427. 1, 1,
  1428. sampleRate, static_cast<int>(bufferSize));
  1429. graph.prepareToPlay(sampleRate, static_cast<int>(bufferSize));
  1430. audioBuffer.setSize(jmax(numAudioIns, numAudioOuts), bufferSize);
  1431. cvInBuffer.setSize(numCVIns, bufferSize);
  1432. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1433. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1434. midiBuffer.clear();
  1435. StringArray channelNames;
  1436. switch (numAudioIns)
  1437. {
  1438. case 2:
  1439. channelNames.add("Left");
  1440. channelNames.add("Right");
  1441. break;
  1442. case 3:
  1443. channelNames.add("Left");
  1444. channelNames.add("Right");
  1445. channelNames.add("Sidechain");
  1446. break;
  1447. }
  1448. if (numAudioIns != 0)
  1449. {
  1450. NamedAudioGraphIOProcessor* const proc(
  1451. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
  1452. proc->setNames(false, channelNames);
  1453. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1454. node->properties.set("isPlugin", false);
  1455. node->properties.set("isOutput", false);
  1456. node->properties.set("isAudio", true);
  1457. node->properties.set("isCV", false);
  1458. node->properties.set("isMIDI", false);
  1459. node->properties.set("isOSC", false);
  1460. }
  1461. if (numAudioOuts != 0)
  1462. {
  1463. NamedAudioGraphIOProcessor* const proc(
  1464. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
  1465. proc->setNames(true, channelNames);
  1466. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1467. node->properties.set("isPlugin", false);
  1468. node->properties.set("isOutput", false);
  1469. node->properties.set("isAudio", true);
  1470. node->properties.set("isCV", false);
  1471. node->properties.set("isMIDI", false);
  1472. node->properties.set("isOSC", false);
  1473. }
  1474. if (numCVIns != 0)
  1475. {
  1476. NamedAudioGraphIOProcessor* const proc(
  1477. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvInputNode));
  1478. // proc->setNames(false, channelNames);
  1479. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1480. node->properties.set("isPlugin", false);
  1481. node->properties.set("isOutput", false);
  1482. node->properties.set("isAudio", false);
  1483. node->properties.set("isCV", true);
  1484. node->properties.set("isMIDI", false);
  1485. node->properties.set("isOSC", false);
  1486. }
  1487. if (numCVOuts != 0)
  1488. {
  1489. NamedAudioGraphIOProcessor* const proc(
  1490. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvOutputNode));
  1491. // proc->setNames(true, channelNames);
  1492. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1493. node->properties.set("isPlugin", false);
  1494. node->properties.set("isOutput", false);
  1495. node->properties.set("isAudio", false);
  1496. node->properties.set("isCV", true);
  1497. node->properties.set("isMIDI", false);
  1498. node->properties.set("isOSC", false);
  1499. }
  1500. {
  1501. NamedAudioGraphIOProcessor* const proc(
  1502. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
  1503. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1504. node->properties.set("isPlugin", false);
  1505. node->properties.set("isOutput", false);
  1506. node->properties.set("isAudio", false);
  1507. node->properties.set("isCV", false);
  1508. node->properties.set("isMIDI", true);
  1509. node->properties.set("isOSC", false);
  1510. }
  1511. {
  1512. NamedAudioGraphIOProcessor* const proc(
  1513. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
  1514. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1515. node->properties.set("isPlugin", false);
  1516. node->properties.set("isOutput", true);
  1517. node->properties.set("isAudio", false);
  1518. node->properties.set("isCV", false);
  1519. node->properties.set("isMIDI", true);
  1520. node->properties.set("isOSC", false);
  1521. }
  1522. startThread();
  1523. }
  1524. PatchbayGraph::~PatchbayGraph()
  1525. {
  1526. stopThread(-1);
  1527. connections.clear();
  1528. extGraph.clear();
  1529. graph.releaseResources();
  1530. graph.clear();
  1531. audioBuffer.clear();
  1532. cvInBuffer.clear();
  1533. cvOutBuffer.clear();
  1534. }
  1535. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1536. {
  1537. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1538. graph.releaseResources();
  1539. graph.prepareToPlay(kEngine->getSampleRate(), static_cast<int>(bufferSize));
  1540. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSize);
  1541. cvInBuffer.setSize(numCVIns, bufferSize);
  1542. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1543. }
  1544. void PatchbayGraph::setSampleRate(const double sampleRate)
  1545. {
  1546. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1547. graph.releaseResources();
  1548. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1549. }
  1550. void PatchbayGraph::setOffline(const bool offline)
  1551. {
  1552. graph.setNonRealtime(offline);
  1553. }
  1554. void PatchbayGraph::addPlugin(const CarlaPluginPtr plugin)
  1555. {
  1556. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1557. carla_debug("PatchbayGraph::addPlugin(%p)", plugin.get());
  1558. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1559. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1560. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1561. const bool sendHost = !usingExternalHost;
  1562. const bool sendOSC = !usingExternalOSC;
  1563. plugin->setPatchbayNodeId(node->nodeId);
  1564. node->properties.set("isPlugin", true);
  1565. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  1566. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, static_cast<int>(plugin->getId()), instance);
  1567. }
  1568. void PatchbayGraph::replacePlugin(const CarlaPluginPtr oldPlugin, const CarlaPluginPtr newPlugin)
  1569. {
  1570. CARLA_SAFE_ASSERT_RETURN(oldPlugin.get() != nullptr,);
  1571. CARLA_SAFE_ASSERT_RETURN(newPlugin.get() != nullptr,);
  1572. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1573. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1574. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1575. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1576. const bool sendHost = !usingExternalHost;
  1577. const bool sendOSC = !usingExternalOSC;
  1578. disconnectInternalGroup(oldNode->nodeId);
  1579. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, oldNode->nodeId, oldNode->getProcessor());
  1580. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1581. graph.removeNode(oldNode->nodeId);
  1582. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1583. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1584. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1585. newPlugin->setPatchbayNodeId(node->nodeId);
  1586. node->properties.set("isPlugin", true);
  1587. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  1588. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, static_cast<int>(newPlugin->getId()), instance);
  1589. }
  1590. void PatchbayGraph::renamePlugin(const CarlaPluginPtr plugin, const char* const newName)
  1591. {
  1592. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1593. carla_debug("PatchbayGraph::renamePlugin(%p)", plugin.get(), newName);
  1594. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1595. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1596. const bool sendHost = !usingExternalHost;
  1597. const bool sendOSC = !usingExternalOSC;
  1598. kEngine->callback(sendHost, sendOSC,
  1599. ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED,
  1600. node->nodeId,
  1601. 0, 0, 0, 0.0f,
  1602. newName);
  1603. }
  1604. void PatchbayGraph::reconfigureForCV(const CarlaPluginPtr plugin, const uint portIndex, bool added)
  1605. {
  1606. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1607. carla_debug("PatchbayGraph::reconfigureForCV(%p, %u, %s)", plugin.get(), portIndex, bool2str(added));
  1608. AudioProcessorGraph::Node* const node = graph.getNodeForId(plugin->getPatchbayNodeId());
  1609. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1610. CarlaPluginInstance* const proc = dynamic_cast<CarlaPluginInstance*>(node->getProcessor());
  1611. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1612. const bool sendHost = !usingExternalHost;
  1613. const bool sendOSC = !usingExternalOSC;
  1614. const uint oldCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1615. // const uint oldCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1616. {
  1617. const CarlaRecursiveMutexLocker crml(graph.getCallbackLock());
  1618. proc->reconfigure();
  1619. graph.buildRenderingSequence();
  1620. }
  1621. const uint newCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1622. // const uint newCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1623. if (added)
  1624. {
  1625. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn > oldCvIn, newCvIn, oldCvIn,);
  1626. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut >= oldCvOut, newCvOut, oldCvOut,);
  1627. kEngine->callback(sendHost, sendOSC,
  1628. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1629. node->nodeId,
  1630. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1631. PATCHBAY_PORT_TYPE_CV|PATCHBAY_PORT_IS_INPUT,
  1632. 0, 0.0f,
  1633. proc->getInputChannelName(AudioProcessor::ChannelTypeCV, portIndex).toRawUTF8());
  1634. }
  1635. else
  1636. {
  1637. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn < oldCvIn, newCvIn, oldCvIn,);
  1638. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut <= oldCvOut, newCvOut, oldCvOut,);
  1639. kEngine->callback(sendHost, sendOSC,
  1640. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1641. node->nodeId,
  1642. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1643. 0, 0, 0.0f, nullptr);
  1644. }
  1645. }
  1646. void PatchbayGraph::removePlugin(const CarlaPluginPtr plugin)
  1647. {
  1648. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  1649. carla_debug("PatchbayGraph::removePlugin(%p)", plugin.get());
  1650. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1651. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1652. const bool sendHost = !usingExternalHost;
  1653. const bool sendOSC = !usingExternalOSC;
  1654. disconnectInternalGroup(node->nodeId);
  1655. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1656. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1657. // Fix plugin Ids properties
  1658. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1659. {
  1660. const CarlaPluginPtr plugin2 = kEngine->getPlugin(i);
  1661. CARLA_SAFE_ASSERT_BREAK(plugin2.get() != nullptr);
  1662. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1663. {
  1664. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != water::var(-1));
  1665. node2->properties.set("pluginId", static_cast<int>(i-1));
  1666. }
  1667. }
  1668. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1669. }
  1670. void PatchbayGraph::removeAllPlugins()
  1671. {
  1672. carla_debug("PatchbayGraph::removeAllPlugins()");
  1673. const bool sendHost = !usingExternalHost;
  1674. const bool sendOSC = !usingExternalOSC;
  1675. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1676. {
  1677. const CarlaPluginPtr plugin = kEngine->getPlugin(i);
  1678. CARLA_SAFE_ASSERT_CONTINUE(plugin.get() != nullptr);
  1679. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1680. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1681. disconnectInternalGroup(node->nodeId);
  1682. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1683. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1684. graph.removeNode(node->nodeId);
  1685. }
  1686. }
  1687. bool PatchbayGraph::connect(const bool external,
  1688. const uint groupA, const uint portA, const uint groupB, const uint portB)
  1689. {
  1690. if (external)
  1691. return extGraph.connect(usingExternalHost, usingExternalOSC, groupA, portA, groupB, portB);
  1692. uint adjustedPortA = portA;
  1693. uint adjustedPortB = portB;
  1694. AudioProcessor::ChannelType channelType;
  1695. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1696. return false;
  1697. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1698. return false;
  1699. if (! graph.addConnection(channelType, groupA, adjustedPortA, groupB, adjustedPortB))
  1700. {
  1701. kEngine->setLastError("Failed from water");
  1702. return false;
  1703. }
  1704. ConnectionToId connectionToId;
  1705. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1706. char strBuf[STR_MAX+1];
  1707. strBuf[STR_MAX] = '\0';
  1708. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1709. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1710. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1711. connectionToId.id,
  1712. 0, 0, 0, 0.0f,
  1713. strBuf);
  1714. connections.list.append(connectionToId);
  1715. return true;
  1716. }
  1717. bool PatchbayGraph::disconnect(const bool external, const uint connectionId)
  1718. {
  1719. if (external)
  1720. return extGraph.disconnect(usingExternalHost, usingExternalOSC, connectionId);
  1721. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1722. {
  1723. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1724. const ConnectionToId& connectionToId(it.getValue(fallback));
  1725. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1726. if (connectionToId.id != connectionId)
  1727. continue;
  1728. uint adjustedPortA = connectionToId.portA;
  1729. uint adjustedPortB = connectionToId.portB;
  1730. AudioProcessor::ChannelType channelType;
  1731. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1732. return false;
  1733. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1734. return false;
  1735. if (! graph.removeConnection(channelType,
  1736. connectionToId.groupA, adjustedPortA,
  1737. connectionToId.groupB, adjustedPortB))
  1738. return false;
  1739. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1740. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1741. connectionToId.id,
  1742. 0, 0, 0, 0.0f,
  1743. nullptr);
  1744. connections.list.remove(it);
  1745. return true;
  1746. }
  1747. kEngine->setLastError("Failed to find connection");
  1748. return false;
  1749. }
  1750. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1751. {
  1752. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1753. {
  1754. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1755. const ConnectionToId& connectionToId(it.getValue(fallback));
  1756. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1757. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1758. continue;
  1759. /*
  1760. uint adjustedPortA = connectionToId.portA;
  1761. uint adjustedPortB = connectionToId.portB;
  1762. AudioProcessor::ChannelType channelType;
  1763. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1764. return false;
  1765. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1766. return false;
  1767. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1768. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1769. */
  1770. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1771. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1772. connectionToId.id,
  1773. 0, 0, 0, 0.0f,
  1774. nullptr);
  1775. connections.list.remove(it);
  1776. }
  1777. }
  1778. void PatchbayGraph::setGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  1779. uint groupId, int x1, int y1, int x2, int y2)
  1780. {
  1781. if (external)
  1782. return extGraph.setGroupPos(sendHost, sendOSC, groupId, x1, y1, x2, y2);
  1783. AudioProcessorGraph::Node* const node(graph.getNodeForId(groupId));
  1784. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1785. node->properties.set("x1", x1);
  1786. node->properties.set("y1", y1);
  1787. node->properties.set("x2", x2);
  1788. node->properties.set("y2", y2);
  1789. kEngine->callback(sendHost, sendOSC,
  1790. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1791. groupId, x1, y1, x2, static_cast<float>(y2),
  1792. nullptr);
  1793. }
  1794. void PatchbayGraph::refresh(const bool sendHost, const bool sendOSC, const bool external, const char* const deviceName)
  1795. {
  1796. if (external)
  1797. return extGraph.refresh(sendHost, sendOSC, deviceName);
  1798. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1799. connections.clear();
  1800. graph.removeIllegalConnections();
  1801. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1802. {
  1803. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1804. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1805. AudioProcessor* const proc(node->getProcessor());
  1806. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1807. int pluginId = -1;
  1808. // plugin node
  1809. if (node->properties.getWithDefault("isPlugin", false) == water::var(true))
  1810. pluginId = node->properties.getWithDefault("pluginId", -1);
  1811. addNodeToPatchbay(sendHost, sendOSC, kEngine, node, pluginId, proc);
  1812. }
  1813. char strBuf[STR_MAX+1];
  1814. strBuf[STR_MAX] = '\0';
  1815. for (size_t i=0, count=graph.getNumConnections(); i<count; ++i)
  1816. {
  1817. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1818. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1819. const uint groupA = conn->sourceNodeId;
  1820. const uint groupB = conn->destNodeId;
  1821. uint portA = conn->sourceChannelIndex;
  1822. uint portB = conn->destChannelIndex;
  1823. switch (conn->channelType)
  1824. {
  1825. case AudioProcessor::ChannelTypeAudio:
  1826. portA += kAudioOutputPortOffset;
  1827. portB += kAudioInputPortOffset;
  1828. break;
  1829. case AudioProcessor::ChannelTypeCV:
  1830. portA += kCVOutputPortOffset;
  1831. portB += kCVInputPortOffset;
  1832. break;
  1833. case AudioProcessor::ChannelTypeMIDI:
  1834. portA += kMidiOutputPortOffset;
  1835. portB += kMidiInputPortOffset;
  1836. break;
  1837. }
  1838. ConnectionToId connectionToId;
  1839. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1840. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1841. kEngine->callback(sendHost, sendOSC,
  1842. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1843. connectionToId.id,
  1844. 0, 0, 0, 0.0f,
  1845. strBuf);
  1846. connections.list.append(connectionToId);
  1847. }
  1848. }
  1849. const char* const* PatchbayGraph::getConnections(const bool external) const
  1850. {
  1851. if (external)
  1852. return extGraph.getConnections();
  1853. if (connections.list.count() == 0)
  1854. return nullptr;
  1855. CarlaStringList connList;
  1856. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1857. {
  1858. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1859. const ConnectionToId& connectionToId(it.getValue(fallback));
  1860. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1861. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1862. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1863. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1864. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1865. AudioProcessor* const procA(nodeA->getProcessor());
  1866. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1867. AudioProcessor* const procB(nodeB->getProcessor());
  1868. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1869. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1870. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1871. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1872. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1873. connList.append(fullPortNameA.toRawUTF8());
  1874. connList.append(fullPortNameB.toRawUTF8());
  1875. }
  1876. if (connList.count() == 0)
  1877. return nullptr;
  1878. retCon = connList.toCharStringListPtr();
  1879. return retCon;
  1880. }
  1881. const CarlaEngine::PatchbayPosition* PatchbayGraph::getPositions(bool external, uint& count) const
  1882. {
  1883. CarlaEngine::PatchbayPosition* ret;
  1884. if (external)
  1885. {
  1886. try {
  1887. ret = new CarlaEngine::PatchbayPosition[kExternalGraphGroupMax];
  1888. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  1889. count = 0;
  1890. for (uint i=kExternalGraphGroupCarla; i<kExternalGraphGroupMax; ++i)
  1891. {
  1892. const PatchbayPosition& eppos(extGraph.positions[i]);
  1893. if (! eppos.active)
  1894. continue;
  1895. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  1896. switch (i)
  1897. {
  1898. case kExternalGraphGroupCarla:
  1899. ppos.name = "Carla";
  1900. break;
  1901. case kExternalGraphGroupAudioIn:
  1902. ppos.name = "AudioIn";
  1903. break;
  1904. case kExternalGraphGroupAudioOut:
  1905. ppos.name = "AudioOut";
  1906. break;
  1907. case kExternalGraphGroupMidiIn:
  1908. ppos.name = "MidiIn";
  1909. break;
  1910. case kExternalGraphGroupMidiOut:
  1911. ppos.name = "MidiOut";
  1912. break;
  1913. }
  1914. ppos.dealloc = false;
  1915. ppos.pluginId = -1;
  1916. ppos.x1 = eppos.x1;
  1917. ppos.y1 = eppos.y1;
  1918. ppos.x2 = eppos.x2;
  1919. ppos.y2 = eppos.y2;
  1920. }
  1921. return ret;
  1922. }
  1923. else
  1924. {
  1925. const int numNodes = graph.getNumNodes();
  1926. CARLA_SAFE_ASSERT_RETURN(numNodes > 0, nullptr);
  1927. try {
  1928. ret = new CarlaEngine::PatchbayPosition[numNodes];
  1929. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  1930. count = 0;
  1931. for (int i=numNodes; --i >= 0;)
  1932. {
  1933. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1934. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1935. if (! node->properties.contains("x1"))
  1936. continue;
  1937. AudioProcessor* const proc(node->getProcessor());
  1938. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1939. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  1940. ppos.name = carla_strdup(proc->getName().toRawUTF8());
  1941. ppos.dealloc = true;
  1942. ppos.pluginId = node->properties.getWithDefault("pluginId", -1);
  1943. ppos.x1 = node->properties.getWithDefault("x1", 0);
  1944. ppos.y1 = node->properties.getWithDefault("y1", 0);
  1945. ppos.x2 = node->properties.getWithDefault("x2", 0);
  1946. ppos.y2 = node->properties.getWithDefault("y2", 0);
  1947. }
  1948. return ret;
  1949. }
  1950. }
  1951. bool PatchbayGraph::getGroupFromName(bool external, const char* groupName, uint& groupId) const
  1952. {
  1953. if (external)
  1954. return extGraph.getGroupFromName(groupName, groupId);
  1955. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1956. {
  1957. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1958. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1959. AudioProcessor* const proc(node->getProcessor());
  1960. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1961. if (proc->getName() != groupName)
  1962. continue;
  1963. groupId = node->nodeId;
  1964. return true;
  1965. }
  1966. return false;
  1967. }
  1968. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  1969. {
  1970. if (external)
  1971. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  1972. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1973. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1974. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1975. {
  1976. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1977. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1978. AudioProcessor* const proc(node->getProcessor());
  1979. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1980. if (proc->getName() != groupName)
  1981. continue;
  1982. groupId = node->nodeId;
  1983. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); j<numInputs; ++j)
  1984. {
  1985. if (proc->getInputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  1986. continue;
  1987. portId = kAudioInputPortOffset+j;
  1988. return true;
  1989. }
  1990. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); j<numOutputs; ++j)
  1991. {
  1992. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  1993. continue;
  1994. portId = kAudioOutputPortOffset+j;
  1995. return true;
  1996. }
  1997. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); j<numInputs; ++j)
  1998. {
  1999. if (proc->getInputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  2000. continue;
  2001. portId = kCVInputPortOffset+j;
  2002. return true;
  2003. }
  2004. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); j<numOutputs; ++j)
  2005. {
  2006. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  2007. continue;
  2008. portId = kCVOutputPortOffset+j;
  2009. return true;
  2010. }
  2011. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); j<numInputs; ++j)
  2012. {
  2013. if (proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  2014. continue;
  2015. portId = kMidiInputPortOffset+j;
  2016. return true;
  2017. }
  2018. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); j<numOutputs; ++j)
  2019. {
  2020. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  2021. continue;
  2022. portId = kMidiOutputPortOffset+j;
  2023. return true;
  2024. }
  2025. }
  2026. return false;
  2027. }
  2028. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data,
  2029. const float* const* const inBuf,
  2030. float* const* const outBuf,
  2031. const uint32_t frames)
  2032. {
  2033. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  2034. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  2035. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  2036. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  2037. // put events in water buffer
  2038. {
  2039. midiBuffer.clear();
  2040. fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  2041. }
  2042. // set audio and cv buffer size, needed for water internals
  2043. if (! audioBuffer.setSizeRT(frames))
  2044. return;
  2045. if (! cvInBuffer.setSizeRT(frames))
  2046. return;
  2047. if (! cvOutBuffer.setSizeRT(frames))
  2048. return;
  2049. // put carla audio and cv in water buffer
  2050. {
  2051. uint32_t i=0;
  2052. for (; i < numAudioIns; ++i) {
  2053. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  2054. audioBuffer.copyFrom(i, 0, inBuf[i], frames);
  2055. }
  2056. for (uint32_t j=0; j < numCVIns; ++j, ++i) {
  2057. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  2058. cvInBuffer.copyFrom(j, 0, inBuf[i], frames);
  2059. }
  2060. // clear remaining channels
  2061. for (uint32_t j=numAudioIns, count=audioBuffer.getNumChannels(); j < count; ++j)
  2062. audioBuffer.clear(j, 0, frames);
  2063. for (uint32_t j=0; j < numCVOuts; ++j)
  2064. cvOutBuffer.clear(j, 0, frames);
  2065. }
  2066. // ready to go!
  2067. graph.processBlockWithCV(audioBuffer, cvInBuffer, cvOutBuffer, midiBuffer);
  2068. // put water audio and cv in carla buffer
  2069. {
  2070. uint32_t i=0;
  2071. for (; i < numAudioOuts; ++i)
  2072. carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
  2073. for (uint32_t j=0; j < numCVOuts; ++j, ++i)
  2074. carla_copyFloats(outBuf[i], cvOutBuffer.getReadPointer(j), frames);
  2075. }
  2076. // put water events in carla buffer
  2077. {
  2078. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  2079. fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
  2080. midiBuffer.clear();
  2081. }
  2082. }
  2083. void PatchbayGraph::run()
  2084. {
  2085. while (! shouldThreadExit())
  2086. {
  2087. carla_msleep(100);
  2088. graph.reorderNowIfNeeded();
  2089. }
  2090. }
  2091. // -----------------------------------------------------------------------
  2092. // InternalGraph
  2093. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  2094. : fIsRack(false),
  2095. fIsReady(false),
  2096. fRack(nullptr),
  2097. kEngine(engine) {}
  2098. EngineInternalGraph::~EngineInternalGraph() noexcept
  2099. {
  2100. CARLA_SAFE_ASSERT(! fIsReady);
  2101. CARLA_SAFE_ASSERT(fRack == nullptr);
  2102. }
  2103. void EngineInternalGraph::create(const uint32_t audioIns, const uint32_t audioOuts,
  2104. const uint32_t cvIns, const uint32_t cvOuts)
  2105. {
  2106. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  2107. if (fIsRack)
  2108. {
  2109. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  2110. fRack = new RackGraph(kEngine, audioIns, audioOuts);
  2111. }
  2112. else
  2113. {
  2114. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  2115. fPatchbay = new PatchbayGraph(kEngine, audioIns, audioOuts, cvIns, cvOuts);
  2116. }
  2117. fIsReady = true;
  2118. }
  2119. void EngineInternalGraph::destroy() noexcept
  2120. {
  2121. if (! fIsReady)
  2122. {
  2123. CARLA_SAFE_ASSERT(fRack == nullptr);
  2124. return;
  2125. }
  2126. if (fIsRack)
  2127. {
  2128. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2129. delete fRack;
  2130. fRack = nullptr;
  2131. }
  2132. else
  2133. {
  2134. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2135. delete fPatchbay;
  2136. fPatchbay = nullptr;
  2137. }
  2138. fIsReady = false;
  2139. }
  2140. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  2141. {
  2142. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2143. if (fIsRack)
  2144. {
  2145. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2146. fRack->setBufferSize(bufferSize);
  2147. }
  2148. else
  2149. {
  2150. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2151. fPatchbay->setBufferSize(bufferSize);
  2152. }
  2153. }
  2154. void EngineInternalGraph::setSampleRate(const double sampleRate)
  2155. {
  2156. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2157. if (fIsRack)
  2158. {
  2159. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2160. }
  2161. else
  2162. {
  2163. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2164. fPatchbay->setSampleRate(sampleRate);
  2165. }
  2166. }
  2167. void EngineInternalGraph::setOffline(const bool offline)
  2168. {
  2169. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2170. if (fIsRack)
  2171. {
  2172. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2173. fRack->setOffline(offline);
  2174. }
  2175. else
  2176. {
  2177. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2178. fPatchbay->setOffline(offline);
  2179. }
  2180. }
  2181. bool EngineInternalGraph::isReady() const noexcept
  2182. {
  2183. return fIsReady;
  2184. }
  2185. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  2186. {
  2187. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  2188. return fRack;
  2189. }
  2190. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  2191. {
  2192. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  2193. return fPatchbay;
  2194. }
  2195. PatchbayGraph* EngineInternalGraph::getPatchbayGraphOrNull() const noexcept
  2196. {
  2197. return fIsRack ? nullptr : fPatchbay;
  2198. }
  2199. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  2200. {
  2201. if (fIsRack)
  2202. {
  2203. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2204. fRack->processHelper(data, inBuf, outBuf, frames);
  2205. }
  2206. else
  2207. {
  2208. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2209. fPatchbay->process(data, inBuf, outBuf, frames);
  2210. }
  2211. }
  2212. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  2213. {
  2214. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  2215. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2216. fRack->process(data, inBuf, outBuf, frames);
  2217. }
  2218. // -----------------------------------------------------------------------
  2219. // used for internal patchbay mode
  2220. void EngineInternalGraph::addPlugin(const CarlaPluginPtr plugin)
  2221. {
  2222. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2223. fPatchbay->addPlugin(plugin);
  2224. }
  2225. void EngineInternalGraph::replacePlugin(const CarlaPluginPtr oldPlugin, const CarlaPluginPtr newPlugin)
  2226. {
  2227. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2228. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  2229. }
  2230. void EngineInternalGraph::renamePlugin(const CarlaPluginPtr plugin, const char* const newName)
  2231. {
  2232. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2233. fPatchbay->renamePlugin(plugin, newName);
  2234. }
  2235. void EngineInternalGraph::removePlugin(const CarlaPluginPtr plugin)
  2236. {
  2237. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2238. fPatchbay->removePlugin(plugin);
  2239. }
  2240. void EngineInternalGraph::removeAllPlugins()
  2241. {
  2242. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2243. fPatchbay->removeAllPlugins();
  2244. }
  2245. bool EngineInternalGraph::isUsingExternalHost() const noexcept
  2246. {
  2247. if (fIsRack)
  2248. return true;
  2249. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2250. return fPatchbay->usingExternalHost;
  2251. }
  2252. bool EngineInternalGraph::isUsingExternalOSC() const noexcept
  2253. {
  2254. if (fIsRack)
  2255. return true;
  2256. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2257. return fPatchbay->usingExternalOSC;
  2258. }
  2259. void EngineInternalGraph::setUsingExternalHost(const bool usingExternal) noexcept
  2260. {
  2261. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2262. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2263. fPatchbay->usingExternalHost = usingExternal;
  2264. }
  2265. void EngineInternalGraph::setUsingExternalOSC(const bool usingExternal) noexcept
  2266. {
  2267. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2268. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2269. fPatchbay->usingExternalOSC = usingExternal;
  2270. }
  2271. // -----------------------------------------------------------------------
  2272. // CarlaEngine Patchbay stuff
  2273. bool CarlaEngine::patchbayConnect(const bool external,
  2274. const uint groupA, const uint portA,
  2275. const uint groupB, const uint portB)
  2276. {
  2277. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2278. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2279. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  2280. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2281. {
  2282. RackGraph* const graph = pData->graph.getRackGraph();
  2283. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2284. return graph->connect(groupA, portA, groupB, portB);
  2285. }
  2286. else
  2287. {
  2288. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2289. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2290. return graph->connect(external, groupA, portA, groupB, portB);
  2291. }
  2292. }
  2293. bool CarlaEngine::patchbayDisconnect(const bool external, const uint connectionId)
  2294. {
  2295. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2296. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2297. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  2298. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2299. {
  2300. RackGraph* const graph = pData->graph.getRackGraph();
  2301. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2302. return graph->disconnect(connectionId);
  2303. }
  2304. else
  2305. {
  2306. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2307. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2308. return graph->disconnect(external, connectionId);
  2309. }
  2310. }
  2311. bool CarlaEngine::patchbaySetGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  2312. const uint groupId, const int x1, const int y1, const int x2, const int y2)
  2313. {
  2314. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  2315. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2316. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2317. carla_debug("CarlaEngine::patchbaySetGroupPos(%u, %i, %i, %i, %i)", groupId, x1, y1, x2, y2);
  2318. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2319. {
  2320. // we don't bother to save position in this case, there is only midi in/out
  2321. return true;
  2322. }
  2323. else
  2324. {
  2325. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2326. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2327. graph->setGroupPos(sendHost, sendOSC, external, groupId, x1, y1, x2, y2);
  2328. return true;
  2329. }
  2330. }
  2331. bool CarlaEngine::patchbayRefresh(const bool sendHost, const bool sendOSC, const bool external)
  2332. {
  2333. // subclasses should handle this
  2334. CARLA_SAFE_ASSERT_RETURN(! external, false);
  2335. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2336. {
  2337. // This is implemented in engine subclasses
  2338. setLastError("Unsupported operation");
  2339. return false;
  2340. }
  2341. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2342. {
  2343. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2344. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2345. graph->refresh(sendHost, sendOSC, external, "");
  2346. return true;
  2347. }
  2348. setLastError("Unsupported operation");
  2349. return false;
  2350. }
  2351. // -----------------------------------------------------------------------
  2352. // Patchbay stuff
  2353. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  2354. {
  2355. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  2356. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  2357. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2358. {
  2359. RackGraph* const graph = pData->graph.getRackGraph();
  2360. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2361. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  2362. return graph->getConnections();
  2363. }
  2364. else
  2365. {
  2366. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2367. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2368. return graph->getConnections(external);
  2369. }
  2370. return nullptr;
  2371. }
  2372. const CarlaEngine::PatchbayPosition* CarlaEngine::getPatchbayPositions(bool external, uint& count) const
  2373. {
  2374. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  2375. carla_debug("CarlaEngine::getPatchbayPositions(%s)", bool2str(external));
  2376. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2377. {
  2378. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2379. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2380. return graph->getPositions(external, count);
  2381. }
  2382. return nullptr;
  2383. }
  2384. void CarlaEngine::restorePatchbayConnection(const bool external,
  2385. const char* const sourcePort,
  2386. const char* const targetPort)
  2387. {
  2388. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  2389. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  2390. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  2391. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  2392. uint groupA, portA;
  2393. uint groupB, portB;
  2394. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2395. {
  2396. RackGraph* const graph = pData->graph.getRackGraph();
  2397. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2398. CARLA_SAFE_ASSERT_RETURN(external,);
  2399. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  2400. return;
  2401. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  2402. return;
  2403. graph->connect(groupA, portA, groupB, portB);
  2404. }
  2405. else
  2406. {
  2407. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2408. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2409. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  2410. return;
  2411. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  2412. return;
  2413. graph->connect(external, groupA, portA, groupB, portB);
  2414. }
  2415. }
  2416. bool CarlaEngine::restorePatchbayGroupPosition(const bool external, PatchbayPosition& ppos)
  2417. {
  2418. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2419. CARLA_SAFE_ASSERT_RETURN(ppos.name != nullptr && ppos.name[0] != '\0', false);
  2420. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2421. {
  2422. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2423. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2424. uint groupId;
  2425. CARLA_SAFE_ASSERT_RETURN(graph->getGroupFromName(external, ppos.name, groupId), false);
  2426. graph->setGroupPos(true, true, external, groupId, ppos.x1, ppos.y1, ppos.x2, ppos.y2);
  2427. }
  2428. return false;
  2429. }
  2430. // -----------------------------------------------------------------------
  2431. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2432. {
  2433. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2434. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2435. RackGraph* const graph(pData->graph.getRackGraph());
  2436. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2437. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2438. switch (connectionType)
  2439. {
  2440. case kExternalGraphConnectionAudioIn1:
  2441. return graph->audioBuffers.connectedIn1.append(portId);
  2442. case kExternalGraphConnectionAudioIn2:
  2443. return graph->audioBuffers.connectedIn2.append(portId);
  2444. case kExternalGraphConnectionAudioOut1:
  2445. return graph->audioBuffers.connectedOut1.append(portId);
  2446. case kExternalGraphConnectionAudioOut2:
  2447. return graph->audioBuffers.connectedOut2.append(portId);
  2448. }
  2449. return false;
  2450. }
  2451. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2452. {
  2453. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2454. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2455. RackGraph* const graph(pData->graph.getRackGraph());
  2456. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2457. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2458. switch (connectionType)
  2459. {
  2460. case kExternalGraphConnectionAudioIn1:
  2461. return graph->audioBuffers.connectedIn1.removeOne(portId);
  2462. case kExternalGraphConnectionAudioIn2:
  2463. return graph->audioBuffers.connectedIn2.removeOne(portId);
  2464. case kExternalGraphConnectionAudioOut1:
  2465. return graph->audioBuffers.connectedOut1.removeOne(portId);
  2466. case kExternalGraphConnectionAudioOut2:
  2467. return graph->audioBuffers.connectedOut2.removeOne(portId);
  2468. }
  2469. return false;
  2470. }
  2471. // -----------------------------------------------------------------------
  2472. CARLA_BACKEND_END_NAMESPACE