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.

3100 lines
107KB

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