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.

2272 lines
76KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 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 uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback) noexcept
  126. {
  127. uint otherGroup, otherPort, carlaPort;
  128. if (groupA == kExternalGraphGroupCarla)
  129. {
  130. CARLA_SAFE_ASSERT_RETURN(groupB != kExternalGraphGroupCarla, false);
  131. carlaPort = portA;
  132. otherGroup = groupB;
  133. otherPort = portB;
  134. }
  135. else
  136. {
  137. CARLA_SAFE_ASSERT_RETURN(groupB == kExternalGraphGroupCarla, false);
  138. carlaPort = portB;
  139. otherGroup = groupA;
  140. otherPort = portA;
  141. }
  142. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  143. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  144. bool makeConnection = false;
  145. switch (carlaPort)
  146. {
  147. case kExternalGraphCarlaPortAudioIn1:
  148. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  149. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  150. break;
  151. case kExternalGraphCarlaPortAudioIn2:
  152. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioIn, false);
  153. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  154. break;
  155. case kExternalGraphCarlaPortAudioOut1:
  156. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  157. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  158. break;
  159. case kExternalGraphCarlaPortAudioOut2:
  160. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupAudioOut, false);
  161. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  162. break;
  163. case kExternalGraphCarlaPortMidiIn:
  164. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiIn, false);
  165. if (const char* const portName = midiPorts.getName(true, otherPort))
  166. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  167. break;
  168. case kExternalGraphCarlaPortMidiOut:
  169. CARLA_SAFE_ASSERT_RETURN(otherGroup == kExternalGraphGroupMidiOut, false);
  170. if (const char* const portName = midiPorts.getName(false, otherPort))
  171. makeConnection = kEngine->connectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  172. break;
  173. }
  174. if (! makeConnection)
  175. {
  176. kEngine->setLastError("Invalid rack connection");
  177. return false;
  178. }
  179. ConnectionToId connectionToId;
  180. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  181. char strBuf[STR_MAX+1];
  182. strBuf[STR_MAX] = '\0';
  183. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  184. if (sendCallback)
  185. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  186. connections.list.append(connectionToId);
  187. return true;
  188. }
  189. bool ExternalGraph::disconnect(const uint connectionId) noexcept
  190. {
  191. CARLA_SAFE_ASSERT_RETURN(connections.list.count() > 0, false);
  192. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  193. {
  194. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  195. const ConnectionToId& connectionToId(it.getValue(fallback));
  196. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  197. if (connectionToId.id != connectionId)
  198. continue;
  199. uint otherGroup, otherPort, carlaPort;
  200. if (connectionToId.groupA == kExternalGraphGroupCarla)
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB != kExternalGraphGroupCarla, false);
  203. carlaPort = connectionToId.portA;
  204. otherGroup = connectionToId.groupB;
  205. otherPort = connectionToId.portB;
  206. }
  207. else
  208. {
  209. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB == kExternalGraphGroupCarla, false);
  210. carlaPort = connectionToId.portB;
  211. otherGroup = connectionToId.groupA;
  212. otherPort = connectionToId.portA;
  213. }
  214. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  215. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  216. bool makeDisconnection = false;
  217. switch (carlaPort)
  218. {
  219. case kExternalGraphCarlaPortAudioIn1:
  220. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  221. break;
  222. case kExternalGraphCarlaPortAudioIn2:
  223. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  224. break;
  225. case kExternalGraphCarlaPortAudioOut1:
  226. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  227. break;
  228. case kExternalGraphCarlaPortAudioOut2:
  229. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  230. break;
  231. case kExternalGraphCarlaPortMidiIn:
  232. if (const char* const portName = midiPorts.getName(true, otherPort))
  233. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  234. break;
  235. case kExternalGraphCarlaPortMidiOut:
  236. if (const char* const portName = midiPorts.getName(false, otherPort))
  237. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  238. break;
  239. }
  240. if (! makeDisconnection)
  241. {
  242. kEngine->setLastError("Invalid rack connection");
  243. return false;
  244. }
  245. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  246. connections.list.remove(it);
  247. return true;
  248. }
  249. kEngine->setLastError("Failed to find connection");
  250. return false;
  251. }
  252. void ExternalGraph::refresh(const char* const deviceName)
  253. {
  254. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  255. const bool isRack(kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  256. // Main
  257. {
  258. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupCarla, PATCHBAY_ICON_CARLA, MAIN_CARLA_PLUGIN_ID, 0.0f, kEngine->getName());
  259. if (isRack)
  260. {
  261. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in1");
  262. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in2");
  263. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out1");
  264. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out2");
  265. }
  266. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiIn, PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "midi-in");
  267. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiOut, PATCHBAY_PORT_TYPE_MIDI, 0.0f, "midi-out");
  268. }
  269. char strBuf[STR_MAX+1];
  270. strBuf[STR_MAX] = '\0';
  271. if (isRack)
  272. {
  273. // Audio In
  274. if (deviceName[0] != '\0')
  275. std::snprintf(strBuf, STR_MAX, "Capture (%s)", deviceName);
  276. else
  277. std::strncpy(strBuf, "Capture", STR_MAX);
  278. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupAudioIn, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  279. const CarlaString groupNameIn(strBuf);
  280. int h = 0;
  281. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.ins.begin2(); it.valid(); it.next())
  282. {
  283. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  284. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  285. portNameToId.setFullName(groupNameIn + portNameToId.name);
  286. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupAudioIn, ++h,
  287. PATCHBAY_PORT_TYPE_AUDIO, 0.0f, portNameToId.name);
  288. }
  289. // Audio Out
  290. if (deviceName[0] != '\0')
  291. std::snprintf(strBuf, STR_MAX, "Playback (%s)", deviceName);
  292. else
  293. std::strncpy(strBuf, "Playback", STR_MAX);
  294. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupAudioOut, PATCHBAY_ICON_HARDWARE, -1, 0.0f, strBuf);
  295. const CarlaString groupNameOut(strBuf);
  296. h = 0;
  297. for (LinkedList<PortNameToId>::Itenerator it = audioPorts.outs.begin2(); it.valid(); it.next())
  298. {
  299. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  300. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  301. portNameToId.setFullName(groupNameOut + portNameToId.name);
  302. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupAudioOut, ++h,
  303. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, portNameToId.name);
  304. }
  305. }
  306. // MIDI In
  307. {
  308. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupMidiIn, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Readable MIDI ports");
  309. const CarlaString groupNamePlus("Readable MIDI ports:");
  310. int h = 0;
  311. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.ins.begin2(); it.valid(); it.next())
  312. {
  313. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  314. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  315. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  316. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupMidiIn, ++h,
  317. PATCHBAY_PORT_TYPE_MIDI, 0.0f, portNameToId.name);
  318. }
  319. }
  320. // MIDI Out
  321. {
  322. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupMidiOut, PATCHBAY_ICON_HARDWARE, -1, 0.0f, "Writable MIDI ports");
  323. const CarlaString groupNamePlus("Writable MIDI ports:");
  324. int h = 0;
  325. for (LinkedList<PortNameToId>::Itenerator it = midiPorts.outs.begin2(); it.valid(); it.next())
  326. {
  327. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  328. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group > 0);
  329. portNameToId.setFullName(groupNamePlus + portNameToId.name);
  330. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, kExternalGraphGroupMidiOut, ++h,
  331. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, portNameToId.name);
  332. }
  333. }
  334. }
  335. const char* const* ExternalGraph::getConnections() const noexcept
  336. {
  337. if (connections.list.count() == 0)
  338. return nullptr;
  339. CarlaStringList connList;
  340. char strBuf[STR_MAX+1];
  341. strBuf[STR_MAX] = '\0';
  342. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  343. {
  344. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  345. const ConnectionToId& connectionToId(it.getValue(fallback));
  346. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  347. uint otherGroup, otherPort, carlaPort;
  348. if (connectionToId.groupA == kExternalGraphGroupCarla)
  349. {
  350. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB != kExternalGraphGroupCarla);
  351. carlaPort = connectionToId.portA;
  352. otherGroup = connectionToId.groupB;
  353. otherPort = connectionToId.portB;
  354. }
  355. else
  356. {
  357. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.groupB == kExternalGraphGroupCarla);
  358. carlaPort = connectionToId.portB;
  359. otherGroup = connectionToId.groupA;
  360. otherPort = connectionToId.portA;
  361. }
  362. CARLA_SAFE_ASSERT_CONTINUE(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax);
  363. CARLA_SAFE_ASSERT_CONTINUE(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax);
  364. switch (carlaPort)
  365. {
  366. case kExternalGraphCarlaPortAudioIn1:
  367. case kExternalGraphCarlaPortAudioIn2:
  368. std::snprintf(strBuf, STR_MAX, "AudioIn:%s", audioPorts.getName(true, otherPort));
  369. connList.append(strBuf);
  370. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  371. break;
  372. case kExternalGraphCarlaPortAudioOut1:
  373. case kExternalGraphCarlaPortAudioOut2:
  374. std::snprintf(strBuf, STR_MAX, "AudioOut:%s", audioPorts.getName(false, otherPort));
  375. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  376. connList.append(strBuf);
  377. break;
  378. case kExternalGraphCarlaPortMidiIn:
  379. std::snprintf(strBuf, STR_MAX, "MidiIn:%s", midiPorts.getName(true, otherPort));
  380. connList.append(strBuf);
  381. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  382. break;
  383. case kExternalGraphCarlaPortMidiOut:
  384. std::snprintf(strBuf, STR_MAX, "MidiOut:%s", midiPorts.getName(false, otherPort));
  385. connList.append(getExternalGraphFullPortNameFromId(carlaPort));
  386. connList.append(strBuf);
  387. break;
  388. }
  389. }
  390. if (connList.count() == 0)
  391. return nullptr;
  392. retCon = connList.toCharStringListPtr();
  393. return retCon;
  394. }
  395. bool ExternalGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  396. {
  397. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', false);
  398. if (std::strncmp(fullPortName, "Carla:", 6) == 0)
  399. {
  400. groupId = kExternalGraphGroupCarla;
  401. portId = getExternalGraphPortIdFromName(fullPortName+6);
  402. if (portId > kExternalGraphCarlaPortNull && portId < kExternalGraphCarlaPortMax)
  403. return true;
  404. }
  405. else if (std::strncmp(fullPortName, "AudioIn:", 8) == 0)
  406. {
  407. groupId = kExternalGraphGroupAudioIn;
  408. if (const char* const portName = fullPortName+8)
  409. {
  410. bool ok;
  411. portId = audioPorts.getPortId(true, portName, &ok);
  412. return ok;
  413. }
  414. }
  415. else if (std::strncmp(fullPortName, "AudioOut:", 9) == 0)
  416. {
  417. groupId = kExternalGraphGroupAudioOut;
  418. if (const char* const portName = fullPortName+9)
  419. {
  420. bool ok;
  421. portId = audioPorts.getPortId(false, portName, &ok);
  422. return ok;
  423. }
  424. }
  425. else if (std::strncmp(fullPortName, "MidiIn:", 7) == 0)
  426. {
  427. groupId = kExternalGraphGroupMidiIn;
  428. if (const char* const portName = fullPortName+7)
  429. {
  430. bool ok;
  431. portId = midiPorts.getPortId(true, portName, &ok);
  432. return ok;
  433. }
  434. }
  435. else if (std::strncmp(fullPortName, "MidiOut:", 8) == 0)
  436. {
  437. groupId = kExternalGraphGroupMidiOut;
  438. if (const char* const portName = fullPortName+8)
  439. {
  440. bool ok;
  441. portId = midiPorts.getPortId(false, portName, &ok);
  442. return ok;
  443. }
  444. }
  445. return false;
  446. }
  447. // -----------------------------------------------------------------------
  448. // RackGraph Buffers
  449. RackGraph::Buffers::Buffers() noexcept
  450. : mutex(),
  451. connectedIn1(),
  452. connectedIn2(),
  453. connectedOut1(),
  454. connectedOut2(),
  455. #ifdef CARLA_PROPER_CPP11_SUPPORT
  456. inBuf{nullptr, nullptr},
  457. inBufTmp{nullptr, nullptr},
  458. outBuf{nullptr, nullptr},
  459. #endif
  460. unusedBuf(nullptr)
  461. {
  462. #ifndef CARLA_PROPER_CPP11_SUPPORT
  463. inBuf[0] = inBuf[1] = nullptr;
  464. inBufTmp[0] = inBufTmp[1] = nullptr;
  465. outBuf[0] = outBuf[1] = nullptr;
  466. #endif
  467. }
  468. RackGraph::Buffers::~Buffers() noexcept
  469. {
  470. const CarlaRecursiveMutexLocker cml(mutex);
  471. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  472. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  473. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  474. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  475. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  476. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  477. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  478. connectedIn1.clear();
  479. connectedIn2.clear();
  480. connectedOut1.clear();
  481. connectedOut2.clear();
  482. }
  483. void RackGraph::Buffers::setBufferSize(const uint32_t bufferSize, const bool createBuffers) noexcept
  484. {
  485. const CarlaRecursiveMutexLocker cml(mutex);
  486. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  487. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  488. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  489. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  490. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  491. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  492. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  493. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  494. try {
  495. inBufTmp[0] = new float[bufferSize];
  496. inBufTmp[1] = new float[bufferSize];
  497. unusedBuf = new float[bufferSize];
  498. if (createBuffers)
  499. {
  500. inBuf[0] = new float[bufferSize];
  501. inBuf[1] = new float[bufferSize];
  502. outBuf[0] = new float[bufferSize];
  503. outBuf[1] = new float[bufferSize];
  504. }
  505. }
  506. catch(...) {
  507. if (inBufTmp[0] != nullptr) { delete[] inBufTmp[0]; inBufTmp[0] = nullptr; }
  508. if (inBufTmp[1] != nullptr) { delete[] inBufTmp[1]; inBufTmp[1] = nullptr; }
  509. if (unusedBuf != nullptr) { delete[] unusedBuf; unusedBuf = nullptr; }
  510. if (createBuffers)
  511. {
  512. if (inBuf[0] != nullptr) { delete[] inBuf[0]; inBuf[0] = nullptr; }
  513. if (inBuf[1] != nullptr) { delete[] inBuf[1]; inBuf[1] = nullptr; }
  514. if (outBuf[0] != nullptr) { delete[] outBuf[0]; outBuf[0] = nullptr; }
  515. if (outBuf[1] != nullptr) { delete[] outBuf[1]; outBuf[1] = nullptr; }
  516. }
  517. return;
  518. }
  519. carla_zeroFloats(inBufTmp[0], bufferSize);
  520. carla_zeroFloats(inBufTmp[1], bufferSize);
  521. if (createBuffers)
  522. {
  523. carla_zeroFloats(inBuf[0], bufferSize);
  524. carla_zeroFloats(inBuf[1], bufferSize);
  525. carla_zeroFloats(outBuf[0], bufferSize);
  526. carla_zeroFloats(outBuf[1], bufferSize);
  527. }
  528. }
  529. // -----------------------------------------------------------------------
  530. // RackGraph
  531. RackGraph::RackGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs) noexcept
  532. : extGraph(engine),
  533. inputs(ins),
  534. outputs(outs),
  535. isOffline(false),
  536. audioBuffers(),
  537. kEngine(engine)
  538. {
  539. setBufferSize(engine->getBufferSize());
  540. }
  541. RackGraph::~RackGraph() noexcept
  542. {
  543. extGraph.clear();
  544. }
  545. void RackGraph::setBufferSize(const uint32_t bufferSize) noexcept
  546. {
  547. audioBuffers.setBufferSize(bufferSize, (inputs > 0 || outputs > 0));
  548. }
  549. void RackGraph::setOffline(const bool offline) noexcept
  550. {
  551. isOffline = offline;
  552. }
  553. bool RackGraph::connect(const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  554. {
  555. return extGraph.connect(groupA, portA, groupB, portB, true);
  556. }
  557. bool RackGraph::disconnect(const uint connectionId) noexcept
  558. {
  559. return extGraph.disconnect(connectionId);
  560. }
  561. void RackGraph::refresh(const char* const deviceName)
  562. {
  563. extGraph.refresh(deviceName);
  564. char strBuf[STR_MAX+1];
  565. strBuf[STR_MAX] = '\0';
  566. // Connections
  567. const CarlaRecursiveMutexLocker cml(audioBuffers.mutex);
  568. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  569. {
  570. const uint& portId(it.getValue(0));
  571. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  572. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  573. ConnectionToId connectionToId;
  574. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn1);
  575. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  576. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  577. extGraph.connections.list.append(connectionToId);
  578. }
  579. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  580. {
  581. const uint& portId(it.getValue(0));
  582. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  583. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.ins.count());
  584. ConnectionToId connectionToId;
  585. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupAudioIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioIn2);
  586. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  587. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  588. extGraph.connections.list.append(connectionToId);
  589. }
  590. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  591. {
  592. const uint& portId(it.getValue(0));
  593. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  594. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  595. ConnectionToId connectionToId;
  596. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut1, kExternalGraphGroupAudioOut, portId);
  597. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  598. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  599. extGraph.connections.list.append(connectionToId);
  600. }
  601. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  602. {
  603. const uint& portId(it.getValue(0));
  604. CARLA_SAFE_ASSERT_CONTINUE(portId > 0);
  605. CARLA_SAFE_ASSERT_CONTINUE(portId <= extGraph.audioPorts.outs.count());
  606. ConnectionToId connectionToId;
  607. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortAudioOut2, kExternalGraphGroupAudioOut, portId);
  608. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  609. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  610. extGraph.connections.list.append(connectionToId);
  611. }
  612. }
  613. const char* const* RackGraph::getConnections() const noexcept
  614. {
  615. return extGraph.getConnections();
  616. }
  617. bool RackGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  618. {
  619. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  620. }
  621. void RackGraph::process(CarlaEngine::ProtectedData* const data, const float* inBufReal[2], float* outBufReal[2], const uint32_t frames)
  622. {
  623. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  624. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  625. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  626. // safe copy
  627. float* const dummyBuf = audioBuffers.unusedBuf;
  628. float* const inBuf0 = audioBuffers.inBufTmp[0];
  629. float* const inBuf1 = audioBuffers.inBufTmp[1];
  630. // initialize audio inputs
  631. carla_copyFloats(inBuf0, inBufReal[0], frames);
  632. carla_copyFloats(inBuf1, inBufReal[1], frames);
  633. // initialize audio outputs (zero)
  634. carla_zeroFloats(outBufReal[0], frames);
  635. carla_zeroFloats(outBufReal[1], frames);
  636. // initialize event outputs (zero)
  637. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  638. uint32_t oldAudioInCount = 0;
  639. uint32_t oldAudioOutCount = 0;
  640. uint32_t oldMidiOutCount = 0;
  641. bool processed = false;
  642. // process plugins
  643. for (uint i=0; i < data->curPluginCount; ++i)
  644. {
  645. CarlaPlugin* const plugin = data->plugins[i].plugin;
  646. if (plugin == nullptr || ! plugin->isEnabled() || ! plugin->tryLock(isOffline))
  647. continue;
  648. if (processed)
  649. {
  650. // initialize audio inputs (from previous outputs)
  651. carla_copyFloats(inBuf0, outBufReal[0], frames);
  652. carla_copyFloats(inBuf1, outBufReal[1], frames);
  653. // initialize audio outputs (zero)
  654. carla_zeroFloats(outBufReal[0], frames);
  655. carla_zeroFloats(outBufReal[1], frames);
  656. // if plugin has no midi out, add previous events
  657. if (oldMidiOutCount == 0 && data->events.in[0].type != kEngineEventTypeNull)
  658. {
  659. if (data->events.out[0].type != kEngineEventTypeNull)
  660. {
  661. // TODO: carefully add to input, sorted events
  662. //carla_stderr("TODO midi event mixing here %s", plugin->getName());
  663. }
  664. // else nothing needed
  665. }
  666. else
  667. {
  668. // initialize event inputs from previous outputs
  669. carla_copyStructs(data->events.in, data->events.out, kMaxEngineEventInternalCount);
  670. // initialize event outputs (zero)
  671. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  672. }
  673. }
  674. oldAudioInCount = plugin->getAudioInCount();
  675. oldAudioOutCount = plugin->getAudioOutCount();
  676. oldMidiOutCount = plugin->getMidiOutCount();
  677. const uint32_t numInBufs = std::max(oldAudioInCount, 2U);
  678. const uint32_t numOutBufs = std::max(oldAudioOutCount, 2U);
  679. const float* inBuf[numInBufs];
  680. inBuf[0] = inBuf0;
  681. inBuf[1] = inBuf1;
  682. float* outBuf[numOutBufs];
  683. outBuf[0] = outBufReal[0];
  684. outBuf[1] = outBufReal[1];
  685. if (numInBufs > 2 || numOutBufs > 2)
  686. {
  687. carla_zeroFloats(dummyBuf, frames);
  688. for (uint32_t i=2; i<numInBufs; ++i)
  689. inBuf[i] = dummyBuf;
  690. for (uint32_t i=2; i<numOutBufs; ++i)
  691. outBuf[i] = dummyBuf;
  692. }
  693. // process
  694. plugin->initBuffers();
  695. plugin->process(inBuf, outBuf, nullptr, nullptr, frames);
  696. plugin->unlock();
  697. // if plugin has no audio inputs, add input buffer
  698. if (oldAudioInCount == 0)
  699. {
  700. carla_addFloats(outBufReal[0], inBuf0, frames);
  701. carla_addFloats(outBufReal[1], inBuf1, frames);
  702. }
  703. // if plugin only has 1 output, copy it to the 2nd
  704. if (oldAudioOutCount == 1)
  705. {
  706. carla_copyFloats(outBufReal[1], outBufReal[0], frames);
  707. }
  708. // set peaks
  709. {
  710. EnginePluginData& pluginData(data->plugins[i]);
  711. if (oldAudioInCount > 0)
  712. {
  713. pluginData.insPeak[0] = carla_findMaxNormalizedFloat(inBuf0, frames);
  714. pluginData.insPeak[1] = carla_findMaxNormalizedFloat(inBuf1, frames);
  715. }
  716. else
  717. {
  718. pluginData.insPeak[0] = 0.0f;
  719. pluginData.insPeak[1] = 0.0f;
  720. }
  721. if (oldAudioOutCount > 0)
  722. {
  723. pluginData.outsPeak[0] = carla_findMaxNormalizedFloat(outBufReal[0], frames);
  724. pluginData.outsPeak[1] = carla_findMaxNormalizedFloat(outBufReal[1], frames);
  725. }
  726. else
  727. {
  728. pluginData.outsPeak[0] = 0.0f;
  729. pluginData.outsPeak[1] = 0.0f;
  730. }
  731. }
  732. processed = true;
  733. }
  734. }
  735. void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  736. {
  737. CARLA_SAFE_ASSERT_RETURN(audioBuffers.outBuf[1] != nullptr,);
  738. const CarlaRecursiveMutexLocker _cml(audioBuffers.mutex);
  739. if (inBuf != nullptr && inputs > 0)
  740. {
  741. bool noConnections = true;
  742. // connect input buffers
  743. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  744. {
  745. const uint& port(it.getValue(0));
  746. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  747. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  748. if (noConnections)
  749. {
  750. carla_copyFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  751. noConnections = false;
  752. }
  753. else
  754. {
  755. carla_addFloats(audioBuffers.inBuf[0], inBuf[port], frames);
  756. }
  757. }
  758. if (noConnections)
  759. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  760. noConnections = true;
  761. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  762. {
  763. const uint& port(it.getValue(0));
  764. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  765. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  766. if (noConnections)
  767. {
  768. carla_copyFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  769. noConnections = false;
  770. }
  771. else
  772. {
  773. carla_addFloats(audioBuffers.inBuf[1], inBuf[port-1], frames);
  774. }
  775. }
  776. if (noConnections)
  777. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  778. }
  779. else
  780. {
  781. carla_zeroFloats(audioBuffers.inBuf[0], frames);
  782. carla_zeroFloats(audioBuffers.inBuf[1], frames);
  783. }
  784. carla_zeroFloats(audioBuffers.outBuf[0], frames);
  785. carla_zeroFloats(audioBuffers.outBuf[1], frames);
  786. // process
  787. process(data, const_cast<const float**>(audioBuffers.inBuf), audioBuffers.outBuf, frames);
  788. // connect output buffers
  789. if (audioBuffers.connectedOut1.count() != 0)
  790. {
  791. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  792. {
  793. const uint& port(it.getValue(0));
  794. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  795. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  796. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[0], frames);
  797. }
  798. }
  799. if (audioBuffers.connectedOut2.count() != 0)
  800. {
  801. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  802. {
  803. const uint& port(it.getValue(0));
  804. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  805. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  806. carla_addFloats(outBuf[port-1], audioBuffers.outBuf[1], frames);
  807. }
  808. }
  809. }
  810. // -----------------------------------------------------------------------
  811. // Patchbay Graph stuff
  812. static const uint32_t kAudioInputPortOffset = MAX_PATCHBAY_PLUGINS*1;
  813. static const uint32_t kAudioOutputPortOffset = MAX_PATCHBAY_PLUGINS*2;
  814. static const uint32_t kMidiInputPortOffset = MAX_PATCHBAY_PLUGINS*3;
  815. static const uint32_t kMidiOutputPortOffset = MAX_PATCHBAY_PLUGINS*3+1;
  816. static const uint kMidiChannelIndex = static_cast<uint>(AudioProcessorGraph::midiChannelIndex);
  817. static inline
  818. bool adjustPatchbayPortIdForWater(uint& portId)
  819. {
  820. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, false);
  821. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, false);
  822. if (portId == kMidiInputPortOffset)
  823. {
  824. portId = kMidiChannelIndex;
  825. return true;
  826. }
  827. if (portId == kMidiOutputPortOffset)
  828. {
  829. portId = kMidiChannelIndex;
  830. return true;
  831. }
  832. if (portId >= kAudioOutputPortOffset)
  833. {
  834. portId -= kAudioOutputPortOffset;
  835. return true;
  836. }
  837. if (portId >= kAudioInputPortOffset)
  838. {
  839. portId -= kAudioInputPortOffset;
  840. return true;
  841. }
  842. return false;
  843. }
  844. static inline
  845. const String getProcessorFullPortName(AudioProcessor* const proc, const uint32_t portId)
  846. {
  847. CARLA_SAFE_ASSERT_RETURN(proc != nullptr, String());
  848. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, String());
  849. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, String());
  850. String fullPortName(proc->getName());
  851. if (portId == kMidiOutputPortOffset)
  852. {
  853. fullPortName += ":events-out";
  854. }
  855. else if (portId == kMidiInputPortOffset)
  856. {
  857. fullPortName += ":events-in";
  858. }
  859. else if (portId >= kAudioOutputPortOffset)
  860. {
  861. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumOutputChannels() > 0, String());
  862. fullPortName += ":" + proc->getOutputChannelName(static_cast<int>(portId-kAudioOutputPortOffset));
  863. }
  864. else if (portId >= kAudioInputPortOffset)
  865. {
  866. CARLA_SAFE_ASSERT_RETURN(proc->getTotalNumInputChannels() > 0, String());
  867. fullPortName += ":" + proc->getInputChannelName(static_cast<int>(portId-kAudioInputPortOffset));
  868. }
  869. else
  870. {
  871. return String();
  872. }
  873. return fullPortName;
  874. }
  875. static inline
  876. void addNodeToPatchbay(CarlaEngine* const engine, const uint32_t groupId, const int clientId, const AudioProcessor* const proc)
  877. {
  878. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  879. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  880. const int icon((clientId >= 0) ? PATCHBAY_ICON_PLUGIN : PATCHBAY_ICON_HARDWARE);
  881. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, groupId, icon, clientId, 0.0f, proc->getName().toRawUTF8());
  882. for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
  883. {
  884. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  885. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, proc->getInputChannelName(i).toRawUTF8());
  886. }
  887. for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
  888. {
  889. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  890. PATCHBAY_PORT_TYPE_AUDIO, 0.0f, proc->getOutputChannelName(i).toRawUTF8());
  891. }
  892. if (proc->acceptsMidi())
  893. {
  894. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiInputPortOffset),
  895. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "events-in");
  896. }
  897. if (proc->producesMidi())
  898. {
  899. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiOutputPortOffset),
  900. PATCHBAY_PORT_TYPE_MIDI, 0.0f, "events-out");
  901. }
  902. }
  903. static inline
  904. void removeNodeFromPatchbay(CarlaEngine* const engine, const uint32_t groupId, const AudioProcessor* const proc)
  905. {
  906. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  907. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  908. for (int i=0, numInputs=proc->getTotalNumInputChannels(); i<numInputs; ++i)
  909. {
  910. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  911. 0, 0.0f, nullptr);
  912. }
  913. for (int i=0, numOutputs=proc->getTotalNumOutputChannels(); i<numOutputs; ++i)
  914. {
  915. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  916. 0, 0.0f, nullptr);
  917. }
  918. if (proc->acceptsMidi())
  919. {
  920. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiInputPortOffset),
  921. 0, 0.0f, nullptr);
  922. }
  923. if (proc->producesMidi())
  924. {
  925. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiOutputPortOffset),
  926. 0, 0.0f, nullptr);
  927. }
  928. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED, groupId, 0, 0, 0.0f, nullptr);
  929. }
  930. // -----------------------------------------------------------------------
  931. class CarlaPluginInstance : public AudioProcessor
  932. {
  933. public:
  934. CarlaPluginInstance(CarlaEngine* const engine, CarlaPlugin* const plugin)
  935. : kEngine(engine),
  936. fPlugin(plugin)
  937. {
  938. setPlayConfigDetails(static_cast<int>(fPlugin->getAudioInCount()),
  939. static_cast<int>(fPlugin->getAudioOutCount()),
  940. getSampleRate(), getBlockSize());
  941. }
  942. ~CarlaPluginInstance() override
  943. {
  944. }
  945. void invalidatePlugin() noexcept
  946. {
  947. fPlugin = nullptr;
  948. }
  949. // -------------------------------------------------------------------
  950. const String getName() const override
  951. {
  952. return fPlugin->getName();
  953. }
  954. void processBlock(AudioSampleBuffer& audio, MidiBuffer& midi) override
  955. {
  956. if (fPlugin == nullptr || ! fPlugin->isEnabled() || ! fPlugin->tryLock(kEngine->isOffline()))
  957. {
  958. audio.clear();
  959. midi.clear();
  960. return;
  961. }
  962. fPlugin->initBuffers();
  963. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  964. {
  965. EngineEvent* const engineEvents(port->fBuffer);
  966. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  967. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  968. fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
  969. }
  970. midi.clear();
  971. // TODO - CV support
  972. const uint32_t numSamples(static_cast<uint32_t>(audio.getNumSamples()));
  973. if (const int numChan = audio.getNumChannels())
  974. {
  975. const uint numChanu = static_cast<uint>(jmin(numChan, 2));
  976. if (fPlugin->getAudioInCount() == 0)
  977. audio.clear();
  978. float* audioBuffers[numChan];
  979. for (int i=0; i<numChan; ++i)
  980. audioBuffers[i] = audio.getWritePointer(i);
  981. float inPeaks[2] = { 0.0f };
  982. float outPeaks[2] = { 0.0f };
  983. for (uint32_t i=0, count=jmin(fPlugin->getAudioInCount(), numChanu); i<count; ++i)
  984. inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  985. fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers, nullptr, nullptr, numSamples);
  986. for (uint32_t i=0, count=jmin(fPlugin->getAudioOutCount(), numChanu); i<count; ++i)
  987. outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  988. kEngine->setPluginPeaks(fPlugin->getId(), inPeaks, outPeaks);
  989. }
  990. else
  991. {
  992. fPlugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
  993. }
  994. midi.clear();
  995. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  996. {
  997. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  998. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  999. fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
  1000. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1001. }
  1002. fPlugin->unlock();
  1003. }
  1004. const String getInputChannelName(int i) const override
  1005. {
  1006. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  1007. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1008. return client->getAudioPortName(true, static_cast<uint>(i));
  1009. }
  1010. const String getOutputChannelName(int i) const override
  1011. {
  1012. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  1013. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1014. return client->getAudioPortName(false, static_cast<uint>(i));
  1015. }
  1016. void prepareToPlay(double, int) override {}
  1017. void releaseResources() override {}
  1018. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  1019. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  1020. private:
  1021. CarlaEngine* const kEngine;
  1022. CarlaPlugin* fPlugin;
  1023. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1024. };
  1025. // -----------------------------------------------------------------------
  1026. // Patchbay Graph
  1027. class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
  1028. {
  1029. public:
  1030. NamedAudioGraphIOProcessor(const IODeviceType iotype)
  1031. : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
  1032. inputNames(),
  1033. outputNames() {}
  1034. const String getInputChannelName (int index) const override
  1035. {
  1036. if (index < inputNames.size())
  1037. return inputNames[index];
  1038. return String("Playback ") + String(index+1);
  1039. }
  1040. const String getOutputChannelName (int index) const override
  1041. {
  1042. if (index < outputNames.size())
  1043. return outputNames[index];
  1044. return String("Capture ") + String(index+1);
  1045. }
  1046. void setNames(const bool setInputNames, const StringArray& names)
  1047. {
  1048. if (setInputNames)
  1049. inputNames = names;
  1050. else
  1051. outputNames = names;
  1052. }
  1053. private:
  1054. StringArray inputNames;
  1055. StringArray outputNames;
  1056. };
  1057. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs)
  1058. : CarlaThread("PatchbayReorderThread"),
  1059. connections(),
  1060. graph(),
  1061. audioBuffer(),
  1062. midiBuffer(),
  1063. inputs(carla_fixedValue(0U, 32U, ins)),
  1064. outputs(carla_fixedValue(0U, 32U, outs)),
  1065. retCon(),
  1066. usingExternal(false),
  1067. extGraph(engine),
  1068. kEngine(engine)
  1069. {
  1070. const int bufferSize(static_cast<int>(engine->getBufferSize()));
  1071. const double sampleRate(engine->getSampleRate());
  1072. graph.setPlayConfigDetails(static_cast<int>(inputs), static_cast<int>(outputs), sampleRate, bufferSize);
  1073. graph.prepareToPlay(sampleRate, bufferSize);
  1074. audioBuffer.setSize(static_cast<int>(jmax(inputs, outputs)), bufferSize);
  1075. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1076. midiBuffer.clear();
  1077. StringArray channelNames;
  1078. switch (inputs)
  1079. {
  1080. case 2:
  1081. channelNames.add("Left");
  1082. channelNames.add("Right");
  1083. break;
  1084. case 3:
  1085. channelNames.add("Left");
  1086. channelNames.add("Right");
  1087. channelNames.add("Sidechain");
  1088. break;
  1089. }
  1090. {
  1091. NamedAudioGraphIOProcessor* const proc(
  1092. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
  1093. proc->setNames(false, channelNames);
  1094. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1095. node->properties.set("isPlugin", false);
  1096. node->properties.set("isOutput", false);
  1097. node->properties.set("isAudio", true);
  1098. node->properties.set("isCV", false);
  1099. node->properties.set("isMIDI", false);
  1100. node->properties.set("isOSC", false);
  1101. }
  1102. {
  1103. NamedAudioGraphIOProcessor* const proc(
  1104. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
  1105. proc->setNames(true, channelNames);
  1106. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1107. node->properties.set("isPlugin", false);
  1108. node->properties.set("isOutput", false);
  1109. node->properties.set("isAudio", true);
  1110. node->properties.set("isCV", false);
  1111. node->properties.set("isMIDI", false);
  1112. node->properties.set("isOSC", false);
  1113. }
  1114. {
  1115. NamedAudioGraphIOProcessor* const proc(
  1116. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
  1117. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1118. node->properties.set("isPlugin", false);
  1119. node->properties.set("isOutput", false);
  1120. node->properties.set("isAudio", false);
  1121. node->properties.set("isCV", false);
  1122. node->properties.set("isMIDI", true);
  1123. node->properties.set("isOSC", false);
  1124. }
  1125. {
  1126. NamedAudioGraphIOProcessor* const proc(
  1127. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
  1128. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1129. node->properties.set("isPlugin", false);
  1130. node->properties.set("isOutput", true);
  1131. node->properties.set("isAudio", false);
  1132. node->properties.set("isCV", false);
  1133. node->properties.set("isMIDI", true);
  1134. node->properties.set("isOSC", false);
  1135. }
  1136. startThread();
  1137. }
  1138. PatchbayGraph::~PatchbayGraph()
  1139. {
  1140. stopThread(-1);
  1141. connections.clear();
  1142. extGraph.clear();
  1143. graph.releaseResources();
  1144. graph.clear();
  1145. audioBuffer.clear();
  1146. }
  1147. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1148. {
  1149. const int bufferSizei(static_cast<int>(bufferSize));
  1150. graph.releaseResources();
  1151. graph.prepareToPlay(kEngine->getSampleRate(), bufferSizei);
  1152. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSizei);
  1153. }
  1154. void PatchbayGraph::setSampleRate(const double sampleRate)
  1155. {
  1156. graph.releaseResources();
  1157. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1158. }
  1159. void PatchbayGraph::setOffline(const bool offline)
  1160. {
  1161. graph.setNonRealtime(offline);
  1162. }
  1163. void PatchbayGraph::addPlugin(CarlaPlugin* const plugin)
  1164. {
  1165. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1166. carla_debug("PatchbayGraph::addPlugin(%p)", plugin);
  1167. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1168. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1169. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1170. plugin->setPatchbayNodeId(node->nodeId);
  1171. node->properties.set("isPlugin", true);
  1172. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  1173. if (! usingExternal)
  1174. addNodeToPatchbay(plugin->getEngine(), node->nodeId, static_cast<int>(plugin->getId()), instance);
  1175. }
  1176. void PatchbayGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1177. {
  1178. CARLA_SAFE_ASSERT_RETURN(oldPlugin != nullptr,);
  1179. CARLA_SAFE_ASSERT_RETURN(newPlugin != nullptr,);
  1180. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1181. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1182. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1183. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1184. if (! usingExternal)
  1185. {
  1186. disconnectInternalGroup(oldNode->nodeId);
  1187. removeNodeFromPatchbay(kEngine, oldNode->nodeId, oldNode->getProcessor());
  1188. }
  1189. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1190. graph.removeNode(oldNode->nodeId);
  1191. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1192. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1193. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1194. newPlugin->setPatchbayNodeId(node->nodeId);
  1195. node->properties.set("isPlugin", true);
  1196. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  1197. if (! usingExternal)
  1198. addNodeToPatchbay(newPlugin->getEngine(), node->nodeId, static_cast<int>(newPlugin->getId()), instance);
  1199. }
  1200. void PatchbayGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  1201. {
  1202. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1203. carla_debug("PatchbayGraph::renamePlugin(%p)", plugin, newName);
  1204. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1205. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1206. if (! usingExternal)
  1207. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED, node->nodeId, 0, 0, 0.0f, newName);
  1208. }
  1209. void PatchbayGraph::removePlugin(CarlaPlugin* const plugin)
  1210. {
  1211. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1212. carla_debug("PatchbayGraph::removePlugin(%p)", plugin);
  1213. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1214. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1215. if (! usingExternal)
  1216. {
  1217. disconnectInternalGroup(node->nodeId);
  1218. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1219. }
  1220. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1221. // Fix plugin Ids properties
  1222. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1223. {
  1224. CarlaPlugin* const plugin2(kEngine->getPlugin(i));
  1225. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  1226. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1227. {
  1228. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != water::var(-1));
  1229. node2->properties.set("pluginId", static_cast<int>(i-1));
  1230. }
  1231. }
  1232. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1233. }
  1234. void PatchbayGraph::removeAllPlugins()
  1235. {
  1236. carla_debug("PatchbayGraph::removeAllPlugins()");
  1237. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1238. {
  1239. CarlaPlugin* const plugin(kEngine->getPlugin(i));
  1240. CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
  1241. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1242. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1243. if (! usingExternal)
  1244. {
  1245. disconnectInternalGroup(node->nodeId);
  1246. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1247. }
  1248. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1249. graph.removeNode(node->nodeId);
  1250. }
  1251. }
  1252. bool PatchbayGraph::connect(const bool external, const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback)
  1253. {
  1254. if (external)
  1255. return extGraph.connect(groupA, portA, groupB, portB, sendCallback);
  1256. uint adjustedPortA = portA;
  1257. uint adjustedPortB = portB;
  1258. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1259. return false;
  1260. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1261. return false;
  1262. if (! graph.addConnection(groupA, static_cast<int>(adjustedPortA), groupB, static_cast<int>(adjustedPortB)))
  1263. {
  1264. kEngine->setLastError("Failed from water");
  1265. return false;
  1266. }
  1267. ConnectionToId connectionToId;
  1268. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1269. char strBuf[STR_MAX+1];
  1270. strBuf[STR_MAX] = '\0';
  1271. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1272. if (sendCallback)
  1273. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1274. connections.list.append(connectionToId);
  1275. return true;
  1276. }
  1277. bool PatchbayGraph::disconnect(const uint connectionId)
  1278. {
  1279. if (usingExternal)
  1280. return extGraph.disconnect(connectionId);
  1281. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1282. {
  1283. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1284. const ConnectionToId& connectionToId(it.getValue(fallback));
  1285. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1286. if (connectionToId.id != connectionId)
  1287. continue;
  1288. uint adjustedPortA = connectionToId.portA;
  1289. uint adjustedPortB = connectionToId.portB;
  1290. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1291. return false;
  1292. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1293. return false;
  1294. if (! graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1295. connectionToId.groupB, static_cast<int>(adjustedPortB)))
  1296. return false;
  1297. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1298. connections.list.remove(it);
  1299. return true;
  1300. }
  1301. kEngine->setLastError("Failed to find connection");
  1302. return false;
  1303. }
  1304. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1305. {
  1306. CARLA_SAFE_ASSERT(! usingExternal);
  1307. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1308. {
  1309. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1310. const ConnectionToId& connectionToId(it.getValue(fallback));
  1311. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1312. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1313. continue;
  1314. /*
  1315. uint adjustedPortA = connectionToId.portA;
  1316. uint adjustedPortB = connectionToId.portB;
  1317. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1318. return false;
  1319. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1320. return false;
  1321. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1322. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1323. */
  1324. if (! usingExternal)
  1325. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1326. connections.list.remove(it);
  1327. }
  1328. }
  1329. void PatchbayGraph::refresh(const char* const deviceName)
  1330. {
  1331. if (usingExternal)
  1332. return extGraph.refresh(deviceName);
  1333. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1334. connections.clear();
  1335. graph.removeIllegalConnections();
  1336. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1337. {
  1338. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1339. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1340. AudioProcessor* const proc(node->getProcessor());
  1341. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1342. int clientId = -1;
  1343. // plugin node
  1344. if (node->properties.getWithDefault("isPlugin", false) == water::var(true))
  1345. clientId = node->properties.getWithDefault("pluginId", -1);
  1346. addNodeToPatchbay(kEngine, node->nodeId, clientId, proc);
  1347. }
  1348. char strBuf[STR_MAX+1];
  1349. strBuf[STR_MAX] = '\0';
  1350. for (int i=0, count=graph.getNumConnections(); i<count; ++i)
  1351. {
  1352. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1353. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1354. CARLA_SAFE_ASSERT_CONTINUE(conn->sourceChannelIndex >= 0);
  1355. CARLA_SAFE_ASSERT_CONTINUE(conn->destChannelIndex >= 0);
  1356. const uint groupA = conn->sourceNodeId;
  1357. const uint groupB = conn->destNodeId;
  1358. uint portA = static_cast<uint>(conn->sourceChannelIndex);
  1359. uint portB = static_cast<uint>(conn->destChannelIndex);
  1360. if (portA == kMidiChannelIndex)
  1361. portA = kMidiOutputPortOffset;
  1362. else
  1363. portA += kAudioOutputPortOffset;
  1364. if (portB == kMidiChannelIndex)
  1365. portB = kMidiInputPortOffset;
  1366. else
  1367. portB += kAudioInputPortOffset;
  1368. ConnectionToId connectionToId;
  1369. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1370. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1371. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1372. connections.list.append(connectionToId);
  1373. }
  1374. }
  1375. const char* const* PatchbayGraph::getConnections(const bool external) const
  1376. {
  1377. if (external)
  1378. return extGraph.getConnections();
  1379. if (connections.list.count() == 0)
  1380. return nullptr;
  1381. CarlaStringList connList;
  1382. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1383. {
  1384. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1385. const ConnectionToId& connectionToId(it.getValue(fallback));
  1386. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1387. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1388. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1389. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1390. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1391. AudioProcessor* const procA(nodeA->getProcessor());
  1392. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1393. AudioProcessor* const procB(nodeB->getProcessor());
  1394. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1395. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1396. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1397. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1398. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1399. connList.append(fullPortNameA.toRawUTF8());
  1400. connList.append(fullPortNameB.toRawUTF8());
  1401. }
  1402. if (connList.count() == 0)
  1403. return nullptr;
  1404. retCon = connList.toCharStringListPtr();
  1405. return retCon;
  1406. }
  1407. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  1408. {
  1409. if (external)
  1410. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  1411. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1412. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1413. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1414. {
  1415. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1416. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1417. AudioProcessor* const proc(node->getProcessor());
  1418. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1419. if (proc->getName() != groupName)
  1420. continue;
  1421. groupId = node->nodeId;
  1422. if (portName == "events-in")
  1423. {
  1424. portId = kMidiInputPortOffset;
  1425. return true;
  1426. }
  1427. if (portName == "events-out")
  1428. {
  1429. portId = kMidiOutputPortOffset;
  1430. return true;
  1431. }
  1432. for (int j=0, numInputs=proc->getTotalNumInputChannels(); j<numInputs; ++j)
  1433. {
  1434. if (proc->getInputChannelName(j) != portName)
  1435. continue;
  1436. portId = kAudioInputPortOffset+static_cast<uint>(j);
  1437. return true;
  1438. }
  1439. for (int j=0, numOutputs=proc->getTotalNumOutputChannels(); j<numOutputs; ++j)
  1440. {
  1441. if (proc->getOutputChannelName(j) != portName)
  1442. continue;
  1443. portId = kAudioOutputPortOffset+static_cast<uint>(j);
  1444. return true;
  1445. }
  1446. }
  1447. return false;
  1448. }
  1449. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const int frames)
  1450. {
  1451. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1452. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  1453. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  1454. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  1455. // put events in water buffer
  1456. {
  1457. midiBuffer.clear();
  1458. fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  1459. }
  1460. // set audio buffer size, needed for water internals
  1461. audioBuffer.setSizeRT(frames);
  1462. // put carla audio in water buffer
  1463. {
  1464. int i=0;
  1465. for (; i < static_cast<int>(inputs); ++i)
  1466. audioBuffer.copyFrom(i, 0, inBuf[i], frames);
  1467. // clear remaining channels
  1468. for (const int count=audioBuffer.getNumChannels(); i<count; ++i)
  1469. audioBuffer.clear(i, 0, frames);
  1470. }
  1471. // ready to go!
  1472. graph.processBlock(audioBuffer, midiBuffer);
  1473. // put water audio in carla buffer
  1474. {
  1475. for (int i=0; i < static_cast<int>(outputs); ++i)
  1476. carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
  1477. }
  1478. // put water events in carla buffer
  1479. {
  1480. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  1481. fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
  1482. midiBuffer.clear();
  1483. }
  1484. }
  1485. void PatchbayGraph::run()
  1486. {
  1487. while (! shouldThreadExit())
  1488. {
  1489. carla_msleep(100);
  1490. graph.reorderNowIfNeeded();
  1491. }
  1492. }
  1493. // -----------------------------------------------------------------------
  1494. // InternalGraph
  1495. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  1496. : fIsReady(false),
  1497. fRack(nullptr),
  1498. kEngine(engine) {}
  1499. EngineInternalGraph::~EngineInternalGraph() noexcept
  1500. {
  1501. CARLA_SAFE_ASSERT(! fIsReady);
  1502. CARLA_SAFE_ASSERT(fRack == nullptr);
  1503. }
  1504. void EngineInternalGraph::create(const uint32_t inputs, const uint32_t outputs)
  1505. {
  1506. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  1507. if (fIsRack)
  1508. {
  1509. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  1510. fRack = new RackGraph(kEngine, inputs, outputs);
  1511. }
  1512. else
  1513. {
  1514. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  1515. fPatchbay = new PatchbayGraph(kEngine, inputs, outputs);
  1516. }
  1517. fIsReady = true;
  1518. }
  1519. void EngineInternalGraph::destroy() noexcept
  1520. {
  1521. if (! fIsReady)
  1522. {
  1523. CARLA_SAFE_ASSERT(fRack == nullptr);
  1524. return;
  1525. }
  1526. if (fIsRack)
  1527. {
  1528. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1529. delete fRack;
  1530. fRack = nullptr;
  1531. }
  1532. else
  1533. {
  1534. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1535. delete fPatchbay;
  1536. fPatchbay = nullptr;
  1537. }
  1538. fIsReady = false;
  1539. }
  1540. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  1541. {
  1542. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1543. if (fIsRack)
  1544. {
  1545. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1546. fRack->setBufferSize(bufferSize);
  1547. }
  1548. else
  1549. {
  1550. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1551. fPatchbay->setBufferSize(bufferSize);
  1552. }
  1553. }
  1554. void EngineInternalGraph::setSampleRate(const double sampleRate)
  1555. {
  1556. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1557. if (fIsRack)
  1558. {
  1559. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1560. }
  1561. else
  1562. {
  1563. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1564. fPatchbay->setSampleRate(sampleRate);
  1565. }
  1566. }
  1567. void EngineInternalGraph::setOffline(const bool offline)
  1568. {
  1569. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1570. if (fIsRack)
  1571. {
  1572. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1573. fRack->setOffline(offline);
  1574. }
  1575. else
  1576. {
  1577. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1578. fPatchbay->setOffline(offline);
  1579. }
  1580. }
  1581. bool EngineInternalGraph::isReady() const noexcept
  1582. {
  1583. return fIsReady;
  1584. }
  1585. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  1586. {
  1587. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  1588. return fRack;
  1589. }
  1590. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  1591. {
  1592. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  1593. return fPatchbay;
  1594. }
  1595. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  1596. {
  1597. if (fIsRack)
  1598. {
  1599. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1600. fRack->processHelper(data, inBuf, outBuf, frames);
  1601. }
  1602. else
  1603. {
  1604. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1605. fPatchbay->process(data, inBuf, outBuf, static_cast<int>(frames));
  1606. }
  1607. }
  1608. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  1609. {
  1610. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  1611. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1612. fRack->process(data, inBuf, outBuf, frames);
  1613. }
  1614. // -----------------------------------------------------------------------
  1615. // used for internal patchbay mode
  1616. void EngineInternalGraph::addPlugin(CarlaPlugin* const plugin)
  1617. {
  1618. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1619. fPatchbay->addPlugin(plugin);
  1620. }
  1621. void EngineInternalGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1622. {
  1623. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1624. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  1625. }
  1626. void EngineInternalGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  1627. {
  1628. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1629. fPatchbay->renamePlugin(plugin, newName);
  1630. }
  1631. void EngineInternalGraph::removePlugin(CarlaPlugin* const plugin)
  1632. {
  1633. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1634. fPatchbay->removePlugin(plugin);
  1635. }
  1636. void EngineInternalGraph::removeAllPlugins()
  1637. {
  1638. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1639. fPatchbay->removeAllPlugins();
  1640. }
  1641. bool EngineInternalGraph::isUsingExternal() const noexcept
  1642. {
  1643. if (fIsRack)
  1644. return true;
  1645. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  1646. return fPatchbay->usingExternal;
  1647. }
  1648. void EngineInternalGraph::setUsingExternal(const bool usingExternal) noexcept
  1649. {
  1650. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  1651. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1652. fPatchbay->usingExternal = usingExternal;
  1653. }
  1654. // -----------------------------------------------------------------------
  1655. // CarlaEngine Patchbay stuff
  1656. bool CarlaEngine::patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB)
  1657. {
  1658. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1659. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1660. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  1661. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1662. {
  1663. RackGraph* const graph = pData->graph.getRackGraph();
  1664. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1665. return graph->connect(groupA, portA, groupB, portB);
  1666. }
  1667. else
  1668. {
  1669. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1670. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1671. return graph->connect(graph->usingExternal, groupA, portA, groupB, portB, true);
  1672. }
  1673. return false;
  1674. }
  1675. bool CarlaEngine::patchbayDisconnect(const uint connectionId)
  1676. {
  1677. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1678. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1679. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  1680. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1681. {
  1682. RackGraph* const graph = pData->graph.getRackGraph();
  1683. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1684. return graph->disconnect(connectionId);
  1685. }
  1686. else
  1687. {
  1688. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1689. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1690. return graph->disconnect(connectionId);
  1691. }
  1692. return false;
  1693. }
  1694. bool CarlaEngine::patchbayRefresh(const bool external)
  1695. {
  1696. // subclasses should handle this
  1697. CARLA_SAFE_ASSERT_RETURN(! external, false);
  1698. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1699. {
  1700. // This is implemented in engine subclasses
  1701. setLastError("Unsupported operation");
  1702. return false;
  1703. }
  1704. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1705. {
  1706. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1707. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1708. graph->refresh("");
  1709. return true;
  1710. }
  1711. setLastError("Unsupported operation");
  1712. return false;
  1713. }
  1714. // -----------------------------------------------------------------------
  1715. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  1716. {
  1717. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  1718. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  1719. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1720. {
  1721. RackGraph* const graph = pData->graph.getRackGraph();
  1722. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1723. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  1724. return graph->getConnections();
  1725. }
  1726. else
  1727. {
  1728. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1729. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1730. return graph->getConnections(external);
  1731. }
  1732. return nullptr;
  1733. }
  1734. void CarlaEngine::restorePatchbayConnection(const bool external, const char* const sourcePort, const char* const targetPort, const bool sendCallback)
  1735. {
  1736. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  1737. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  1738. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  1739. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  1740. uint groupA, portA;
  1741. uint groupB, portB;
  1742. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1743. {
  1744. RackGraph* const graph = pData->graph.getRackGraph();
  1745. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1746. CARLA_SAFE_ASSERT_RETURN(external,);
  1747. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  1748. return;
  1749. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  1750. return;
  1751. graph->connect(groupA, portA, groupB, portB);
  1752. }
  1753. else
  1754. {
  1755. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1756. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1757. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  1758. return;
  1759. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  1760. return;
  1761. graph->connect(external, groupA, portA, groupB, portB, sendCallback);
  1762. }
  1763. }
  1764. // -----------------------------------------------------------------------
  1765. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1766. {
  1767. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1768. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1769. RackGraph* const graph(pData->graph.getRackGraph());
  1770. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1771. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1772. switch (connectionType)
  1773. {
  1774. case kExternalGraphConnectionAudioIn1:
  1775. return graph->audioBuffers.connectedIn1.append(portId);
  1776. case kExternalGraphConnectionAudioIn2:
  1777. return graph->audioBuffers.connectedIn2.append(portId);
  1778. case kExternalGraphConnectionAudioOut1:
  1779. return graph->audioBuffers.connectedOut1.append(portId);
  1780. case kExternalGraphConnectionAudioOut2:
  1781. return graph->audioBuffers.connectedOut2.append(portId);
  1782. }
  1783. return false;
  1784. }
  1785. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1786. {
  1787. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1788. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1789. RackGraph* const graph(pData->graph.getRackGraph());
  1790. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1791. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1792. switch (connectionType)
  1793. {
  1794. case kExternalGraphConnectionAudioIn1:
  1795. return graph->audioBuffers.connectedIn1.removeOne(portId);
  1796. case kExternalGraphConnectionAudioIn2:
  1797. return graph->audioBuffers.connectedIn2.removeOne(portId);
  1798. case kExternalGraphConnectionAudioOut1:
  1799. return graph->audioBuffers.connectedOut1.removeOne(portId);
  1800. case kExternalGraphConnectionAudioOut2:
  1801. return graph->audioBuffers.connectedOut2.removeOne(portId);
  1802. }
  1803. return false;
  1804. }
  1805. // -----------------------------------------------------------------------
  1806. CARLA_BACKEND_END_NAMESPACE