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.

2233 lines
76KB

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