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.

702 lines
22KB

  1. /*
  2. * Carla Juce Engine
  3. * Copyright (C) 2013 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 "RtList.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. // Connections
  243. rack->connectLock.lock();
  244. for (List<uint>::Itenerator it = rack->connectedIns[0].begin(); it.valid(); it.next())
  245. {
  246. const uint& port(it.getConstValue());
  247. CARLA_SAFE_ASSERT_CONTINUE(port < pData->bufAudio.inCount);
  248. ConnectionToId connectionToId;
  249. connectionToId.id = rack->lastConnectionId;
  250. connectionToId.portOut = RACK_PATCHBAY_GROUP_AUDIO_IN*1000 + port;
  251. connectionToId.portIn = RACK_PATCHBAY_PORT_AUDIO_IN1;
  252. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  253. rack->usedConnections.append(connectionToId);
  254. rack->lastConnectionId++;
  255. }
  256. for (List<uint>::Itenerator it = rack->connectedIns[1].begin(); it.valid(); it.next())
  257. {
  258. const uint& port(it.getConstValue());
  259. CARLA_SAFE_ASSERT_CONTINUE(port < pData->bufAudio.inCount);
  260. ConnectionToId connectionToId;
  261. connectionToId.id = rack->lastConnectionId;
  262. connectionToId.portOut = RACK_PATCHBAY_GROUP_AUDIO_IN*1000 + port;
  263. connectionToId.portIn = RACK_PATCHBAY_PORT_AUDIO_IN2;
  264. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  265. rack->usedConnections.append(connectionToId);
  266. rack->lastConnectionId++;
  267. }
  268. for (List<uint>::Itenerator it = rack->connectedOuts[0].begin(); it.valid(); it.next())
  269. {
  270. const uint& port(it.getConstValue());
  271. CARLA_SAFE_ASSERT_CONTINUE(port < pData->bufAudio.outCount);
  272. ConnectionToId connectionToId;
  273. connectionToId.id = rack->lastConnectionId;
  274. connectionToId.portOut = RACK_PATCHBAY_PORT_AUDIO_OUT1;
  275. connectionToId.portIn = RACK_PATCHBAY_GROUP_AUDIO_OUT*1000 + port;
  276. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  277. rack->usedConnections.append(connectionToId);
  278. rack->lastConnectionId++;
  279. }
  280. for (List<uint>::Itenerator it = rack->connectedOuts[1].begin(); it.valid(); it.next())
  281. {
  282. const uint& port(it.getConstValue());
  283. CARLA_SAFE_ASSERT_CONTINUE(port < pData->bufAudio.outCount);
  284. ConnectionToId connectionToId;
  285. connectionToId.id = rack->lastConnectionId;
  286. connectionToId.portOut = RACK_PATCHBAY_PORT_AUDIO_OUT2;
  287. connectionToId.portIn = RACK_PATCHBAY_GROUP_AUDIO_OUT*1000 + port;
  288. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  289. rack->usedConnections.append(connectionToId);
  290. rack->lastConnectionId++;
  291. }
  292. pData->bufAudio.rack->connectLock.unlock();
  293. #if 0
  294. for (List<MidiPort>::Itenerator it=fMidiIns.begin(); it.valid(); it.next())
  295. {
  296. const MidiPort& midiPort(it.getConstValue());
  297. ConnectionToId connectionToId;
  298. connectionToId.id = rack->lastConnectionId;
  299. connectionToId.portOut = RACK_PATCHBAY_GROUP_MIDI_IN*1000 + midiPort.portId;
  300. connectionToId.portIn = RACK_PATCHBAY_PORT_MIDI_IN;
  301. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  302. rack->usedConnections.append(connectionToId);
  303. rack->lastConnectionId++;
  304. }
  305. for (List<MidiPort>::Itenerator it=fMidiOuts.begin(); it.valid(); it.next())
  306. {
  307. const MidiPort& midiPort(it.getConstValue());
  308. ConnectionToId connectionToId;
  309. connectionToId.id = rack->lastConnectionId;
  310. connectionToId.portOut = RACK_PATCHBAY_PORT_MIDI_OUT;
  311. connectionToId.portIn = RACK_PATCHBAY_GROUP_MIDI_OUT*1000 + midiPort.portId;
  312. callback(ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED, rack->lastConnectionId, connectionToId.portOut, connectionToId.portIn, 0.0f, nullptr);
  313. rack->usedConnections.append(connectionToId);
  314. rack->lastConnectionId++;
  315. }
  316. #endif
  317. return true;
  318. }
  319. // -------------------------------------------------------------------
  320. protected:
  321. void audioDeviceIOCallback(const float** inputChannelData, int numInputChannels, float** outputChannelData, int numOutputChannels, int numSamples) override
  322. {
  323. // assert juce buffers
  324. CARLA_SAFE_ASSERT_RETURN(numInputChannels == static_cast<int>(pData->bufAudio.inCount),);
  325. CARLA_SAFE_ASSERT_RETURN(numOutputChannels == static_cast<int>(pData->bufAudio.outCount),);
  326. CARLA_SAFE_ASSERT_RETURN(outputChannelData != nullptr,);
  327. CARLA_SAFE_ASSERT_RETURN(numSamples == static_cast<int>(pData->bufferSize),);
  328. if (numOutputChannels == 0 || ! pData->bufAudio.isReady)
  329. return runPendingRtEvents();
  330. // initialize input events
  331. carla_zeroStruct<EngineEvent>(pData->bufEvents.in, kEngineMaxInternalEventCount);
  332. // TODO - get events from juce
  333. if (pData->bufAudio.usePatchbay)
  334. {
  335. }
  336. else
  337. {
  338. pData->processRackFull(const_cast<float**>(inputChannelData), numInputChannels, outputChannelData, numOutputChannels, numSamples, false);
  339. }
  340. // output events
  341. {
  342. // TODO
  343. //fMidiOutEvents...
  344. }
  345. runPendingRtEvents();
  346. return;
  347. // unused
  348. (void)inputChannelData;
  349. (void)numInputChannels;
  350. }
  351. void audioDeviceAboutToStart(AudioIODevice* /*device*/) override
  352. {
  353. }
  354. void audioDeviceStopped() override
  355. {
  356. }
  357. void audioDeviceError(const String& errorMessage) override
  358. {
  359. callback(ENGINE_CALLBACK_ERROR, 0, 0, 0, 0.0f, errorMessage.toRawUTF8());
  360. }
  361. // -------------------------------------------------------------------
  362. bool connectRackMidiInPort(const int) override
  363. {
  364. return false;
  365. }
  366. bool connectRackMidiOutPort(const int) override
  367. {
  368. return false;
  369. }
  370. bool disconnectRackMidiInPort(const int) override
  371. {
  372. return false;
  373. }
  374. bool disconnectRackMidiOutPort(const int) override
  375. {
  376. return false;
  377. }
  378. // -------------------------------------
  379. private:
  380. ScopedPointer<AudioIODevice> fDevice;
  381. AudioIODeviceType* const fDeviceType;
  382. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJuce)
  383. };
  384. // -----------------------------------------
  385. CarlaEngine* CarlaEngine::newJuce(const AudioApi api)
  386. {
  387. initJuceDevices();
  388. String juceApi;
  389. switch (api)
  390. {
  391. case AUDIO_API_NULL:
  392. case AUDIO_API_OSS:
  393. case AUDIO_API_PULSE:
  394. break;
  395. case AUDIO_API_JACK:
  396. juceApi = "JACK";
  397. break;
  398. case AUDIO_API_ALSA:
  399. juceApi = "ALSA";
  400. break;
  401. case AUDIO_API_CORE:
  402. juceApi = "CoreAudio";
  403. break;
  404. case AUDIO_API_ASIO:
  405. juceApi = "ASIO";
  406. break;
  407. case AUDIO_API_DS:
  408. juceApi = "DirectSound";
  409. break;
  410. }
  411. if (juceApi.isEmpty())
  412. return nullptr;
  413. AudioIODeviceType* deviceType = nullptr;
  414. for (int i=0, count=gJuceDeviceTypes.size(); i < count; ++i)
  415. {
  416. deviceType = gJuceDeviceTypes[i];
  417. if (deviceType == nullptr || deviceType->getTypeName() == juceApi)
  418. break;
  419. }
  420. if (deviceType == nullptr)
  421. return nullptr;
  422. deviceType->scanForDevices();
  423. return new CarlaEngineJuce(deviceType);
  424. }
  425. unsigned int CarlaEngine::getJuceApiCount()
  426. {
  427. initJuceDevices();
  428. return static_cast<unsigned int>(gJuceDeviceTypes.size());
  429. }
  430. const char* CarlaEngine::getJuceApiName(const unsigned int index)
  431. {
  432. initJuceDevices();
  433. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  434. return nullptr;
  435. AudioIODeviceType* const deviceType(gJuceDeviceTypes[index]);
  436. if (deviceType == nullptr)
  437. return nullptr;
  438. return deviceType->getTypeName().toRawUTF8();
  439. }
  440. const char* const* CarlaEngine::getJuceApiDeviceNames(const unsigned int index)
  441. {
  442. initJuceDevices();
  443. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  444. return nullptr;
  445. AudioIODeviceType* const deviceType(gJuceDeviceTypes[index]);
  446. if (deviceType == nullptr)
  447. return nullptr;
  448. deviceType->scanForDevices();
  449. StringArray deviceNames(deviceType->getDeviceNames());
  450. const int deviceNameCount(deviceNames.size());
  451. if (deviceNameCount <= 0)
  452. return nullptr;
  453. if (gRetNames != nullptr)
  454. {
  455. for (int i=0; gRetNames[i] != nullptr; ++i)
  456. delete[] gRetNames[i];
  457. delete[] gRetNames;
  458. }
  459. gRetNames = new const char*[deviceNameCount+1];
  460. for (int i=0; i < deviceNameCount; ++i)
  461. gRetNames[i] = carla_strdup(deviceNames[i].toRawUTF8());
  462. gRetNames[deviceNameCount] = nullptr;
  463. return gRetNames;
  464. }
  465. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const unsigned int index, const char* const deviceName)
  466. {
  467. initJuceDevices();
  468. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  469. {
  470. carla_stderr("here 001");
  471. return nullptr;
  472. }
  473. AudioIODeviceType* const deviceType(gJuceDeviceTypes[index]);
  474. if (deviceType == nullptr)
  475. return nullptr;
  476. deviceType->scanForDevices();
  477. ScopedPointer<AudioIODevice> device(deviceType->createDevice(deviceName, deviceName));
  478. if (device == nullptr)
  479. return nullptr;
  480. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  481. static uint32_t dummyBufferSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  482. static double dummySampleRates[14] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  483. // reset
  484. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE | ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE;
  485. // cleanup
  486. if (devInfo.bufferSizes != nullptr && devInfo.bufferSizes != dummyBufferSizes)
  487. {
  488. delete[] devInfo.bufferSizes;
  489. devInfo.bufferSizes = nullptr;
  490. }
  491. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  492. {
  493. delete[] devInfo.sampleRates;
  494. devInfo.sampleRates = nullptr;
  495. }
  496. if (device->hasControlPanel())
  497. devInfo.hints |= ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL;
  498. if (int bufferSizesCount = device->getNumBufferSizesAvailable())
  499. {
  500. uint32_t* const bufferSizes(new uint32_t[bufferSizesCount+1]);
  501. for (int i=0; i < bufferSizesCount; ++i)
  502. bufferSizes[i] = device->getBufferSizeSamples(i);
  503. bufferSizes[bufferSizesCount] = 0;
  504. devInfo.bufferSizes = bufferSizes;
  505. }
  506. else
  507. {
  508. devInfo.bufferSizes = dummyBufferSizes;
  509. }
  510. if (int sampleRatesCount = device->getNumSampleRates())
  511. {
  512. double* const sampleRates(new double[sampleRatesCount+1]);
  513. for (int i=0; i < sampleRatesCount; ++i)
  514. sampleRates[i] = device->getSampleRate(i);
  515. sampleRates[sampleRatesCount] = 0.0;
  516. devInfo.sampleRates = sampleRates;
  517. }
  518. else
  519. {
  520. devInfo.sampleRates = dummySampleRates;
  521. }
  522. return &devInfo;
  523. }
  524. // -----------------------------------------
  525. CARLA_BACKEND_END_NAMESPACE