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.

3131 lines
108KB

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