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.

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