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.

919 lines
27KB

  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 GPL.txt file
  16. */
  17. #include "CarlaEngineGraph.hpp"
  18. #include "CarlaEngineInternal.hpp"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaStringList.hpp"
  21. #include "RtLinkedList.hpp"
  22. #include "juce_audio_devices.h"
  23. using namespace juce;
  24. CARLA_BACKEND_START_NAMESPACE
  25. // -------------------------------------------------------------------------------------------------------------------
  26. // Global static data
  27. static CharStringListPtr gDeviceNames;
  28. static OwnedArray<AudioIODeviceType> gDeviceTypes;
  29. struct JuceCleanup : public DeletedAtShutdown {
  30. JuceCleanup() noexcept {}
  31. ~JuceCleanup()
  32. {
  33. gDeviceTypes.clear(true);
  34. }
  35. };
  36. // -------------------------------------------------------------------------------------------------------------------
  37. // Cleanup
  38. static void initJuceDevicesIfNeeded()
  39. {
  40. static AudioDeviceManager sDeviceManager;
  41. if (gDeviceTypes.size() != 0)
  42. return;
  43. sDeviceManager.createAudioDeviceTypes(gDeviceTypes);
  44. CARLA_SAFE_ASSERT_RETURN(gDeviceTypes.size() != 0,);
  45. new JuceCleanup();
  46. // remove JACK from device list
  47. for (int i=0, count=gDeviceTypes.size(); i < count; ++i)
  48. {
  49. if (gDeviceTypes[i]->getTypeName() == "JACK")
  50. {
  51. gDeviceTypes.remove(i, true);
  52. break;
  53. }
  54. }
  55. }
  56. // -------------------------------------------------------------------------------------------------------------------
  57. // Juce Engine
  58. class CarlaEngineJuce : public CarlaEngine,
  59. public AudioIODeviceCallback,
  60. public MidiInputCallback
  61. {
  62. public:
  63. CarlaEngineJuce(AudioIODeviceType* const devType)
  64. : CarlaEngine(),
  65. AudioIODeviceCallback(),
  66. fDevice(),
  67. fDeviceType(devType),
  68. fMidiIns(),
  69. fMidiInEvents(),
  70. fMidiOuts(),
  71. fMidiOutMutex(),
  72. leakDetector_CarlaEngineJuce()
  73. {
  74. carla_debug("CarlaEngineJuce::CarlaEngineJuce(%p)", devType);
  75. // just to make sure
  76. pData->options.transportMode = ENGINE_TRANSPORT_MODE_INTERNAL;
  77. }
  78. ~CarlaEngineJuce() override
  79. {
  80. carla_debug("CarlaEngineJuce::~CarlaEngineJuce()");
  81. }
  82. // -------------------------------------
  83. bool init(const char* const clientName) override
  84. {
  85. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  86. carla_debug("CarlaEngineJuce::init(\"%s\")", clientName);
  87. if (pData->options.processMode != ENGINE_PROCESS_MODE_CONTINUOUS_RACK && pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  88. {
  89. setLastError("Invalid process mode");
  90. return false;
  91. }
  92. String deviceName;
  93. if (pData->options.audioDevice != nullptr && pData->options.audioDevice[0] != '\0')
  94. {
  95. deviceName = pData->options.audioDevice;
  96. }
  97. else
  98. {
  99. const int defaultIndex(fDeviceType->getDefaultDeviceIndex(false));
  100. StringArray deviceNames(fDeviceType->getDeviceNames());
  101. if (defaultIndex >= 0 && defaultIndex < deviceNames.size())
  102. deviceName = deviceNames[defaultIndex];
  103. }
  104. if (deviceName.isEmpty())
  105. {
  106. setLastError("Audio device has not been selected yet and a default one is not available");
  107. return false;
  108. }
  109. fDevice = fDeviceType->createDevice(deviceName, deviceName);
  110. if (fDevice == nullptr)
  111. {
  112. setLastError("Failed to create device");
  113. return false;
  114. }
  115. StringArray inputNames(fDevice->getInputChannelNames());
  116. StringArray outputNames(fDevice->getOutputChannelNames());
  117. if (inputNames.size() < 0 || outputNames.size() <= 0)
  118. {
  119. setLastError("Selected device does not have any outputs");
  120. return false;
  121. }
  122. BigInteger inputChannels;
  123. inputChannels.setRange(0, inputNames.size(), true);
  124. BigInteger outputChannels;
  125. outputChannels.setRange(0, outputNames.size(), true);
  126. String error = fDevice->open(inputChannels, outputChannels, pData->options.audioSampleRate, static_cast<int>(pData->options.audioBufferSize));
  127. if (error.isNotEmpty())
  128. {
  129. setLastError(error.toUTF8());
  130. fDevice = nullptr;
  131. return false;
  132. }
  133. if (! pData->init(clientName))
  134. {
  135. close();
  136. setLastError("Failed to init internal data");
  137. return false;
  138. }
  139. pData->bufferSize = static_cast<uint32_t>(fDevice->getCurrentBufferSizeSamples());
  140. pData->sampleRate = fDevice->getCurrentSampleRate();
  141. pData->graph.create(static_cast<uint32_t>(inputNames.size()), static_cast<uint32_t>(outputNames.size()));
  142. fDevice->start(this);
  143. patchbayRefresh(false);
  144. callback(ENGINE_CALLBACK_ENGINE_STARTED, 0, pData->options.processMode, pData->options.transportMode, 0.0f, getCurrentDriverName());
  145. return true;
  146. }
  147. bool close() override
  148. {
  149. carla_debug("CarlaEngineJuce::close()");
  150. bool hasError = false;
  151. // stop stream first
  152. if (fDevice != nullptr && fDevice->isPlaying())
  153. fDevice->stop();
  154. // clear engine data
  155. CarlaEngine::close();
  156. pData->graph.destroy();
  157. for (LinkedList<MidiInPort>::Itenerator it = fMidiIns.begin(); it.valid(); it.next())
  158. {
  159. MidiInPort& inPort(it.getValue());
  160. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  161. inPort.port->stop();
  162. delete inPort.port;
  163. }
  164. fMidiIns.clear();
  165. fMidiInEvents.clear();
  166. fMidiOutMutex.lock();
  167. for (LinkedList<MidiOutPort>::Itenerator it = fMidiOuts.begin(); it.valid(); it.next())
  168. {
  169. MidiOutPort& outPort(it.getValue());
  170. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  171. outPort.port->stopBackgroundThread();
  172. delete outPort.port;
  173. }
  174. fMidiOuts.clear();
  175. fMidiOutMutex.unlock();
  176. // close stream
  177. if (fDevice != nullptr)
  178. {
  179. if (fDevice->isOpen())
  180. fDevice->close();
  181. fDevice = nullptr;
  182. }
  183. return !hasError;
  184. }
  185. bool isRunning() const noexcept override
  186. {
  187. return fDevice != nullptr && fDevice->isOpen();
  188. }
  189. bool isOffline() const noexcept override
  190. {
  191. return false;
  192. }
  193. EngineType getType() const noexcept override
  194. {
  195. return kEngineTypeJuce;
  196. }
  197. const char* getCurrentDriverName() const noexcept override
  198. {
  199. return fDeviceType->getTypeName().toRawUTF8();
  200. }
  201. // -------------------------------------------------------------------
  202. // Patchbay
  203. template<class Graph>
  204. void refreshGraphPorts(Graph* const graph)
  205. {
  206. char strBuf[STR_MAX+1];
  207. strBuf[STR_MAX] = '\0';
  208. // ---------------------------------------------------------------
  209. // clear last ports
  210. graph->clearPorts();
  211. // ---------------------------------------------------------------
  212. // fill in new ones
  213. // Audio In
  214. {
  215. StringArray inputNames(fDevice->getInputChannelNames());
  216. for (int i=0, count=inputNames.size(); i<count; ++i)
  217. {
  218. PortNameToId portNameToId;
  219. portNameToId.setData(RACK_GRAPH_GROUP_AUDIO_IN, uint(i+1), inputNames[i].toRawUTF8(), "");
  220. graph->audioPorts.ins.append(portNameToId);
  221. }
  222. }
  223. // Audio Out
  224. {
  225. StringArray outputNames(fDevice->getOutputChannelNames());
  226. for (int i=0, count=outputNames.size(); i<count; ++i)
  227. {
  228. PortNameToId portNameToId;
  229. portNameToId.setData(RACK_GRAPH_GROUP_AUDIO_OUT, uint(i+1), outputNames[i].toRawUTF8(), "");
  230. }
  231. }
  232. // MIDI In
  233. {
  234. StringArray midiIns(MidiInput::getDevices());
  235. for (int i=0, count=midiIns.size(); i<count; ++i)
  236. {
  237. PortNameToId portNameToId;
  238. portNameToId.setData(RACK_GRAPH_GROUP_MIDI_IN, uint(i+1), midiIns[i].toRawUTF8(), "");
  239. graph->midiPorts.ins.append(portNameToId);
  240. }
  241. }
  242. // MIDI Out
  243. {
  244. StringArray midiOuts(MidiOutput::getDevices());
  245. for (int i=0, count=midiOuts.size(); i<count; ++i)
  246. {
  247. PortNameToId portNameToId;
  248. portNameToId.setData(RACK_GRAPH_GROUP_MIDI_OUT, uint(i+1), midiOuts[i].toRawUTF8(), "");
  249. graph->midiPorts.outs.append(portNameToId);
  250. }
  251. }
  252. // ---------------------------------------------------------------
  253. // now refresh
  254. {
  255. String deviceName(fDevice->getName());
  256. if (deviceName.isNotEmpty())
  257. deviceName = deviceName.dropLastCharacters(deviceName.fromFirstOccurrenceOf(", ", true, false).length());
  258. graph->refresh(deviceName.toRawUTF8());
  259. }
  260. // ---------------------------------------------------------------
  261. // add midi connections
  262. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
  263. {
  264. const MidiInPort& inPort(it.getValue());
  265. const uint portId(graph->midiPorts.getPortId(true, inPort.name));
  266. CARLA_SAFE_ASSERT_CONTINUE(portId < graph->midiPorts.ins.count());
  267. ConnectionToId connectionToId;
  268. connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_MIDI_IN, portId, RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_MIDI_IN);
  269. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  270. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  271. graph->connections.list.append(connectionToId);
  272. }
  273. fMidiOutMutex.lock();
  274. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  275. {
  276. const MidiOutPort& outPort(it.getValue());
  277. const uint portId(graph->midiPorts.getPortId(false, outPort.name));
  278. CARLA_SAFE_ASSERT_CONTINUE(portId < graph->midiPorts.outs.count());
  279. ConnectionToId connectionToId;
  280. connectionToId.setData(++(graph->connections.lastId), RACK_GRAPH_GROUP_CARLA, RACK_GRAPH_CARLA_PORT_MIDI_OUT, RACK_GRAPH_GROUP_MIDI_OUT, portId);
  281. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  282. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  283. graph->connections.list.append(connectionToId);
  284. }
  285. fMidiOutMutex.unlock();
  286. }
  287. bool patchbayRefresh(const bool /*external*/) override
  288. {
  289. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  290. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  291. {
  292. RackGraph* const graph(pData->graph.getRackGraph());
  293. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  294. refreshGraphPorts<RackGraph>(graph);
  295. }
  296. else
  297. {
  298. PatchbayGraph* const graph(pData->graph.getPatchbayGraph());
  299. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  300. refreshGraphPorts<PatchbayGraph>(graph);
  301. }
  302. return true;
  303. }
  304. // -------------------------------------------------------------------
  305. protected:
  306. void audioDeviceIOCallback(const float** inputChannelData, int numInputChannels, float** outputChannelData, int numOutputChannels, int numSamples) override
  307. {
  308. const PendingRtEventsRunner prt(this);
  309. // assert juce buffers
  310. CARLA_SAFE_ASSERT_RETURN(numInputChannels >= 0,);
  311. CARLA_SAFE_ASSERT_RETURN(numOutputChannels > 0,);
  312. CARLA_SAFE_ASSERT_RETURN(outputChannelData != nullptr,);
  313. CARLA_SAFE_ASSERT_RETURN(numSamples == static_cast<int>(pData->bufferSize),);
  314. const uint32_t nframes(static_cast<uint32_t>(numSamples));
  315. // initialize juce output
  316. for (int i=0; i < numOutputChannels; ++i)
  317. FloatVectorOperations::clear(outputChannelData[i], numSamples);
  318. // initialize events
  319. carla_zeroStruct<EngineEvent>(pData->events.in, kMaxEngineEventInternalCount);
  320. carla_zeroStruct<EngineEvent>(pData->events.out, kMaxEngineEventInternalCount);
  321. if (fMidiInEvents.mutex.tryLock())
  322. {
  323. uint32_t engineEventIndex = 0;
  324. fMidiInEvents.splice();
  325. for (LinkedList<RtMidiEvent>::Itenerator it = fMidiInEvents.data.begin(); it.valid(); it.next())
  326. {
  327. const RtMidiEvent& midiEvent(it.getValue());
  328. EngineEvent& engineEvent(pData->events.in[engineEventIndex++]);
  329. if (midiEvent.time < pData->timeInfo.frame)
  330. {
  331. engineEvent.time = 0;
  332. }
  333. else if (midiEvent.time >= pData->timeInfo.frame + nframes)
  334. {
  335. carla_stderr("MIDI Event in the future!, %i vs %i", engineEvent.time, pData->timeInfo.frame);
  336. engineEvent.time = static_cast<uint32_t>(pData->timeInfo.frame) + nframes - 1;
  337. }
  338. else
  339. engineEvent.time = static_cast<uint32_t>(midiEvent.time - pData->timeInfo.frame);
  340. engineEvent.fillFromMidiData(midiEvent.size, midiEvent.data);
  341. if (engineEventIndex >= kMaxEngineEventInternalCount)
  342. break;
  343. }
  344. fMidiInEvents.data.clear();
  345. fMidiInEvents.mutex.unlock();
  346. }
  347. pData->graph.process(pData, inputChannelData, outputChannelData, static_cast<uint32_t>(numSamples));
  348. fMidiOutMutex.lock();
  349. if (fMidiOuts.count() > 0)
  350. {
  351. uint8_t size = 0;
  352. uint8_t data[3] = { 0, 0, 0 };
  353. const uint8_t* dataPtr = data;
  354. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  355. {
  356. const EngineEvent& engineEvent(pData->events.out[i]);
  357. if (engineEvent.type == kEngineEventTypeNull)
  358. break;
  359. else if (engineEvent.type == kEngineEventTypeControl)
  360. {
  361. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  362. ctrlEvent.convertToMidiData(engineEvent.channel, size, data);
  363. dataPtr = data;
  364. }
  365. else if (engineEvent.type == kEngineEventTypeMidi)
  366. {
  367. const EngineMidiEvent& midiEvent(engineEvent.midi);
  368. size = midiEvent.size;
  369. if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
  370. dataPtr = midiEvent.dataExt;
  371. else
  372. dataPtr = midiEvent.data;
  373. }
  374. else
  375. {
  376. continue;
  377. }
  378. if (size > 0)
  379. {
  380. MidiMessage message(static_cast<const void*>(dataPtr), static_cast<int>(size), static_cast<double>(engineEvent.time)/nframes);
  381. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  382. {
  383. MidiOutPort& outPort(it.getValue());
  384. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  385. outPort.port->sendMessageNow(message);
  386. }
  387. }
  388. }
  389. }
  390. fMidiOutMutex.unlock();
  391. }
  392. void audioDeviceAboutToStart(AudioIODevice* /*device*/) override
  393. {
  394. }
  395. void audioDeviceStopped() override
  396. {
  397. }
  398. void audioDeviceError(const String& errorMessage) override
  399. {
  400. callback(ENGINE_CALLBACK_ERROR, 0, 0, 0, 0.0f, errorMessage.toRawUTF8());
  401. }
  402. // -------------------------------------------------------------------
  403. void handleIncomingMidiMessage(MidiInput* /*source*/, const MidiMessage& message) override
  404. {
  405. const int messageSize(message.getRawDataSize());
  406. if (messageSize <= 0 || messageSize > EngineMidiEvent::kDataSize)
  407. return;
  408. const uint8_t* const messageData(message.getRawData());
  409. RtMidiEvent midiEvent;
  410. midiEvent.time = 0; // TODO
  411. midiEvent.size = static_cast<uint8_t>(messageSize);
  412. int i=0;
  413. for (; i < messageSize; ++i)
  414. midiEvent.data[i] = messageData[i];
  415. for (; i < EngineMidiEvent::kDataSize; ++i)
  416. midiEvent.data[i] = 0;
  417. fMidiInEvents.append(midiEvent);
  418. }
  419. // -------------------------------------------------------------------
  420. bool connectRackMidiInPort(const char* const portName) override
  421. {
  422. CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
  423. carla_debug("CarlaEngineJuce::connectRackMidiInPort(\"%s\")", portName);
  424. RackGraph* const graph(pData->graph.getRackGraph());
  425. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  426. CARLA_SAFE_ASSERT_RETURN(graph->midiPorts.ins.count() > 0, false);
  427. StringArray midiIns(MidiInput::getDevices());
  428. if (! midiIns.contains(portName))
  429. return false;
  430. MidiInput* const juceMidiIn(MidiInput::openDevice(midiIns.indexOf(portName), this));
  431. juceMidiIn->start();
  432. MidiInPort midiPort;
  433. midiPort.port = juceMidiIn;
  434. std::strncpy(midiPort.name, portName, STR_MAX);
  435. midiPort.name[STR_MAX] = '\0';
  436. fMidiIns.append(midiPort);
  437. return true;
  438. }
  439. bool connectRackMidiOutPort(const char* const portName) override
  440. {
  441. CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
  442. carla_debug("CarlaEngineJuce::connectRackMidiOutPort(\"%s\")", portName);
  443. RackGraph* const graph(pData->graph.getRackGraph());
  444. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  445. CARLA_SAFE_ASSERT_RETURN(graph->midiPorts.ins.count() > 0, false);
  446. StringArray midiOuts(MidiOutput::getDevices());
  447. if (! midiOuts.contains(portName))
  448. return false;
  449. MidiOutput* const juceMidiOut(MidiOutput::openDevice(midiOuts.indexOf(portName)));
  450. juceMidiOut->startBackgroundThread();
  451. MidiOutPort midiPort;
  452. midiPort.port = juceMidiOut;
  453. std::strncpy(midiPort.name, portName, STR_MAX);
  454. midiPort.name[STR_MAX] = '\0';
  455. const CarlaMutexLocker cml(fMidiOutMutex);
  456. fMidiOuts.append(midiPort);
  457. return true;
  458. }
  459. bool disconnectRackMidiInPort(const char* const portName) override
  460. {
  461. CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
  462. carla_debug("CarlaEngineRtAudio::disconnectRackMidiInPort(\"%s\")", portName);
  463. RackGraph* const graph(pData->graph.getRackGraph());
  464. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  465. CARLA_SAFE_ASSERT_RETURN(graph->midiPorts.ins.count() > 0, false);
  466. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
  467. {
  468. MidiInPort& inPort(it.getValue());
  469. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  470. if (std::strcmp(inPort.name, portName) != 0)
  471. continue;
  472. inPort.port->stop();
  473. delete inPort.port;
  474. fMidiIns.remove(it);
  475. return true;
  476. }
  477. return false;
  478. }
  479. bool disconnectRackMidiOutPort(const char* const portName) override
  480. {
  481. CARLA_SAFE_ASSERT_RETURN(portName != nullptr && portName[0] != '\0', false);
  482. carla_debug("CarlaEngineRtAudio::disconnectRackMidiOutPort(\"%s\")", portName);
  483. RackGraph* const graph(pData->graph.getRackGraph());
  484. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  485. CARLA_SAFE_ASSERT_RETURN(graph->midiPorts.outs.count() > 0, false);
  486. const CarlaMutexLocker cml(fMidiOutMutex);
  487. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  488. {
  489. MidiOutPort& outPort(it.getValue());
  490. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  491. if (std::strcmp(outPort.name, portName) != 0)
  492. continue;
  493. outPort.port->stopBackgroundThread();
  494. delete outPort.port;
  495. fMidiOuts.remove(it);
  496. return true;
  497. }
  498. return false;
  499. }
  500. // -------------------------------------
  501. private:
  502. ScopedPointer<AudioIODevice> fDevice;
  503. AudioIODeviceType* const fDeviceType;
  504. struct MidiInPort {
  505. MidiInput* port;
  506. char name[STR_MAX+1];
  507. };
  508. struct MidiOutPort {
  509. MidiOutput* port;
  510. char name[STR_MAX+1];
  511. };
  512. struct RtMidiEvent {
  513. uint64_t time; // needs to compare to internal time
  514. uint8_t size;
  515. uint8_t data[EngineMidiEvent::kDataSize];
  516. };
  517. struct RtMidiEvents {
  518. CarlaMutex mutex;
  519. RtLinkedList<RtMidiEvent>::Pool dataPool;
  520. RtLinkedList<RtMidiEvent> data;
  521. RtLinkedList<RtMidiEvent> dataPending;
  522. RtMidiEvents()
  523. : mutex(),
  524. dataPool(512, 512),
  525. data(dataPool),
  526. dataPending(dataPool) {}
  527. ~RtMidiEvents()
  528. {
  529. clear();
  530. }
  531. void append(const RtMidiEvent& event)
  532. {
  533. mutex.lock();
  534. dataPending.append(event);
  535. mutex.unlock();
  536. }
  537. void clear()
  538. {
  539. mutex.lock();
  540. data.clear();
  541. dataPending.clear();
  542. mutex.unlock();
  543. }
  544. void splice()
  545. {
  546. if (dataPending.count() > 0)
  547. dataPending.moveTo(data, true /* append */);
  548. }
  549. };
  550. LinkedList<MidiInPort> fMidiIns;
  551. RtMidiEvents fMidiInEvents;
  552. LinkedList<MidiOutPort> fMidiOuts;
  553. CarlaMutex fMidiOutMutex;
  554. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJuce)
  555. };
  556. // -----------------------------------------
  557. CarlaEngine* CarlaEngine::newJuce(const AudioApi api)
  558. {
  559. initJuceDevicesIfNeeded();
  560. String juceApi;
  561. switch (api)
  562. {
  563. case AUDIO_API_NULL:
  564. case AUDIO_API_OSS:
  565. case AUDIO_API_PULSE:
  566. break;
  567. case AUDIO_API_JACK:
  568. juceApi = "JACK";
  569. break;
  570. case AUDIO_API_ALSA:
  571. juceApi = "ALSA";
  572. break;
  573. case AUDIO_API_CORE:
  574. juceApi = "CoreAudio";
  575. break;
  576. case AUDIO_API_ASIO:
  577. juceApi = "ASIO";
  578. break;
  579. case AUDIO_API_DS:
  580. juceApi = "DirectSound";
  581. break;
  582. }
  583. if (juceApi.isEmpty())
  584. return nullptr;
  585. AudioIODeviceType* deviceType = nullptr;
  586. for (int i=0, count=gDeviceTypes.size(); i < count; ++i)
  587. {
  588. deviceType = gDeviceTypes[i];
  589. if (deviceType == nullptr || deviceType->getTypeName() == juceApi)
  590. break;
  591. }
  592. if (deviceType == nullptr)
  593. return nullptr;
  594. deviceType->scanForDevices();
  595. return new CarlaEngineJuce(deviceType);
  596. }
  597. uint CarlaEngine::getJuceApiCount()
  598. {
  599. initJuceDevicesIfNeeded();
  600. return static_cast<uint>(gDeviceTypes.size());
  601. }
  602. const char* CarlaEngine::getJuceApiName(const uint uindex)
  603. {
  604. initJuceDevicesIfNeeded();
  605. const int index(static_cast<int>(uindex));
  606. CARLA_SAFE_ASSERT_RETURN(index < gDeviceTypes.size(), nullptr);
  607. AudioIODeviceType* const deviceType(gDeviceTypes[index]);
  608. CARLA_SAFE_ASSERT_RETURN(deviceType != nullptr, nullptr);
  609. return deviceType->getTypeName().toRawUTF8();
  610. }
  611. const char* const* CarlaEngine::getJuceApiDeviceNames(const uint uindex)
  612. {
  613. initJuceDevicesIfNeeded();
  614. const int index(static_cast<int>(uindex));
  615. CARLA_SAFE_ASSERT_RETURN(index < gDeviceTypes.size(), nullptr);
  616. AudioIODeviceType* const deviceType(gDeviceTypes[index]);
  617. CARLA_SAFE_ASSERT_RETURN(deviceType != nullptr, nullptr);
  618. deviceType->scanForDevices();
  619. StringArray juceDeviceNames(deviceType->getDeviceNames());
  620. const int juceDeviceNameCount(juceDeviceNames.size());
  621. if (juceDeviceNameCount <= 0)
  622. return nullptr;
  623. CarlaStringList devNames;
  624. for (int i=0; i < juceDeviceNameCount; ++i)
  625. devNames.append(juceDeviceNames[i].toRawUTF8());
  626. gDeviceNames = devNames.toCharStringListPtr();
  627. return gDeviceNames;
  628. }
  629. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const uint uindex, const char* const deviceName)
  630. {
  631. initJuceDevicesIfNeeded();
  632. const int index(static_cast<int>(uindex));
  633. CARLA_SAFE_ASSERT_RETURN(index < gDeviceTypes.size(), nullptr);
  634. AudioIODeviceType* const deviceType(gDeviceTypes[index]);
  635. CARLA_SAFE_ASSERT_RETURN(deviceType != nullptr, nullptr);
  636. deviceType->scanForDevices();
  637. ScopedPointer<AudioIODevice> device(deviceType->createDevice(deviceName, deviceName));
  638. if (device == nullptr)
  639. return nullptr;
  640. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  641. static uint32_t dummyBufferSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  642. static double dummySampleRates[14] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  643. // reset
  644. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE | ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE;
  645. // cleanup
  646. if (devInfo.bufferSizes != nullptr && devInfo.bufferSizes != dummyBufferSizes)
  647. {
  648. delete[] devInfo.bufferSizes;
  649. devInfo.bufferSizes = nullptr;
  650. }
  651. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  652. {
  653. delete[] devInfo.sampleRates;
  654. devInfo.sampleRates = nullptr;
  655. }
  656. if (device->hasControlPanel())
  657. devInfo.hints |= ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL;
  658. Array<int> juceBufferSizes = device->getAvailableBufferSizes();
  659. if (int bufferSizesCount = juceBufferSizes.size())
  660. {
  661. uint32_t* const bufferSizes(new uint32_t[bufferSizesCount+1]);
  662. for (int i=0; i < bufferSizesCount; ++i)
  663. bufferSizes[i] = static_cast<uint32_t>(juceBufferSizes[i]);
  664. bufferSizes[bufferSizesCount] = 0;
  665. devInfo.bufferSizes = bufferSizes;
  666. }
  667. else
  668. {
  669. devInfo.bufferSizes = dummyBufferSizes;
  670. }
  671. Array<double> juceSampleRates = device->getAvailableSampleRates();
  672. if (int sampleRatesCount = juceSampleRates.size())
  673. {
  674. double* const sampleRates(new double[sampleRatesCount+1]);
  675. for (int i=0; i < sampleRatesCount; ++i)
  676. sampleRates[i] = juceSampleRates[i];
  677. sampleRates[sampleRatesCount] = 0.0;
  678. devInfo.sampleRates = sampleRates;
  679. }
  680. else
  681. {
  682. devInfo.sampleRates = dummySampleRates;
  683. }
  684. return &devInfo;
  685. }
  686. // -----------------------------------------
  687. CARLA_BACKEND_END_NAMESPACE