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.

704 lines
22KB

  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. #ifndef HAVE_JUCE
  18. # error This file should not be compiled if Juce is disabled
  19. #endif
  20. #include "CarlaEngineInternal.hpp"
  21. #include "CarlaBackendUtils.hpp"
  22. // #include "RtLinkedList.hpp"
  23. #include "juce_audio_devices.h"
  24. using namespace juce;
  25. CARLA_BACKEND_START_NAMESPACE
  26. #if 0
  27. } // Fix editor indentation
  28. #endif
  29. // -------------------------------------------------------------------------------------------------------------------
  30. static const char** gRetNames = nullptr;
  31. static OwnedArray<AudioIODeviceType> gJuceDeviceTypes;
  32. static void initJuceDevices()
  33. {
  34. static AudioDeviceManager manager;
  35. if (gJuceDeviceTypes.size() == 0)
  36. manager.createAudioDeviceTypes(gJuceDeviceTypes);
  37. }
  38. // -------------------------------------------------------------------------------------------------------------------
  39. // Juce Engine
  40. class CarlaEngineJuce : public CarlaEngine,
  41. public AudioIODeviceCallback
  42. {
  43. public:
  44. CarlaEngineJuce(AudioIODeviceType* const devType)
  45. : CarlaEngine(),
  46. AudioIODeviceCallback(),
  47. fDeviceType(devType)
  48. {
  49. carla_debug("CarlaEngineJuce::CarlaEngineJuce(%p)", devType);
  50. // just to make sure
  51. pData->options.transportMode = ENGINE_TRANSPORT_MODE_INTERNAL;
  52. }
  53. ~CarlaEngineJuce() override
  54. {
  55. carla_debug("CarlaEngineJuce::~CarlaEngineJuce()");
  56. if (gRetNames != nullptr)
  57. {
  58. delete[] gRetNames;
  59. gRetNames = nullptr;
  60. }
  61. gJuceDeviceTypes.clear(true);
  62. }
  63. // -------------------------------------
  64. bool init(const char* const clientName) override
  65. {
  66. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0', false);
  67. carla_debug("CarlaEngineJuce::init(\"%s\")", clientName);
  68. if (pData->options.processMode != ENGINE_PROCESS_MODE_CONTINUOUS_RACK && pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  69. {
  70. setLastError("Invalid process mode");
  71. return false;
  72. }
  73. pData->bufAudio.usePatchbay = (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY);
  74. String deviceName;
  75. if (pData->options.audioDevice != nullptr && pData->options.audioDevice[0] != '\0')
  76. {
  77. deviceName = pData->options.audioDevice;
  78. }
  79. else
  80. {
  81. const int defaultIndex(fDeviceType->getDefaultDeviceIndex(false));
  82. StringArray deviceNames(fDeviceType->getDeviceNames());
  83. if (defaultIndex >= 0 && defaultIndex < deviceNames.size())
  84. deviceName = deviceNames[defaultIndex];
  85. }
  86. if (deviceName.isEmpty())
  87. {
  88. setLastError("Audio device has not been selected yet and a default one is not available");
  89. return false;
  90. }
  91. fDevice = fDeviceType->createDevice(deviceName, deviceName);
  92. if (fDevice == nullptr)
  93. {
  94. setLastError("Failed to create device");
  95. return false;
  96. }
  97. StringArray inputNames(fDevice->getInputChannelNames());
  98. StringArray outputNames(fDevice->getOutputChannelNames());
  99. BigInteger inputChannels;
  100. inputChannels.setRange(0, inputNames.size(), true);
  101. BigInteger outputChannels;
  102. outputChannels.setRange(0, outputNames.size(), true);
  103. String error = fDevice->open(inputChannels, outputChannels, pData->options.audioSampleRate, static_cast<int>(pData->options.audioBufferSize));
  104. if (error.isNotEmpty())
  105. {
  106. fDevice = nullptr;
  107. setLastError(error.toUTF8());
  108. return false;
  109. }
  110. pData->bufferSize = fDevice->getCurrentBufferSizeSamples();
  111. pData->sampleRate = fDevice->getCurrentSampleRate();
  112. pData->bufAudio.inCount = inputChannels.countNumberOfSetBits();
  113. pData->bufAudio.outCount = outputChannels.countNumberOfSetBits();
  114. CARLA_ASSERT(pData->bufAudio.outCount > 0);
  115. pData->bufAudio.create(pData->bufferSize);
  116. fDevice->start(this);
  117. CarlaEngine::init(clientName);
  118. patchbayRefresh();
  119. return true;
  120. }
  121. bool close() override
  122. {
  123. carla_debug("CarlaEngineJuce::close()");
  124. pData->bufAudio.isReady = false;
  125. bool hasError = !CarlaEngine::close();
  126. if (fDevice != nullptr)
  127. {
  128. if (fDevice->isPlaying())
  129. fDevice->stop();
  130. if (fDevice->isOpen())
  131. fDevice->close();
  132. fDevice = nullptr;
  133. }
  134. pData->bufAudio.clear();
  135. return !hasError;
  136. }
  137. bool isRunning() const noexcept override
  138. {
  139. return fDevice != nullptr && fDevice->isPlaying();
  140. }
  141. bool isOffline() const noexcept override
  142. {
  143. return false;
  144. }
  145. EngineType getType() const noexcept override
  146. {
  147. return kEngineTypeJuce;
  148. }
  149. const char* getCurrentDriverName() const noexcept override
  150. {
  151. return fDeviceType->getTypeName().toRawUTF8();
  152. }
  153. // -------------------------------------------------------------------
  154. // Patchbay
  155. bool patchbayRefresh() override
  156. {
  157. CARLA_SAFE_ASSERT_RETURN(pData->bufAudio.isReady, false);
  158. pData->bufAudio.initPatchbay();
  159. if (pData->bufAudio.usePatchbay)
  160. {
  161. // not implemented yet
  162. return false;
  163. }
  164. char strBuf[STR_MAX+1];
  165. strBuf[STR_MAX] = '\0';
  166. //EngineRackBuffers* const rack(pData->bufAudio.rack);
  167. // Main
  168. {
  169. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_PATCHBAY_GROUP_CARLA, 0, 0, 0.0f, getName());
  170. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_AUDIO_IN1, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in1");
  171. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_AUDIO_IN2, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, "audio-in2");
  172. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_AUDIO_OUT1, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out1");
  173. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_AUDIO_OUT2, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, "audio-out2");
  174. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_MIDI_IN, PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, 0.0f, "midi-in");
  175. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_CARLA, RACK_PATCHBAY_PORT_MIDI_OUT, PATCHBAY_PORT_TYPE_MIDI, 0.0f, "midi-out");
  176. }
  177. const String& deviceName(fDevice->getName());
  178. // Audio In
  179. {
  180. if (deviceName.isNotEmpty())
  181. std::snprintf(strBuf, STR_MAX, "Capture (%s)", deviceName.toRawUTF8());
  182. else
  183. std::strncpy(strBuf, "Capture", STR_MAX);
  184. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_PATCHBAY_GROUP_AUDIO_IN, 0, 0, 0.0f, strBuf);
  185. StringArray inputNames(fDevice->getInputChannelNames());
  186. CARLA_ASSERT(inputNames.size() == static_cast<int>(pData->bufAudio.inCount));
  187. for (uint i=0; i < pData->bufAudio.inCount; ++i)
  188. {
  189. String inputName(inputNames[i]);
  190. if (inputName.trim().isNotEmpty())
  191. std::snprintf(strBuf, STR_MAX, "%s", inputName.toRawUTF8());
  192. else
  193. std::snprintf(strBuf, STR_MAX, "capture_%i", i+1);
  194. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_AUDIO_IN, RACK_PATCHBAY_GROUP_AUDIO_IN*1000 + i, PATCHBAY_PORT_TYPE_AUDIO, 0.0f, strBuf);
  195. }
  196. }
  197. // Audio Out
  198. {
  199. if (deviceName.isNotEmpty())
  200. std::snprintf(strBuf, STR_MAX, "Playback (%s)", deviceName.toRawUTF8());
  201. else
  202. std::strncpy(strBuf, "Playback", STR_MAX);
  203. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_PATCHBAY_GROUP_AUDIO_OUT, 0, 0, 0.0f, strBuf);
  204. StringArray outputNames(fDevice->getOutputChannelNames());
  205. CARLA_ASSERT(outputNames.size() == static_cast<int>(pData->bufAudio.outCount));
  206. for (uint i=0; i < pData->bufAudio.outCount; ++i)
  207. {
  208. String outputName(outputNames[i]);
  209. if (outputName.trim().isNotEmpty())
  210. std::snprintf(strBuf, STR_MAX, "%s", outputName.toRawUTF8());
  211. else
  212. std::snprintf(strBuf, STR_MAX, "playback_%i", i+1);
  213. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_AUDIO_OUT, RACK_PATCHBAY_GROUP_AUDIO_OUT*1000 + i, PATCHBAY_PORT_TYPE_AUDIO|PATCHBAY_PORT_IS_INPUT, 0.0f, strBuf);
  214. }
  215. }
  216. #if 0 // midi implemented yet
  217. // MIDI In
  218. {
  219. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, RACK_PATCHBAY_GROUP_MIDI_IN, 0, 0, 0.0f, "Readable MIDI ports");
  220. for (unsigned int i=0, count=fDummyMidiIn.getPortCount(); i < count; ++i)
  221. {
  222. PortNameToId portNameToId;
  223. portNameToId.portId = RACK_PATCHBAY_GROUP_MIDI_IN*1000 + i;
  224. std::strncpy(portNameToId.name, fDummyMidiIn.getPortName(i).c_str(), STR_MAX);
  225. fUsedMidiIns.append(portNameToId);
  226. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, RACK_PATCHBAY_GROUP_MIDI_IN, portNameToId.portId, PATCHBAY_PORT_TYPE_MIDI, 0.0f, portNameToId.name);
  227. }
  228. }
  229. // MIDI Out
  230. {
  231. callback(ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED, 0, RACK_PATCHBAY_GROUP_MIDI_OUT, 0, 0.0f, "Writable MIDI ports");
  232. for (unsigned int i=0, count=fDummyMidiOut.getPortCount(); i < count; ++i)
  233. {
  234. PortNameToId portNameToId;
  235. portNameToId.portId = RACK_PATCHBAY_GROUP_MIDI_OUT*1000 + i;
  236. std::strncpy(portNameToId.name, fDummyMidiOut.getPortName(i).c_str(), STR_MAX);
  237. fUsedMidiOuts.append(portNameToId);
  238. callback(ENGINE_CALLBACK_PATCHBAY_PORT_ADDED, 0, RACK_PATCHBAY_GROUP_MIDI_OUT, portNameToId.portId, PATCHBAY_PORT_TYPE_MIDI|PATCHBAY_PORT_IS_INPUT, portNameToId.name);
  239. }
  240. }
  241. #endif
  242. #if 0
  243. // Connections
  244. rack->connectLock.lock();
  245. for (LinkedList<uint>::Itenerator it = rack->connectedIns[0].begin(); it.valid(); it.next())
  246. {
  247. const uint& port(it.getValue());
  248. CARLA_SAFE_ASSERT_CONTINUE(port < pData->bufAudio.inCount);
  249. ConnectionToId connectionToId;
  250. connectionToId.id = rack->lastConnectionId;
  251. connectionToId.portOut = RACK_PATCHBAY_GROUP_AUDIO_IN*1000 + port;
  252. connectionToId.portIn = RACK_PATCHBAY_PORT_AUDIO_IN1;
  253. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  254. rack->usedConnections.append(connectionToId);
  255. rack->lastConnectionId++;
  256. }
  257. for (LinkedList<uint>::Itenerator it = rack->connectedIns[1].begin(); it.valid(); it.next())
  258. {
  259. const uint& port(it.getValue());
  260. CARLA_SAFE_ASSERT_CONTINUE(port < pData->bufAudio.inCount);
  261. ConnectionToId connectionToId;
  262. connectionToId.id = rack->lastConnectionId;
  263. connectionToId.portOut = RACK_PATCHBAY_GROUP_AUDIO_IN*1000 + port;
  264. connectionToId.portIn = RACK_PATCHBAY_PORT_AUDIO_IN2;
  265. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  266. rack->usedConnections.append(connectionToId);
  267. rack->lastConnectionId++;
  268. }
  269. for (LinkedList<uint>::Itenerator it = rack->connectedOuts[0].begin(); it.valid(); it.next())
  270. {
  271. const uint& port(it.getValue());
  272. CARLA_SAFE_ASSERT_CONTINUE(port < pData->bufAudio.outCount);
  273. ConnectionToId connectionToId;
  274. connectionToId.id = rack->lastConnectionId;
  275. connectionToId.portOut = RACK_PATCHBAY_PORT_AUDIO_OUT1;
  276. connectionToId.portIn = RACK_PATCHBAY_GROUP_AUDIO_OUT*1000 + port;
  277. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  278. rack->usedConnections.append(connectionToId);
  279. rack->lastConnectionId++;
  280. }
  281. for (LinkedList<uint>::Itenerator it = rack->connectedOuts[1].begin(); it.valid(); it.next())
  282. {
  283. const uint& port(it.getValue());
  284. CARLA_SAFE_ASSERT_CONTINUE(port < pData->bufAudio.outCount);
  285. ConnectionToId connectionToId;
  286. connectionToId.id = rack->lastConnectionId;
  287. connectionToId.portOut = RACK_PATCHBAY_PORT_AUDIO_OUT2;
  288. connectionToId.portIn = RACK_PATCHBAY_GROUP_AUDIO_OUT*1000 + port;
  289. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  290. rack->usedConnections.append(connectionToId);
  291. rack->lastConnectionId++;
  292. }
  293. pData->bufAudio.rack->connectLock.unlock();
  294. #if 0
  295. for (LinkedList<MidiPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
  296. {
  297. const MidiPort& midiPort(it.getValue());
  298. ConnectionToId connectionToId;
  299. connectionToId.id = rack->lastConnectionId;
  300. connectionToId.portOut = RACK_PATCHBAY_GROUP_MIDI_IN*1000 + midiPort.portId;
  301. connectionToId.portIn = RACK_PATCHBAY_PORT_MIDI_IN;
  302. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  303. rack->usedConnections.append(connectionToId);
  304. rack->lastConnectionId++;
  305. }
  306. for (LinkedList<MidiPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  307. {
  308. const MidiPort& midiPort(it.getValue());
  309. ConnectionToId connectionToId;
  310. connectionToId.id = rack->lastConnectionId;
  311. connectionToId.portOut = RACK_PATCHBAY_PORT_MIDI_OUT;
  312. connectionToId.portIn = RACK_PATCHBAY_GROUP_MIDI_OUT*1000 + midiPort.portId;
  313. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  314. rack->usedConnections.append(connectionToId);
  315. rack->lastConnectionId++;
  316. }
  317. #endif
  318. #endif
  319. return true;
  320. }
  321. // -------------------------------------------------------------------
  322. protected:
  323. void audioDeviceIOCallback(const float** inputChannelData, int numInputChannels, float** outputChannelData, int numOutputChannels, int numSamples) override
  324. {
  325. // assert juce buffers
  326. CARLA_SAFE_ASSERT_RETURN(numInputChannels == static_cast<int>(pData->bufAudio.inCount),);
  327. CARLA_SAFE_ASSERT_RETURN(numOutputChannels == static_cast<int>(pData->bufAudio.outCount),);
  328. CARLA_SAFE_ASSERT_RETURN(outputChannelData != nullptr,);
  329. CARLA_SAFE_ASSERT_RETURN(numSamples == static_cast<int>(pData->bufferSize),);
  330. if (numOutputChannels == 0 || ! pData->bufAudio.isReady)
  331. return runPendingRtEvents();
  332. // initialize input events
  333. carla_zeroStruct<EngineEvent>(pData->bufEvents.in, kMaxEngineEventInternalCount);
  334. // TODO - get events from juce
  335. if (pData->bufAudio.usePatchbay)
  336. {
  337. }
  338. else
  339. {
  340. pData->processRackFull(const_cast<float**>(inputChannelData), numInputChannels, outputChannelData, numOutputChannels, numSamples, false);
  341. }
  342. // output events
  343. {
  344. // TODO
  345. //fMidiOutEvents...
  346. }
  347. runPendingRtEvents();
  348. return;
  349. // unused
  350. (void)inputChannelData;
  351. (void)numInputChannels;
  352. }
  353. void audioDeviceAboutToStart(AudioIODevice* /*device*/) override
  354. {
  355. }
  356. void audioDeviceStopped() override
  357. {
  358. }
  359. void audioDeviceError(const String& errorMessage) override
  360. {
  361. callback(ENGINE_CALLBACK_ERROR, 0, 0, 0, 0.0f, errorMessage.toRawUTF8());
  362. }
  363. // -------------------------------------------------------------------
  364. bool connectRackMidiInPort(const int) override
  365. {
  366. return false;
  367. }
  368. bool connectRackMidiOutPort(const int) override
  369. {
  370. return false;
  371. }
  372. bool disconnectRackMidiInPort(const int) override
  373. {
  374. return false;
  375. }
  376. bool disconnectRackMidiOutPort(const int) override
  377. {
  378. return false;
  379. }
  380. // -------------------------------------
  381. private:
  382. ScopedPointer<AudioIODevice> fDevice;
  383. AudioIODeviceType* const fDeviceType;
  384. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJuce)
  385. };
  386. // -----------------------------------------
  387. CarlaEngine* CarlaEngine::newJuce(const AudioApi api)
  388. {
  389. initJuceDevices();
  390. String juceApi;
  391. switch (api)
  392. {
  393. case AUDIO_API_NULL:
  394. case AUDIO_API_OSS:
  395. case AUDIO_API_PULSE:
  396. break;
  397. case AUDIO_API_JACK:
  398. juceApi = "JACK";
  399. break;
  400. case AUDIO_API_ALSA:
  401. juceApi = "ALSA";
  402. break;
  403. case AUDIO_API_CORE:
  404. juceApi = "CoreAudio";
  405. break;
  406. case AUDIO_API_ASIO:
  407. juceApi = "ASIO";
  408. break;
  409. case AUDIO_API_DS:
  410. juceApi = "DirectSound";
  411. break;
  412. }
  413. if (juceApi.isEmpty())
  414. return nullptr;
  415. AudioIODeviceType* deviceType = nullptr;
  416. for (int i=0, count=gJuceDeviceTypes.size(); i < count; ++i)
  417. {
  418. deviceType = gJuceDeviceTypes[i];
  419. if (deviceType == nullptr || deviceType->getTypeName() == juceApi)
  420. break;
  421. }
  422. if (deviceType == nullptr)
  423. return nullptr;
  424. deviceType->scanForDevices();
  425. return new CarlaEngineJuce(deviceType);
  426. }
  427. unsigned int CarlaEngine::getJuceApiCount()
  428. {
  429. initJuceDevices();
  430. return static_cast<unsigned int>(gJuceDeviceTypes.size());
  431. }
  432. const char* CarlaEngine::getJuceApiName(const unsigned int index)
  433. {
  434. initJuceDevices();
  435. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  436. return nullptr;
  437. AudioIODeviceType* const deviceType(gJuceDeviceTypes[index]);
  438. if (deviceType == nullptr)
  439. return nullptr;
  440. return deviceType->getTypeName().toRawUTF8();
  441. }
  442. const char* const* CarlaEngine::getJuceApiDeviceNames(const unsigned int index)
  443. {
  444. initJuceDevices();
  445. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  446. return nullptr;
  447. AudioIODeviceType* const deviceType(gJuceDeviceTypes[index]);
  448. if (deviceType == nullptr)
  449. return nullptr;
  450. deviceType->scanForDevices();
  451. StringArray deviceNames(deviceType->getDeviceNames());
  452. const int deviceNameCount(deviceNames.size());
  453. if (deviceNameCount <= 0)
  454. return nullptr;
  455. if (gRetNames != nullptr)
  456. {
  457. for (int i=0; gRetNames[i] != nullptr; ++i)
  458. delete[] gRetNames[i];
  459. delete[] gRetNames;
  460. }
  461. gRetNames = new const char*[deviceNameCount+1];
  462. for (int i=0; i < deviceNameCount; ++i)
  463. gRetNames[i] = carla_strdup(deviceNames[i].toRawUTF8());
  464. gRetNames[deviceNameCount] = nullptr;
  465. return gRetNames;
  466. }
  467. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const unsigned int index, const char* const deviceName)
  468. {
  469. initJuceDevices();
  470. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  471. {
  472. carla_stderr("here 001");
  473. return nullptr;
  474. }
  475. AudioIODeviceType* const deviceType(gJuceDeviceTypes[index]);
  476. if (deviceType == nullptr)
  477. return nullptr;
  478. deviceType->scanForDevices();
  479. ScopedPointer<AudioIODevice> device(deviceType->createDevice(deviceName, deviceName));
  480. if (device == nullptr)
  481. return nullptr;
  482. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  483. static uint32_t dummyBufferSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  484. static double dummySampleRates[14] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  485. // reset
  486. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE | ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE;
  487. // cleanup
  488. if (devInfo.bufferSizes != nullptr && devInfo.bufferSizes != dummyBufferSizes)
  489. {
  490. delete[] devInfo.bufferSizes;
  491. devInfo.bufferSizes = nullptr;
  492. }
  493. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  494. {
  495. delete[] devInfo.sampleRates;
  496. devInfo.sampleRates = nullptr;
  497. }
  498. if (device->hasControlPanel())
  499. devInfo.hints |= ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL;
  500. if (int bufferSizesCount = device->getNumBufferSizesAvailable())
  501. {
  502. uint32_t* const bufferSizes(new uint32_t[bufferSizesCount+1]);
  503. for (int i=0; i < bufferSizesCount; ++i)
  504. bufferSizes[i] = device->getBufferSizeSamples(i);
  505. bufferSizes[bufferSizesCount] = 0;
  506. devInfo.bufferSizes = bufferSizes;
  507. }
  508. else
  509. {
  510. devInfo.bufferSizes = dummyBufferSizes;
  511. }
  512. if (int sampleRatesCount = device->getNumSampleRates())
  513. {
  514. double* const sampleRates(new double[sampleRatesCount+1]);
  515. for (int i=0; i < sampleRatesCount; ++i)
  516. sampleRates[i] = device->getSampleRate(i);
  517. sampleRates[sampleRatesCount] = 0.0;
  518. devInfo.sampleRates = sampleRates;
  519. }
  520. else
  521. {
  522. devInfo.sampleRates = dummySampleRates;
  523. }
  524. return &devInfo;
  525. }
  526. // -----------------------------------------
  527. CARLA_BACKEND_END_NAMESPACE