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.

922 lines
28KB

  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. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  145. refreshExternalGraphPorts<PatchbayGraph>(pData->graph.getPatchbayGraph(), false);
  146. callback(ENGINE_CALLBACK_ENGINE_STARTED, 0, pData->options.processMode, pData->options.transportMode, 0.0f, getCurrentDriverName());
  147. return true;
  148. }
  149. bool close() override
  150. {
  151. carla_debug("CarlaEngineJuce::close()");
  152. bool hasError = false;
  153. // stop stream first
  154. if (fDevice != nullptr && fDevice->isPlaying())
  155. fDevice->stop();
  156. // clear engine data
  157. CarlaEngine::close();
  158. pData->graph.destroy();
  159. for (LinkedList<MidiInPort>::Itenerator it = fMidiIns.begin(); it.valid(); it.next())
  160. {
  161. MidiInPort& inPort(it.getValue());
  162. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  163. inPort.port->stop();
  164. delete inPort.port;
  165. }
  166. fMidiIns.clear();
  167. fMidiInEvents.clear();
  168. fMidiOutMutex.lock();
  169. for (LinkedList<MidiOutPort>::Itenerator it = fMidiOuts.begin(); it.valid(); it.next())
  170. {
  171. MidiOutPort& outPort(it.getValue());
  172. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  173. outPort.port->stopBackgroundThread();
  174. delete outPort.port;
  175. }
  176. fMidiOuts.clear();
  177. fMidiOutMutex.unlock();
  178. // close stream
  179. if (fDevice != nullptr)
  180. {
  181. if (fDevice->isOpen())
  182. fDevice->close();
  183. fDevice = nullptr;
  184. }
  185. return !hasError;
  186. }
  187. bool isRunning() const noexcept override
  188. {
  189. return fDevice != nullptr && fDevice->isOpen();
  190. }
  191. bool isOffline() const noexcept override
  192. {
  193. return false;
  194. }
  195. EngineType getType() const noexcept override
  196. {
  197. return kEngineTypeJuce;
  198. }
  199. const char* getCurrentDriverName() const noexcept override
  200. {
  201. return fDeviceType->getTypeName().toRawUTF8();
  202. }
  203. // -------------------------------------------------------------------
  204. // Patchbay
  205. template<class Graph>
  206. bool refreshExternalGraphPorts(Graph* const graph, const bool sendCallback)
  207. {
  208. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  209. char strBuf[STR_MAX+1];
  210. strBuf[STR_MAX] = '\0';
  211. ExternalGraph& extGraph(graph->extGraph);
  212. // ---------------------------------------------------------------
  213. // clear last ports
  214. extGraph.clear();
  215. // ---------------------------------------------------------------
  216. // fill in new ones
  217. // Audio In
  218. {
  219. StringArray inputNames(fDevice->getInputChannelNames());
  220. for (int i=0, count=inputNames.size(); i<count; ++i)
  221. {
  222. PortNameToId portNameToId;
  223. portNameToId.setData(kExternalGraphGroupAudioIn, uint(i+1), inputNames[i].toRawUTF8(), "");
  224. extGraph.audioPorts.ins.append(portNameToId);
  225. }
  226. }
  227. // Audio Out
  228. {
  229. StringArray outputNames(fDevice->getOutputChannelNames());
  230. for (int i=0, count=outputNames.size(); i<count; ++i)
  231. {
  232. PortNameToId portNameToId;
  233. portNameToId.setData(kExternalGraphGroupAudioOut, uint(i+1), outputNames[i].toRawUTF8(), "");
  234. }
  235. }
  236. // MIDI In
  237. {
  238. StringArray midiIns(MidiInput::getDevices());
  239. for (int i=0, count=midiIns.size(); i<count; ++i)
  240. {
  241. PortNameToId portNameToId;
  242. portNameToId.setData(kExternalGraphGroupMidiIn, uint(i+1), midiIns[i].toRawUTF8(), "");
  243. extGraph.midiPorts.ins.append(portNameToId);
  244. }
  245. }
  246. // MIDI Out
  247. {
  248. StringArray midiOuts(MidiOutput::getDevices());
  249. for (int i=0, count=midiOuts.size(); i<count; ++i)
  250. {
  251. PortNameToId portNameToId;
  252. portNameToId.setData(kExternalGraphGroupMidiOut, uint(i+1), midiOuts[i].toRawUTF8(), "");
  253. extGraph.midiPorts.outs.append(portNameToId);
  254. }
  255. }
  256. // ---------------------------------------------------------------
  257. // now refresh
  258. if (sendCallback)
  259. {
  260. String deviceName(fDevice->getName());
  261. if (deviceName.isNotEmpty())
  262. deviceName = deviceName.dropLastCharacters(deviceName.fromFirstOccurrenceOf(", ", true, false).length());
  263. graph->refresh(deviceName.toRawUTF8());
  264. }
  265. // ---------------------------------------------------------------
  266. // add midi connections
  267. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
  268. {
  269. const MidiInPort& inPort(it.getValue());
  270. const uint portId(extGraph.midiPorts.getPortId(true, inPort.name));
  271. CARLA_SAFE_ASSERT_CONTINUE(portId < extGraph.midiPorts.ins.count());
  272. ConnectionToId connectionToId;
  273. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupMidiIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiIn);
  274. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  275. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  276. extGraph.connections.list.append(connectionToId);
  277. }
  278. fMidiOutMutex.lock();
  279. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  280. {
  281. const MidiOutPort& outPort(it.getValue());
  282. const uint portId(extGraph.midiPorts.getPortId(false, outPort.name));
  283. CARLA_SAFE_ASSERT_CONTINUE(portId < extGraph.midiPorts.outs.count());
  284. ConnectionToId connectionToId;
  285. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiOut, kExternalGraphGroupMidiOut, portId);
  286. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  287. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  288. extGraph.connections.list.append(connectionToId);
  289. }
  290. fMidiOutMutex.unlock();
  291. }
  292. bool patchbayRefresh(const bool external) override
  293. {
  294. CARLA_SAFE_ASSERT_RETURN(pData->graph.isReady(), false);
  295. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  296. {
  297. return refreshExternalGraphPorts<RackGraph>(pData->graph.getRackGraph(), true);
  298. }
  299. else
  300. {
  301. pData->graph.setUsingExternal(external);
  302. if (external)
  303. return refreshExternalGraphPorts<PatchbayGraph>(pData->graph.getPatchbayGraph(), true);
  304. else
  305. return CarlaEngine::patchbayRefresh(false);
  306. }
  307. return false;
  308. }
  309. // -------------------------------------------------------------------
  310. protected:
  311. void audioDeviceIOCallback(const float** inputChannelData, int numInputChannels, float** outputChannelData, int numOutputChannels, int numSamples) override
  312. {
  313. const PendingRtEventsRunner prt(this);
  314. // assert juce buffers
  315. CARLA_SAFE_ASSERT_RETURN(numInputChannels >= 0,);
  316. CARLA_SAFE_ASSERT_RETURN(numOutputChannels > 0,);
  317. CARLA_SAFE_ASSERT_RETURN(outputChannelData != nullptr,);
  318. CARLA_SAFE_ASSERT_RETURN(numSamples == static_cast<int>(pData->bufferSize),);
  319. const uint32_t nframes(static_cast<uint32_t>(numSamples));
  320. // initialize juce output
  321. for (int i=0; i < numOutputChannels; ++i)
  322. FloatVectorOperations::clear(outputChannelData[i], numSamples);
  323. // initialize events
  324. carla_zeroStruct<EngineEvent>(pData->events.in, kMaxEngineEventInternalCount);
  325. carla_zeroStruct<EngineEvent>(pData->events.out, kMaxEngineEventInternalCount);
  326. if (fMidiInEvents.mutex.tryLock())
  327. {
  328. uint32_t engineEventIndex = 0;
  329. fMidiInEvents.splice();
  330. for (LinkedList<RtMidiEvent>::Itenerator it = fMidiInEvents.data.begin(); it.valid(); it.next())
  331. {
  332. const RtMidiEvent& midiEvent(it.getValue());
  333. EngineEvent& engineEvent(pData->events.in[engineEventIndex++]);
  334. if (midiEvent.time < pData->timeInfo.frame)
  335. {
  336. engineEvent.time = 0;
  337. }
  338. else if (midiEvent.time >= pData->timeInfo.frame + nframes)
  339. {
  340. carla_stderr("MIDI Event in the future!, %i vs %i", engineEvent.time, pData->timeInfo.frame);
  341. engineEvent.time = static_cast<uint32_t>(pData->timeInfo.frame) + nframes - 1;
  342. }
  343. else
  344. engineEvent.time = static_cast<uint32_t>(midiEvent.time - pData->timeInfo.frame);
  345. engineEvent.fillFromMidiData(midiEvent.size, midiEvent.data);
  346. if (engineEventIndex >= kMaxEngineEventInternalCount)
  347. break;
  348. }
  349. fMidiInEvents.data.clear();
  350. fMidiInEvents.mutex.unlock();
  351. }
  352. pData->graph.process(pData, inputChannelData, outputChannelData, static_cast<uint32_t>(numSamples));
  353. fMidiOutMutex.lock();
  354. if (fMidiOuts.count() > 0)
  355. {
  356. uint8_t size = 0;
  357. uint8_t data[3] = { 0, 0, 0 };
  358. const uint8_t* dataPtr = data;
  359. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  360. {
  361. const EngineEvent& engineEvent(pData->events.out[i]);
  362. if (engineEvent.type == kEngineEventTypeNull)
  363. break;
  364. else if (engineEvent.type == kEngineEventTypeControl)
  365. {
  366. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  367. ctrlEvent.convertToMidiData(engineEvent.channel, size, data);
  368. dataPtr = data;
  369. }
  370. else if (engineEvent.type == kEngineEventTypeMidi)
  371. {
  372. const EngineMidiEvent& midiEvent(engineEvent.midi);
  373. size = midiEvent.size;
  374. if (size > EngineMidiEvent::kDataSize && midiEvent.dataExt != nullptr)
  375. dataPtr = midiEvent.dataExt;
  376. else
  377. dataPtr = midiEvent.data;
  378. }
  379. else
  380. {
  381. continue;
  382. }
  383. if (size > 0)
  384. {
  385. MidiMessage message(static_cast<const void*>(dataPtr), static_cast<int>(size), static_cast<double>(engineEvent.time)/nframes);
  386. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  387. {
  388. MidiOutPort& outPort(it.getValue());
  389. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  390. outPort.port->sendMessageNow(message);
  391. }
  392. }
  393. }
  394. }
  395. fMidiOutMutex.unlock();
  396. }
  397. void audioDeviceAboutToStart(AudioIODevice* /*device*/) override
  398. {
  399. }
  400. void audioDeviceStopped() override
  401. {
  402. }
  403. void audioDeviceError(const String& errorMessage) override
  404. {
  405. callback(ENGINE_CALLBACK_ERROR, 0, 0, 0, 0.0f, errorMessage.toRawUTF8());
  406. }
  407. // -------------------------------------------------------------------
  408. void handleIncomingMidiMessage(MidiInput* /*source*/, const MidiMessage& message) override
  409. {
  410. const int messageSize(message.getRawDataSize());
  411. if (messageSize <= 0 || messageSize > EngineMidiEvent::kDataSize)
  412. return;
  413. const uint8_t* const messageData(message.getRawData());
  414. RtMidiEvent midiEvent;
  415. midiEvent.time = 0; // TODO
  416. midiEvent.size = static_cast<uint8_t>(messageSize);
  417. int i=0;
  418. for (; i < messageSize; ++i)
  419. midiEvent.data[i] = messageData[i];
  420. for (; i < EngineMidiEvent::kDataSize; ++i)
  421. midiEvent.data[i] = 0;
  422. fMidiInEvents.append(midiEvent);
  423. }
  424. // -------------------------------------------------------------------
  425. bool connectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName) override
  426. {
  427. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  428. carla_stdout("CarlaEngineJuce::connectExternalGraphPort(%u, %u, \"%s\")", connectionType, portId, portName);
  429. switch (connectionType)
  430. {
  431. case kExternalGraphConnectionAudioIn1:
  432. case kExternalGraphConnectionAudioIn2:
  433. case kExternalGraphConnectionAudioOut1:
  434. case kExternalGraphConnectionAudioOut2:
  435. return CarlaEngine::connectExternalGraphPort(connectionType, portId, portName);
  436. case kExternalGraphConnectionMidiInput: {
  437. StringArray midiIns(MidiInput::getDevices());
  438. if (! midiIns.contains(portName))
  439. return false;
  440. MidiInput* const juceMidiIn(MidiInput::openDevice(midiIns.indexOf(portName), this));
  441. juceMidiIn->start();
  442. MidiInPort midiPort;
  443. midiPort.port = juceMidiIn;
  444. std::strncpy(midiPort.name, portName, STR_MAX);
  445. midiPort.name[STR_MAX] = '\0';
  446. fMidiIns.append(midiPort);
  447. return true;
  448. } break;
  449. case kExternalGraphConnectionMidiOutput: {
  450. StringArray midiOuts(MidiOutput::getDevices());
  451. if (! midiOuts.contains(portName))
  452. return false;
  453. MidiOutput* const juceMidiOut(MidiOutput::openDevice(midiOuts.indexOf(portName)));
  454. juceMidiOut->startBackgroundThread();
  455. MidiOutPort midiPort;
  456. midiPort.port = juceMidiOut;
  457. std::strncpy(midiPort.name, portName, STR_MAX);
  458. midiPort.name[STR_MAX] = '\0';
  459. const CarlaMutexLocker cml(fMidiOutMutex);
  460. fMidiOuts.append(midiPort);
  461. return true;
  462. } break;
  463. }
  464. }
  465. bool disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName) override
  466. {
  467. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  468. carla_debug("CarlaEngineJuce::disconnectExternalGraphPort(%u, %u, \"%s\")", connectionType, portId, portName);
  469. switch (connectionType)
  470. {
  471. case kExternalGraphConnectionAudioIn1:
  472. case kExternalGraphConnectionAudioIn2:
  473. case kExternalGraphConnectionAudioOut1:
  474. case kExternalGraphConnectionAudioOut2:
  475. return CarlaEngine::disconnectExternalGraphPort(connectionType, portId, portName);
  476. case kExternalGraphConnectionMidiInput:
  477. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
  478. {
  479. MidiInPort& inPort(it.getValue());
  480. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  481. if (std::strcmp(inPort.name, portName) != 0)
  482. continue;
  483. inPort.port->stop();
  484. delete inPort.port;
  485. fMidiIns.remove(it);
  486. return true;
  487. }
  488. break;
  489. case kExternalGraphConnectionMidiOutput: {
  490. const CarlaMutexLocker cml(fMidiOutMutex);
  491. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  492. {
  493. MidiOutPort& outPort(it.getValue());
  494. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  495. if (std::strcmp(outPort.name, portName) != 0)
  496. continue;
  497. outPort.port->stopBackgroundThread();
  498. delete outPort.port;
  499. fMidiOuts.remove(it);
  500. return true;
  501. }
  502. } break;
  503. }
  504. return false;
  505. }
  506. // -------------------------------------
  507. private:
  508. ScopedPointer<AudioIODevice> fDevice;
  509. AudioIODeviceType* const fDeviceType;
  510. struct MidiInPort {
  511. MidiInput* port;
  512. char name[STR_MAX+1];
  513. };
  514. struct MidiOutPort {
  515. MidiOutput* port;
  516. char name[STR_MAX+1];
  517. };
  518. struct RtMidiEvent {
  519. uint64_t time; // needs to compare to internal time
  520. uint8_t size;
  521. uint8_t data[EngineMidiEvent::kDataSize];
  522. };
  523. struct RtMidiEvents {
  524. CarlaMutex mutex;
  525. RtLinkedList<RtMidiEvent>::Pool dataPool;
  526. RtLinkedList<RtMidiEvent> data;
  527. RtLinkedList<RtMidiEvent> dataPending;
  528. RtMidiEvents()
  529. : mutex(),
  530. dataPool(512, 512),
  531. data(dataPool),
  532. dataPending(dataPool) {}
  533. ~RtMidiEvents()
  534. {
  535. clear();
  536. }
  537. void append(const RtMidiEvent& event)
  538. {
  539. mutex.lock();
  540. dataPending.append(event);
  541. mutex.unlock();
  542. }
  543. void clear()
  544. {
  545. mutex.lock();
  546. data.clear();
  547. dataPending.clear();
  548. mutex.unlock();
  549. }
  550. void splice()
  551. {
  552. if (dataPending.count() > 0)
  553. dataPending.moveTo(data, true /* append */);
  554. }
  555. };
  556. LinkedList<MidiInPort> fMidiIns;
  557. RtMidiEvents fMidiInEvents;
  558. LinkedList<MidiOutPort> fMidiOuts;
  559. CarlaMutex fMidiOutMutex;
  560. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJuce)
  561. };
  562. // -----------------------------------------
  563. CarlaEngine* CarlaEngine::newJuce(const AudioApi api)
  564. {
  565. initJuceDevicesIfNeeded();
  566. String juceApi;
  567. switch (api)
  568. {
  569. case AUDIO_API_NULL:
  570. case AUDIO_API_OSS:
  571. case AUDIO_API_PULSE:
  572. break;
  573. case AUDIO_API_JACK:
  574. juceApi = "JACK";
  575. break;
  576. case AUDIO_API_ALSA:
  577. juceApi = "ALSA";
  578. break;
  579. case AUDIO_API_CORE:
  580. juceApi = "CoreAudio";
  581. break;
  582. case AUDIO_API_ASIO:
  583. juceApi = "ASIO";
  584. break;
  585. case AUDIO_API_DS:
  586. juceApi = "DirectSound";
  587. break;
  588. }
  589. if (juceApi.isEmpty())
  590. return nullptr;
  591. AudioIODeviceType* deviceType = nullptr;
  592. for (int i=0, count=gDeviceTypes.size(); i < count; ++i)
  593. {
  594. deviceType = gDeviceTypes[i];
  595. if (deviceType == nullptr || deviceType->getTypeName() == juceApi)
  596. break;
  597. }
  598. if (deviceType == nullptr)
  599. return nullptr;
  600. deviceType->scanForDevices();
  601. return new CarlaEngineJuce(deviceType);
  602. }
  603. uint CarlaEngine::getJuceApiCount()
  604. {
  605. initJuceDevicesIfNeeded();
  606. return static_cast<uint>(gDeviceTypes.size());
  607. }
  608. const char* CarlaEngine::getJuceApiName(const uint uindex)
  609. {
  610. initJuceDevicesIfNeeded();
  611. const int index(static_cast<int>(uindex));
  612. CARLA_SAFE_ASSERT_RETURN(index < gDeviceTypes.size(), nullptr);
  613. AudioIODeviceType* const deviceType(gDeviceTypes[index]);
  614. CARLA_SAFE_ASSERT_RETURN(deviceType != nullptr, nullptr);
  615. return deviceType->getTypeName().toRawUTF8();
  616. }
  617. const char* const* CarlaEngine::getJuceApiDeviceNames(const uint uindex)
  618. {
  619. initJuceDevicesIfNeeded();
  620. const int index(static_cast<int>(uindex));
  621. CARLA_SAFE_ASSERT_RETURN(index < gDeviceTypes.size(), nullptr);
  622. AudioIODeviceType* const deviceType(gDeviceTypes[index]);
  623. CARLA_SAFE_ASSERT_RETURN(deviceType != nullptr, nullptr);
  624. deviceType->scanForDevices();
  625. StringArray juceDeviceNames(deviceType->getDeviceNames());
  626. const int juceDeviceNameCount(juceDeviceNames.size());
  627. if (juceDeviceNameCount <= 0)
  628. return nullptr;
  629. CarlaStringList devNames;
  630. for (int i=0; i < juceDeviceNameCount; ++i)
  631. devNames.append(juceDeviceNames[i].toRawUTF8());
  632. gDeviceNames = devNames.toCharStringListPtr();
  633. return gDeviceNames;
  634. }
  635. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const uint uindex, const char* const deviceName)
  636. {
  637. initJuceDevicesIfNeeded();
  638. const int index(static_cast<int>(uindex));
  639. CARLA_SAFE_ASSERT_RETURN(index < gDeviceTypes.size(), nullptr);
  640. AudioIODeviceType* const deviceType(gDeviceTypes[index]);
  641. CARLA_SAFE_ASSERT_RETURN(deviceType != nullptr, nullptr);
  642. deviceType->scanForDevices();
  643. ScopedPointer<AudioIODevice> device(deviceType->createDevice(deviceName, deviceName));
  644. if (device == nullptr)
  645. return nullptr;
  646. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  647. static uint32_t dummyBufferSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  648. static double dummySampleRates[14] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  649. // reset
  650. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE | ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE;
  651. // cleanup
  652. if (devInfo.bufferSizes != nullptr && devInfo.bufferSizes != dummyBufferSizes)
  653. {
  654. delete[] devInfo.bufferSizes;
  655. devInfo.bufferSizes = nullptr;
  656. }
  657. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  658. {
  659. delete[] devInfo.sampleRates;
  660. devInfo.sampleRates = nullptr;
  661. }
  662. if (device->hasControlPanel())
  663. devInfo.hints |= ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL;
  664. Array<int> juceBufferSizes = device->getAvailableBufferSizes();
  665. if (int bufferSizesCount = juceBufferSizes.size())
  666. {
  667. uint32_t* const bufferSizes(new uint32_t[bufferSizesCount+1]);
  668. for (int i=0; i < bufferSizesCount; ++i)
  669. bufferSizes[i] = static_cast<uint32_t>(juceBufferSizes[i]);
  670. bufferSizes[bufferSizesCount] = 0;
  671. devInfo.bufferSizes = bufferSizes;
  672. }
  673. else
  674. {
  675. devInfo.bufferSizes = dummyBufferSizes;
  676. }
  677. Array<double> juceSampleRates = device->getAvailableSampleRates();
  678. if (int sampleRatesCount = juceSampleRates.size())
  679. {
  680. double* const sampleRates(new double[sampleRatesCount+1]);
  681. for (int i=0; i < sampleRatesCount; ++i)
  682. sampleRates[i] = juceSampleRates[i];
  683. sampleRates[sampleRatesCount] = 0.0;
  684. devInfo.sampleRates = sampleRates;
  685. }
  686. else
  687. {
  688. devInfo.sampleRates = dummySampleRates;
  689. }
  690. return &devInfo;
  691. }
  692. // -----------------------------------------
  693. CARLA_BACKEND_END_NAMESPACE