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.

706 lines
23KB

  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 = static_cast<uint32_t>(fDevice->getCurrentBufferSizeSamples());
  111. pData->sampleRate = fDevice->getCurrentSampleRate();
  112. pData->bufAudio.inCount = static_cast<uint32_t>(inputChannels.countNumberOfSetBits());
  113. pData->bufAudio.outCount = static_cast<uint32_t>(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[static_cast<int>(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, static_cast<int>(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[static_cast<int>(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, static_cast<int>(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), static_cast<uint32_t>(numInputChannels),
  341. outputChannelData, static_cast<uint32_t>(numOutputChannels),
  342. static_cast<uint32_t>(numSamples), false);
  343. }
  344. // output events
  345. {
  346. // TODO
  347. //fMidiOutEvents...
  348. }
  349. runPendingRtEvents();
  350. return;
  351. // unused
  352. (void)inputChannelData;
  353. (void)numInputChannels;
  354. }
  355. void audioDeviceAboutToStart(AudioIODevice* /*device*/) override
  356. {
  357. }
  358. void audioDeviceStopped() override
  359. {
  360. }
  361. void audioDeviceError(const String& errorMessage) override
  362. {
  363. callback(ENGINE_CALLBACK_ERROR, 0, 0, 0, 0.0f, errorMessage.toRawUTF8());
  364. }
  365. // -------------------------------------------------------------------
  366. bool connectRackMidiInPort(const int) override
  367. {
  368. return false;
  369. }
  370. bool connectRackMidiOutPort(const int) override
  371. {
  372. return false;
  373. }
  374. bool disconnectRackMidiInPort(const int) override
  375. {
  376. return false;
  377. }
  378. bool disconnectRackMidiOutPort(const int) override
  379. {
  380. return false;
  381. }
  382. // -------------------------------------
  383. private:
  384. ScopedPointer<AudioIODevice> fDevice;
  385. AudioIODeviceType* const fDeviceType;
  386. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJuce)
  387. };
  388. // -----------------------------------------
  389. CarlaEngine* CarlaEngine::newJuce(const AudioApi api)
  390. {
  391. initJuceDevices();
  392. String juceApi;
  393. switch (api)
  394. {
  395. case AUDIO_API_NULL:
  396. case AUDIO_API_OSS:
  397. case AUDIO_API_PULSE:
  398. break;
  399. case AUDIO_API_JACK:
  400. juceApi = "JACK";
  401. break;
  402. case AUDIO_API_ALSA:
  403. juceApi = "ALSA";
  404. break;
  405. case AUDIO_API_CORE:
  406. juceApi = "CoreAudio";
  407. break;
  408. case AUDIO_API_ASIO:
  409. juceApi = "ASIO";
  410. break;
  411. case AUDIO_API_DS:
  412. juceApi = "DirectSound";
  413. break;
  414. }
  415. if (juceApi.isEmpty())
  416. return nullptr;
  417. AudioIODeviceType* deviceType = nullptr;
  418. for (int i=0, count=gJuceDeviceTypes.size(); i < count; ++i)
  419. {
  420. deviceType = gJuceDeviceTypes[i];
  421. if (deviceType == nullptr || deviceType->getTypeName() == juceApi)
  422. break;
  423. }
  424. if (deviceType == nullptr)
  425. return nullptr;
  426. deviceType->scanForDevices();
  427. return new CarlaEngineJuce(deviceType);
  428. }
  429. unsigned int CarlaEngine::getJuceApiCount()
  430. {
  431. initJuceDevices();
  432. return static_cast<unsigned int>(gJuceDeviceTypes.size());
  433. }
  434. const char* CarlaEngine::getJuceApiName(const unsigned int index)
  435. {
  436. initJuceDevices();
  437. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  438. return nullptr;
  439. AudioIODeviceType* const deviceType(gJuceDeviceTypes[static_cast<int>(index)]);
  440. if (deviceType == nullptr)
  441. return nullptr;
  442. return deviceType->getTypeName().toRawUTF8();
  443. }
  444. const char* const* CarlaEngine::getJuceApiDeviceNames(const unsigned int index)
  445. {
  446. initJuceDevices();
  447. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  448. return nullptr;
  449. AudioIODeviceType* const deviceType(gJuceDeviceTypes[static_cast<int>(index)]);
  450. if (deviceType == nullptr)
  451. return nullptr;
  452. deviceType->scanForDevices();
  453. StringArray deviceNames(deviceType->getDeviceNames());
  454. const int deviceNameCount(deviceNames.size());
  455. if (deviceNameCount <= 0)
  456. return nullptr;
  457. if (gRetNames != nullptr)
  458. {
  459. for (int i=0; gRetNames[i] != nullptr; ++i)
  460. delete[] gRetNames[i];
  461. delete[] gRetNames;
  462. }
  463. gRetNames = new const char*[deviceNameCount+1];
  464. for (int i=0; i < deviceNameCount; ++i)
  465. gRetNames[i] = carla_strdup(deviceNames[i].toRawUTF8());
  466. gRetNames[deviceNameCount] = nullptr;
  467. return gRetNames;
  468. }
  469. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const unsigned int index, const char* const deviceName)
  470. {
  471. initJuceDevices();
  472. if (static_cast<int>(index) >= gJuceDeviceTypes.size())
  473. {
  474. carla_stderr("here 001");
  475. return nullptr;
  476. }
  477. AudioIODeviceType* const deviceType(gJuceDeviceTypes[static_cast<int>(index)]);
  478. if (deviceType == nullptr)
  479. return nullptr;
  480. deviceType->scanForDevices();
  481. ScopedPointer<AudioIODevice> device(deviceType->createDevice(deviceName, deviceName));
  482. if (device == nullptr)
  483. return nullptr;
  484. static EngineDriverDeviceInfo devInfo = { 0x0, nullptr, nullptr };
  485. static uint32_t dummyBufferSizes[11] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 0 };
  486. static double dummySampleRates[14] = { 22050.0, 32000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0, 0.0 };
  487. // reset
  488. devInfo.hints = ENGINE_DRIVER_DEVICE_VARIABLE_BUFFER_SIZE | ENGINE_DRIVER_DEVICE_VARIABLE_SAMPLE_RATE;
  489. // cleanup
  490. if (devInfo.bufferSizes != nullptr && devInfo.bufferSizes != dummyBufferSizes)
  491. {
  492. delete[] devInfo.bufferSizes;
  493. devInfo.bufferSizes = nullptr;
  494. }
  495. if (devInfo.sampleRates != nullptr && devInfo.sampleRates != dummySampleRates)
  496. {
  497. delete[] devInfo.sampleRates;
  498. devInfo.sampleRates = nullptr;
  499. }
  500. if (device->hasControlPanel())
  501. devInfo.hints |= ENGINE_DRIVER_DEVICE_HAS_CONTROL_PANEL;
  502. if (int bufferSizesCount = device->getNumBufferSizesAvailable())
  503. {
  504. uint32_t* const bufferSizes(new uint32_t[bufferSizesCount+1]);
  505. for (int i=0; i < bufferSizesCount; ++i)
  506. bufferSizes[i] = static_cast<uint32_t>(device->getBufferSizeSamples(i));
  507. bufferSizes[bufferSizesCount] = 0;
  508. devInfo.bufferSizes = bufferSizes;
  509. }
  510. else
  511. {
  512. devInfo.bufferSizes = dummyBufferSizes;
  513. }
  514. if (int sampleRatesCount = device->getNumSampleRates())
  515. {
  516. double* const sampleRates(new double[sampleRatesCount+1]);
  517. for (int i=0; i < sampleRatesCount; ++i)
  518. sampleRates[i] = device->getSampleRate(i);
  519. sampleRates[sampleRatesCount] = 0.0;
  520. devInfo.sampleRates = sampleRates;
  521. }
  522. else
  523. {
  524. devInfo.sampleRates = dummySampleRates;
  525. }
  526. return &devInfo;
  527. }
  528. // -----------------------------------------
  529. CARLA_BACKEND_END_NAMESPACE