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.

2239 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.begin2() : outs.begin2(); 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.begin2() : outs.begin2(); 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.begin2(); it.valid(); it.next())
  193. {
  194. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  195. const ConnectionToId& connectionToId(it.getValue(fallback));
  196. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  197. if (connectionToId.id != connectionId)
  198. continue;
  199. uint otherGroup, otherPort, carlaPort;
  200. if (connectionToId.groupA == kExternalGraphGroupCarla)
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB != kExternalGraphGroupCarla, false);
  203. carlaPort = connectionToId.portA;
  204. otherGroup = connectionToId.groupB;
  205. otherPort = connectionToId.portB;
  206. }
  207. else
  208. {
  209. CARLA_SAFE_ASSERT_RETURN(connectionToId.groupB == kExternalGraphGroupCarla, false);
  210. carlaPort = connectionToId.portB;
  211. otherGroup = connectionToId.groupA;
  212. otherPort = connectionToId.portA;
  213. }
  214. CARLA_SAFE_ASSERT_RETURN(carlaPort > kExternalGraphCarlaPortNull && carlaPort < kExternalGraphCarlaPortMax, false);
  215. CARLA_SAFE_ASSERT_RETURN(otherGroup > kExternalGraphGroupCarla && otherGroup < kExternalGraphGroupMax, false);
  216. bool makeDisconnection = false;
  217. switch (carlaPort)
  218. {
  219. case kExternalGraphCarlaPortAudioIn1:
  220. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn1, otherPort, nullptr);
  221. break;
  222. case kExternalGraphCarlaPortAudioIn2:
  223. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioIn2, otherPort, nullptr);
  224. break;
  225. case kExternalGraphCarlaPortAudioOut1:
  226. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut1, otherPort, nullptr);
  227. break;
  228. case kExternalGraphCarlaPortAudioOut2:
  229. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionAudioOut2, otherPort, nullptr);
  230. break;
  231. case kExternalGraphCarlaPortMidiIn:
  232. if (const char* const portName = midiPorts.getName(true, otherPort))
  233. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiInput, 0, portName);
  234. break;
  235. case kExternalGraphCarlaPortMidiOut:
  236. if (const char* const portName = midiPorts.getName(false, otherPort))
  237. makeDisconnection = kEngine->disconnectExternalGraphPort(kExternalGraphConnectionMidiOutput, 0, portName);
  238. break;
  239. }
  240. if (! makeDisconnection)
  241. {
  242. kEngine->setLastError("Invalid rack connection");
  243. return false;
  244. }
  245. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  246. connections.list.remove(it);
  247. return true;
  248. }
  249. kEngine->setLastError("Failed to find connection");
  250. return false;
  251. }
  252. void ExternalGraph::refresh(const char* const deviceName)
  253. {
  254. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  255. const bool isRack(kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  256. // Main
  257. {
  258. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, kExternalGraphGroupCarla, PATCHBAY_ICON_CARLA, -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.begin2(); 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.begin2(); 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.begin2(); 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.begin2(); 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.begin2(); 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.begin2(); 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.begin2(); 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.begin2(); 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.begin2(); 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_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  633. uint32_t oldAudioInCount = 0;
  634. uint32_t oldAudioOutCount = 0;
  635. uint32_t oldMidiOutCount = 0;
  636. bool processed = false;
  637. juce::Range<float> range;
  638. // process plugins
  639. for (uint i=0; i < data->curPluginCount; ++i)
  640. {
  641. CarlaPlugin* const plugin = data->plugins[i].plugin;
  642. if (plugin == nullptr || ! plugin->isEnabled() || ! plugin->tryLock(isOffline))
  643. continue;
  644. if (processed)
  645. {
  646. // initialize audio inputs (from previous outputs)
  647. FloatVectorOperations::copy(inBuf0, outBuf[0], iframes);
  648. FloatVectorOperations::copy(inBuf1, outBuf[1], iframes);
  649. // initialize audio outputs (zero)
  650. FloatVectorOperations::clear(outBuf[0], iframes);
  651. FloatVectorOperations::clear(outBuf[1], iframes);
  652. // if plugin has no midi out, add previous events
  653. if (oldMidiOutCount == 0 && data->events.in[0].type != kEngineEventTypeNull)
  654. {
  655. if (data->events.out[0].type != kEngineEventTypeNull)
  656. {
  657. // TODO: carefully add to input, sorted events
  658. }
  659. // else nothing needed
  660. }
  661. else
  662. {
  663. // initialize event inputs from previous outputs
  664. carla_copyStructs(data->events.in, data->events.out, kMaxEngineEventInternalCount);
  665. // initialize event outputs (zero)
  666. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  667. }
  668. }
  669. oldAudioInCount = plugin->getAudioInCount();
  670. oldAudioOutCount = plugin->getAudioOutCount();
  671. oldMidiOutCount = plugin->getMidiOutCount();
  672. // process
  673. plugin->initBuffers();
  674. plugin->process(inBuf, outBuf, nullptr, nullptr, frames);
  675. plugin->unlock();
  676. // if plugin has no audio inputs, add input buffer
  677. if (oldAudioInCount == 0)
  678. {
  679. FloatVectorOperations::add(outBuf[0], inBuf0, iframes);
  680. FloatVectorOperations::add(outBuf[1], inBuf1, iframes);
  681. }
  682. // if plugin only has 1 output, copy it to the 2nd
  683. if (oldAudioOutCount == 1)
  684. {
  685. FloatVectorOperations::copy(outBuf[1], outBuf[0], iframes);
  686. }
  687. // set peaks
  688. {
  689. EnginePluginData& pluginData(data->plugins[i]);
  690. if (oldAudioInCount > 0)
  691. {
  692. range = FloatVectorOperations::findMinAndMax(inBuf0, iframes);
  693. pluginData.insPeak[0] = carla_maxLimited<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  694. range = FloatVectorOperations::findMinAndMax(inBuf1, iframes);
  695. pluginData.insPeak[1] = carla_maxLimited<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  696. }
  697. else
  698. {
  699. pluginData.insPeak[0] = 0.0f;
  700. pluginData.insPeak[1] = 0.0f;
  701. }
  702. if (oldAudioOutCount > 0)
  703. {
  704. range = FloatVectorOperations::findMinAndMax(outBuf[0], iframes);
  705. pluginData.outsPeak[0] = carla_maxLimited<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  706. range = FloatVectorOperations::findMinAndMax(outBuf[1], iframes);
  707. pluginData.outsPeak[1] = carla_maxLimited<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  708. }
  709. else
  710. {
  711. pluginData.outsPeak[0] = 0.0f;
  712. pluginData.outsPeak[1] = 0.0f;
  713. }
  714. }
  715. processed = true;
  716. }
  717. }
  718. void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  719. {
  720. CARLA_SAFE_ASSERT_RETURN(audioBuffers.outBuf[1] != nullptr,);
  721. const int iframes(static_cast<int>(frames));
  722. const CarlaRecursiveMutexLocker _cml(audioBuffers.mutex);
  723. if (inBuf != nullptr && inputs > 0)
  724. {
  725. bool noConnections = true;
  726. // connect input buffers
  727. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn1.begin2(); it.valid(); it.next())
  728. {
  729. const uint& port(it.getValue(0));
  730. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  731. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  732. if (noConnections)
  733. {
  734. FloatVectorOperations::copy(audioBuffers.inBuf[0], inBuf[port], iframes);
  735. noConnections = false;
  736. }
  737. else
  738. {
  739. FloatVectorOperations::add(audioBuffers.inBuf[0], inBuf[port], iframes);
  740. }
  741. }
  742. if (noConnections)
  743. FloatVectorOperations::clear(audioBuffers.inBuf[0], iframes);
  744. noConnections = true;
  745. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedIn2.begin2(); it.valid(); it.next())
  746. {
  747. const uint& port(it.getValue(0));
  748. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  749. CARLA_SAFE_ASSERT_CONTINUE(port <= inputs);
  750. if (noConnections)
  751. {
  752. FloatVectorOperations::copy(audioBuffers.inBuf[1], inBuf[port-1], iframes);
  753. noConnections = false;
  754. }
  755. else
  756. {
  757. FloatVectorOperations::add(audioBuffers.inBuf[1], inBuf[port-1], iframes);
  758. }
  759. }
  760. if (noConnections)
  761. FloatVectorOperations::clear(audioBuffers.inBuf[1], iframes);
  762. }
  763. else
  764. {
  765. FloatVectorOperations::clear(audioBuffers.inBuf[0], iframes);
  766. FloatVectorOperations::clear(audioBuffers.inBuf[1], iframes);
  767. }
  768. FloatVectorOperations::clear(audioBuffers.outBuf[0], iframes);
  769. FloatVectorOperations::clear(audioBuffers.outBuf[1], iframes);
  770. // process
  771. process(data, const_cast<const float**>(audioBuffers.inBuf), audioBuffers.outBuf, frames);
  772. // connect output buffers
  773. if (audioBuffers.connectedOut1.count() != 0)
  774. {
  775. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut1.begin2(); it.valid(); it.next())
  776. {
  777. const uint& port(it.getValue(0));
  778. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  779. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  780. FloatVectorOperations::add(outBuf[port-1], audioBuffers.outBuf[0], iframes);
  781. }
  782. }
  783. if (audioBuffers.connectedOut2.count() != 0)
  784. {
  785. for (LinkedList<uint>::Itenerator it = audioBuffers.connectedOut2.begin2(); it.valid(); it.next())
  786. {
  787. const uint& port(it.getValue(0));
  788. CARLA_SAFE_ASSERT_CONTINUE(port > 0);
  789. CARLA_SAFE_ASSERT_CONTINUE(port <= outputs);
  790. FloatVectorOperations::add(outBuf[port-1], audioBuffers.outBuf[1], iframes);
  791. }
  792. }
  793. }
  794. // -----------------------------------------------------------------------
  795. // Patchbay Graph stuff
  796. static const uint32_t kAudioInputPortOffset = MAX_PATCHBAY_PLUGINS*1;
  797. static const uint32_t kAudioOutputPortOffset = MAX_PATCHBAY_PLUGINS*2;
  798. static const uint32_t kMidiInputPortOffset = MAX_PATCHBAY_PLUGINS*3;
  799. static const uint32_t kMidiOutputPortOffset = MAX_PATCHBAY_PLUGINS*3+1;
  800. static const uint kMidiChannelIndex = static_cast<uint>(AudioProcessorGraph::midiChannelIndex);
  801. static inline
  802. bool adjustPatchbayPortIdForJuce(uint& portId)
  803. {
  804. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, false);
  805. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, false);
  806. if (portId == kMidiInputPortOffset)
  807. {
  808. portId = kMidiChannelIndex;
  809. return true;
  810. }
  811. if (portId == kMidiOutputPortOffset)
  812. {
  813. portId = kMidiChannelIndex;
  814. return true;
  815. }
  816. if (portId >= kAudioOutputPortOffset)
  817. {
  818. portId -= kAudioOutputPortOffset;
  819. return true;
  820. }
  821. if (portId >= kAudioInputPortOffset)
  822. {
  823. portId -= kAudioInputPortOffset;
  824. return true;
  825. }
  826. return false;
  827. }
  828. static inline
  829. const String getProcessorFullPortName(AudioProcessor* const proc, const uint32_t portId)
  830. {
  831. CARLA_SAFE_ASSERT_RETURN(proc != nullptr, String());
  832. CARLA_SAFE_ASSERT_RETURN(portId >= kAudioInputPortOffset, String());
  833. CARLA_SAFE_ASSERT_RETURN(portId <= kMidiOutputPortOffset, String());
  834. String fullPortName(proc->getName());
  835. if (portId == kMidiOutputPortOffset)
  836. {
  837. fullPortName += ":events-out";
  838. }
  839. else if (portId == kMidiInputPortOffset)
  840. {
  841. fullPortName += ":events-in";
  842. }
  843. else if (portId >= kAudioOutputPortOffset)
  844. {
  845. CARLA_SAFE_ASSERT_RETURN(proc->getNumOutputChannels() > 0, String());
  846. fullPortName += ":" + proc->getOutputChannelName(static_cast<int>(portId-kAudioOutputPortOffset));
  847. }
  848. else if (portId >= kAudioInputPortOffset)
  849. {
  850. CARLA_SAFE_ASSERT_RETURN(proc->getNumInputChannels() > 0, String());
  851. fullPortName += ":" + proc->getInputChannelName(static_cast<int>(portId-kAudioInputPortOffset));
  852. }
  853. else
  854. {
  855. return String();
  856. }
  857. return fullPortName;
  858. }
  859. static inline
  860. void addNodeToPatchbay(CarlaEngine* const engine, const uint32_t groupId, const int clientId, const AudioProcessor* const proc)
  861. {
  862. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  863. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  864. const int icon((clientId >= 0) ? PATCHBAY_ICON_PLUGIN : PATCHBAY_ICON_HARDWARE);
  865. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, groupId, icon, clientId, 0.0f, proc->getName().toRawUTF8());
  866. for (int i=0, numInputs=proc->getNumInputChannels(); i<numInputs; ++i)
  867. {
  868. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  869. PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, proc->getInputChannelName(i).toRawUTF8());
  870. }
  871. for (int i=0, numOutputs=proc->getNumOutputChannels(); i<numOutputs; ++i)
  872. {
  873. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  874. PATCHBAY_PORT_TYPE_AUDIO, 0.0f, proc->getOutputChannelName(i).toRawUTF8());
  875. }
  876. if (proc->acceptsMidi())
  877. {
  878. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiInputPortOffset),
  879. PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "events-in");
  880. }
  881. if (proc->producesMidi())
  882. {
  883. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, groupId, static_cast<int>(kMidiOutputPortOffset),
  884. PATCHBAY_PORT_TYPE_MIDI, 0.0f, "events-out");
  885. }
  886. }
  887. static inline
  888. void removeNodeFromPatchbay(CarlaEngine* const engine, const uint32_t groupId, const AudioProcessor* const proc)
  889. {
  890. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  891. CARLA_SAFE_ASSERT_RETURN(proc != nullptr,);
  892. for (int i=0, numInputs=proc->getNumInputChannels(); i<numInputs; ++i)
  893. {
  894. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioInputPortOffset)+i,
  895. 0, 0.0f, nullptr);
  896. }
  897. for (int i=0, numOutputs=proc->getNumOutputChannels(); i<numOutputs; ++i)
  898. {
  899. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kAudioOutputPortOffset)+i,
  900. 0, 0.0f, nullptr);
  901. }
  902. if (proc->acceptsMidi())
  903. {
  904. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiInputPortOffset),
  905. 0, 0.0f, nullptr);
  906. }
  907. if (proc->producesMidi())
  908. {
  909. engine->callback(ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED, groupId, static_cast<int>(kMidiOutputPortOffset),
  910. 0, 0.0f, nullptr);
  911. }
  912. engine->callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED, groupId, 0, 0, 0.0f, nullptr);
  913. }
  914. // -----------------------------------------------------------------------
  915. class CarlaPluginInstance : public AudioPluginInstance
  916. {
  917. public:
  918. CarlaPluginInstance(CarlaEngine* const engine, CarlaPlugin* const plugin)
  919. : kEngine(engine),
  920. fPlugin(plugin),
  921. leakDetector_CarlaPluginInstance()
  922. {
  923. setPlayConfigDetails(static_cast<int>(fPlugin->getAudioInCount()),
  924. static_cast<int>(fPlugin->getAudioOutCount()),
  925. getSampleRate(), getBlockSize());
  926. }
  927. ~CarlaPluginInstance() override
  928. {
  929. }
  930. void invalidatePlugin() noexcept
  931. {
  932. fPlugin = nullptr;
  933. }
  934. // -------------------------------------------------------------------
  935. void* getPlatformSpecificData() noexcept override
  936. {
  937. return fPlugin;
  938. }
  939. void fillInPluginDescription(PluginDescription& d) const override
  940. {
  941. d.pluginFormatName = "Carla";
  942. d.category = "Carla Plugin";
  943. d.version = "1.0";
  944. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  945. char strBuf[STR_MAX+1];
  946. strBuf[STR_MAX] = '\0';
  947. fPlugin->getRealName(strBuf);
  948. d.name = strBuf;
  949. fPlugin->getLabel(strBuf);
  950. d.descriptiveName = strBuf;
  951. fPlugin->getMaker(strBuf);
  952. d.manufacturerName = strBuf;
  953. d.uid = d.name.hashCode();
  954. d.isInstrument = (fPlugin->getHints() & PLUGIN_IS_SYNTH);
  955. d.numInputChannels = static_cast<int>(fPlugin->getAudioInCount());
  956. d.numOutputChannels = static_cast<int>(fPlugin->getAudioOutCount());
  957. //d.hasSharedContainer = true;
  958. }
  959. // -------------------------------------------------------------------
  960. const String getName() const override
  961. {
  962. return fPlugin->getName();
  963. }
  964. void processBlock(AudioSampleBuffer& audio, MidiBuffer& midi)
  965. {
  966. if (fPlugin == nullptr || ! fPlugin->isEnabled())
  967. {
  968. audio.clear();
  969. midi.clear();
  970. return;
  971. }
  972. if (! fPlugin->tryLock(kEngine->isOffline()))
  973. {
  974. audio.clear();
  975. midi.clear();
  976. return;
  977. }
  978. fPlugin->initBuffers();
  979. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  980. {
  981. EngineEvent* const engineEvents(port->fBuffer);
  982. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  983. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  984. fillEngineEventsFromJuceMidiBuffer(engineEvents, midi);
  985. }
  986. midi.clear();
  987. // TODO - CV support
  988. const int numSamples(audio.getNumSamples());
  989. if (const int numChan = audio.getNumChannels())
  990. {
  991. if (fPlugin->getAudioInCount() == 0)
  992. audio.clear();
  993. float* audioBuffers[numChan];
  994. for (int i=0; i<numChan; ++i)
  995. audioBuffers[i] = audio.getWritePointer(i);
  996. float inPeaks[2] = { 0.0f };
  997. float outPeaks[2] = { 0.0f };
  998. juce::Range<float> range;
  999. for (int i=jmin(fPlugin->getAudioInCount(), 2U); --i>=0;)
  1000. {
  1001. range = FloatVectorOperations::findMinAndMax(audioBuffers[i], numSamples);
  1002. inPeaks[i] = carla_maxLimited<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  1003. }
  1004. fPlugin->process(const_cast<const float**>(audioBuffers), audioBuffers, nullptr, nullptr, static_cast<uint32_t>(numSamples));
  1005. for (int i=jmin(fPlugin->getAudioOutCount(), 2U); --i>=0;)
  1006. {
  1007. range = FloatVectorOperations::findMinAndMax(audioBuffers[i], numSamples);
  1008. outPeaks[i] = carla_maxLimited<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  1009. }
  1010. kEngine->setPluginPeaks(fPlugin->getId(), inPeaks, outPeaks);
  1011. }
  1012. else
  1013. {
  1014. fPlugin->process(nullptr, nullptr, nullptr, nullptr, static_cast<uint32_t>(numSamples));
  1015. }
  1016. midi.clear();
  1017. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  1018. {
  1019. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  1020. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  1021. fillJuceMidiBufferFromEngineEvents(midi, engineEvents);
  1022. carla_zeroStructs(engineEvents, kMaxEngineEventInternalCount);
  1023. }
  1024. fPlugin->unlock();
  1025. }
  1026. const String getInputChannelName(int i) const override
  1027. {
  1028. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  1029. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1030. return client->getAudioPortName(true, static_cast<uint>(i));
  1031. }
  1032. const String getOutputChannelName(int i) const override
  1033. {
  1034. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  1035. CarlaEngineClient* const client(fPlugin->getEngineClient());
  1036. return client->getAudioPortName(false, static_cast<uint>(i));
  1037. }
  1038. void prepareToPlay(double, int) override {}
  1039. void releaseResources() override {}
  1040. const String getParameterName(int) override { return String(); }
  1041. String getParameterName(int, int) override { return String(); }
  1042. const String getParameterText(int) override { return String(); }
  1043. String getParameterText(int, int) override { return String(); }
  1044. const String getProgramName(int) override { return String(); }
  1045. double getTailLengthSeconds() const override { return 0.0; }
  1046. float getParameter(int) override { return 0.0f; }
  1047. bool isInputChannelStereoPair(int) const override { return false; }
  1048. bool isOutputChannelStereoPair(int) const override { return false; }
  1049. bool silenceInProducesSilenceOut() const override { return true; }
  1050. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  1051. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  1052. void setParameter(int, float) override {}
  1053. void setCurrentProgram(int) override {}
  1054. void changeProgramName(int, const String&) override {}
  1055. void getStateInformation(MemoryBlock&) override {}
  1056. void setStateInformation(const void*, int) override {}
  1057. int getNumParameters() override { return 0; }
  1058. int getNumPrograms() override { return 0; }
  1059. int getCurrentProgram() override { return 0; }
  1060. #ifndef JUCE_AUDIO_PROCESSOR_NO_GUI
  1061. bool hasEditor() const override { return false; }
  1062. AudioProcessorEditor* createEditor() override { return nullptr; }
  1063. #endif
  1064. // -------------------------------------------------------------------
  1065. private:
  1066. CarlaEngine* const kEngine;
  1067. CarlaPlugin* fPlugin;
  1068. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  1069. };
  1070. // -----------------------------------------------------------------------
  1071. // Patchbay Graph
  1072. PatchbayGraph::PatchbayGraph(CarlaEngine* const engine, const uint32_t ins, const uint32_t outs)
  1073. : connections(),
  1074. graph(),
  1075. audioBuffer(),
  1076. midiBuffer(),
  1077. inputs(carla_fixedValue(0U, MAX_PATCHBAY_PLUGINS-2, ins)),
  1078. outputs(carla_fixedValue(0U, MAX_PATCHBAY_PLUGINS-2, outs)),
  1079. retCon(),
  1080. usingExternal(false),
  1081. extGraph(engine),
  1082. kEngine(engine)
  1083. {
  1084. const int bufferSize(static_cast<int>(engine->getBufferSize()));
  1085. const double sampleRate(engine->getSampleRate());
  1086. graph.setPlayConfigDetails(static_cast<int>(inputs), static_cast<int>(outputs), sampleRate, bufferSize);
  1087. graph.prepareToPlay(sampleRate, bufferSize);
  1088. audioBuffer.setSize(static_cast<int>(jmax(inputs, outputs)), bufferSize);
  1089. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  1090. midiBuffer.clear();
  1091. {
  1092. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::audioInputNode));
  1093. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1094. node->properties.set("isPlugin", false);
  1095. node->properties.set("isOutput", false);
  1096. node->properties.set("isAudio", true);
  1097. node->properties.set("isCV", false);
  1098. node->properties.set("isMIDI", false);
  1099. node->properties.set("isOSC", false);
  1100. }
  1101. {
  1102. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::audioOutputNode));
  1103. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1104. node->properties.set("isPlugin", false);
  1105. node->properties.set("isOutput", false);
  1106. node->properties.set("isAudio", true);
  1107. node->properties.set("isCV", false);
  1108. node->properties.set("isMIDI", false);
  1109. node->properties.set("isOSC", false);
  1110. }
  1111. {
  1112. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::midiInputNode));
  1113. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1114. node->properties.set("isPlugin", false);
  1115. node->properties.set("isOutput", false);
  1116. node->properties.set("isAudio", false);
  1117. node->properties.set("isCV", false);
  1118. node->properties.set("isMIDI", true);
  1119. node->properties.set("isOSC", false);
  1120. }
  1121. {
  1122. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::midiOutputNode));
  1123. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  1124. node->properties.set("isPlugin", false);
  1125. node->properties.set("isOutput", true);
  1126. node->properties.set("isAudio", false);
  1127. node->properties.set("isCV", false);
  1128. node->properties.set("isMIDI", true);
  1129. node->properties.set("isOSC", false);
  1130. }
  1131. }
  1132. PatchbayGraph::~PatchbayGraph()
  1133. {
  1134. connections.clear();
  1135. extGraph.clear();
  1136. graph.releaseResources();
  1137. graph.clear();
  1138. audioBuffer.clear();
  1139. }
  1140. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  1141. {
  1142. const int bufferSizei(static_cast<int>(bufferSize));
  1143. graph.releaseResources();
  1144. graph.prepareToPlay(kEngine->getSampleRate(), bufferSizei);
  1145. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSizei);
  1146. }
  1147. void PatchbayGraph::setSampleRate(const double sampleRate)
  1148. {
  1149. graph.releaseResources();
  1150. graph.prepareToPlay(sampleRate, static_cast<int>(kEngine->getBufferSize()));
  1151. }
  1152. void PatchbayGraph::setOffline(const bool offline)
  1153. {
  1154. graph.setNonRealtime(offline);
  1155. }
  1156. void PatchbayGraph::addPlugin(CarlaPlugin* const plugin)
  1157. {
  1158. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1159. carla_debug("PatchbayGraph::addPlugin(%p)", plugin);
  1160. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, plugin));
  1161. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1162. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1163. plugin->setPatchbayNodeId(node->nodeId);
  1164. node->properties.set("isPlugin", true);
  1165. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  1166. if (! usingExternal)
  1167. addNodeToPatchbay(plugin->getEngine(), node->nodeId, static_cast<int>(plugin->getId()), instance);
  1168. }
  1169. void PatchbayGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1170. {
  1171. CARLA_SAFE_ASSERT_RETURN(oldPlugin != nullptr,);
  1172. CARLA_SAFE_ASSERT_RETURN(newPlugin != nullptr,);
  1173. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  1174. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  1175. AudioProcessorGraph::Node* const oldNode(graph.getNodeForId(oldPlugin->getPatchbayNodeId()));
  1176. CARLA_SAFE_ASSERT_RETURN(oldNode != nullptr,);
  1177. if (! usingExternal)
  1178. {
  1179. disconnectInternalGroup(oldNode->nodeId);
  1180. removeNodeFromPatchbay(kEngine, oldNode->nodeId, oldNode->getProcessor());
  1181. }
  1182. ((CarlaPluginInstance*)oldNode->getProcessor())->invalidatePlugin();
  1183. graph.removeNode(oldNode->nodeId);
  1184. CarlaPluginInstance* const instance(new CarlaPluginInstance(kEngine, newPlugin));
  1185. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  1186. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1187. newPlugin->setPatchbayNodeId(node->nodeId);
  1188. node->properties.set("isPlugin", true);
  1189. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  1190. if (! usingExternal)
  1191. addNodeToPatchbay(newPlugin->getEngine(), node->nodeId, static_cast<int>(newPlugin->getId()), instance);
  1192. }
  1193. void PatchbayGraph::removePlugin(CarlaPlugin* const plugin)
  1194. {
  1195. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1196. carla_debug("PatchbayGraph::removePlugin(%p)", plugin);
  1197. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1198. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1199. if (! usingExternal)
  1200. {
  1201. disconnectInternalGroup(node->nodeId);
  1202. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1203. }
  1204. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1205. // Fix plugin Ids properties
  1206. for (uint i=plugin->getId()+1, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1207. {
  1208. CarlaPlugin* const plugin2(kEngine->getPlugin(i));
  1209. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  1210. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1211. {
  1212. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != juce::var(-1));
  1213. node2->properties.set("pluginId", static_cast<int>(i-1));
  1214. }
  1215. }
  1216. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(node->nodeId),);
  1217. }
  1218. void PatchbayGraph::removeAllPlugins()
  1219. {
  1220. carla_debug("PatchbayGraph::removeAllPlugins()");
  1221. for (uint i=0, count=kEngine->getCurrentPluginCount(); i<count; ++i)
  1222. {
  1223. CarlaPlugin* const plugin(kEngine->getPlugin(i));
  1224. CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
  1225. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1226. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1227. if (! usingExternal)
  1228. {
  1229. disconnectInternalGroup(node->nodeId);
  1230. removeNodeFromPatchbay(kEngine, node->nodeId, node->getProcessor());
  1231. }
  1232. ((CarlaPluginInstance*)node->getProcessor())->invalidatePlugin();
  1233. graph.removeNode(node->nodeId);
  1234. }
  1235. }
  1236. bool PatchbayGraph::connect(const bool external, const uint groupA, const uint portA, const uint groupB, const uint portB, const bool sendCallback)
  1237. {
  1238. if (external)
  1239. return extGraph.connect(groupA, portA, groupB, portB, sendCallback);
  1240. uint adjustedPortA = portA;
  1241. uint adjustedPortB = portB;
  1242. if (! adjustPatchbayPortIdForJuce(adjustedPortA))
  1243. return false;
  1244. if (! adjustPatchbayPortIdForJuce(adjustedPortB))
  1245. return false;
  1246. if (! graph.addConnection(groupA, static_cast<int>(adjustedPortA), groupB, static_cast<int>(adjustedPortB)))
  1247. {
  1248. kEngine->setLastError("Failed from juce");
  1249. return false;
  1250. }
  1251. ConnectionToId connectionToId;
  1252. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1253. char strBuf[STR_MAX+1];
  1254. strBuf[STR_MAX] = '\0';
  1255. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1256. if (sendCallback)
  1257. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1258. connections.list.append(connectionToId);
  1259. return true;
  1260. }
  1261. bool PatchbayGraph::disconnect(const uint connectionId)
  1262. {
  1263. if (usingExternal)
  1264. return extGraph.disconnect(connectionId);
  1265. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1266. {
  1267. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1268. const ConnectionToId& connectionToId(it.getValue(fallback));
  1269. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1270. if (connectionToId.id != connectionId)
  1271. continue;
  1272. uint adjustedPortA = connectionToId.portA;
  1273. uint adjustedPortB = connectionToId.portB;
  1274. if (! adjustPatchbayPortIdForJuce(adjustedPortA))
  1275. return false;
  1276. if (! adjustPatchbayPortIdForJuce(adjustedPortB))
  1277. return false;
  1278. if (! graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1279. connectionToId.groupB, static_cast<int>(adjustedPortB)))
  1280. return false;
  1281. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1282. connections.list.remove(it);
  1283. return true;
  1284. }
  1285. kEngine->setLastError("Failed to find connection");
  1286. return false;
  1287. }
  1288. void PatchbayGraph::disconnectInternalGroup(const uint groupId) noexcept
  1289. {
  1290. CARLA_SAFE_ASSERT(! usingExternal);
  1291. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1292. {
  1293. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1294. const ConnectionToId& connectionToId(it.getValue(fallback));
  1295. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1296. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1297. continue;
  1298. /*
  1299. uint adjustedPortA = connectionToId.portA;
  1300. uint adjustedPortB = connectionToId.portB;
  1301. if (! adjustPatchbayPortIdForJuce(adjustedPortA))
  1302. return false;
  1303. if (! adjustPatchbayPortIdForJuce(adjustedPortB))
  1304. return false;
  1305. graph.removeConnection(connectionToId.groupA, static_cast<int>(adjustedPortA),
  1306. connectionToId.groupB, static_cast<int>(adjustedPortB));
  1307. */
  1308. if (! usingExternal)
  1309. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connectionToId.id, 0, 0, 0.0f, nullptr);
  1310. connections.list.remove(it);
  1311. }
  1312. }
  1313. void PatchbayGraph::refresh(const char* const deviceName)
  1314. {
  1315. if (usingExternal)
  1316. return extGraph.refresh(deviceName);
  1317. CARLA_SAFE_ASSERT_RETURN(deviceName != nullptr,);
  1318. connections.clear();
  1319. graph.removeIllegalConnections();
  1320. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1321. {
  1322. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1323. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1324. AudioProcessor* const proc(node->getProcessor());
  1325. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1326. int clientId = -1;
  1327. // plugin node
  1328. if (node->properties.getWithDefault("isPlugin", false) == juce::var(true))
  1329. clientId = node->properties.getWithDefault("pluginId", -1);
  1330. addNodeToPatchbay(kEngine, node->nodeId, clientId, proc);
  1331. }
  1332. char strBuf[STR_MAX+1];
  1333. strBuf[STR_MAX] = '\0';
  1334. for (int i=0, count=graph.getNumConnections(); i<count; ++i)
  1335. {
  1336. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1337. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1338. CARLA_SAFE_ASSERT_CONTINUE(conn->sourceChannelIndex >= 0);
  1339. CARLA_SAFE_ASSERT_CONTINUE(conn->destChannelIndex >= 0);
  1340. const uint groupA = conn->sourceNodeId;
  1341. const uint groupB = conn->destNodeId;
  1342. uint portA = static_cast<uint>(conn->sourceChannelIndex);
  1343. uint portB = static_cast<uint>(conn->destChannelIndex);
  1344. if (portA == kMidiChannelIndex)
  1345. portA = kMidiOutputPortOffset;
  1346. else
  1347. portA += kAudioOutputPortOffset;
  1348. if (portB == kMidiChannelIndex)
  1349. portB = kMidiInputPortOffset;
  1350. else
  1351. portB += kAudioInputPortOffset;
  1352. ConnectionToId connectionToId;
  1353. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1354. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1355. kEngine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1356. connections.list.append(connectionToId);
  1357. }
  1358. }
  1359. const char* const* PatchbayGraph::getConnections(const bool external) const
  1360. {
  1361. if (external)
  1362. return extGraph.getConnections();
  1363. if (connections.list.count() == 0)
  1364. return nullptr;
  1365. CarlaStringList connList;
  1366. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin2(); it.valid(); it.next())
  1367. {
  1368. static const ConnectionToId fallback = { 0, 0, 0, 0, 0 };
  1369. const ConnectionToId& connectionToId(it.getValue(fallback));
  1370. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id > 0);
  1371. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connectionToId.groupA));
  1372. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1373. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connectionToId.groupB));
  1374. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1375. AudioProcessor* const procA(nodeA->getProcessor());
  1376. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1377. AudioProcessor* const procB(nodeB->getProcessor());
  1378. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1379. String fullPortNameA(getProcessorFullPortName(procA, connectionToId.portA));
  1380. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1381. String fullPortNameB(getProcessorFullPortName(procB, connectionToId.portB));
  1382. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1383. connList.append(fullPortNameA.toRawUTF8());
  1384. connList.append(fullPortNameB.toRawUTF8());
  1385. }
  1386. if (connList.count() == 0)
  1387. return nullptr;
  1388. retCon = connList.toCharStringListPtr();
  1389. return retCon;
  1390. }
  1391. bool PatchbayGraph::getGroupAndPortIdFromFullName(const bool external, const char* const fullPortName, uint& groupId, uint& portId) const
  1392. {
  1393. if (external)
  1394. return extGraph.getGroupAndPortIdFromFullName(fullPortName, groupId, portId);
  1395. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1396. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1397. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1398. {
  1399. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1400. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1401. AudioProcessor* const proc(node->getProcessor());
  1402. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1403. if (proc->getName() != groupName)
  1404. continue;
  1405. groupId = node->nodeId;
  1406. if (portName == "events-in")
  1407. {
  1408. portId = kMidiInputPortOffset;
  1409. return true;
  1410. }
  1411. if (portName == "events-out")
  1412. {
  1413. portId = kMidiOutputPortOffset;
  1414. return true;
  1415. }
  1416. for (int j=0, numInputs=proc->getNumInputChannels(); j<numInputs; ++j)
  1417. {
  1418. if (proc->getInputChannelName(j) != portName)
  1419. continue;
  1420. portId = kAudioInputPortOffset+static_cast<uint>(j);
  1421. return true;
  1422. }
  1423. for (int j=0, numOutputs=proc->getNumOutputChannels(); j<numOutputs; ++j)
  1424. {
  1425. if (proc->getOutputChannelName(j) != portName)
  1426. continue;
  1427. portId = kAudioOutputPortOffset+static_cast<uint>(j);
  1428. return true;
  1429. }
  1430. }
  1431. return false;
  1432. }
  1433. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const int frames)
  1434. {
  1435. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1436. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  1437. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  1438. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  1439. // put events in juce buffer
  1440. {
  1441. midiBuffer.clear();
  1442. fillJuceMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  1443. }
  1444. // put carla audio in juce buffer
  1445. {
  1446. int i=0;
  1447. for (; i < static_cast<int>(inputs); ++i)
  1448. FloatVectorOperations::copy(audioBuffer.getWritePointer(i), inBuf[i], frames);
  1449. // clear remaining channels
  1450. for (const int count=audioBuffer.getNumChannels(); i<count; ++i)
  1451. audioBuffer.clear(i, 0, frames);
  1452. }
  1453. graph.processBlock(audioBuffer, midiBuffer);
  1454. // put juce audio in carla buffer
  1455. {
  1456. for (int i=0; i < static_cast<int>(outputs); ++i)
  1457. FloatVectorOperations::copy(outBuf[i], audioBuffer.getReadPointer(i), frames);
  1458. }
  1459. // put juce events in carla buffer
  1460. {
  1461. carla_zeroStructs(data->events.out, kMaxEngineEventInternalCount);
  1462. fillEngineEventsFromJuceMidiBuffer(data->events.out, midiBuffer);
  1463. midiBuffer.clear();
  1464. }
  1465. }
  1466. // -----------------------------------------------------------------------
  1467. // InternalGraph
  1468. EngineInternalGraph::EngineInternalGraph(CarlaEngine* const engine) noexcept
  1469. : fIsRack(true),
  1470. fIsReady(false),
  1471. kEngine(engine)
  1472. {
  1473. fRack = nullptr;
  1474. }
  1475. EngineInternalGraph::~EngineInternalGraph() noexcept
  1476. {
  1477. CARLA_SAFE_ASSERT(! fIsReady);
  1478. CARLA_SAFE_ASSERT(fRack == nullptr);
  1479. }
  1480. void EngineInternalGraph::create(const uint32_t inputs, const uint32_t outputs)
  1481. {
  1482. fIsRack = (kEngine->getOptions().processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK);
  1483. if (fIsRack)
  1484. {
  1485. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  1486. fRack = new RackGraph(kEngine, inputs, outputs);
  1487. }
  1488. else
  1489. {
  1490. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  1491. fPatchbay = new PatchbayGraph(kEngine, inputs, outputs);
  1492. }
  1493. fIsReady = true;
  1494. }
  1495. void EngineInternalGraph::destroy() noexcept
  1496. {
  1497. if (! fIsReady)
  1498. {
  1499. CARLA_SAFE_ASSERT(fRack == nullptr);
  1500. return;
  1501. }
  1502. if (fIsRack)
  1503. {
  1504. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1505. delete fRack;
  1506. fRack = nullptr;
  1507. }
  1508. else
  1509. {
  1510. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1511. delete fPatchbay;
  1512. fPatchbay = nullptr;
  1513. }
  1514. fIsReady = false;
  1515. }
  1516. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  1517. {
  1518. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1519. if (fIsRack)
  1520. {
  1521. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1522. fRack->setBufferSize(bufferSize);
  1523. }
  1524. else
  1525. {
  1526. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1527. fPatchbay->setBufferSize(bufferSize);
  1528. }
  1529. }
  1530. void EngineInternalGraph::setSampleRate(const double sampleRate)
  1531. {
  1532. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1533. if (fIsRack)
  1534. {
  1535. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1536. }
  1537. else
  1538. {
  1539. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1540. fPatchbay->setSampleRate(sampleRate);
  1541. }
  1542. }
  1543. void EngineInternalGraph::setOffline(const bool offline)
  1544. {
  1545. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1546. if (fIsRack)
  1547. {
  1548. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1549. fRack->setOffline(offline);
  1550. }
  1551. else
  1552. {
  1553. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1554. fPatchbay->setOffline(offline);
  1555. }
  1556. }
  1557. bool EngineInternalGraph::isReady() const noexcept
  1558. {
  1559. return fIsReady;
  1560. }
  1561. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  1562. {
  1563. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  1564. return fRack;
  1565. }
  1566. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  1567. {
  1568. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  1569. return fPatchbay;
  1570. }
  1571. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  1572. {
  1573. if (fIsRack)
  1574. {
  1575. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1576. fRack->processHelper(data, inBuf, outBuf, frames);
  1577. }
  1578. else
  1579. {
  1580. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1581. fPatchbay->process(data, inBuf, outBuf, static_cast<int>(frames));
  1582. }
  1583. }
  1584. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  1585. {
  1586. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  1587. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1588. fRack->process(data, inBuf, outBuf, frames);
  1589. }
  1590. // -----------------------------------------------------------------------
  1591. // used for internal patchbay mode
  1592. void EngineInternalGraph::addPlugin(CarlaPlugin* const plugin)
  1593. {
  1594. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1595. fPatchbay->addPlugin(plugin);
  1596. }
  1597. void EngineInternalGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1598. {
  1599. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1600. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  1601. }
  1602. void EngineInternalGraph::removePlugin(CarlaPlugin* const plugin)
  1603. {
  1604. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1605. fPatchbay->removePlugin(plugin);
  1606. }
  1607. void EngineInternalGraph::removeAllPlugins()
  1608. {
  1609. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1610. fPatchbay->removeAllPlugins();
  1611. }
  1612. bool EngineInternalGraph::isUsingExternal() const noexcept
  1613. {
  1614. if (fIsRack)
  1615. return true;
  1616. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr, false);
  1617. return fPatchbay->usingExternal;
  1618. }
  1619. void EngineInternalGraph::setUsingExternal(const bool usingExternal) noexcept
  1620. {
  1621. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1622. fPatchbay->usingExternal = usingExternal;
  1623. }
  1624. // -----------------------------------------------------------------------
  1625. // CarlaEngine Patchbay stuff
  1626. bool CarlaEngine::patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB)
  1627. {
  1628. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1629. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1630. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  1631. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1632. {
  1633. RackGraph* const graph = pData->graph.getRackGraph();
  1634. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1635. return graph->connect(groupA, portA, groupB, portB);
  1636. }
  1637. else
  1638. {
  1639. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1640. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1641. return graph->connect(graph->usingExternal, groupA, portA, groupB, portB, true);
  1642. }
  1643. return false;
  1644. }
  1645. bool CarlaEngine::patchbayDisconnect(const uint connectionId)
  1646. {
  1647. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1648. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1649. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  1650. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1651. {
  1652. RackGraph* const graph = pData->graph.getRackGraph();
  1653. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1654. return graph->disconnect(connectionId);
  1655. }
  1656. else
  1657. {
  1658. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1659. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1660. return graph->disconnect(connectionId);
  1661. }
  1662. return false;
  1663. }
  1664. bool CarlaEngine::patchbayRefresh(const bool external)
  1665. {
  1666. // subclasses should handle this
  1667. CARLA_SAFE_ASSERT_RETURN(! external, false);
  1668. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1669. {
  1670. // This is implemented in engine subclasses
  1671. setLastError("Unsupported operation");
  1672. return false;
  1673. }
  1674. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1675. {
  1676. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1677. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1678. graph->refresh("");
  1679. return true;
  1680. }
  1681. setLastError("Unsupported operation");
  1682. return false;
  1683. }
  1684. // -----------------------------------------------------------------------
  1685. const char* const* CarlaEngine::getPatchbayConnections(const bool external) const
  1686. {
  1687. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  1688. carla_debug("CarlaEngine::getPatchbayConnections(%s)", bool2str(external));
  1689. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1690. {
  1691. RackGraph* const graph = pData->graph.getRackGraph();
  1692. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1693. CARLA_SAFE_ASSERT_RETURN(external, nullptr);
  1694. return graph->getConnections();
  1695. }
  1696. else
  1697. {
  1698. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1699. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, nullptr);
  1700. return graph->getConnections(external);
  1701. }
  1702. return nullptr;
  1703. }
  1704. void CarlaEngine::restorePatchbayConnection(const bool external, const char* const sourcePort, const char* const targetPort, const bool sendCallback)
  1705. {
  1706. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  1707. CARLA_SAFE_ASSERT_RETURN(sourcePort != nullptr && sourcePort[0] != '\0',);
  1708. CARLA_SAFE_ASSERT_RETURN(targetPort != nullptr && targetPort[0] != '\0',);
  1709. carla_debug("CarlaEngine::restorePatchbayConnection(%s, \"%s\", \"%s\")", bool2str(external), sourcePort, targetPort);
  1710. uint groupA, portA;
  1711. uint groupB, portB;
  1712. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1713. {
  1714. RackGraph* const graph = pData->graph.getRackGraph();
  1715. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1716. CARLA_SAFE_ASSERT_RETURN(external,);
  1717. if (! graph->getGroupAndPortIdFromFullName(sourcePort, groupA, portA))
  1718. return;
  1719. if (! graph->getGroupAndPortIdFromFullName(targetPort, groupB, portB))
  1720. return;
  1721. graph->connect(groupA, portA, groupB, portB);
  1722. }
  1723. else
  1724. {
  1725. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1726. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1727. if (! graph->getGroupAndPortIdFromFullName(external, sourcePort, groupA, portA))
  1728. return;
  1729. if (! graph->getGroupAndPortIdFromFullName(external, targetPort, groupB, portB))
  1730. return;
  1731. graph->connect(external, groupA, portA, groupB, portB, sendCallback);
  1732. }
  1733. }
  1734. // -----------------------------------------------------------------------
  1735. bool CarlaEngine::connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1736. {
  1737. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1738. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1739. RackGraph* const graph(pData->graph.getRackGraph());
  1740. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1741. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1742. switch (connectionType)
  1743. {
  1744. case kExternalGraphConnectionAudioIn1:
  1745. return graph->audioBuffers.connectedIn1.append(portId);
  1746. case kExternalGraphConnectionAudioIn2:
  1747. return graph->audioBuffers.connectedIn2.append(portId);
  1748. case kExternalGraphConnectionAudioOut1:
  1749. return graph->audioBuffers.connectedOut1.append(portId);
  1750. case kExternalGraphConnectionAudioOut2:
  1751. return graph->audioBuffers.connectedOut2.append(portId);
  1752. }
  1753. return false;
  1754. }
  1755. bool CarlaEngine::disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName)
  1756. {
  1757. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  1758. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK, false);
  1759. RackGraph* const graph(pData->graph.getRackGraph());
  1760. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1761. const CarlaRecursiveMutexLocker cml(graph->audioBuffers.mutex);
  1762. switch (connectionType)
  1763. {
  1764. case kExternalGraphConnectionAudioIn1:
  1765. return graph->audioBuffers.connectedIn1.removeOne(portId);
  1766. case kExternalGraphConnectionAudioIn2:
  1767. return graph->audioBuffers.connectedIn2.removeOne(portId);
  1768. case kExternalGraphConnectionAudioOut1:
  1769. return graph->audioBuffers.connectedOut1.removeOne(portId);
  1770. case kExternalGraphConnectionAudioOut2:
  1771. return graph->audioBuffers.connectedOut2.removeOne(portId);
  1772. }
  1773. return false;
  1774. }
  1775. // -----------------------------------------------------------------------
  1776. CARLA_BACKEND_END_NAMESPACE
  1777. // -----------------------------------------------------------------------