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.

3147 lines
109KB

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