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.

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