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.

1387 lines
44KB

  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. #include "juce_audio_processors.h"
  23. //#include "JuceInternalFilters.hpp"
  24. using juce::jmax;
  25. using juce::AudioPluginInstance;
  26. using juce::AudioProcessor;
  27. using juce::AudioProcessorEditor;
  28. using juce::FloatVectorOperations;
  29. using juce::MemoryBlock;
  30. using juce::PluginDescription;
  31. using juce::String;
  32. CARLA_BACKEND_START_NAMESPACE
  33. // -----------------------------------------------------------------------
  34. // Rack Graph stuff
  35. static inline
  36. uint getCarlaRackPortIdFromName(const char* const shortname) noexcept
  37. {
  38. if (std::strcmp(shortname, "AudioIn1") == 0 || std::strcmp(shortname, "audio-in1") == 0)
  39. return RACK_GRAPH_CARLA_PORT_AUDIO_IN1;
  40. if (std::strcmp(shortname, "AudioIn2") == 0 || std::strcmp(shortname, "audio-in2") == 0)
  41. return RACK_GRAPH_CARLA_PORT_AUDIO_IN2;
  42. if (std::strcmp(shortname, "AudioOut1") == 0 || std::strcmp(shortname, "audio-out1") == 0)
  43. return RACK_GRAPH_CARLA_PORT_AUDIO_OUT1;
  44. if (std::strcmp(shortname, "AudioOut2") == 0 || std::strcmp(shortname, "audio-out2") == 0)
  45. return RACK_GRAPH_CARLA_PORT_AUDIO_OUT2;
  46. if (std::strcmp(shortname, "MidiIn") == 0 || std::strcmp(shortname, "midi-in") == 0)
  47. return RACK_GRAPH_CARLA_PORT_MIDI_IN;
  48. if (std::strcmp(shortname, "MidiOut") == 0 || std::strcmp(shortname, "midi-out") == 0)
  49. return RACK_GRAPH_CARLA_PORT_MIDI_OUT;
  50. carla_stderr("CarlaBackend::getCarlaRackPortIdFromName(%s) - invalid short name", shortname);
  51. return RACK_GRAPH_CARLA_PORT_NULL;
  52. }
  53. static inline
  54. const char* getCarlaRackFullPortNameFromId(const /*RackGraphCarlaPortIds*/ uint portId)
  55. {
  56. switch (portId)
  57. {
  58. case RACK_GRAPH_CARLA_PORT_AUDIO_IN1:
  59. return "Carla:AudioIn1";
  60. case RACK_GRAPH_CARLA_PORT_AUDIO_IN2:
  61. return "Carla:AudioIn2";
  62. case RACK_GRAPH_CARLA_PORT_AUDIO_OUT1:
  63. return "Carla:AudioOut1";
  64. case RACK_GRAPH_CARLA_PORT_AUDIO_OUT2:
  65. return "Carla:AudioOut2";
  66. case RACK_GRAPH_CARLA_PORT_MIDI_IN:
  67. return "Carla:MidiIn";
  68. case RACK_GRAPH_CARLA_PORT_MIDI_OUT:
  69. return "Carla:MidiOut";
  70. //case RACK_GRAPH_CARLA_PORT_NULL:
  71. //case RACK_GRAPH_CARLA_PORT_MAX:
  72. // break;
  73. }
  74. carla_stderr("CarlaBackend::getCarlaRackFullPortNameFromId(%i) - invalid port id", portId);
  75. return nullptr;
  76. }
  77. // -----------------------------------------------------------------------
  78. // RackGraph MIDI
  79. const char* RackGraph::MIDI::getName(const bool isInput, const uint portId) const noexcept
  80. {
  81. for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin() : outs.begin(); it.valid(); it.next())
  82. {
  83. const PortNameToId& port(it.getValue());
  84. if (port.port == portId)
  85. return port.name;
  86. }
  87. return nullptr;
  88. }
  89. uint RackGraph::MIDI::getPortId(const bool isInput, const char portName[], bool* const ok) const noexcept
  90. {
  91. for (LinkedList<PortNameToId>::Itenerator it = isInput ? ins.begin() : outs.begin(); it.valid(); it.next())
  92. {
  93. const PortNameToId& port(it.getValue());
  94. if (std::strcmp(port.name, portName) == 0)
  95. {
  96. if (ok != nullptr)
  97. *ok = true;
  98. return port.port;
  99. }
  100. }
  101. if (ok != nullptr)
  102. *ok = false;
  103. return 0;
  104. }
  105. // -----------------------------------------------------------------------
  106. // RackGraph
  107. RackGraph::RackGraph(const uint32_t bufferSize, const uint32_t ins, const uint32_t outs) noexcept
  108. : inputs(ins),
  109. outputs(outs),
  110. isOffline(false)
  111. {
  112. audio.inBuf[0] = audio.inBuf[1] = nullptr;
  113. audio.inBufTmp[0] = audio.inBufTmp[1] = nullptr;
  114. audio.outBuf[0] = audio.outBuf[1] = nullptr;
  115. setBufferSize(bufferSize);
  116. }
  117. RackGraph::~RackGraph() noexcept
  118. {
  119. clearConnections();
  120. }
  121. void RackGraph::setBufferSize(const uint32_t bufferSize) noexcept
  122. {
  123. if (audio.inBuf[0] != nullptr) { delete[] audio.inBuf[0]; audio.inBuf[0] = nullptr; }
  124. if (audio.inBuf[1] != nullptr) { delete[] audio.inBuf[1]; audio.inBuf[1] = nullptr; }
  125. if (audio.inBufTmp[0] != nullptr) { delete[] audio.inBufTmp[0]; audio.inBufTmp[0] = nullptr; }
  126. if (audio.inBufTmp[1] != nullptr) { delete[] audio.inBufTmp[1]; audio.inBufTmp[1] = nullptr; }
  127. if (audio.outBuf[0] != nullptr) { delete[] audio.outBuf[0]; audio.outBuf[0] = nullptr; }
  128. if (audio.outBuf[1] != nullptr) { delete[] audio.outBuf[1]; audio.outBuf[1] = nullptr; }
  129. try {
  130. audio.inBufTmp[0] = new float[bufferSize];
  131. audio.inBufTmp[1] = new float[bufferSize];
  132. if (inputs > 0 || outputs > 0)
  133. {
  134. audio.inBuf[0] = new float[bufferSize];
  135. audio.inBuf[1] = new float[bufferSize];
  136. audio.outBuf[0] = new float[bufferSize];
  137. audio.outBuf[1] = new float[bufferSize];
  138. }
  139. }
  140. catch(...) {
  141. if (audio.inBufTmp[0] != nullptr) { delete[] audio.inBufTmp[0]; audio.inBufTmp[0] = nullptr; }
  142. if (audio.inBufTmp[1] != nullptr) { delete[] audio.inBufTmp[1]; audio.inBufTmp[1] = nullptr; }
  143. if (inputs > 0 || outputs > 0)
  144. {
  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.outBuf[0] != nullptr) { delete[] audio.outBuf[0]; audio.outBuf[0] = nullptr; }
  148. if (audio.outBuf[1] != nullptr) { delete[] audio.outBuf[1]; audio.outBuf[1] = nullptr; }
  149. }
  150. return;
  151. }
  152. FloatVectorOperations::clear(audio.inBufTmp[0], bufferSize);
  153. FloatVectorOperations::clear(audio.inBufTmp[1], bufferSize);
  154. if (inputs > 0 || outputs > 0)
  155. {
  156. FloatVectorOperations::clear(audio.inBuf[0], bufferSize);
  157. FloatVectorOperations::clear(audio.inBuf[1], bufferSize);
  158. FloatVectorOperations::clear(audio.outBuf[0], bufferSize);
  159. FloatVectorOperations::clear(audio.outBuf[1], bufferSize);
  160. }
  161. }
  162. void RackGraph::setOffline(const bool offline) noexcept
  163. {
  164. isOffline = offline;
  165. }
  166. bool RackGraph::connect(CarlaEngine* const engine, const uint groupA, const uint portA, const uint groupB, const uint portB) noexcept
  167. {
  168. CARLA_SAFE_ASSERT_RETURN(engine != nullptr, false);
  169. uint otherGroup, otherPort, carlaPort;
  170. if (groupA == RACK_GRAPH_GROUP_CARLA)
  171. {
  172. CARLA_SAFE_ASSERT_RETURN(groupB != RACK_GRAPH_GROUP_CARLA, false);
  173. carlaPort = portA;
  174. otherGroup = groupB;
  175. otherPort = portB;
  176. }
  177. else
  178. {
  179. CARLA_SAFE_ASSERT_RETURN(groupB == RACK_GRAPH_GROUP_CARLA, false);
  180. carlaPort = portB;
  181. otherGroup = groupA;
  182. otherPort = portA;
  183. }
  184. CARLA_SAFE_ASSERT_RETURN(carlaPort > RACK_GRAPH_CARLA_PORT_NULL && carlaPort < RACK_GRAPH_CARLA_PORT_MAX, false);
  185. CARLA_SAFE_ASSERT_RETURN(otherGroup > RACK_GRAPH_GROUP_CARLA && otherGroup < RACK_GRAPH_GROUP_MAX, false);
  186. bool makeConnection = false;
  187. switch (carlaPort)
  188. {
  189. case RACK_GRAPH_CARLA_PORT_AUDIO_IN1:
  190. CARLA_SAFE_ASSERT_RETURN(otherGroup == RACK_GRAPH_GROUP_AUDIO_IN, false);
  191. audio.mutex.lock();
  192. makeConnection = audio.connectedIn1.append(otherPort);
  193. audio.mutex.unlock();
  194. break;
  195. case RACK_GRAPH_CARLA_PORT_AUDIO_IN2:
  196. CARLA_SAFE_ASSERT_RETURN(otherGroup == RACK_GRAPH_GROUP_AUDIO_IN, false);
  197. audio.mutex.lock();
  198. makeConnection = audio.connectedIn2.append(otherPort);
  199. audio.mutex.unlock();
  200. break;
  201. case RACK_GRAPH_CARLA_PORT_AUDIO_OUT1:
  202. CARLA_SAFE_ASSERT_RETURN(otherGroup == RACK_GRAPH_GROUP_AUDIO_OUT, false);
  203. audio.mutex.lock();
  204. makeConnection = audio.connectedOut1.append(otherPort);
  205. audio.mutex.unlock();
  206. break;
  207. case RACK_GRAPH_CARLA_PORT_AUDIO_OUT2:
  208. CARLA_SAFE_ASSERT_RETURN(otherGroup == RACK_GRAPH_GROUP_AUDIO_OUT, false);
  209. audio.mutex.lock();
  210. makeConnection = audio.connectedOut2.append(otherPort);
  211. audio.mutex.unlock();
  212. break;
  213. case RACK_GRAPH_CARLA_PORT_MIDI_IN:
  214. CARLA_SAFE_ASSERT_RETURN(otherGroup == RACK_GRAPH_GROUP_MIDI_IN, false);
  215. if (const char* const portName = midi.getName(true, otherPort))
  216. makeConnection = engine->connectRackMidiInPort(portName);
  217. break;
  218. case RACK_GRAPH_CARLA_PORT_MIDI_OUT:
  219. CARLA_SAFE_ASSERT_RETURN(otherGroup == RACK_GRAPH_GROUP_MIDI_OUT, false);
  220. if (const char* const portName = midi.getName(false, otherPort))
  221. makeConnection = engine->connectRackMidiOutPort(portName);
  222. break;
  223. }
  224. if (! makeConnection)
  225. {
  226. engine->setLastError("Invalid rack connection");
  227. return false;
  228. }
  229. ConnectionToId connectionToId;
  230. connectionToId.setData(++connections.lastId, groupA, portA, groupB, portB);
  231. char strBuf[STR_MAX+1];
  232. strBuf[STR_MAX] = '\0';
  233. std::snprintf(strBuf, STR_MAX, "%u:%u:%u:%u", groupA, portA, groupB, portB);
  234. engine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  235. connections.list.append(connectionToId);
  236. return true;
  237. }
  238. bool RackGraph::disconnect(CarlaEngine* const engine, const uint connectionId) noexcept
  239. {
  240. CARLA_SAFE_ASSERT_RETURN(engine != nullptr, false);
  241. CARLA_SAFE_ASSERT_RETURN(connections.list.count() > 0, false);
  242. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin(); it.valid(); it.next())
  243. {
  244. const ConnectionToId& connection(it.getValue());
  245. if (connection.id != connectionId)
  246. continue;
  247. uint otherGroup, otherPort, carlaPort;
  248. if (connection.groupA == RACK_GRAPH_GROUP_CARLA)
  249. {
  250. CARLA_SAFE_ASSERT_RETURN(connection.groupB != RACK_GRAPH_GROUP_CARLA, false);
  251. carlaPort = connection.portA;
  252. otherGroup = connection.groupB;
  253. otherPort = connection.portB;
  254. }
  255. else
  256. {
  257. CARLA_SAFE_ASSERT_RETURN(connection.groupB == RACK_GRAPH_GROUP_CARLA, false);
  258. carlaPort = connection.portB;
  259. otherGroup = connection.groupA;
  260. otherPort = connection.portA;
  261. }
  262. CARLA_SAFE_ASSERT_RETURN(carlaPort > RACK_GRAPH_CARLA_PORT_NULL && carlaPort < RACK_GRAPH_CARLA_PORT_MAX, false);
  263. CARLA_SAFE_ASSERT_RETURN(otherGroup > RACK_GRAPH_GROUP_CARLA && otherGroup < RACK_GRAPH_GROUP_MAX, false);
  264. bool makeDisconnection = false;
  265. switch (carlaPort)
  266. {
  267. case RACK_GRAPH_CARLA_PORT_AUDIO_IN1:
  268. audio.mutex.lock();
  269. makeDisconnection = audio.connectedIn1.removeOne(otherPort);
  270. audio.mutex.unlock();
  271. break;
  272. case RACK_GRAPH_CARLA_PORT_AUDIO_IN2:
  273. audio.mutex.lock();
  274. makeDisconnection = audio.connectedIn2.removeOne(otherPort);
  275. audio.mutex.unlock();
  276. break;
  277. case RACK_GRAPH_CARLA_PORT_AUDIO_OUT1:
  278. audio.mutex.lock();
  279. makeDisconnection = audio.connectedOut1.removeOne(otherPort);
  280. audio.mutex.unlock();
  281. break;
  282. case RACK_GRAPH_CARLA_PORT_AUDIO_OUT2:
  283. audio.mutex.lock();
  284. makeDisconnection = audio.connectedOut2.removeOne(otherPort);
  285. audio.mutex.unlock();
  286. break;
  287. case RACK_GRAPH_CARLA_PORT_MIDI_IN:
  288. if (const char* const portName = midi.getName(true, otherPort))
  289. makeDisconnection = engine->disconnectRackMidiInPort(portName);
  290. break;
  291. case RACK_GRAPH_CARLA_PORT_MIDI_OUT:
  292. if (const char* const portName = midi.getName(false, otherPort))
  293. makeDisconnection = engine->disconnectRackMidiOutPort(portName);
  294. break;
  295. }
  296. if (! makeDisconnection)
  297. {
  298. engine->setLastError("Invalid rack connection");
  299. return false;
  300. }
  301. engine->callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED, connection.id, 0, 0, 0.0f, nullptr);
  302. connections.list.remove(it);
  303. return true;
  304. }
  305. engine->setLastError("Failed to find connection");
  306. return false;
  307. }
  308. void RackGraph::clearConnections() noexcept
  309. {
  310. connections.clear();
  311. audio.mutex.lock();
  312. audio.connectedIn1.clear();
  313. audio.connectedIn2.clear();
  314. audio.connectedOut1.clear();
  315. audio.connectedOut2.clear();
  316. audio.mutex.unlock();
  317. midi.ins.clear();
  318. midi.outs.clear();
  319. }
  320. const char* const* RackGraph::getConnections() const noexcept
  321. {
  322. if (connections.list.count() == 0)
  323. return nullptr;
  324. CarlaStringList connList;
  325. char strBuf[STR_MAX+1];
  326. strBuf[STR_MAX] = '\0';
  327. for (LinkedList<ConnectionToId>::Itenerator it=connections.list.begin(); it.valid(); it.next())
  328. {
  329. const ConnectionToId& connection(it.getValue());
  330. uint otherGroup, otherPort, carlaPort;
  331. if (connection.groupA == RACK_GRAPH_GROUP_CARLA)
  332. {
  333. CARLA_SAFE_ASSERT_CONTINUE(connection.groupB != RACK_GRAPH_GROUP_CARLA);
  334. carlaPort = connection.portA;
  335. otherGroup = connection.groupB;
  336. otherPort = connection.portB;
  337. }
  338. else
  339. {
  340. CARLA_SAFE_ASSERT_CONTINUE(connection.groupB == RACK_GRAPH_GROUP_CARLA);
  341. carlaPort = connection.portB;
  342. otherGroup = connection.groupA;
  343. otherPort = connection.portA;
  344. }
  345. CARLA_SAFE_ASSERT_CONTINUE(carlaPort > RACK_GRAPH_CARLA_PORT_NULL && carlaPort < RACK_GRAPH_CARLA_PORT_MAX);
  346. CARLA_SAFE_ASSERT_CONTINUE(otherGroup > RACK_GRAPH_GROUP_CARLA && otherGroup < RACK_GRAPH_GROUP_MAX);
  347. switch (carlaPort)
  348. {
  349. case RACK_GRAPH_CARLA_PORT_AUDIO_IN1:
  350. case RACK_GRAPH_CARLA_PORT_AUDIO_IN2:
  351. std::snprintf(strBuf, STR_MAX, "AudioIn:%i", otherPort+1);
  352. connList.append(strBuf);
  353. connList.append(getCarlaRackFullPortNameFromId(carlaPort));
  354. break;
  355. case RACK_GRAPH_CARLA_PORT_AUDIO_OUT1:
  356. case RACK_GRAPH_CARLA_PORT_AUDIO_OUT2:
  357. std::snprintf(strBuf, STR_MAX, "AudioOut:%i", otherPort+1);
  358. connList.append(getCarlaRackFullPortNameFromId(carlaPort));
  359. connList.append(strBuf);
  360. break;
  361. case RACK_GRAPH_CARLA_PORT_MIDI_IN:
  362. std::snprintf(strBuf, STR_MAX, "MidiIn:%s", midi.getName(true, otherPort));
  363. connList.append(strBuf);
  364. connList.append(getCarlaRackFullPortNameFromId(carlaPort));
  365. break;
  366. case RACK_GRAPH_CARLA_PORT_MIDI_OUT:
  367. std::snprintf(strBuf, STR_MAX, "MidiOut:%s", midi.getName(false, otherPort));
  368. connList.append(getCarlaRackFullPortNameFromId(carlaPort));
  369. connList.append(strBuf);
  370. break;
  371. }
  372. }
  373. if (connList.count() == 0)
  374. return nullptr;
  375. retCon = connList.toCharStringListPtr();
  376. return retCon;
  377. }
  378. bool RackGraph::getGroupAndPortIdFromFullName(const char* const fullPortName, uint& groupId, uint& portId) const noexcept
  379. {
  380. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', false);
  381. if (std::strncmp(fullPortName, "Carla:", 6) == 0)
  382. {
  383. groupId = RACK_GRAPH_GROUP_CARLA;
  384. portId = getCarlaRackPortIdFromName(fullPortName+6);
  385. if (portId > RACK_GRAPH_CARLA_PORT_NULL && portId < RACK_GRAPH_CARLA_PORT_MAX)
  386. return true;
  387. }
  388. else if (std::strncmp(fullPortName, "AudioIn:", 8) == 0)
  389. {
  390. groupId = RACK_GRAPH_GROUP_AUDIO_IN;
  391. if (const int portTest = std::atoi(fullPortName+8))
  392. {
  393. portId = static_cast<uint>(portTest-1);
  394. return true;
  395. }
  396. }
  397. else if (std::strncmp(fullPortName, "AudioOut:", 9) == 0)
  398. {
  399. groupId = RACK_GRAPH_GROUP_AUDIO_OUT;
  400. if (const int portTest = std::atoi(fullPortName+9))
  401. {
  402. portId = static_cast<uint>(portTest-1);
  403. return true;
  404. }
  405. }
  406. else if (std::strncmp(fullPortName, "MidiIn:", 7) == 0)
  407. {
  408. groupId = RACK_GRAPH_GROUP_MIDI_IN;
  409. if (const char* const portName = fullPortName+7)
  410. {
  411. bool ok;
  412. portId = midi.getPortId(true, portName, &ok);
  413. return ok;
  414. }
  415. }
  416. else if (std::strncmp(fullPortName, "MidiOut:", 8) == 0)
  417. {
  418. groupId = RACK_GRAPH_GROUP_MIDI_OUT;
  419. if (const char* const portName = fullPortName+8)
  420. {
  421. bool ok;
  422. portId = midi.getPortId(false, portName, &ok);
  423. return ok;
  424. }
  425. }
  426. return false;
  427. }
  428. void RackGraph::process(CarlaEngine::ProtectedData* const data, const float* inBufReal[2], float* outBuf[2], const uint32_t frames)
  429. {
  430. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  431. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  432. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  433. // safe copy
  434. float inBuf0[frames];
  435. float inBuf1[frames];
  436. float* inBuf[2] = { inBuf0, inBuf1 };
  437. // initialize audio inputs
  438. FloatVectorOperations::copy(inBuf0, inBufReal[0], frames);
  439. FloatVectorOperations::copy(inBuf1, inBufReal[1], frames);
  440. // initialize audio outputs (zero)
  441. FloatVectorOperations::clear(outBuf[0], frames);
  442. FloatVectorOperations::clear(outBuf[1], frames);
  443. // initialize event outputs (zero)
  444. carla_zeroStruct<EngineEvent>(data->events.out, kMaxEngineEventInternalCount);
  445. bool processed = false;
  446. uint32_t oldAudioInCount = 0;
  447. uint32_t oldMidiOutCount = 0;
  448. // process plugins
  449. for (uint i=0; i < data->curPluginCount; ++i)
  450. {
  451. CarlaPlugin* const plugin = data->plugins[i].plugin;
  452. if (plugin == nullptr || ! plugin->isEnabled() || ! plugin->tryLock(isOffline))
  453. continue;
  454. if (processed)
  455. {
  456. // initialize audio inputs (from previous outputs)
  457. FloatVectorOperations::copy(inBuf0, outBuf[0], frames);
  458. FloatVectorOperations::copy(inBuf1, outBuf[1], frames);
  459. // initialize audio outputs (zero)
  460. FloatVectorOperations::clear(outBuf[0], frames);
  461. FloatVectorOperations::clear(outBuf[1], frames);
  462. // if plugin has no midi out, add previous events
  463. if (oldMidiOutCount == 0 && data->events.in[0].type != kEngineEventTypeNull)
  464. {
  465. if (data->events.out[0].type != kEngineEventTypeNull)
  466. {
  467. // TODO: carefully add to input, sorted events
  468. }
  469. // else nothing needed
  470. }
  471. else
  472. {
  473. // initialize event inputs from previous outputs
  474. carla_copyStruct<EngineEvent>(data->events.in, data->events.out, kMaxEngineEventInternalCount);
  475. // initialize event outputs (zero)
  476. carla_zeroStruct<EngineEvent>(data->events.out, kMaxEngineEventInternalCount);
  477. }
  478. }
  479. oldAudioInCount = plugin->getAudioInCount();
  480. oldMidiOutCount = plugin->getMidiOutCount();
  481. // process
  482. plugin->initBuffers();
  483. plugin->process(inBuf, outBuf, frames);
  484. plugin->unlock();
  485. // if plugin has no audio inputs, add input buffer
  486. if (oldAudioInCount == 0)
  487. {
  488. FloatVectorOperations::add(outBuf[0], inBuf0, frames);
  489. FloatVectorOperations::add(outBuf[1], inBuf1, frames);
  490. }
  491. // set peaks
  492. {
  493. EnginePluginData& pluginData(data->plugins[i]);
  494. juce::Range<float> range;
  495. if (oldAudioInCount > 0)
  496. {
  497. range = FloatVectorOperations::findMinAndMax(inBuf0, static_cast<int>(frames));
  498. pluginData.insPeak[0] = carla_max<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  499. range = FloatVectorOperations::findMinAndMax(inBuf1, static_cast<int>(frames));
  500. pluginData.insPeak[1] = carla_max<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  501. }
  502. else
  503. {
  504. pluginData.insPeak[0] = 0.0f;
  505. pluginData.insPeak[1] = 0.0f;
  506. }
  507. if (plugin->getAudioOutCount() > 0)
  508. {
  509. range = FloatVectorOperations::findMinAndMax(outBuf[0], static_cast<int>(frames));
  510. pluginData.outsPeak[0] = carla_max<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  511. range = FloatVectorOperations::findMinAndMax(outBuf[1], static_cast<int>(frames));
  512. pluginData.outsPeak[1] = carla_max<float>(std::abs(range.getStart()), std::abs(range.getEnd()), 1.0f);
  513. }
  514. else
  515. {
  516. pluginData.outsPeak[0] = 0.0f;
  517. pluginData.outsPeak[1] = 0.0f;
  518. }
  519. }
  520. processed = true;
  521. }
  522. }
  523. void RackGraph::processHelper(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  524. {
  525. CARLA_SAFE_ASSERT_RETURN(audio.outBuf[1] != nullptr,);
  526. const CarlaRecursiveMutexLocker _cml(audio.mutex);
  527. if (inBuf != nullptr && inputs > 0)
  528. {
  529. bool noConnections = true;
  530. // connect input buffers
  531. for (LinkedList<uint>::Itenerator it = audio.connectedIn1.begin(); it.valid(); it.next())
  532. {
  533. const uint& port(it.getValue());
  534. CARLA_SAFE_ASSERT_CONTINUE(port < inputs);
  535. if (noConnections)
  536. {
  537. FloatVectorOperations::copy(audio.inBuf[0], inBuf[port], frames);
  538. noConnections = false;
  539. }
  540. else
  541. {
  542. FloatVectorOperations::add(audio.inBuf[0], inBuf[port], frames);
  543. }
  544. }
  545. if (noConnections)
  546. FloatVectorOperations::clear(audio.inBuf[0], frames);
  547. noConnections = true;
  548. for (LinkedList<uint>::Itenerator it = audio.connectedIn2.begin(); it.valid(); it.next())
  549. {
  550. const uint& port(it.getValue());
  551. CARLA_SAFE_ASSERT_CONTINUE(port < inputs);
  552. if (noConnections)
  553. {
  554. FloatVectorOperations::copy(audio.inBuf[1], inBuf[port], frames);
  555. noConnections = false;
  556. }
  557. else
  558. {
  559. FloatVectorOperations::add(audio.inBuf[1], inBuf[port], frames);
  560. }
  561. }
  562. if (noConnections)
  563. FloatVectorOperations::clear(audio.inBuf[1], frames);
  564. }
  565. else
  566. {
  567. FloatVectorOperations::clear(audio.inBuf[0], frames);
  568. FloatVectorOperations::clear(audio.inBuf[1], frames);
  569. }
  570. FloatVectorOperations::clear(audio.outBuf[0], frames);
  571. FloatVectorOperations::clear(audio.outBuf[1], frames);
  572. // process
  573. process(data, const_cast<const float**>(audio.inBuf), audio.outBuf, frames);
  574. // connect output buffers
  575. if (audio.connectedOut1.count() != 0)
  576. {
  577. for (LinkedList<uint>::Itenerator it = audio.connectedOut1.begin(); it.valid(); it.next())
  578. {
  579. const uint& port(it.getValue());
  580. CARLA_SAFE_ASSERT_CONTINUE(port < outputs);
  581. FloatVectorOperations::add(outBuf[port], audio.outBuf[0], frames);
  582. }
  583. }
  584. if (audio.connectedOut2.count() != 0)
  585. {
  586. for (LinkedList<uint>::Itenerator it = audio.connectedOut2.begin(); it.valid(); it.next())
  587. {
  588. const uint& port(it.getValue());
  589. CARLA_SAFE_ASSERT_CONTINUE(port < outputs);
  590. FloatVectorOperations::add(outBuf[port], audio.outBuf[1], frames);
  591. }
  592. }
  593. }
  594. // -----------------------------------------------------------------------
  595. class CarlaPluginInstance : public AudioPluginInstance
  596. {
  597. public:
  598. CarlaPluginInstance(CarlaPlugin* const plugin)
  599. : fPlugin(plugin),
  600. fGraph(nullptr) {}
  601. ~CarlaPluginInstance() override
  602. {
  603. }
  604. // -------------------------------------------------------------------
  605. AudioProcessorGraph* getParentGraph() const noexcept
  606. {
  607. return fGraph;
  608. }
  609. void setParentGraph(AudioProcessorGraph*)
  610. {
  611. }
  612. void* getPlatformSpecificData() noexcept override
  613. {
  614. return fPlugin;
  615. }
  616. void fillInPluginDescription(PluginDescription& d) const override
  617. {
  618. d.pluginFormatName = "Carla";
  619. d.category = "Carla Plugin";
  620. d.version = "1.0";
  621. CARLA_SAFE_ASSERT_RETURN(fPlugin != nullptr,);
  622. char strBuf[STR_MAX+1];
  623. strBuf[STR_MAX] = '\0';
  624. fPlugin->getRealName(strBuf);
  625. d.name = strBuf;
  626. fPlugin->getLabel(strBuf);
  627. d.descriptiveName = strBuf;
  628. fPlugin->getMaker(strBuf);
  629. d.manufacturerName = strBuf;
  630. d.uid = d.name.hashCode();
  631. d.isInstrument = (fPlugin->getHints() & PLUGIN_IS_SYNTH);
  632. d.numInputChannels = fPlugin->getAudioInCount();
  633. d.numOutputChannels = fPlugin->getAudioOutCount();
  634. //d.hasSharedContainer = true;
  635. }
  636. // -------------------------------------------------------------------
  637. const String getName() const override
  638. {
  639. return fPlugin->getName();
  640. }
  641. void processBlock(AudioSampleBuffer& audio, MidiBuffer& midi)
  642. {
  643. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventInPort())
  644. {
  645. EngineEvent* const buffer(port->fBuffer);
  646. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  647. carla_zeroStruct<EngineEvent>(buffer, kMaxEngineEventInternalCount);
  648. const uint8_t* midiData;
  649. int numBytes;
  650. int sampleNumber;
  651. ushort engineEventIndex = 0;
  652. for (MidiBuffer::Iterator outBufferIterator(midi); outBufferIterator.getNextEvent(midiData, numBytes, sampleNumber);)
  653. {
  654. if (numBytes <= 0 || numBytes >= MAX_MIDI_VALUE)
  655. continue;
  656. EngineEvent& engineEvent(buffer[engineEventIndex++]);
  657. engineEvent.time = sampleNumber;
  658. engineEvent.fillFromMidiData(static_cast<uint8_t>(numBytes), midiData);
  659. if (engineEventIndex >= kMaxEngineEventInternalCount)
  660. break;
  661. }
  662. }
  663. if (const int numChan = audio.getNumChannels())
  664. {
  665. float* audioBuffers[numChan];
  666. for (int i=0; i<numChan; ++i)
  667. audioBuffers[i] = audio.getWritePointer(i);
  668. fPlugin->process(audioBuffers, audioBuffers, audio.getNumSamples());
  669. }
  670. else
  671. {
  672. fPlugin->process(nullptr, nullptr, audio.getNumSamples());
  673. }
  674. midi.clear();
  675. if (CarlaEngineEventPort* const port = fPlugin->getDefaultEventOutPort())
  676. {
  677. EngineEvent* const buffer(port->fBuffer);
  678. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  679. uint8_t size = 0;
  680. uint8_t mdata[3] = { 0, 0, 0 };
  681. const uint8_t* mdataPtr = mdata;
  682. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  683. {
  684. const EngineEvent& engineEvent(buffer[i]);
  685. if (engineEvent.type == kEngineEventTypeNull)
  686. break;
  687. else if (engineEvent.type == kEngineEventTypeControl)
  688. {
  689. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  690. ctrlEvent.convertToMidiData(engineEvent.channel, size, mdata);
  691. mdataPtr = mdata;
  692. }
  693. else if (engineEvent.type == kEngineEventTypeMidi)
  694. {
  695. const EngineMidiEvent& midiEvent(engineEvent.midi);
  696. size = midiEvent.size;
  697. if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
  698. mdataPtr = midiEvent.dataExt;
  699. else
  700. mdataPtr = midiEvent.data;
  701. }
  702. else
  703. {
  704. continue;
  705. }
  706. if (size > 0)
  707. midi.addEvent(mdataPtr, size, engineEvent.time);
  708. }
  709. }
  710. }
  711. void prepareToPlay(double, int) override {}
  712. void releaseResources() override {}
  713. const String getInputChannelName(int) const override { return String(); }
  714. const String getOutputChannelName(int) const override { return String(); }
  715. const String getParameterName(int) override { return String(); }
  716. const String getParameterText(int) override { return String(); }
  717. const String getProgramName(int) override { return String(); }
  718. double getTailLengthSeconds() const override { return 0.0; }
  719. float getParameter(int) override { return 0.0f; }
  720. bool isInputChannelStereoPair(int) const override { return true; }
  721. bool isOutputChannelStereoPair(int) const override { return true; }
  722. bool silenceInProducesSilenceOut() const override { return true; }
  723. bool hasEditor() const override { return false; }
  724. bool acceptsMidi() const override { return fPlugin->getMidiInCount() > 0; }
  725. bool producesMidi() const override { return fPlugin->getMidiOutCount() > 0; }
  726. void setParameter(int, float) override {}
  727. void setCurrentProgram(int) override {}
  728. void changeProgramName(int, const String&) override {}
  729. void getStateInformation(MemoryBlock&) override {}
  730. void setStateInformation(const void*, int) override {}
  731. int getNumParameters() override { return 0; }
  732. int getNumPrograms() override { return 0; }
  733. int getCurrentProgram() override { return 0; }
  734. AudioProcessorEditor* createEditor() override { return nullptr; }
  735. // -------------------------------------------------------------------
  736. private:
  737. CarlaPlugin* const fPlugin;
  738. AudioProcessorGraph* fGraph;
  739. };
  740. // -----------------------------------------------------------------------
  741. // PatchbayGraph
  742. static const int kMidiInputNodeId = MAX_PATCHBAY_PLUGINS*3+1;
  743. static const int kMidiOutputNodeId = MAX_PATCHBAY_PLUGINS*3+2;
  744. PatchbayGraph::PatchbayGraph(const uint32_t bufferSize, const double sampleRate, const uint32_t ins, const uint32_t outs)
  745. : inputs(ins),
  746. outputs(outs)
  747. {
  748. graph.setPlayConfigDetails(inputs, outputs, sampleRate, bufferSize);
  749. graph.prepareToPlay(sampleRate, bufferSize);
  750. audioBuffer.setSize(jmax(ins, outs), bufferSize);
  751. midiBuffer.ensureSize(kMaxEngineEventInternalCount*2);
  752. midiBuffer.clear();
  753. for (uint32_t i=0; i<ins && i<MAX_PATCHBAY_PLUGINS; ++i)
  754. {
  755. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::audioInputNode));
  756. graph.addNode(proc, MAX_PATCHBAY_PLUGINS + i);
  757. }
  758. for (uint32_t i=0; i<outs && i<MAX_PATCHBAY_PLUGINS; ++i)
  759. {
  760. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::audioOutputNode));
  761. graph.addNode(proc, MAX_PATCHBAY_PLUGINS*2 + i);
  762. }
  763. {
  764. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::midiInputNode));
  765. graph.addNode(proc, kMidiInputNodeId);
  766. }
  767. {
  768. AudioProcessorGraph::AudioGraphIOProcessor* const proc(new AudioProcessorGraph::AudioGraphIOProcessor(AudioProcessorGraph::AudioGraphIOProcessor::midiOutputNode));
  769. graph.addNode(proc, kMidiOutputNodeId);
  770. }
  771. }
  772. PatchbayGraph::~PatchbayGraph()
  773. {
  774. graph.releaseResources();
  775. graph.clear();
  776. audioBuffer.clear();
  777. }
  778. void PatchbayGraph::setBufferSize(const uint32_t bufferSize)
  779. {
  780. graph.releaseResources();
  781. graph.prepareToPlay(graph.getSampleRate(), bufferSize);
  782. audioBuffer.setSize(audioBuffer.getNumChannels(), bufferSize);
  783. }
  784. void PatchbayGraph::setSampleRate(const double sampleRate)
  785. {
  786. graph.releaseResources();
  787. graph.prepareToPlay(sampleRate, graph.getBlockSize());
  788. }
  789. void PatchbayGraph::setOffline(const bool offline)
  790. {
  791. graph.setNonRealtime(offline);
  792. }
  793. void PatchbayGraph::addPlugin(CarlaPlugin* const plugin)
  794. {
  795. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  796. CarlaPluginInstance* const instance(new CarlaPluginInstance(plugin));
  797. graph.addNode(instance, plugin->getId());
  798. }
  799. void PatchbayGraph::replacePlugin(CarlaPlugin* const oldPlugin, CarlaPlugin* const newPlugin)
  800. {
  801. CARLA_SAFE_ASSERT_RETURN(oldPlugin != nullptr,);
  802. CARLA_SAFE_ASSERT_RETURN(newPlugin != nullptr,);
  803. CARLA_SAFE_ASSERT_RETURN(oldPlugin != newPlugin,);
  804. CARLA_SAFE_ASSERT_RETURN(oldPlugin->getId() == newPlugin->getId(),);
  805. CarlaPluginInstance* const instance(new CarlaPluginInstance(newPlugin));
  806. graph.addNode(instance, newPlugin->getId());
  807. }
  808. void PatchbayGraph::removePlugin(CarlaPlugin* const plugin)
  809. {
  810. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr,);
  811. graph.removeNode(plugin->getId());
  812. // move all plugins 1 spot backwards
  813. for (uint i=plugin->getId(); i<MAX_PATCHBAY_PLUGINS; ++i)
  814. {
  815. if (AudioProcessorGraph::Node* const node = graph.getNodeForId(i+1))
  816. {
  817. if (AudioProcessor* const proc = node->getProcessor())
  818. graph.addNode(proc, i);
  819. continue;
  820. }
  821. break;
  822. }
  823. }
  824. void PatchbayGraph::removeAllPlugins()
  825. {
  826. graph.clear();
  827. // TODO
  828. }
  829. #if 0
  830. bool PatchbayGraph::connect(CarlaEngine* const engine, const uint /*groupA*/, const uint /*portA*/, const uint /*groupB*/, const uint /*portB*/) noexcept
  831. {
  832. CARLA_SAFE_ASSERT_RETURN(engine != nullptr, false);
  833. return false;
  834. }
  835. bool PatchbayGraph::disconnect(CarlaEngine* const engine, const uint /*connectionId*/) noexcept
  836. {
  837. CARLA_SAFE_ASSERT_RETURN(engine != nullptr, false);
  838. return false;
  839. }
  840. const char* const* PatchbayGraph::getConnections() const noexcept
  841. {
  842. return nullptr;
  843. }
  844. bool PatchbayGraph::getPortIdFromFullName(const char* const /*fillPortName*/, uint& /*groupId*/, uint& /*portId*/) const noexcept
  845. {
  846. return false;
  847. }
  848. #endif
  849. void PatchbayGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  850. {
  851. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  852. CARLA_SAFE_ASSERT_RETURN(data->events.in != nullptr,);
  853. CARLA_SAFE_ASSERT_RETURN(data->events.out != nullptr,);
  854. // put events in juce buffer
  855. {
  856. uint8_t size = 0;
  857. uint8_t mdata[3] = { 0, 0, 0 };
  858. const uint8_t* mdataPtr = mdata;
  859. midiBuffer.clear();
  860. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  861. {
  862. const EngineEvent& engineEvent(data->events.in[i]);
  863. if (engineEvent.type == kEngineEventTypeNull)
  864. break;
  865. else if (engineEvent.type == kEngineEventTypeControl)
  866. {
  867. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  868. ctrlEvent.convertToMidiData(engineEvent.channel, size, mdata);
  869. mdataPtr = mdata;
  870. }
  871. else if (engineEvent.type == kEngineEventTypeMidi)
  872. {
  873. const EngineMidiEvent& midiEvent(engineEvent.midi);
  874. size = midiEvent.size;
  875. if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
  876. mdataPtr = midiEvent.dataExt;
  877. else
  878. mdataPtr = midiEvent.data;
  879. }
  880. else
  881. {
  882. continue;
  883. }
  884. if (size > 0)
  885. midiBuffer.addEvent(mdataPtr, size, engineEvent.time);
  886. }
  887. }
  888. // initialize event outputs (zero)
  889. carla_zeroStruct<EngineEvent>(data->events.out, kMaxEngineEventInternalCount);
  890. // put carla audio in juce buffer
  891. {
  892. int i=0;
  893. for (; i<inputs; ++i)
  894. FloatVectorOperations::copy(audioBuffer.getWritePointer(i), inBuf[i], frames);
  895. // clear remaining channels
  896. for (int count=audioBuffer.getNumChannels(); i<count; ++i)
  897. audioBuffer.clear(i, 0, frames);
  898. }
  899. graph.processBlock(audioBuffer, midiBuffer);
  900. // put juce audio in carla buffer
  901. {
  902. for (int i=0; i<outputs; ++i)
  903. FloatVectorOperations::copy(outBuf[i], audioBuffer.getReadPointer(i), frames);
  904. }
  905. // put juce events in carla buffer
  906. {
  907. const uint8_t* midiData;
  908. int numBytes;
  909. int sampleNumber;
  910. ushort engineEventIndex = 0;
  911. for (MidiBuffer::Iterator outBufferIterator(midiBuffer); outBufferIterator.getNextEvent(midiData, numBytes, sampleNumber);)
  912. {
  913. if (numBytes <= 0 || numBytes >= MAX_MIDI_VALUE)
  914. continue;
  915. EngineEvent& engineEvent(data->events.out[engineEventIndex++]);
  916. engineEvent.time = sampleNumber;
  917. engineEvent.fillFromMidiData(static_cast<uint8_t>(numBytes), midiData);
  918. if (engineEventIndex >= kMaxEngineEventInternalCount)
  919. break;
  920. }
  921. }
  922. }
  923. // -----------------------------------------------------------------------
  924. // InternalGraph
  925. EngineInternalGraph::EngineInternalGraph() noexcept
  926. : fIsRack(true),
  927. fIsReady(false)
  928. {
  929. fRack = nullptr;
  930. }
  931. EngineInternalGraph::~EngineInternalGraph() noexcept
  932. {
  933. CARLA_SAFE_ASSERT(! fIsReady);
  934. CARLA_SAFE_ASSERT(fRack == nullptr);
  935. }
  936. void EngineInternalGraph::create(const bool isRack, const double sampleRate, const uint32_t bufferSize, const uint32_t inputs, const uint32_t outputs)
  937. {
  938. fIsRack = isRack;
  939. if (isRack)
  940. {
  941. CARLA_SAFE_ASSERT_RETURN(fRack == nullptr,);
  942. fRack = new RackGraph(bufferSize, inputs, outputs);
  943. }
  944. else
  945. {
  946. CARLA_SAFE_ASSERT_RETURN(fPatchbay == nullptr,);
  947. fPatchbay = new PatchbayGraph(sampleRate, bufferSize, inputs, outputs);
  948. }
  949. fIsReady = true;
  950. }
  951. void EngineInternalGraph::destroy() noexcept
  952. {
  953. if (! fIsReady)
  954. {
  955. CARLA_SAFE_ASSERT(fRack == nullptr);
  956. return;
  957. }
  958. fIsReady = false;
  959. if (fIsRack)
  960. {
  961. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  962. delete fRack;
  963. fRack = nullptr;
  964. }
  965. else
  966. {
  967. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  968. delete fPatchbay;
  969. fPatchbay = nullptr;
  970. }
  971. }
  972. void EngineInternalGraph::setBufferSize(const uint32_t bufferSize)
  973. {
  974. ScopedValueSetter<bool> svs(fIsReady, false, true);
  975. if (fIsRack)
  976. {
  977. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  978. fRack->setBufferSize(bufferSize);
  979. }
  980. else
  981. {
  982. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  983. fPatchbay->setBufferSize(bufferSize);
  984. }
  985. }
  986. void EngineInternalGraph::setSampleRate(const double sampleRate)
  987. {
  988. ScopedValueSetter<bool> svs(fIsReady, false, true);
  989. if (fIsRack)
  990. {
  991. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  992. }
  993. else
  994. {
  995. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  996. fPatchbay->setSampleRate(sampleRate);
  997. }
  998. }
  999. void EngineInternalGraph::setOffline(const bool offline)
  1000. {
  1001. ScopedValueSetter<bool> svs(fIsReady, false, true);
  1002. if (fIsRack)
  1003. {
  1004. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1005. fRack->setOffline(offline);
  1006. }
  1007. else
  1008. {
  1009. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1010. fPatchbay->setOffline(offline);
  1011. }
  1012. }
  1013. bool EngineInternalGraph::isReady() const noexcept
  1014. {
  1015. return fIsReady;
  1016. }
  1017. RackGraph* EngineInternalGraph::getRackGraph() const noexcept
  1018. {
  1019. CARLA_SAFE_ASSERT_RETURN(fIsRack, nullptr);
  1020. return fRack;
  1021. }
  1022. PatchbayGraph* EngineInternalGraph::getPatchbayGraph() const noexcept
  1023. {
  1024. CARLA_SAFE_ASSERT_RETURN(! fIsRack, nullptr);
  1025. return fPatchbay;
  1026. }
  1027. void EngineInternalGraph::process(CarlaEngine::ProtectedData* const data, const float* const* const inBuf, float* const* const outBuf, const uint32_t frames)
  1028. {
  1029. if (fIsRack)
  1030. {
  1031. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1032. fRack->processHelper(data, inBuf, outBuf, frames);
  1033. }
  1034. else
  1035. {
  1036. CARLA_SAFE_ASSERT_RETURN(fPatchbay != nullptr,);
  1037. fPatchbay->process(data, inBuf, outBuf, frames);
  1038. }
  1039. }
  1040. void EngineInternalGraph::processRack(CarlaEngine::ProtectedData* const data, const float* inBuf[2], float* outBuf[2], const uint32_t frames)
  1041. {
  1042. CARLA_SAFE_ASSERT_RETURN(fIsRack,);
  1043. CARLA_SAFE_ASSERT_RETURN(fRack != nullptr,);
  1044. fRack->process(data, inBuf, outBuf, frames);
  1045. }
  1046. // -----------------------------------------------------------------------
  1047. // CarlaEngine Patchbay stuff
  1048. bool CarlaEngine::patchbayConnect(const uint groupA, const uint portA, const uint groupB, const uint portB)
  1049. {
  1050. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1051. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1052. carla_stdout("CarlaEngine::patchbayConnect(%u, %u, %u, %u)", groupA, portA, groupB, portB);
  1053. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1054. {
  1055. if (RackGraph* const graph = pData->graph.getRackGraph())
  1056. return graph->connect(this, groupA, portA, groupB, portB);
  1057. }
  1058. else
  1059. {
  1060. }
  1061. return false;
  1062. }
  1063. bool CarlaEngine::patchbayDisconnect(const uint connectionId)
  1064. {
  1065. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY, false);
  1066. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  1067. carla_stdout("CarlaEngine::patchbayDisconnect(%u)", connectionId);
  1068. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1069. {
  1070. if (RackGraph* const graph = pData->graph.getRackGraph())
  1071. return graph->disconnect(this, connectionId);
  1072. }
  1073. else
  1074. {
  1075. }
  1076. return false;
  1077. }
  1078. bool CarlaEngine::patchbayRefresh()
  1079. {
  1080. setLastError("Unsupported operation");
  1081. return false;
  1082. }
  1083. // -----------------------------------------------------------------------
  1084. const char* const* CarlaEngine::getPatchbayConnections() const
  1085. {
  1086. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), nullptr);
  1087. carla_debug("CarlaEngine::getPatchbayConnections()");
  1088. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1089. {
  1090. if (RackGraph* const graph = pData->graph.getRackGraph())
  1091. return graph->getConnections();
  1092. }
  1093. else
  1094. {
  1095. }
  1096. return nullptr;
  1097. }
  1098. void CarlaEngine::restorePatchbayConnection(const char* const connSource, const char* const connTarget)
  1099. {
  1100. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(),);
  1101. CARLA_SAFE_ASSERT_RETURN(connSource != nullptr && connSource[0] != '\0',);
  1102. CARLA_SAFE_ASSERT_RETURN(connTarget != nullptr && connTarget[0] != '\0',);
  1103. carla_debug("CarlaEngine::restorePatchbayConnection(\"%s\", \"%s\")", connSource, connTarget);
  1104. uint groupA, portA;
  1105. uint groupB, portB;
  1106. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1107. {
  1108. RackGraph* const graph = pData->graph.getRackGraph();
  1109. CARLA_SAFE_ASSERT_RETURN(graph != nullptr,);
  1110. if (! graph->getGroupAndPortIdFromFullName(connSource, groupA, portA))
  1111. return;
  1112. if (! graph->getGroupAndPortIdFromFullName(connTarget, groupB, portB))
  1113. return;
  1114. }
  1115. else
  1116. {
  1117. return;
  1118. }
  1119. patchbayConnect(groupA, portA, groupB, portB);
  1120. }
  1121. // -----------------------------------------------------------------------
  1122. CARLA_BACKEND_END_NAMESPACE