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.

3117 lines
107KB

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