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.

1850 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. 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, 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. const uint32_t bufferSize(static_cast<uint32_t>(audio.getNumSamples()));
  809. if (const int numChan = audio.getNumChannels())
  810. {
  811. if (fPlugin->getAudioInCount() == 0)
  812. audio.clear();
  813. float* audioBuffers[numChan];
  814. for (int i=0; i<numChan; ++i)
  815. audioBuffers[i] = audio.getWritePointer(i);
  816. float inPeaks[2] = { 0.0f };
  817. float outPeaks[2] = { 0.0f };
  818. for (int i=0; i<numChan; ++i)
  819. {
  820. for (uint32_t j=0; j < bufferSize; ++j)
  821. {
  822. const float absV(std::abs(audioBuffers[i][j]));
  823. if (absV > inPeaks[i])
  824. inPeaks[i] = absV;
  825. }
  826. }
  827. fPlugin->process(audioBuffers, audioBuffers, bufferSize);
  828. for (int i=0; i<numChan; ++i)
  829. {
  830. for (uint32_t j=0; j < bufferSize; ++j)
  831. {
  832. const float absV(std::abs(audioBuffers[i][j]));
  833. if (absV > outPeaks[i])
  834. outPeaks[i] = absV;
  835. }
  836. }
  837. engine->setPluginPeaks(fPlugin->getId(), inPeaks, outPeaks);
  838. }
  839. else
  840. {
  841. fPlugin->process(nullptr, nullptr, bufferSize);
  842. }
  843. midi.clear();
  844. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  845. {
  846. /*const*/ EngineEvent* const engineEvents(port->fBuffer);
  847. CARLA_SAFE_ASSERT_RETURN(engineEvents != nullptr,);
  848. fillJuceMidiBufferFromEngineEvents(midi, engineEvents);
  849. carla_zeroStruct<EngineEvent>(engineEvents, kMaxEngineEventInternalCount);
  850. }
  851. fPlugin->unlock();
  852. }
  853. const String getInputChannelName(int i) const override
  854. {
  855. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  856. CarlaEngineClient* const client(fPlugin->getEngineClient());
  857. return client->getAudioInputPortName(static_cast<uint>(i));
  858. }
  859. const String getOutputChannelName(int i) const override
  860. {
  861. CARLA_SAFE_ASSERT_RETURN(i >= 0, String());
  862. CarlaEngineClient* const client(fPlugin->getEngineClient());
  863. return client->getAudioOutputPortName(static_cast<uint>(i));
  864. }
  865. void prepareToPlay(double, int) override {}
  866. void releaseResources() override {}
  867. const String getParameterName(int) override { return String(); }
  868. String getParameterName(int, int) override { return String(); }
  869. const String getParameterText(int) override { return String(); }
  870. String getParameterText(int, int) override { return String(); }
  871. const String getProgramName(int) override { return String(); }
  872. double getTailLengthSeconds() const override { return 0.0; }
  873. float getParameter(int) override { return 0.0f; }
  874. bool isInputChannelStereoPair(int) const override { return false; }
  875. bool isOutputChannelStereoPair(int) const override { return false; }
  876. bool silenceInProducesSilenceOut() const override { return true; }
  877. bool acceptsMidi() const override { return fPlugin->getDefaultEventInPort() != nullptr; }
  878. bool producesMidi() const override { return fPlugin->getDefaultEventOutPort() != nullptr; }
  879. void setParameter(int, float) override {}
  880. void setCurrentProgram(int) override {}
  881. void changeProgramName(int, const String&) override {}
  882. void getStateInformation(MemoryBlock&) override {}
  883. void setStateInformation(const void*, int) override {}
  884. int getNumParameters() override { return 0; }
  885. int getNumPrograms() override { return 0; }
  886. int getCurrentProgram() override { return 0; }
  887. #ifndef JUCE_AUDIO_PROCESSOR_NO_GUI
  888. bool hasEditor() const override { return false; }
  889. AudioProcessorEditor* createEditor() override { return nullptr; }
  890. #endif
  891. // -------------------------------------------------------------------
  892. private:
  893. CarlaPlugin* const fPlugin;
  894. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginInstance)
  895. };
  896. // -----------------------------------------------------------------------
  897. // Patchbay Graph
  898. PatchbayGraph::PatchbayGraph(const int bufferSize, const double sampleRate, const uint32_t ins, const uint32_t outs)
  899. : connections(),
  900. graph(),
  901. audioBuffer(),
  902. midiBuffer(),
  903. inputs(carla_fixValue(0U, MAX_PATCHBAY_PLUGINS-2, ins)),
  904. outputs(carla_fixValue(0U, MAX_PATCHBAY_PLUGINS-2, outs)),
  905. ignorePathbay(false),
  906. retCon()
  907. {
  908. graph.setPlayConfigDetails(static_cast<int>(inputs), static_cast<int>(outputs), sampleRate, bufferSize);
  909. graph.prepareToPlay(sampleRate, bufferSize);
  910. audioBuffer.setSize(static_cast<int>(jmax(inputs, outputs)), bufferSize);
  911. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  912. midiBuffer.clear();
  913. {
  914. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::audioInputNode));
  915. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  916. node->properties.set("isPlugin", false);
  917. node->properties.set("isOutput", false);
  918. node->properties.set("isAudio", true);
  919. node->properties.set("isMIDI", false);
  920. }
  921. {
  922. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::audioOutputNode));
  923. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  924. node->properties.set("isPlugin", false);
  925. node->properties.set("isOutput", false);
  926. node->properties.set("isAudio", true);
  927. node->properties.set("isMIDI", false);
  928. }
  929. {
  930. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::midiInputNode));
  931. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  932. node->properties.set("isPlugin", false);
  933. node->properties.set("isOutput", false);
  934. node->properties.set("isAudio", false);
  935. node->properties.set("isMIDI", true);
  936. }
  937. {
  938. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::midiOutputNode));
  939. AudioProcessorGraph::Node* const node(graph.addNode(proc));
  940. node->properties.set("isPlugin", false);
  941. node->properties.set("isOutput", true);
  942. node->properties.set("isAudio", false);
  943. node->properties.set("isMIDI", true);
  944. }
  945. }
  946. PatchbayGraph::~PatchbayGraph()
  947. {
  948. clearConnections();
  949. graph.releaseResources();
  950. graph.clear();
  951. audioBuffer.clear();
  952. }
  953. void PatchbayGraph::setBufferSize(const int bufferSize)
  954. {
  955. graph.releaseResources();
  956. graph.prepareToPlay(graph.getSampleRate(), bufferSize);
  957. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSize);
  958. }
  959. void PatchbayGraph::setSampleRate(const double sampleRate)
  960. {
  961. graph.releaseResources();
  962. graph.prepareToPlay(sampleRate, graph.getBlockSize());
  963. }
  964. void PatchbayGraph::setOffline(const bool offline)
  965. {
  966. graph.setNonRealtime(offline);
  967. }
  968. void PatchbayGraph::addPlugin(CarlaPlugin* const plugin)
  969. {
  970. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  971. carla_debug("PatchbayGraph::addPlugin(%p)", plugin);
  972. CarlaPluginInstance* const instance(new CarlaPluginInstance(plugin));
  973. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  974. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  975. plugin->setPatchbayNodeId(node->nodeId);
  976. node->properties.set("isPlugin", true);
  977. node->properties.set("pluginId", static_cast<int>(plugin->getId()));
  978. if (! ignorePathbay)
  979. addNodeToPatchbay(plugin->getEngine(), node->nodeId, static_cast<int>(plugin->getId()), instance);
  980. }
  981. void PatchbayGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  982. {
  983. CARLA_SAFE_ASSERT_RETURN(oldPlugin != nullptr,);
  984. CARLA_SAFE_ASSERT_RETURN(newPlugin != nullptr,);
  985. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  986. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  987. // FIXME
  988. graph.removeNode(oldPlugin->getPatchbayNodeId());
  989. CarlaPluginInstance* const instance(new CarlaPluginInstance(newPlugin));
  990. AudioProcessorGraph::Node* const node(graph.addNode(instance));
  991. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  992. newPlugin->setPatchbayNodeId(node->nodeId);
  993. node->properties.set("isPlugin", true);
  994. node->properties.set("pluginId", static_cast<int>(newPlugin->getId()));
  995. if (! ignorePathbay)
  996. addNodeToPatchbay(newPlugin->getEngine(), node->nodeId, static_cast<int>(newPlugin->getId()), instance);
  997. }
  998. void PatchbayGraph::removePlugin(CarlaPlugin* const plugin)
  999. {
  1000. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  1001. carla_debug("PatchbayGraph::removePlugin(%p)", plugin);
  1002. CarlaEngine* const engine(plugin->getEngine());
  1003. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1004. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1005. CARLA_SAFE_ASSERT_RETURN(node != nullptr,);
  1006. if (! ignorePathbay)
  1007. removeNodeFromPatchbay(engine, plugin->getPatchbayNodeId(), node->getProcessor());
  1008. // Fix plugin Ids properties
  1009. for (uint i=plugin->getId(), count=engine->getCurrentPluginCount(); i<count; ++i)
  1010. {
  1011. CarlaPlugin* const plugin2(engine->getPlugin(i));
  1012. CARLA_SAFE_ASSERT_BREAK(plugin2 != nullptr);
  1013. if (AudioProcessorGraph::Node* const node2 = graph.getNodeForId(plugin2->getPatchbayNodeId()))
  1014. {
  1015. CARLA_SAFE_ASSERT_CONTINUE(node2->properties.getWithDefault("pluginId", -1) != juce::var(-1));
  1016. node2->properties.set("pluginId", static_cast<int>(plugin2->getId()));
  1017. }
  1018. }
  1019. CARLA_SAFE_ASSERT_RETURN(graph.removeNode(plugin->getPatchbayNodeId()),);
  1020. }
  1021. void PatchbayGraph::removeAllPlugins(CarlaEngine* const engine)
  1022. {
  1023. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1024. carla_debug("PatchbayGraph::removeAllPlugins(%p)", engine);
  1025. for (uint i=0, count=engine->getCurrentPluginCount(); i<count; ++i)
  1026. {
  1027. CarlaPlugin* const plugin(engine->getPlugin(i));
  1028. CARLA_SAFE_ASSERT_CONTINUE(plugin != nullptr);
  1029. AudioProcessorGraph::Node* const node(graph.getNodeForId(plugin->getPatchbayNodeId()));
  1030. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1031. if (! ignorePathbay)
  1032. removeNodeFromPatchbay(engine, node->nodeId, node->getProcessor());
  1033. graph.removeNode(node->nodeId);
  1034. }
  1035. }
  1036. bool PatchbayGraph::connect(CarlaEngine* const engine, const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  1037. {
  1038. CARLA_SAFE_ASSERT_RETURN(engine != nullptr, false);
  1039. uint adjustedPortA = portA;
  1040. uint adjustedPortB = portB;
  1041. if (! adjustPatchbayPortIdForJuce(adjustedPortA))
  1042. return false;
  1043. if (! adjustPatchbayPortIdForJuce(adjustedPortB))
  1044. return false;
  1045. if (! graph.addConnection(groupA, static_cast<int>(adjustedPortA), groupB, static_cast<int>(adjustedPortB)))
  1046. {
  1047. engine->setLastError("Failed from juce");
  1048. return false;
  1049. }
  1050. ConnectionToId connectionToId;
  1051. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1052. char strBuf[STR_MAX+1];
  1053. strBuf[STR_MAX] = '\0';
  1054. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  1055. engine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1056. connections.list.append(connectionToId);
  1057. return true;
  1058. }
  1059. bool PatchbayGraph::disconnect(CarlaEngine* const engine, const uint connectionId) noexcept
  1060. {
  1061. CARLA_SAFE_ASSERT_RETURN(engine != nullptr, false);
  1062. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin(); it.valid(); it.next())
  1063. {
  1064. const ConnectionToId& connection(it.getValue());
  1065. if (connection.id != connectionId)
  1066. continue;
  1067. uint adjustedPortA = connection.portA;
  1068. uint adjustedPortB = connection.portB;
  1069. if (! adjustPatchbayPortIdForJuce(adjustedPortA))
  1070. return false;
  1071. if (! adjustPatchbayPortIdForJuce(adjustedPortB))
  1072. return false;
  1073. if (! graph.removeConnection(connection.groupA, static_cast<int>(adjustedPortA),
  1074. connection.groupB, static_cast<int>(adjustedPortB)))
  1075. return false;
  1076. engine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connection.id, 0, 0, 0.0f, nullptr);
  1077. connections.list.remove(it);
  1078. return true;
  1079. }
  1080. engine->setLastError("Failed to find connection");
  1081. return false;
  1082. }
  1083. void PatchbayGraph::clearConnections()
  1084. {
  1085. connections.clear();
  1086. for (int i=0, count=graph.getNumConnections(); i<count; ++i)
  1087. graph.removeConnection(0);
  1088. }
  1089. void PatchbayGraph::refreshConnections(CarlaEngine* const engine)
  1090. {
  1091. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  1092. connections.clear();
  1093. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1094. {
  1095. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1096. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1097. AudioProcessor* const proc(node->getProcessor());
  1098. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1099. int clientId = -1;
  1100. if (node->properties.getWithDefault("isPlugin", false) == juce::var(true))
  1101. clientId = node->properties.getWithDefault("pluginId", -1);
  1102. if (! ignorePathbay)
  1103. addNodeToPatchbay(engine, node->nodeId, clientId, proc);
  1104. }
  1105. char strBuf[STR_MAX+1];
  1106. for (int i=0, count=graph.getNumConnections(); i<count; ++i)
  1107. {
  1108. const AudioProcessorGraph::Connection* const conn(graph.getConnection(i));
  1109. CARLA_SAFE_ASSERT_CONTINUE(conn != nullptr);
  1110. CARLA_SAFE_ASSERT_CONTINUE(conn->sourceChannelIndex >= 0);
  1111. CARLA_SAFE_ASSERT_CONTINUE(conn->destChannelIndex >= 0);
  1112. const uint groupA = conn->sourceNodeId;
  1113. const uint groupB = conn->destNodeId;
  1114. uint portA = static_cast<uint>(conn->sourceChannelIndex);
  1115. uint portB = static_cast<uint>(conn->destChannelIndex);
  1116. if (portA == kMidiChannelIndex)
  1117. portA = kMidiOutputPortOffset;
  1118. else
  1119. portA += kAudioOutputPortOffset;
  1120. if (portB == kMidiChannelIndex)
  1121. portB = kMidiInputPortOffset;
  1122. else
  1123. portB += kAudioInputPortOffset;
  1124. ConnectionToId connectionToId;
  1125. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  1126. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", groupA, portA, groupB, portB);
  1127. strBuf[STR_MAX] = '\0';
  1128. engine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  1129. connections.list.append(connectionToId);
  1130. }
  1131. }
  1132. const char* const* PatchbayGraph::getConnections() const
  1133. {
  1134. if (connections.list.count() == 0)
  1135. return nullptr;
  1136. CarlaStringList connList;
  1137. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin(); it.valid(); it.next())
  1138. {
  1139. const ConnectionToId& connection(it.getValue());
  1140. AudioProcessorGraph::Node* const nodeA(graph.getNodeForId(connection.groupA));
  1141. CARLA_SAFE_ASSERT_CONTINUE(nodeA != nullptr);
  1142. AudioProcessorGraph::Node* const nodeB(graph.getNodeForId(connection.groupB));
  1143. CARLA_SAFE_ASSERT_CONTINUE(nodeB != nullptr);
  1144. AudioProcessor* const procA(nodeA->getProcessor());
  1145. CARLA_SAFE_ASSERT_CONTINUE(procA != nullptr);
  1146. AudioProcessor* const procB(nodeB->getProcessor());
  1147. CARLA_SAFE_ASSERT_CONTINUE(procB != nullptr);
  1148. String fullPortNameA(getProcessorFullPortName(procA, connection.portA));
  1149. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameA.isNotEmpty());
  1150. String fullPortNameB(getProcessorFullPortName(procB, connection.portB));
  1151. CARLA_SAFE_ASSERT_CONTINUE(fullPortNameB.isNotEmpty());
  1152. connList.append(fullPortNameA.toRawUTF8());
  1153. connList.append(fullPortNameB.toRawUTF8());
  1154. }
  1155. if (connList.count() == 0)
  1156. return nullptr;
  1157. retCon = connList.toCharStringListPtr();
  1158. return retCon;
  1159. }
  1160. bool PatchbayGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const
  1161. {
  1162. String groupName(String(fullPortName).upToFirstOccurrenceOf(":", false, false));
  1163. String portName(String(fullPortName).fromFirstOccurrenceOf(":", false, false));
  1164. for (int i=0, count=graph.getNumNodes(); i<count; ++i)
  1165. {
  1166. AudioProcessorGraph::Node* const node(graph.getNode(i));
  1167. CARLA_SAFE_ASSERT_CONTINUE(node != nullptr);
  1168. AudioProcessor* const proc(node->getProcessor());
  1169. CARLA_SAFE_ASSERT_CONTINUE(proc != nullptr);
  1170. if (proc->getName() != groupName)
  1171. continue;
  1172. groupId = node->nodeId;
  1173. if (portName == "events-in")
  1174. {
  1175. portId = kMidiInputPortOffset;
  1176. return true;
  1177. }
  1178. if (portName == "events-out")
  1179. {
  1180. portId = kMidiOutputPortOffset;
  1181. return true;
  1182. }
  1183. else
  1184. {
  1185. for (int j=0, numInputs=proc->getNumInputChannels(); j<numInputs; ++j)
  1186. {
  1187. if (proc->getInputChannelName(j) != portName)
  1188. continue;
  1189. portId = kAudioInputPortOffset+static_cast<uint>(j);
  1190. return true;
  1191. }
  1192. for (int j=0, numOutputs=proc->getNumOutputChannels(); j<numOutputs; ++j)
  1193. {
  1194. if (proc->getOutputChannelName(j) != portName)
  1195. continue;
  1196. portId = kAudioOutputPortOffset+static_cast<uint>(j);
  1197. return true;
  1198. }
  1199. }
  1200. }
  1201. return false;
  1202. }
  1203. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const int frames)
  1204. {
  1205. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  1206. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  1207. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  1208. CARLA_SAFE_ASSERT_RETURN(frames > 0,);
  1209. // put events in juce buffer
  1210. {
  1211. midiBuffer.clear();
  1212. fillJuceMidiBufferFromEngineEvents(midiBuffer, data->events.in);
  1213. }
  1214. // put carla audio in juce buffer
  1215. {
  1216. int i=0;
  1217. for (; i < static_cast<int>(inputs); ++i)
  1218. FloatVectorOperations::copy(audioBuffer.getWritePointer(i), inBuf[i], frames);
  1219. // clear remaining channels
  1220. for (int count=audioBuffer.getNumChannels(); i<count; ++i)
  1221. audioBuffer.clear(i, 0, frames);
  1222. }
  1223. graph.processBlock(audioBuffer, midiBuffer);
  1224. // put juce audio in carla buffer
  1225. {
  1226. for (int i=0; i < static_cast<int>(outputs); ++i)
  1227. FloatVectorOperations::copy(outBuf[i], audioBuffer.getReadPointer(i), frames);
  1228. }
  1229. // put juce events in carla buffer
  1230. {
  1231. carla_zeroStruct<EngineEvent>(data->events.out, kMaxEngineEventInternalCount);
  1232. fillEngineEventsFromJuceMidiBuffer(data->events.out, midiBuffer);
  1233. midiBuffer.clear();
  1234. }
  1235. }
  1236. // -----------------------------------------------------------------------
  1237. // InternalGraph
  1238. EngineInternalGraph::EngineInternalGraph() noexcept
  1239. : fIsRack(true),
  1240. fIsReady(false)
  1241. {
  1242. fRack = nullptr;
  1243. }
  1244. EngineInternalGraph::~EngineInternalGraph() noexcept
  1245. {
  1246. CARLA_SAFE_ASSERT(! fIsReady);
  1247. CARLA_SAFE_ASSERT(fRack == nullptr);
  1248. }
  1249. void EngineInternalGraph::create(const bool isRack, const double sampleRate, const uint32_t bufferSize, const uint32_t inputs, const uint32_t outputs)
  1250. {
  1251. fIsRack = isRack;
  1252. if (isRack)
  1253. {
  1254. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  1255. fRack = new RackGraph(bufferSize, inputs, outputs);
  1256. }
  1257. else
  1258. {
  1259. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  1260. fPatchbay = new PatchbayGraph(static_cast<int>(bufferSize), sampleRate, inputs, outputs);
  1261. }
  1262. fIsReady = true;
  1263. }
  1264. void EngineInternalGraph::destroy() noexcept
  1265. {
  1266. if (! fIsReady)
  1267. {
  1268. CARLA_SAFE_ASSERT(fRack == nullptr);
  1269. return;
  1270. }
  1271. fIsReady = false;
  1272. if (fIsRack)
  1273. {
  1274. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1275. delete fRack;
  1276. fRack = nullptr;
  1277. }
  1278. else
  1279. {
  1280. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1281. delete fPatchbay;
  1282. fPatchbay = nullptr;
  1283. }
  1284. }
  1285. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  1286. {
  1287. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1288. if (fIsRack)
  1289. {
  1290. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1291. fRack->setBufferSize(bufferSize);
  1292. }
  1293. else
  1294. {
  1295. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1296. fPatchbay->setBufferSize(static_cast<int>(bufferSize));
  1297. }
  1298. }
  1299. void EngineInternalGraph::setSampleRate(const double sampleRate)
  1300. {
  1301. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1302. if (fIsRack)
  1303. {
  1304. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1305. }
  1306. else
  1307. {
  1308. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1309. fPatchbay->setSampleRate(sampleRate);
  1310. }
  1311. }
  1312. void EngineInternalGraph::setOffline(const bool offline)
  1313. {
  1314. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1315. if (fIsRack)
  1316. {
  1317. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1318. fRack->setOffline(offline);
  1319. }
  1320. else
  1321. {
  1322. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1323. fPatchbay->setOffline(offline);
  1324. }
  1325. }
  1326. bool EngineInternalGraph::isReady() const noexcept
  1327. {
  1328. return fIsReady;
  1329. }
  1330. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  1331. {
  1332. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  1333. return fRack;
  1334. }
  1335. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  1336. {
  1337. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  1338. return fPatchbay;
  1339. }
  1340. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  1341. {
  1342. if (fIsRack)
  1343. {
  1344. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1345. fRack->processHelper(data, inBuf, outBuf, frames);
  1346. }
  1347. else
  1348. {
  1349. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1350. fPatchbay->process(data, inBuf, outBuf, static_cast<int>(frames));
  1351. }
  1352. }
  1353. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  1354. {
  1355. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  1356. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1357. fRack->process(data, inBuf, outBuf, frames);
  1358. }
  1359. // -----------------------------------------------------------------------
  1360. // used for internal patchbay mode
  1361. void EngineInternalGraph::addPlugin(CarlaPlugin* const plugin)
  1362. {
  1363. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1364. fPatchbay->addPlugin(plugin);
  1365. }
  1366. void EngineInternalGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  1367. {
  1368. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1369. fPatchbay->replacePlugin(oldPlugin, newPlugin);
  1370. }
  1371. void EngineInternalGraph::removePlugin(CarlaPlugin* const plugin)
  1372. {
  1373. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1374. fPatchbay->removePlugin(plugin);
  1375. }
  1376. void EngineInternalGraph::removeAllPlugins(CarlaEngine* const engine)
  1377. {
  1378. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1379. fPatchbay->removeAllPlugins(engine);
  1380. }
  1381. void EngineInternalGraph::setIgnorePatchbay(const bool ignore) noexcept
  1382. {
  1383. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1384. fPatchbay->ignorePathbay = ignore;
  1385. }
  1386. // -----------------------------------------------------------------------
  1387. // CarlaEngine Patchbay stuff
  1388. bool CarlaEngine::patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB)
  1389. {
  1390. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1391. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1392. carla_debug("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  1393. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1394. {
  1395. if (RackGraph* const graph = pData->graph.getRackGraph())
  1396. return graph->connect(this, groupA, portA, groupB, portB);
  1397. }
  1398. else
  1399. {
  1400. if (PatchbayGraph* const graph = pData->graph.getPatchbayGraph())
  1401. return graph->connect(this, groupA, portA, groupB, portB);
  1402. }
  1403. return false;
  1404. }
  1405. bool CarlaEngine::patchbayDisconnect(const uint connectionId)
  1406. {
  1407. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1408. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1409. carla_debug("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  1410. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1411. {
  1412. if (RackGraph* const graph = pData->graph.getRackGraph())
  1413. return graph->disconnect(this, connectionId);
  1414. }
  1415. else
  1416. {
  1417. if (PatchbayGraph* const graph = pData->graph.getPatchbayGraph())
  1418. return graph->disconnect(this, connectionId);
  1419. }
  1420. return false;
  1421. }
  1422. bool CarlaEngine::patchbayRefresh(const bool external)
  1423. {
  1424. // subclasses should handle this
  1425. CARLA_SAFE_ASSERT_RETURN(! external, false);
  1426. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1427. {
  1428. // This is implemented in engine subclasses for MIDI support
  1429. setLastError("Unsupported operation");
  1430. return false;
  1431. }
  1432. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1433. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1434. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  1435. graph->refreshConnections(this);
  1436. return true;
  1437. }
  1438. // -----------------------------------------------------------------------
  1439. const char* const* CarlaEngine::getPatchbayConnections() const
  1440. {
  1441. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  1442. carla_debug("CarlaEngine::getPatchbayConnections()");
  1443. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1444. {
  1445. if (RackGraph* const graph = pData->graph.getRackGraph())
  1446. return graph->getConnections();
  1447. }
  1448. else
  1449. {
  1450. if (PatchbayGraph* const graph = pData->graph.getPatchbayGraph())
  1451. return graph->getConnections();
  1452. }
  1453. return nullptr;
  1454. }
  1455. void CarlaEngine::restorePatchbayConnection(const char* const connSource, const char* const connTarget)
  1456. {
  1457. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  1458. CARLA_SAFE_ASSERT_RETURN(connSource != nullptr && connSource[0] != '\0',);
  1459. CARLA_SAFE_ASSERT_RETURN(connTarget != nullptr && connTarget[0] != '\0',);
  1460. carla_debug("CarlaEngine::restorePatchbayConnection(\"%s\", \"%s\")", connSource, connTarget);
  1461. uint groupA, portA;
  1462. uint groupB, portB;
  1463. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1464. {
  1465. RackGraph* const graph = pData->graph.getRackGraph();
  1466. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1467. if (! graph->getGroupAndPortIdFromFullName(connSource, groupA, portA))
  1468. return;
  1469. if (! graph->getGroupAndPortIdFromFullName(connTarget, groupB, portB))
  1470. return;
  1471. }
  1472. else
  1473. {
  1474. PatchbayGraph* const graph = pData->graph.getPatchbayGraph();
  1475. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1476. if (! graph->getGroupAndPortIdFromFullName(connSource, groupA, portA))
  1477. return;
  1478. if (! graph->getGroupAndPortIdFromFullName(connTarget, groupB, portB))
  1479. return;
  1480. }
  1481. patchbayConnect(groupA, portA, groupB, portB);
  1482. }
  1483. // -----------------------------------------------------------------------
  1484. CARLA_BACKEND_END_NAMESPACE
  1485. // -----------------------------------------------------------------------