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.

3132 lines
108KB

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