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.

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