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.

2784 lines
96KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaEngineGraph.hpp"
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaPlugin.hpp"
  20. #include "CarlaMathUtils.hpp"
  21. #include "CarlaScopeUtils.hpp"
  22. #include "CarlaMIDI.h"
  23. using water::jmax;
  24. using water::jmin;
  25. using water::AudioProcessor;
  26. using water::MidiBuffer;
  27. using water::String;
  28. using water::StringArray;
  29. CARLA_BACKEND_START_NAMESPACE
  30. // -----------------------------------------------------------------------
  31. // Fallback data
  32. static const PortNameToId kPortNameToIdFallback = { 0, 0, { '\0' }, { '\0' } };
  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. CarlaEngineClient* const client = plugin->getEngineClient();
  1161. setPlayConfigDetails(client->getPortCount(kEnginePortTypeAudio, true),
  1162. client->getPortCount(kEnginePortTypeAudio, false),
  1163. client->getPortCount(kEnginePortTypeCV, true),
  1164. client->getPortCount(kEnginePortTypeCV, false),
  1165. client->getPortCount(kEnginePortTypeEvent, true),
  1166. client->getPortCount(kEnginePortTypeEvent, false),
  1167. getSampleRate(), getBlockSize());
  1168. }
  1169. ~CarlaPluginInstance() override
  1170. {
  1171. }
  1172. void reconfigure() override
  1173. {
  1174. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  1175. CarlaEngineClient* const client = fPlugin->getEngineClient();
  1176. CARLA_SAFE_ASSERT_RETURN(client != nullptr,);
  1177. carla_stdout("reconfigure called");
  1178. setPlayConfigDetails(client->getPortCount(kEnginePortTypeAudio, true),
  1179. client->getPortCount(kEnginePortTypeAudio, false),
  1180. client->getPortCount(kEnginePortTypeCV, true),
  1181. client->getPortCount(kEnginePortTypeCV, false),
  1182. client->getPortCount(kEnginePortTypeEvent, true),
  1183. client->getPortCount(kEnginePortTypeEvent, false),
  1184. getSampleRate(), getBlockSize());
  1185. }
  1186. void invalidatePlugin() noexcept
  1187. {
  1188. fPlugin = nullptr;
  1189. }
  1190. // -------------------------------------------------------------------
  1191. const String getName() const override
  1192. {
  1193. return fPlugin->getName();
  1194. }
  1195. void processBlockWithCV(AudioSampleBuffer& audio,
  1196. const AudioSampleBuffer& cvIn,
  1197. AudioSampleBuffer& cvOut,
  1198. MidiBuffer& midi) override
  1199. {
  1200. if (fPlugin == nullptr || ! fPlugin->isEnabled() || ! fPlugin->tryLock(kEngine->isOffline()))
  1201. {
  1202. audio.clear();
  1203. cvOut.clear();
  1204. midi.clear();
  1205. return;
  1206. }
  1207. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  1208. {
  1209. EngineEvent* const engineEvents(port->fBuffer);
  1210. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1211. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1212. fillEngineEventsFromWaterMidiBuffer(engineEvents, midi);
  1213. }
  1214. midi.clear();
  1215. fPlugin->initBuffers();
  1216. const uint32_t numSamples = audio.getNumSamples();
  1217. const uint32_t numAudioChan = audio.getNumChannels();
  1218. const uint32_t numCVInChan = cvIn.getNumChannels();
  1219. const uint32_t numCVOutChan = cvOut.getNumChannels();
  1220. if (numAudioChan+numCVInChan+numCVOutChan == 0)
  1221. {
  1222. // nothing to process
  1223. fPlugin->process(nullptr, nullptr, nullptr, nullptr, numSamples);
  1224. }
  1225. else if (numAudioChan != 0)
  1226. {
  1227. // processing audio, include code for peaks
  1228. const uint32_t numChan2 = jmin(numAudioChan, 2U);
  1229. if (fPlugin->getAudioInCount() == 0)
  1230. audio.clear();
  1231. float* audioBuffers[numAudioChan];
  1232. float* cvOutBuffers[numCVOutChan];
  1233. const float* cvInBuffers[numCVInChan];
  1234. for (uint32_t i=0; i<numAudioChan; ++i)
  1235. audioBuffers[i] = audio.getWritePointer(i);
  1236. for (uint32_t i=0; i<numCVOutChan; ++i)
  1237. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1238. for (uint32_t i=0; i<numCVInChan; ++i)
  1239. cvInBuffers[i] = cvIn.getReadPointer(i);
  1240. float inPeaks[2] = { 0.0f };
  1241. float outPeaks[2] = { 0.0f };
  1242. for (uint32_t i=0, count=jmin(fPlugin->getAudioInCount(), numChan2); i<count; ++i)
  1243. inPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1244. fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers,
  1245. cvInBuffers, cvOutBuffers,
  1246. numSamples);
  1247. for (uint32_t i=0, count=jmin(fPlugin->getAudioOutCount(), numChan2); i<count; ++i)
  1248. outPeaks[i] = carla_findMaxNormalizedFloat(audioBuffers[i], numSamples);
  1249. kEngine->setPluginPeaksRT(fPlugin->getId(), inPeaks, outPeaks);
  1250. }
  1251. else
  1252. {
  1253. // processing CV only, skip audiopeaks
  1254. float* cvOutBuffers[numCVOutChan];
  1255. const float* cvInBuffers[numCVInChan];
  1256. for (uint32_t i=0; i<numCVOutChan; ++i)
  1257. cvOutBuffers[i] = cvOut.getWritePointer(i);
  1258. for (uint32_t i=0; i<numCVInChan; ++i)
  1259. cvInBuffers[i] = cvIn.getReadPointer(i);
  1260. fPlugin->process(nullptr, nullptr,
  1261. cvInBuffers, cvOutBuffers,
  1262. numSamples);
  1263. }
  1264. midi.clear();
  1265. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  1266. {
  1267. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  1268. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1269. fillWaterMidiBufferFromEngineEvents(midi, engineEvents);
  1270. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1271. }
  1272. fPlugin->unlock();
  1273. }
  1274. const String getInputChannelName(ChannelType t, uint i) const override
  1275. {
  1276. CarlaEngineClient* const client = fPlugin->getEngineClient();
  1277. switch (t)
  1278. {
  1279. case ChannelTypeAudio:
  1280. return client->getAudioPortName(true, i);
  1281. case ChannelTypeCV:
  1282. return client->getCVPortName(true, i);
  1283. case ChannelTypeMIDI:
  1284. return client->getEventPortName(true, i);
  1285. }
  1286. return String();
  1287. }
  1288. const String getOutputChannelName(ChannelType t, uint i) const override
  1289. {
  1290. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1291. switch (t)
  1292. {
  1293. case ChannelTypeAudio:
  1294. return client->getAudioPortName(false, i);
  1295. case ChannelTypeCV:
  1296. return client->getCVPortName(false, i);
  1297. case ChannelTypeMIDI:
  1298. return client->getEventPortName(false, i);
  1299. }
  1300. return String();
  1301. }
  1302. void prepareToPlay(double, int) override {}
  1303. void releaseResources() override {}
  1304. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  1305. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  1306. private:
  1307. CarlaEngine* const kEngine;
  1308. CarlaPlugin* fPlugin;
  1309. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1310. };
  1311. // -----------------------------------------------------------------------
  1312. // Patchbay Graph
  1313. class NamedAudioGraphIOProcessor : public AudioProcessorGraph::AudioGraphIOProcessor
  1314. {
  1315. public:
  1316. NamedAudioGraphIOProcessor(const IODeviceType iotype)
  1317. : AudioProcessorGraph::AudioGraphIOProcessor(iotype),
  1318. inputNames(),
  1319. outputNames() {}
  1320. const String getInputChannelName (ChannelType, uint _index) const override
  1321. {
  1322. const int index = static_cast<int>(_index); // FIXME
  1323. if (index < inputNames.size())
  1324. return inputNames[index];
  1325. return String("Playback ") + String(index+1);
  1326. }
  1327. const String getOutputChannelName (ChannelType, uint _index) const override
  1328. {
  1329. const int index = static_cast<int>(_index); // FIXME
  1330. if (index < outputNames.size())
  1331. return outputNames[index];
  1332. return String("Capture ") + String(index+1);
  1333. }
  1334. void setNames(const bool setInputNames, const StringArray& names)
  1335. {
  1336. if (setInputNames)
  1337. inputNames = names;
  1338. else
  1339. outputNames = names;
  1340. }
  1341. private:
  1342. StringArray inputNames;
  1343. StringArray outputNames;
  1344. };
  1345. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine,
  1346. const uint32_t audioIns, const uint32_t audioOuts,
  1347. const uint32_t cvIns, const uint32_t cvOuts)
  1348. : CarlaThread("PatchbayReorderThread"),
  1349. connections(),
  1350. graph(),
  1351. audioBuffer(),
  1352. cvInBuffer(),
  1353. cvOutBuffer(),
  1354. midiBuffer(),
  1355. numAudioIns(carla_fixedValue(0U, 64U, audioIns)),
  1356. numAudioOuts(carla_fixedValue(0U, 64U, audioOuts)),
  1357. numCVIns(carla_fixedValue(0U, 8U, cvIns)),
  1358. numCVOuts(carla_fixedValue(0U, 8U, cvOuts)),
  1359. retCon(),
  1360. usingExternalHost(false),
  1361. usingExternalOSC(false),
  1362. extGraph(engine),
  1363. kEngine(engine)
  1364. {
  1365. const uint32_t bufferSize(engine->getBufferSize());
  1366. const double sampleRate(engine->getSampleRate());
  1367. graph.setPlayConfigDetails(numAudioIns, numAudioOuts,
  1368. numCVIns, numCVOuts,
  1369. 1, 1,
  1370. sampleRate, static_cast<int>(bufferSize));
  1371. graph.prepareToPlay(sampleRate, static_cast<int>(bufferSize));
  1372. audioBuffer.setSize(jmax(numAudioIns, numAudioOuts), bufferSize);
  1373. cvInBuffer.setSize(numCVIns, bufferSize);
  1374. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1375. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1376. midiBuffer.clear();
  1377. StringArray channelNames;
  1378. switch (numAudioIns)
  1379. {
  1380. case 2:
  1381. channelNames.add("Left");
  1382. channelNames.add("Right");
  1383. break;
  1384. case 3:
  1385. channelNames.add("Left");
  1386. channelNames.add("Right");
  1387. channelNames.add("Sidechain");
  1388. break;
  1389. }
  1390. if (numAudioIns != 0)
  1391. {
  1392. NamedAudioGraphIOProcessor* const proc(
  1393. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioInputNode));
  1394. proc->setNames(false, channelNames);
  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", true);
  1399. node->properties.set("isCV", false);
  1400. node->properties.set("isMIDI", false);
  1401. node->properties.set("isOSC", false);
  1402. }
  1403. if (numAudioOuts != 0)
  1404. {
  1405. NamedAudioGraphIOProcessor* const proc(
  1406. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::audioOutputNode));
  1407. proc->setNames(true, channelNames);
  1408. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1409. node->properties.set("isPlugin", false);
  1410. node->properties.set("isOutput", false);
  1411. node->properties.set("isAudio", true);
  1412. node->properties.set("isCV", false);
  1413. node->properties.set("isMIDI", false);
  1414. node->properties.set("isOSC", false);
  1415. }
  1416. if (numCVIns != 0)
  1417. {
  1418. NamedAudioGraphIOProcessor* const proc(
  1419. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvInputNode));
  1420. // proc->setNames(false, channelNames);
  1421. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1422. node->properties.set("isPlugin", false);
  1423. node->properties.set("isOutput", false);
  1424. node->properties.set("isAudio", false);
  1425. node->properties.set("isCV", true);
  1426. node->properties.set("isMIDI", false);
  1427. node->properties.set("isOSC", false);
  1428. }
  1429. if (numCVOuts != 0)
  1430. {
  1431. NamedAudioGraphIOProcessor* const proc(
  1432. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::cvOutputNode));
  1433. // proc->setNames(true, channelNames);
  1434. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1435. node->properties.set("isPlugin", false);
  1436. node->properties.set("isOutput", false);
  1437. node->properties.set("isAudio", false);
  1438. node->properties.set("isCV", true);
  1439. node->properties.set("isMIDI", false);
  1440. node->properties.set("isOSC", false);
  1441. }
  1442. {
  1443. NamedAudioGraphIOProcessor* const proc(
  1444. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiInputNode));
  1445. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1446. node->properties.set("isPlugin", false);
  1447. node->properties.set("isOutput", false);
  1448. node->properties.set("isAudio", false);
  1449. node->properties.set("isCV", false);
  1450. node->properties.set("isMIDI", true);
  1451. node->properties.set("isOSC", false);
  1452. }
  1453. {
  1454. NamedAudioGraphIOProcessor* const proc(
  1455. new NamedAudioGraphIOProcessor(NamedAudioGraphIOProcessor::midiOutputNode));
  1456. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1457. node->properties.set("isPlugin", false);
  1458. node->properties.set("isOutput", true);
  1459. node->properties.set("isAudio", false);
  1460. node->properties.set("isCV", false);
  1461. node->properties.set("isMIDI", true);
  1462. node->properties.set("isOSC", false);
  1463. }
  1464. startThread();
  1465. }
  1466. PatchbayGraph::~PatchbayGraph()
  1467. {
  1468. stopThread(-1);
  1469. connections.clear();
  1470. extGraph.clear();
  1471. graph.releaseResources();
  1472. graph.clear();
  1473. audioBuffer.clear();
  1474. cvInBuffer.clear();
  1475. cvOutBuffer.clear();
  1476. }
  1477. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1478. {
  1479. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1480. graph.releaseResources();
  1481. graph.prepareToPlay(kEngine->getSampleRate(), static_cast<int>(bufferSize));
  1482. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSize);
  1483. cvInBuffer.setSize(numCVIns, bufferSize);
  1484. cvOutBuffer.setSize(numCVOuts, bufferSize);
  1485. }
  1486. void PatchbayGraph::setSampleRate(const double sampleRate)
  1487. {
  1488. const CarlaRecursiveMutexLocker cml1(graph.getReorderMutex());
  1489. graph.releaseResources();
  1490. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1491. }
  1492. void PatchbayGraph::setOffline(const bool offline)
  1493. {
  1494. graph.setNonRealtime(offline);
  1495. }
  1496. void PatchbayGraph::addPlugin(CarlaPlugin* const plugin)
  1497. {
  1498. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1499. carla_debug("PatchbayGraph::addPlugin(%p)", plugin);
  1500. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1501. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1502. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1503. const bool sendHost = !usingExternalHost;
  1504. const bool sendOSC = !usingExternalOSC;
  1505. plugin->setPatchbayNodeId(node->nodeId);
  1506. node->properties.set("isPlugin", true);
  1507. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  1508. addNodeToPatchbay(sendHost, sendOSC, kEngine, node->nodeId, static_cast<int>(plugin->getId()), instance);
  1509. }
  1510. void PatchbayGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1511. {
  1512. CARLA_SAFE_ASSERT_RETURN(oldPlugin != nullptr,);
  1513. CARLA_SAFE_ASSERT_RETURN(newPlugin != nullptr,);
  1514. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1515. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1516. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1517. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1518. const bool sendHost = !usingExternalHost;
  1519. const bool sendOSC = !usingExternalOSC;
  1520. disconnectInternalGroup(oldNode->nodeId);
  1521. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, oldNode->nodeId, oldNode->getProcessor());
  1522. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1523. graph.removeNode(oldNode->nodeId);
  1524. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1525. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1526. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1527. newPlugin->setPatchbayNodeId(node->nodeId);
  1528. node->properties.set("isPlugin", true);
  1529. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  1530. addNodeToPatchbay(sendHost, sendOSC, kEngine, node->nodeId, static_cast<int>(newPlugin->getId()), instance);
  1531. }
  1532. void PatchbayGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  1533. {
  1534. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1535. carla_debug("PatchbayGraph::renamePlugin(%p)", plugin, newName);
  1536. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1537. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1538. const bool sendHost = !usingExternalHost;
  1539. const bool sendOSC = !usingExternalOSC;
  1540. kEngine->callback(sendHost, sendOSC,
  1541. ENGINE_CALLBACK_PATCHBAY_CLIENT_RENAMED,
  1542. node->nodeId,
  1543. 0, 0, 0, 0.0f,
  1544. newName);
  1545. }
  1546. void PatchbayGraph::reconfigureForCV(CarlaPlugin* const plugin, const uint portIndex, bool added)
  1547. {
  1548. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1549. carla_debug("PatchbayGraph::reconfigureForCV(%p, %u, %s)", plugin, portIndex, bool2str(added));
  1550. AudioProcessorGraph::Node* const node = graph.getNodeForId(plugin->getPatchbayNodeId());
  1551. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1552. CarlaPluginInstance* const proc = dynamic_cast<CarlaPluginInstance*>(node->getProcessor());
  1553. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  1554. const bool sendHost = !usingExternalHost;
  1555. const bool sendOSC = !usingExternalOSC;
  1556. const uint oldCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1557. // const uint oldCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1558. {
  1559. const CarlaRecursiveMutexLocker crml(graph.getCallbackLock());
  1560. proc->reconfigure();
  1561. graph.buildRenderingSequence();
  1562. }
  1563. const uint newCvIn = proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV);
  1564. // const uint newCvOut = proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV);
  1565. if (added)
  1566. {
  1567. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn > oldCvIn, newCvIn, oldCvIn,);
  1568. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut >= oldCvOut, newCvOut, oldCvOut,);
  1569. kEngine->callback(sendHost, sendOSC,
  1570. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  1571. node->nodeId,
  1572. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1573. PATCHBAY_PORT_TYPE_CV|PATCHBAY_PORT_IS_INPUT,
  1574. 0, 0.0f,
  1575. proc->getInputChannelName(AudioProcessor::ChannelTypeCV, portIndex).toRawUTF8());
  1576. }
  1577. else
  1578. {
  1579. CARLA_SAFE_ASSERT_UINT2_RETURN(newCvIn < oldCvIn, newCvIn, oldCvIn,);
  1580. // CARLA_SAFE_ASSERT_UINT2_RETURN(newCvOut <= oldCvOut, newCvOut, oldCvOut,);
  1581. kEngine->callback(sendHost, sendOSC,
  1582. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1583. node->nodeId,
  1584. static_cast<int>(kCVInputPortOffset + plugin->getCVInCount() + portIndex),
  1585. 0, 0, 0.0f, nullptr);
  1586. }
  1587. }
  1588. void PatchbayGraph::removePlugin(CarlaPlugin* const plugin)
  1589. {
  1590. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1591. carla_debug("PatchbayGraph::removePlugin(%p)", plugin);
  1592. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1593. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1594. const bool sendHost = !usingExternalHost;
  1595. const bool sendOSC = !usingExternalOSC;
  1596. disconnectInternalGroup(node->nodeId);
  1597. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1598. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1599. // Fix plugin Ids properties
  1600. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1601. {
  1602. CarlaPlugin* const plugin2(kEngine->getPlugin(i));
  1603. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  1604. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1605. {
  1606. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != water::var(-1));
  1607. node2->properties.set("pluginId", static_cast<int>(i-1));
  1608. }
  1609. }
  1610. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1611. }
  1612. void PatchbayGraph::removeAllPlugins()
  1613. {
  1614. carla_debug("PatchbayGraph::removeAllPlugins()");
  1615. const bool sendHost = !usingExternalHost;
  1616. const bool sendOSC = !usingExternalOSC;
  1617. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1618. {
  1619. CarlaPlugin* const plugin(kEngine->getPlugin(i));
  1620. CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
  1621. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1622. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1623. disconnectInternalGroup(node->nodeId);
  1624. removeNodeFromPatchbay(sendHost, sendOSC, kEngine, node->nodeId, node->getProcessor());
  1625. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1626. graph.removeNode(node->nodeId);
  1627. }
  1628. }
  1629. bool PatchbayGraph::connect(const bool external,
  1630. const uint groupA, const uint portA, const uint groupB, const uint portB)
  1631. {
  1632. if (external)
  1633. return extGraph.connect(usingExternalHost, usingExternalOSC, groupA, portA, groupB, portB);
  1634. uint adjustedPortA = portA;
  1635. uint adjustedPortB = portB;
  1636. AudioProcessor::ChannelType channelType;
  1637. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1638. return false;
  1639. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1640. return false;
  1641. if (! graph.addConnection(channelType, groupA, adjustedPortA, groupB, adjustedPortB))
  1642. {
  1643. kEngine->setLastError("Failed from water");
  1644. return false;
  1645. }
  1646. ConnectionToId connectionToId;
  1647. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1648. char strBuf[STR_MAX+1];
  1649. strBuf[STR_MAX] = '\0';
  1650. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1651. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1652. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1653. connectionToId.id,
  1654. 0, 0, 0, 0.0f,
  1655. strBuf);
  1656. connections.list.append(connectionToId);
  1657. return true;
  1658. }
  1659. bool PatchbayGraph::disconnect(const bool external, const uint connectionId)
  1660. {
  1661. if (external)
  1662. return extGraph.disconnect(usingExternalHost, usingExternalOSC, connectionId);
  1663. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1664. {
  1665. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1666. const ConnectionToId& connectionToId(it.getValue(fallback));
  1667. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1668. if (connectionToId.id != connectionId)
  1669. continue;
  1670. uint adjustedPortA = connectionToId.portA;
  1671. uint adjustedPortB = connectionToId.portB;
  1672. AudioProcessor::ChannelType channelType;
  1673. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortA))
  1674. return false;
  1675. if (! adjustPatchbayPortIdForWater(channelType, adjustedPortB))
  1676. return false;
  1677. if (! graph.removeConnection(channelType,
  1678. connectionToId.groupA, adjustedPortA,
  1679. connectionToId.groupB, adjustedPortB))
  1680. return false;
  1681. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1682. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1683. connectionToId.id,
  1684. 0, 0, 0, 0.0f,
  1685. nullptr);
  1686. connections.list.remove(it);
  1687. return true;
  1688. }
  1689. kEngine->setLastError("Failed to find connection");
  1690. return false;
  1691. }
  1692. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1693. {
  1694. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1695. {
  1696. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1697. const ConnectionToId& connectionToId(it.getValue(fallback));
  1698. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1699. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1700. continue;
  1701. /*
  1702. uint adjustedPortA = connectionToId.portA;
  1703. uint adjustedPortB = connectionToId.portB;
  1704. AudioProcessor::ChannelType channelType;
  1705. if (! adjustPatchbayPortIdForWater(adjustedPortA))
  1706. return false;
  1707. if (! adjustPatchbayPortIdForWater(adjustedPortB))
  1708. return false;
  1709. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1710. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1711. */
  1712. kEngine->callback(!usingExternalHost, !usingExternalOSC,
  1713. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1714. connectionToId.id,
  1715. 0, 0, 0, 0.0f,
  1716. nullptr);
  1717. connections.list.remove(it);
  1718. }
  1719. }
  1720. void PatchbayGraph::refresh(const bool sendHost, const bool sendOSC, const bool external, const char* const deviceName)
  1721. {
  1722. if (external)
  1723. return extGraph.refresh(sendHost, sendOSC, deviceName);
  1724. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1725. connections.clear();
  1726. graph.removeIllegalConnections();
  1727. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1728. {
  1729. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1730. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1731. AudioProcessor* const proc(node->getProcessor());
  1732. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1733. int clientId = -1;
  1734. // plugin node
  1735. if (node->properties.getWithDefault("isPlugin", false) == water::var(true))
  1736. clientId = node->properties.getWithDefault("pluginId", -1);
  1737. addNodeToPatchbay(sendHost, sendOSC, kEngine, node->nodeId, clientId, proc);
  1738. }
  1739. char strBuf[STR_MAX+1];
  1740. strBuf[STR_MAX] = '\0';
  1741. for (size_t i=0, count=graph.getNumConnections(); i<count; ++i)
  1742. {
  1743. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1744. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1745. const uint groupA = conn->sourceNodeId;
  1746. const uint groupB = conn->destNodeId;
  1747. uint portA = conn->sourceChannelIndex;
  1748. uint portB = conn->destChannelIndex;
  1749. switch (conn->channelType)
  1750. {
  1751. case AudioProcessor::ChannelTypeAudio:
  1752. portA += kAudioOutputPortOffset;
  1753. portB += kAudioInputPortOffset;
  1754. break;
  1755. case AudioProcessor::ChannelTypeCV:
  1756. portA += kCVOutputPortOffset;
  1757. portB += kCVInputPortOffset;
  1758. break;
  1759. case AudioProcessor::ChannelTypeMIDI:
  1760. portA += kMidiOutputPortOffset;
  1761. portB += kMidiInputPortOffset;
  1762. break;
  1763. }
  1764. ConnectionToId connectionToId;
  1765. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1766. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1767. kEngine->callback(sendHost, sendOSC,
  1768. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  1769. connectionToId.id,
  1770. 0, 0, 0, 0.0f,
  1771. strBuf);
  1772. connections.list.append(connectionToId);
  1773. }
  1774. }
  1775. const char* const* PatchbayGraph::getConnections(const bool external) const
  1776. {
  1777. if (external)
  1778. return extGraph.getConnections();
  1779. if (connections.list.count() == 0)
  1780. return nullptr;
  1781. CarlaStringList connList;
  1782. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1783. {
  1784. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1785. const ConnectionToId& connectionToId(it.getValue(fallback));
  1786. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1787. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1788. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1789. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1790. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1791. AudioProcessor* const procA(nodeA->getProcessor());
  1792. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1793. AudioProcessor* const procB(nodeB->getProcessor());
  1794. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1795. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1796. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1797. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1798. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1799. connList.append(fullPortNameA.toRawUTF8());
  1800. connList.append(fullPortNameB.toRawUTF8());
  1801. }
  1802. if (connList.count() == 0)
  1803. return nullptr;
  1804. retCon = connList.toCharStringListPtr();
  1805. return retCon;
  1806. }
  1807. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  1808. {
  1809. if (external)
  1810. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  1811. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1812. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1813. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1814. {
  1815. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1816. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1817. AudioProcessor* const proc(node->getProcessor());
  1818. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1819. if (proc->getName() != groupName)
  1820. continue;
  1821. groupId = node->nodeId;
  1822. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeAudio); j<numInputs; ++j)
  1823. {
  1824. if (proc->getInputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  1825. continue;
  1826. portId = kAudioInputPortOffset+j;
  1827. return true;
  1828. }
  1829. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeAudio); j<numOutputs; ++j)
  1830. {
  1831. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeAudio, j) != portName)
  1832. continue;
  1833. portId = kAudioOutputPortOffset+j;
  1834. return true;
  1835. }
  1836. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeCV); j<numInputs; ++j)
  1837. {
  1838. if (proc->getInputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  1839. continue;
  1840. portId = kCVInputPortOffset+j;
  1841. return true;
  1842. }
  1843. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeCV); j<numOutputs; ++j)
  1844. {
  1845. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeCV, j) != portName)
  1846. continue;
  1847. portId = kCVOutputPortOffset+j;
  1848. return true;
  1849. }
  1850. for (uint j=0, numInputs=proc->getTotalNumInputChannels(AudioProcessor::ChannelTypeMIDI); j<numInputs; ++j)
  1851. {
  1852. if (proc->getInputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  1853. continue;
  1854. portId = kMidiInputPortOffset+j;
  1855. return true;
  1856. }
  1857. for (uint j=0, numOutputs=proc->getTotalNumOutputChannels(AudioProcessor::ChannelTypeMIDI); j<numOutputs; ++j)
  1858. {
  1859. if (proc->getOutputChannelName(AudioProcessor::ChannelTypeMIDI, j) != portName)
  1860. continue;
  1861. portId = kMidiOutputPortOffset+j;
  1862. return true;
  1863. }
  1864. }
  1865. return false;
  1866. }
  1867. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data,
  1868. const float* const* const inBuf,
  1869. float* const* const outBuf,
  1870. const uint32_t frames)
  1871. {
  1872. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1873. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  1874. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  1875. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  1876. // put events in water buffer
  1877. {
  1878. midiBuffer.clear();
  1879. fillWaterMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  1880. }
  1881. // set audio and cv buffer size, needed for water internals
  1882. if (! audioBuffer.setSizeRT(frames))
  1883. return;
  1884. if (! cvInBuffer.setSizeRT(frames))
  1885. return;
  1886. if (! cvOutBuffer.setSizeRT(frames))
  1887. return;
  1888. // put carla audio and cv in water buffer
  1889. {
  1890. uint32_t i=0;
  1891. for (; i < numAudioIns; ++i) {
  1892. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  1893. audioBuffer.copyFrom(i, 0, inBuf[i], frames);
  1894. }
  1895. for (uint32_t j=0; j < numCVIns; ++j, ++i) {
  1896. CARLA_SAFE_ASSERT_BREAK(inBuf[i]);
  1897. cvInBuffer.copyFrom(j, 0, inBuf[i], frames);
  1898. }
  1899. // clear remaining channels
  1900. for (uint32_t j=numAudioIns, count=audioBuffer.getNumChannels(); j < count; ++j)
  1901. audioBuffer.clear(j, 0, frames);
  1902. for (uint32_t j=0; j < numCVOuts; ++j)
  1903. cvOutBuffer.clear(j, 0, frames);
  1904. }
  1905. // ready to go!
  1906. graph.processBlockWithCV(audioBuffer, cvInBuffer, cvOutBuffer, midiBuffer);
  1907. // put water audio and cv in carla buffer
  1908. {
  1909. uint32_t i=0;
  1910. for (; i < numAudioOuts; ++i)
  1911. carla_copyFloats(outBuf[i], audioBuffer.getReadPointer(i), frames);
  1912. for (uint32_t j=0; j < numCVOuts; ++j, ++i)
  1913. carla_copyFloats(outBuf[i], cvOutBuffer.getReadPointer(j), frames);
  1914. }
  1915. // put water events in carla buffer
  1916. {
  1917. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  1918. fillEngineEventsFromWaterMidiBuffer(data->events.out, midiBuffer);
  1919. midiBuffer.clear();
  1920. }
  1921. }
  1922. void PatchbayGraph::run()
  1923. {
  1924. while (! shouldThreadExit())
  1925. {
  1926. carla_msleep(100);
  1927. graph.reorderNowIfNeeded();
  1928. }
  1929. }
  1930. // -----------------------------------------------------------------------
  1931. // InternalGraph
  1932. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  1933. : fIsRack(false),
  1934. fIsReady(false),
  1935. fRack(nullptr),
  1936. kEngine(engine) {}
  1937. EngineInternalGraph::~EngineInternalGraph() noexcept
  1938. {
  1939. CARLA_SAFE_ASSERT(! fIsReady);
  1940. CARLA_SAFE_ASSERT(fRack == nullptr);
  1941. }
  1942. void EngineInternalGraph::create(const uint32_t audioIns, const uint32_t audioOuts,
  1943. const uint32_t cvIns, const uint32_t cvOuts)
  1944. {
  1945. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  1946. if (fIsRack)
  1947. {
  1948. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  1949. fRack = new RackGraph(kEngine, audioIns, audioOuts);
  1950. }
  1951. else
  1952. {
  1953. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  1954. fPatchbay = new PatchbayGraph(kEngine, audioIns, audioOuts, cvIns, cvOuts);
  1955. }
  1956. fIsReady = true;
  1957. }
  1958. void EngineInternalGraph::destroy() noexcept
  1959. {
  1960. if (! fIsReady)
  1961. {
  1962. CARLA_SAFE_ASSERT(fRack == nullptr);
  1963. return;
  1964. }
  1965. if (fIsRack)
  1966. {
  1967. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1968. delete fRack;
  1969. fRack = nullptr;
  1970. }
  1971. else
  1972. {
  1973. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1974. delete fPatchbay;
  1975. fPatchbay = nullptr;
  1976. }
  1977. fIsReady = false;
  1978. }
  1979. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  1980. {
  1981. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  1982. if (fIsRack)
  1983. {
  1984. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1985. fRack->setBufferSize(bufferSize);
  1986. }
  1987. else
  1988. {
  1989. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1990. fPatchbay->setBufferSize(bufferSize);
  1991. }
  1992. }
  1993. void EngineInternalGraph::setSampleRate(const double sampleRate)
  1994. {
  1995. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  1996. if (fIsRack)
  1997. {
  1998. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1999. }
  2000. else
  2001. {
  2002. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2003. fPatchbay->setSampleRate(sampleRate);
  2004. }
  2005. }
  2006. void EngineInternalGraph::setOffline(const bool offline)
  2007. {
  2008. CarlaScopedValueSetter<volatile bool> svs(fIsReady, false, true);
  2009. if (fIsRack)
  2010. {
  2011. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2012. fRack->setOffline(offline);
  2013. }
  2014. else
  2015. {
  2016. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2017. fPatchbay->setOffline(offline);
  2018. }
  2019. }
  2020. bool EngineInternalGraph::isReady() const noexcept
  2021. {
  2022. return fIsReady;
  2023. }
  2024. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  2025. {
  2026. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  2027. return fRack;
  2028. }
  2029. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  2030. {
  2031. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  2032. return fPatchbay;
  2033. }
  2034. PatchbayGraph* EngineInternalGraph::getPatchbayGraphOrNull() const noexcept
  2035. {
  2036. return fIsRack ? nullptr : fPatchbay;
  2037. }
  2038. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  2039. {
  2040. if (fIsRack)
  2041. {
  2042. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2043. fRack->processHelper(data, inBuf, outBuf, frames);
  2044. }
  2045. else
  2046. {
  2047. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2048. fPatchbay->process(data, inBuf, outBuf, frames);
  2049. }
  2050. }
  2051. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  2052. {
  2053. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  2054. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  2055. fRack->process(data, inBuf, outBuf, frames);
  2056. }
  2057. // -----------------------------------------------------------------------
  2058. // used for internal patchbay mode
  2059. void EngineInternalGraph::addPlugin(CarlaPlugin* const plugin)
  2060. {
  2061. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2062. fPatchbay->addPlugin(plugin);
  2063. }
  2064. void EngineInternalGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  2065. {
  2066. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2067. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  2068. }
  2069. void EngineInternalGraph::renamePlugin(CarlaPlugin* const plugin, const char* const newName)
  2070. {
  2071. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2072. fPatchbay->renamePlugin(plugin, newName);
  2073. }
  2074. void EngineInternalGraph::removePlugin(CarlaPlugin* const plugin)
  2075. {
  2076. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2077. fPatchbay->removePlugin(plugin);
  2078. }
  2079. void EngineInternalGraph::removeAllPlugins()
  2080. {
  2081. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2082. fPatchbay->removeAllPlugins();
  2083. }
  2084. bool EngineInternalGraph::isUsingExternalHost() const noexcept
  2085. {
  2086. if (fIsRack)
  2087. return true;
  2088. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2089. return fPatchbay->usingExternalHost;
  2090. }
  2091. bool EngineInternalGraph::isUsingExternalOSC() const noexcept
  2092. {
  2093. if (fIsRack)
  2094. return true;
  2095. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  2096. return fPatchbay->usingExternalOSC;
  2097. }
  2098. void EngineInternalGraph::setUsingExternalHost(const bool usingExternal) noexcept
  2099. {
  2100. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2101. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2102. fPatchbay->usingExternalHost = usingExternal;
  2103. }
  2104. void EngineInternalGraph::setUsingExternalOSC(const bool usingExternal) noexcept
  2105. {
  2106. CARLA_SAFE_ASSERT_RETURN(! fIsRack,);
  2107. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  2108. fPatchbay->usingExternalOSC = usingExternal;
  2109. }
  2110. // -----------------------------------------------------------------------
  2111. // CarlaEngine Patchbay stuff
  2112. bool CarlaEngine::patchbayConnect(const bool external,
  2113. const uint groupA, const uint portA,
  2114. const uint groupB, const uint portB)
  2115. {
  2116. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2117. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2118. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  2119. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2120. {
  2121. RackGraph* const graph = pData->graph.getRackGraph();
  2122. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2123. return graph->connect(groupA, portA, groupB, portB);
  2124. }
  2125. else
  2126. {
  2127. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2128. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2129. return graph->connect(external, groupA, portA, groupB, portB);
  2130. }
  2131. return false;
  2132. }
  2133. bool CarlaEngine::patchbayDisconnect(const bool external, const uint connectionId)
  2134. {
  2135. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  2136. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  2137. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  2138. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2139. {
  2140. RackGraph* const graph = pData->graph.getRackGraph();
  2141. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2142. return graph->disconnect(connectionId);
  2143. }
  2144. else
  2145. {
  2146. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2147. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2148. return graph->disconnect(external, connectionId);
  2149. }
  2150. return false;
  2151. }
  2152. bool CarlaEngine::patchbayRefresh(const bool sendHost, const bool sendOSC, const bool external)
  2153. {
  2154. // subclasses should handle this
  2155. CARLA_SAFE_ASSERT_RETURN(! external, false);
  2156. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2157. {
  2158. // This is implemented in engine subclasses
  2159. setLastError("Unsupported operation");
  2160. return false;
  2161. }
  2162. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2163. {
  2164. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2165. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2166. graph->refresh(sendHost, sendOSC, external, "");
  2167. return true;
  2168. }
  2169. setLastError("Unsupported operation");
  2170. return false;
  2171. }
  2172. // -----------------------------------------------------------------------
  2173. // Patchbay stuff
  2174. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  2175. {
  2176. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  2177. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  2178. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2179. {
  2180. RackGraph* const graph = pData->graph.getRackGraph();
  2181. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2182. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  2183. return graph->getConnections();
  2184. }
  2185. else
  2186. {
  2187. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2188. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  2189. return graph->getConnections(external);
  2190. }
  2191. return nullptr;
  2192. }
  2193. void CarlaEngine::restorePatchbayConnection(const bool external,
  2194. const char* const sourcePort,
  2195. const char* const targetPort)
  2196. {
  2197. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  2198. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  2199. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  2200. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  2201. uint groupA, portA;
  2202. uint groupB, portB;
  2203. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2204. {
  2205. RackGraph* const graph = pData->graph.getRackGraph();
  2206. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2207. CARLA_SAFE_ASSERT_RETURN(external,);
  2208. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  2209. return;
  2210. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  2211. return;
  2212. graph->connect(groupA, portA, groupB, portB);
  2213. }
  2214. else
  2215. {
  2216. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  2217. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  2218. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  2219. return;
  2220. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  2221. return;
  2222. graph->connect(external, groupA, portA, groupB, portB);
  2223. }
  2224. }
  2225. // -----------------------------------------------------------------------
  2226. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2227. {
  2228. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2229. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2230. RackGraph* const graph(pData->graph.getRackGraph());
  2231. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2232. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2233. switch (connectionType)
  2234. {
  2235. case kExternalGraphConnectionAudioIn1:
  2236. return graph->audioBuffers.connectedIn1.append(portId);
  2237. case kExternalGraphConnectionAudioIn2:
  2238. return graph->audioBuffers.connectedIn2.append(portId);
  2239. case kExternalGraphConnectionAudioOut1:
  2240. return graph->audioBuffers.connectedOut1.append(portId);
  2241. case kExternalGraphConnectionAudioOut2:
  2242. return graph->audioBuffers.connectedOut2.append(portId);
  2243. }
  2244. return false;
  2245. }
  2246. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  2247. {
  2248. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  2249. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  2250. RackGraph* const graph(pData->graph.getRackGraph());
  2251. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  2252. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  2253. switch (connectionType)
  2254. {
  2255. case kExternalGraphConnectionAudioIn1:
  2256. return graph->audioBuffers.connectedIn1.removeOne(portId);
  2257. case kExternalGraphConnectionAudioIn2:
  2258. return graph->audioBuffers.connectedIn2.removeOne(portId);
  2259. case kExternalGraphConnectionAudioOut1:
  2260. return graph->audioBuffers.connectedOut1.removeOne(portId);
  2261. case kExternalGraphConnectionAudioOut2:
  2262. return graph->audioBuffers.connectedOut2.removeOne(portId);
  2263. }
  2264. return false;
  2265. }
  2266. // -----------------------------------------------------------------------
  2267. CARLA_BACKEND_END_NAMESPACE