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.

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