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.

3029 lines
104KB

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