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.

2706 lines
93KB

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