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.

1852 lines
60KB

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