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.

2639 lines
91KB

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