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.

925 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. {
  73. carla_debug("CarlaEngineJuce::CarlaEngineJuce(%p)", devType);
  74. // just to make sure
  75. pData->options.transportMode = ENGINE_TRANSPORT_MODE_INTERNAL;
  76. }
  77. ~CarlaEngineJuce() override
  78. {
  79. carla_debug("CarlaEngineJuce::~CarlaEngineJuce()");
  80. }
  81. // -------------------------------------
  82. bool init(const char* const clientName) override
  83. {
  84. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  85. carla_debug("CarlaEngineJuce::init(\"%s\")", clientName);
  86. if (pData->options.processMode != ENGINE_PROCESS_MODE_CONTINUOUS_RACK && pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  87. {
  88. setLastError("Invalid process mode");
  89. return false;
  90. }
  91. String deviceName;
  92. if (pData->options.audioDevice != nullptr && pData->options.audioDevice[0] != '\0')
  93. {
  94. deviceName = pData->options.audioDevice;
  95. }
  96. else
  97. {
  98. const int defaultIndex(fDeviceType->getDefaultDeviceIndex(false));
  99. StringArray deviceNames(fDeviceType->getDeviceNames());
  100. if (defaultIndex >= 0 && defaultIndex < deviceNames.size())
  101. deviceName = deviceNames[defaultIndex];
  102. }
  103. if (deviceName.isEmpty())
  104. {
  105. setLastError("Audio device has not been selected yet and a default one is not available");
  106. return false;
  107. }
  108. fDevice = fDeviceType->createDevice(deviceName, deviceName);
  109. if (fDevice == nullptr)
  110. {
  111. setLastError("Failed to create device");
  112. return false;
  113. }
  114. StringArray inputNames(fDevice->getInputChannelNames());
  115. StringArray outputNames(fDevice->getOutputChannelNames());
  116. if (inputNames.size() < 0 || outputNames.size() <= 0)
  117. {
  118. setLastError("Selected device does not have any outputs");
  119. return false;
  120. }
  121. BigInteger inputChannels;
  122. inputChannels.setRange(0, inputNames.size(), true);
  123. BigInteger outputChannels;
  124. outputChannels.setRange(0, outputNames.size(), true);
  125. String error = fDevice->open(inputChannels, outputChannels, pData->options.audioSampleRate, static_cast<int>(pData->options.audioBufferSize));
  126. if (error.isNotEmpty())
  127. {
  128. setLastError(error.toUTF8());
  129. fDevice = nullptr;
  130. return false;
  131. }
  132. if (! pData->init(clientName))
  133. {
  134. close();
  135. setLastError("Failed to init internal data");
  136. return false;
  137. }
  138. pData->bufferSize = static_cast<uint32_t>(fDevice->getCurrentBufferSizeSamples());
  139. pData->sampleRate = fDevice->getCurrentSampleRate();
  140. pData->graph.create(static_cast<uint32_t>(inputNames.size()), static_cast<uint32_t>(outputNames.size()));
  141. fDevice->start(this);
  142. patchbayRefresh(false);
  143. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  144. refreshExternalGraphPorts<PatchbayGraph>(pData->graph.getPatchbayGraph(), false);
  145. callback(ENGINE_CALLBACK_ENGINE_STARTED, 0, pData->options.processMode, pData->options.transportMode, 0.0f, getCurrentDriverName());
  146. return true;
  147. }
  148. bool close() override
  149. {
  150. carla_debug("CarlaEngineJuce::close()");
  151. bool hasError = false;
  152. // stop stream first
  153. if (fDevice != nullptr && fDevice->isPlaying())
  154. fDevice->stop();
  155. // clear engine data
  156. CarlaEngine::close();
  157. pData->graph.destroy();
  158. for (LinkedList<MidiInPort>::Itenerator it = fMidiIns.begin2(); it.valid(); it.next())
  159. {
  160. MidiInPort& inPort(it.getValue());
  161. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  162. inPort.port->stop();
  163. delete inPort.port;
  164. }
  165. fMidiIns.clear();
  166. fMidiInEvents.clear();
  167. fMidiOutMutex.lock();
  168. for (LinkedList<MidiOutPort>::Itenerator it = fMidiOuts.begin2(); it.valid(); it.next())
  169. {
  170. MidiOutPort& outPort(it.getValue());
  171. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  172. outPort.port->stopBackgroundThread();
  173. delete outPort.port;
  174. }
  175. fMidiOuts.clear();
  176. fMidiOutMutex.unlock();
  177. // close stream
  178. if (fDevice != nullptr)
  179. {
  180. if (fDevice->isOpen())
  181. fDevice->close();
  182. fDevice = nullptr;
  183. }
  184. return !hasError;
  185. }
  186. bool isRunning() const noexcept override
  187. {
  188. return fDevice != nullptr && fDevice->isOpen();
  189. }
  190. bool isOffline() const noexcept override
  191. {
  192. return false;
  193. }
  194. EngineType getType() const noexcept override
  195. {
  196. return kEngineTypeJuce;
  197. }
  198. const char* getCurrentDriverName() const noexcept override
  199. {
  200. return fDeviceType->getTypeName().toRawUTF8();
  201. }
  202. // -------------------------------------------------------------------
  203. // Patchbay
  204. template<class Graph>
  205. bool refreshExternalGraphPorts(Graph* const graph, const bool sendCallback)
  206. {
  207. CARLA_SAFE_ASSERT_RETURN(graph != nullptr, false);
  208. char strBuf[STR_MAX+1];
  209. strBuf[STR_MAX] = '\0';
  210. ExternalGraph& extGraph(graph->extGraph);
  211. // ---------------------------------------------------------------
  212. // clear last ports
  213. extGraph.clear();
  214. // ---------------------------------------------------------------
  215. // fill in new ones
  216. // Audio In
  217. {
  218. StringArray inputNames(fDevice->getInputChannelNames());
  219. for (int i=0, count=inputNames.size(); i<count; ++i)
  220. {
  221. PortNameToId portNameToId;
  222. portNameToId.setData(kExternalGraphGroupAudioIn, uint(i+1), inputNames[i].toRawUTF8(), "");
  223. extGraph.audioPorts.ins.append(portNameToId);
  224. }
  225. }
  226. // Audio Out
  227. {
  228. StringArray outputNames(fDevice->getOutputChannelNames());
  229. for (int i=0, count=outputNames.size(); i<count; ++i)
  230. {
  231. PortNameToId portNameToId;
  232. portNameToId.setData(kExternalGraphGroupAudioOut, uint(i+1), outputNames[i].toRawUTF8(), "");
  233. }
  234. }
  235. // MIDI In
  236. {
  237. StringArray midiIns(MidiInput::getDevices());
  238. for (int i=0, count=midiIns.size(); i<count; ++i)
  239. {
  240. PortNameToId portNameToId;
  241. portNameToId.setData(kExternalGraphGroupMidiIn, uint(i+1), midiIns[i].toRawUTF8(), "");
  242. extGraph.midiPorts.ins.append(portNameToId);
  243. }
  244. }
  245. // MIDI Out
  246. {
  247. StringArray midiOuts(MidiOutput::getDevices());
  248. for (int i=0, count=midiOuts.size(); i<count; ++i)
  249. {
  250. PortNameToId portNameToId;
  251. portNameToId.setData(kExternalGraphGroupMidiOut, uint(i+1), midiOuts[i].toRawUTF8(), "");
  252. extGraph.midiPorts.outs.append(portNameToId);
  253. }
  254. }
  255. // ---------------------------------------------------------------
  256. // now refresh
  257. if (sendCallback)
  258. {
  259. String deviceName(fDevice->getName());
  260. if (deviceName.isNotEmpty())
  261. deviceName = deviceName.dropLastCharacters(deviceName.fromFirstOccurrenceOf(", ", true, false).length());
  262. graph->refresh(deviceName.toRawUTF8());
  263. }
  264. // ---------------------------------------------------------------
  265. // add midi connections
  266. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin2(); it.valid(); it.next())
  267. {
  268. const MidiInPort& inPort(it.getValue());
  269. const uint portId(extGraph.midiPorts.getPortId(true, inPort.name));
  270. CARLA_SAFE_ASSERT_CONTINUE(portId < extGraph.midiPorts.ins.count());
  271. ConnectionToId connectionToId;
  272. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupMidiIn, portId, kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiIn);
  273. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  274. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  275. extGraph.connections.list.append(connectionToId);
  276. }
  277. fMidiOutMutex.lock();
  278. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin2(); it.valid(); it.next())
  279. {
  280. const MidiOutPort& outPort(it.getValue());
  281. const uint portId(extGraph.midiPorts.getPortId(false, outPort.name));
  282. CARLA_SAFE_ASSERT_CONTINUE(portId < extGraph.midiPorts.outs.count());
  283. ConnectionToId connectionToId;
  284. connectionToId.setData(++(extGraph.connections.lastId), kExternalGraphGroupCarla, kExternalGraphCarlaPortMidiOut, kExternalGraphGroupMidiOut, portId);
  285. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i", connectionToId.groupA, connectionToId.portA, connectionToId.groupB, connectionToId.portB);
  286. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, connectionToId.id, 0, 0, 0.0f, strBuf);
  287. extGraph.connections.list.append(connectionToId);
  288. }
  289. fMidiOutMutex.unlock();
  290. return true;
  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_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  325. carla_zeroStructs(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.begin2(); 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, 0);
  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.begin2(); 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. return false;
  465. }
  466. bool disconnectExternalGraphPort(const uint connectionType, const uint portId, const char* const portName) override
  467. {
  468. CARLA_SAFE_ASSERT_RETURN(connectionType != 0 || (portName != nullptr && portName[0] != '\0'), false);
  469. carla_debug("CarlaEngineJuce::disconnectExternalGraphPort(%u, %u, \"%s\")", connectionType, portId, portName);
  470. switch (connectionType)
  471. {
  472. case kExternalGraphConnectionAudioIn1:
  473. case kExternalGraphConnectionAudioIn2:
  474. case kExternalGraphConnectionAudioOut1:
  475. case kExternalGraphConnectionAudioOut2:
  476. return CarlaEngine::disconnectExternalGraphPort(connectionType, portId, portName);
  477. case kExternalGraphConnectionMidiInput:
  478. for (LinkedList<MidiInPort>::Itenerator it=fMidiIns.begin2(); it.valid(); it.next())
  479. {
  480. MidiInPort& inPort(it.getValue());
  481. CARLA_SAFE_ASSERT_CONTINUE(inPort.port != nullptr);
  482. if (std::strcmp(inPort.name, portName) != 0)
  483. continue;
  484. inPort.port->stop();
  485. delete inPort.port;
  486. fMidiIns.remove(it);
  487. return true;
  488. }
  489. break;
  490. case kExternalGraphConnectionMidiOutput: {
  491. const CarlaMutexLocker cml(fMidiOutMutex);
  492. for (LinkedList<MidiOutPort>::Itenerator it=fMidiOuts.begin2(); it.valid(); it.next())
  493. {
  494. MidiOutPort& outPort(it.getValue());
  495. CARLA_SAFE_ASSERT_CONTINUE(outPort.port != nullptr);
  496. if (std::strcmp(outPort.name, portName) != 0)
  497. continue;
  498. outPort.port->stopBackgroundThread();
  499. delete outPort.port;
  500. fMidiOuts.remove(it);
  501. return true;
  502. }
  503. } break;
  504. }
  505. return false;
  506. }
  507. // -------------------------------------
  508. private:
  509. ScopedPointer<AudioIODevice> fDevice;
  510. AudioIODeviceType* const fDeviceType;
  511. struct MidiInPort {
  512. MidiInput* port;
  513. char name[STR_MAX+1];
  514. };
  515. struct MidiOutPort {
  516. MidiOutput* port;
  517. char name[STR_MAX+1];
  518. };
  519. struct RtMidiEvent {
  520. uint64_t time; // needs to compare to internal time
  521. uint8_t size;
  522. uint8_t data[EngineMidiEvent::kDataSize];
  523. };
  524. struct RtMidiEvents {
  525. CarlaMutex mutex;
  526. RtLinkedList<RtMidiEvent>::Pool dataPool;
  527. RtLinkedList<RtMidiEvent> data;
  528. RtLinkedList<RtMidiEvent> dataPending;
  529. RtMidiEvents()
  530. : mutex(),
  531. dataPool(512, 512),
  532. data(dataPool),
  533. dataPending(dataPool) {}
  534. ~RtMidiEvents()
  535. {
  536. clear();
  537. }
  538. void append(const RtMidiEvent& event)
  539. {
  540. mutex.lock();
  541. dataPending.append(event);
  542. mutex.unlock();
  543. }
  544. void clear()
  545. {
  546. mutex.lock();
  547. data.clear();
  548. dataPending.clear();
  549. mutex.unlock();
  550. }
  551. void splice()
  552. {
  553. if (dataPending.count() > 0)
  554. dataPending.moveTo(data, true /* append */);
  555. }
  556. };
  557. LinkedList<MidiInPort> fMidiIns;
  558. RtMidiEvents fMidiInEvents;
  559. LinkedList<MidiOutPort> fMidiOuts;
  560. CarlaMutex fMidiOutMutex;
  561. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJuce)
  562. };
  563. // -----------------------------------------
  564. CarlaEngine* CarlaEngine::newJuce(const AudioApi api)
  565. {
  566. initJuceDevicesIfNeeded();
  567. String juceApi;
  568. switch (api)
  569. {
  570. case AUDIO_API_NULL:
  571. case AUDIO_API_OSS:
  572. case AUDIO_API_PULSE:
  573. break;
  574. case AUDIO_API_JACK:
  575. juceApi = "JACK";
  576. break;
  577. case AUDIO_API_ALSA:
  578. juceApi = "ALSA";
  579. break;
  580. case AUDIO_API_CORE:
  581. juceApi = "CoreAudio";
  582. break;
  583. case AUDIO_API_ASIO:
  584. juceApi = "ASIO";
  585. break;
  586. case AUDIO_API_DS:
  587. juceApi = "DirectSound";
  588. break;
  589. }
  590. if (juceApi.isEmpty())
  591. return nullptr;
  592. AudioIODeviceType* deviceType = nullptr;
  593. for (int i=0, count=gDeviceTypes.size(); i < count; ++i)
  594. {
  595. deviceType = gDeviceTypes[i];
  596. if (deviceType == nullptr || deviceType->getTypeName() == juceApi)
  597. break;
  598. }
  599. if (deviceType == nullptr)
  600. return nullptr;
  601. deviceType->scanForDevices();
  602. return new CarlaEngineJuce(deviceType);
  603. }
  604. uint CarlaEngine::getJuceApiCount()
  605. {
  606. initJuceDevicesIfNeeded();
  607. return static_cast<uint>(gDeviceTypes.size());
  608. }
  609. const char* CarlaEngine::getJuceApiName(const uint uindex)
  610. {
  611. initJuceDevicesIfNeeded();
  612. const int index(static_cast<int>(uindex));
  613. CARLA_SAFE_ASSERT_RETURN(index < gDeviceTypes.size(), nullptr);
  614. AudioIODeviceType* const deviceType(gDeviceTypes[index]);
  615. CARLA_SAFE_ASSERT_RETURN(deviceType != nullptr, nullptr);
  616. return deviceType->getTypeName().toRawUTF8();
  617. }
  618. const char* const* CarlaEngine::getJuceApiDeviceNames(const uint uindex)
  619. {
  620. initJuceDevicesIfNeeded();
  621. const int index(static_cast<int>(uindex));
  622. CARLA_SAFE_ASSERT_RETURN(index < gDeviceTypes.size(), nullptr);
  623. AudioIODeviceType* const deviceType(gDeviceTypes[index]);
  624. CARLA_SAFE_ASSERT_RETURN(deviceType != nullptr, nullptr);
  625. deviceType->scanForDevices();
  626. StringArray juceDeviceNames(deviceType->getDeviceNames());
  627. const int juceDeviceNameCount(juceDeviceNames.size());
  628. if (juceDeviceNameCount <= 0)
  629. return nullptr;
  630. CarlaStringList devNames;
  631. for (int i=0; i < juceDeviceNameCount; ++i)
  632. devNames.append(juceDeviceNames[i].toRawUTF8());
  633. gDeviceNames = devNames.toCharStringListPtr();
  634. return gDeviceNames;
  635. }
  636. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const uint uindex, const char* const deviceName)
  637. {
  638. initJuceDevicesIfNeeded();
  639. const int index(static_cast<int>(uindex));
  640. CARLA_SAFE_ASSERT_RETURN(index < gDeviceTypes.size(), nullptr);
  641. AudioIODeviceType* const deviceType(gDeviceTypes[index]);
  642. CARLA_SAFE_ASSERT_RETURN(deviceType != nullptr, nullptr);
  643. deviceType->scanForDevices();
  644. ScopedPointer<AudioIODevice> device(deviceType->createDevice(deviceName, deviceName));
  645. if (device == nullptr)
  646. return nullptr;
  647. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  648. static uint32_t dummyBufferSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  649. static double dummySampleRates[14] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  650. // reset
  651. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE | ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE;
  652. // cleanup
  653. if (devInfo.bufferSizes != nullptr && devInfo.bufferSizes != dummyBufferSizes)
  654. {
  655. delete[] devInfo.bufferSizes;
  656. devInfo.bufferSizes = nullptr;
  657. }
  658. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  659. {
  660. delete[] devInfo.sampleRates;
  661. devInfo.sampleRates = nullptr;
  662. }
  663. if (device->hasControlPanel())
  664. devInfo.hints |= ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL;
  665. Array<int> juceBufferSizes = device->getAvailableBufferSizes();
  666. if (int bufferSizesCount = juceBufferSizes.size())
  667. {
  668. uint32_t* const bufferSizes(new uint32_t[bufferSizesCount+1]);
  669. for (int i=0; i < bufferSizesCount; ++i)
  670. bufferSizes[i] = static_cast<uint32_t>(juceBufferSizes[i]);
  671. bufferSizes[bufferSizesCount] = 0;
  672. devInfo.bufferSizes = bufferSizes;
  673. }
  674. else
  675. {
  676. devInfo.bufferSizes = dummyBufferSizes;
  677. }
  678. Array<double> juceSampleRates = device->getAvailableSampleRates();
  679. if (int sampleRatesCount = juceSampleRates.size())
  680. {
  681. double* const sampleRates(new double[sampleRatesCount+1]);
  682. for (int i=0; i < sampleRatesCount; ++i)
  683. sampleRates[i] = juceSampleRates[i];
  684. sampleRates[sampleRatesCount] = 0.0;
  685. devInfo.sampleRates = sampleRates;
  686. }
  687. else
  688. {
  689. devInfo.sampleRates = dummySampleRates;
  690. }
  691. return &devInfo;
  692. }
  693. // -----------------------------------------
  694. CARLA_BACKEND_END_NAMESPACE