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.

1848 lines
65KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. template <typename Setup>
  20. static auto getSetupInfo (Setup& s, bool isInput)
  21. {
  22. struct SetupInfo
  23. {
  24. // double brackets so that we get the expression type, i.e. a (possibly const) reference
  25. decltype ((s.inputDeviceName)) name;
  26. decltype ((s.inputChannels)) channels;
  27. decltype ((s.useDefaultInputChannels)) useDefault;
  28. };
  29. return isInput ? SetupInfo { s.inputDeviceName, s.inputChannels, s.useDefaultInputChannels }
  30. : SetupInfo { s.outputDeviceName, s.outputChannels, s.useDefaultOutputChannels };
  31. }
  32. static auto tie (const AudioDeviceManager::AudioDeviceSetup& s)
  33. {
  34. return std::tie (s.outputDeviceName,
  35. s.inputDeviceName,
  36. s.sampleRate,
  37. s.bufferSize,
  38. s.inputChannels,
  39. s.useDefaultInputChannels,
  40. s.outputChannels,
  41. s.useDefaultOutputChannels);
  42. }
  43. bool AudioDeviceManager::AudioDeviceSetup::operator== (const AudioDeviceManager::AudioDeviceSetup& other) const
  44. {
  45. return tie (*this) == tie (other);
  46. }
  47. bool AudioDeviceManager::AudioDeviceSetup::operator!= (const AudioDeviceManager::AudioDeviceSetup& other) const
  48. {
  49. return tie (*this) != tie (other);
  50. }
  51. //==============================================================================
  52. class AudioDeviceManager::CallbackHandler : public AudioIODeviceCallback,
  53. public MidiInputCallback,
  54. public AudioIODeviceType::Listener
  55. {
  56. public:
  57. CallbackHandler (AudioDeviceManager& adm) noexcept : owner (adm) {}
  58. private:
  59. void audioDeviceIOCallbackWithContext (const float** ins,
  60. int numIns,
  61. float** outs,
  62. int numOuts,
  63. int numSamples,
  64. const AudioIODeviceCallbackContext& context) override
  65. {
  66. owner.audioDeviceIOCallbackInt (ins, numIns, outs, numOuts, numSamples, context);
  67. }
  68. void audioDeviceAboutToStart (AudioIODevice* device) override
  69. {
  70. owner.audioDeviceAboutToStartInt (device);
  71. }
  72. void audioDeviceStopped() override
  73. {
  74. owner.audioDeviceStoppedInt();
  75. }
  76. void audioDeviceError (const String& message) override
  77. {
  78. owner.audioDeviceErrorInt (message);
  79. }
  80. void handleIncomingMidiMessage (MidiInput* source, const MidiMessage& message) override
  81. {
  82. owner.handleIncomingMidiMessageInt (source, message);
  83. }
  84. void audioDeviceListChanged() override
  85. {
  86. owner.audioDeviceListChanged();
  87. }
  88. AudioDeviceManager& owner;
  89. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CallbackHandler)
  90. };
  91. //==============================================================================
  92. AudioDeviceManager::AudioDeviceManager()
  93. {
  94. callbackHandler.reset (new CallbackHandler (*this));
  95. }
  96. AudioDeviceManager::~AudioDeviceManager()
  97. {
  98. currentAudioDevice.reset();
  99. defaultMidiOutput.reset();
  100. }
  101. //==============================================================================
  102. void AudioDeviceManager::createDeviceTypesIfNeeded()
  103. {
  104. if (availableDeviceTypes.size() == 0)
  105. {
  106. OwnedArray<AudioIODeviceType> types;
  107. createAudioDeviceTypes (types);
  108. for (auto* t : types)
  109. addAudioDeviceType (std::unique_ptr<AudioIODeviceType> (t));
  110. types.clear (false);
  111. for (auto* type : availableDeviceTypes)
  112. type->scanForDevices();
  113. pickCurrentDeviceTypeWithDevices();
  114. }
  115. }
  116. void AudioDeviceManager::pickCurrentDeviceTypeWithDevices()
  117. {
  118. const auto deviceTypeHasDevices = [] (const AudioIODeviceType* ptr)
  119. {
  120. return ! ptr->getDeviceNames (true) .isEmpty()
  121. || ! ptr->getDeviceNames (false).isEmpty();
  122. };
  123. if (auto* type = findType (currentDeviceType))
  124. if (deviceTypeHasDevices (type))
  125. return;
  126. const auto iter = std::find_if (availableDeviceTypes.begin(),
  127. availableDeviceTypes.end(),
  128. deviceTypeHasDevices);
  129. if (iter != availableDeviceTypes.end())
  130. currentDeviceType = (*iter)->getTypeName();
  131. }
  132. const OwnedArray<AudioIODeviceType>& AudioDeviceManager::getAvailableDeviceTypes()
  133. {
  134. scanDevicesIfNeeded();
  135. return availableDeviceTypes;
  136. }
  137. void AudioDeviceManager::updateCurrentSetup()
  138. {
  139. if (currentAudioDevice != nullptr)
  140. {
  141. currentSetup.sampleRate = currentAudioDevice->getCurrentSampleRate();
  142. currentSetup.bufferSize = currentAudioDevice->getCurrentBufferSizeSamples();
  143. currentSetup.inputChannels = currentAudioDevice->getActiveInputChannels();
  144. currentSetup.outputChannels = currentAudioDevice->getActiveOutputChannels();
  145. }
  146. }
  147. void AudioDeviceManager::audioDeviceListChanged()
  148. {
  149. if (currentAudioDevice != nullptr)
  150. {
  151. auto currentDeviceStillAvailable = [&]
  152. {
  153. auto currentTypeName = currentAudioDevice->getTypeName();
  154. auto currentDeviceName = currentAudioDevice->getName();
  155. for (auto* deviceType : availableDeviceTypes)
  156. {
  157. if (currentTypeName == deviceType->getTypeName())
  158. {
  159. for (auto& deviceName : deviceType->getDeviceNames (true))
  160. if (currentDeviceName == deviceName)
  161. return true;
  162. for (auto& deviceName : deviceType->getDeviceNames (false))
  163. if (currentDeviceName == deviceName)
  164. return true;
  165. }
  166. }
  167. return false;
  168. }();
  169. if (! currentDeviceStillAvailable)
  170. {
  171. closeAudioDevice();
  172. if (auto e = createStateXml())
  173. initialiseFromXML (*e, true, preferredDeviceName, &currentSetup);
  174. else
  175. initialiseDefault (preferredDeviceName, &currentSetup);
  176. }
  177. updateCurrentSetup();
  178. }
  179. sendChangeMessage();
  180. }
  181. //==============================================================================
  182. static void addIfNotNull (OwnedArray<AudioIODeviceType>& list, AudioIODeviceType* const device)
  183. {
  184. if (device != nullptr)
  185. list.add (device);
  186. }
  187. void AudioDeviceManager::createAudioDeviceTypes (OwnedArray<AudioIODeviceType>& list)
  188. {
  189. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_WASAPI (WASAPIDeviceMode::shared));
  190. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_WASAPI (WASAPIDeviceMode::exclusive));
  191. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_WASAPI (WASAPIDeviceMode::sharedLowLatency));
  192. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_DirectSound());
  193. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_ASIO());
  194. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_CoreAudio());
  195. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_iOSAudio());
  196. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_Bela());
  197. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_JACK());
  198. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_ALSA());
  199. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_Oboe());
  200. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_OpenSLES());
  201. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_Android());
  202. }
  203. void AudioDeviceManager::addAudioDeviceType (std::unique_ptr<AudioIODeviceType> newDeviceType)
  204. {
  205. if (newDeviceType != nullptr)
  206. {
  207. jassert (lastDeviceTypeConfigs.size() == availableDeviceTypes.size());
  208. availableDeviceTypes.add (newDeviceType.release());
  209. lastDeviceTypeConfigs.add (new AudioDeviceSetup());
  210. availableDeviceTypes.getLast()->addListener (callbackHandler.get());
  211. }
  212. }
  213. void AudioDeviceManager::removeAudioDeviceType (AudioIODeviceType* deviceTypeToRemove)
  214. {
  215. if (deviceTypeToRemove != nullptr)
  216. {
  217. jassert (lastDeviceTypeConfigs.size() == availableDeviceTypes.size());
  218. auto index = availableDeviceTypes.indexOf (deviceTypeToRemove);
  219. if (auto removed = std::unique_ptr<AudioIODeviceType> (availableDeviceTypes.removeAndReturn (index)))
  220. {
  221. removed->removeListener (callbackHandler.get());
  222. lastDeviceTypeConfigs.remove (index, true);
  223. }
  224. }
  225. }
  226. static bool deviceListContains (AudioIODeviceType* type, bool isInput, const String& name)
  227. {
  228. for (auto& deviceName : type->getDeviceNames (isInput))
  229. if (deviceName.trim().equalsIgnoreCase (name.trim()))
  230. return true;
  231. return false;
  232. }
  233. //==============================================================================
  234. String AudioDeviceManager::initialise (const int numInputChannelsNeeded,
  235. const int numOutputChannelsNeeded,
  236. const XmlElement* const xml,
  237. const bool selectDefaultDeviceOnFailure,
  238. const String& preferredDefaultDeviceName,
  239. const AudioDeviceSetup* preferredSetupOptions)
  240. {
  241. scanDevicesIfNeeded();
  242. pickCurrentDeviceTypeWithDevices();
  243. numInputChansNeeded = numInputChannelsNeeded;
  244. numOutputChansNeeded = numOutputChannelsNeeded;
  245. preferredDeviceName = preferredDefaultDeviceName;
  246. if (xml != nullptr && xml->hasTagName ("DEVICESETUP"))
  247. return initialiseFromXML (*xml, selectDefaultDeviceOnFailure,
  248. preferredDeviceName, preferredSetupOptions);
  249. return initialiseDefault (preferredDeviceName, preferredSetupOptions);
  250. }
  251. String AudioDeviceManager::initialiseDefault (const String& preferredDefaultDeviceName,
  252. const AudioDeviceSetup* preferredSetupOptions)
  253. {
  254. AudioDeviceSetup setup;
  255. if (preferredSetupOptions != nullptr)
  256. {
  257. setup = *preferredSetupOptions;
  258. }
  259. else if (preferredDefaultDeviceName.isNotEmpty())
  260. {
  261. const auto nameMatches = [&preferredDefaultDeviceName] (const String& name)
  262. {
  263. return name.matchesWildcard (preferredDefaultDeviceName, true);
  264. };
  265. struct WildcardMatch
  266. {
  267. String value;
  268. bool successful;
  269. };
  270. const auto getWildcardMatch = [&nameMatches] (const StringArray& names)
  271. {
  272. const auto iter = std::find_if (names.begin(), names.end(), nameMatches);
  273. return WildcardMatch { iter != names.end() ? *iter : String(), iter != names.end() };
  274. };
  275. struct WildcardMatches
  276. {
  277. WildcardMatch input, output;
  278. };
  279. const auto getMatchesForType = [&getWildcardMatch] (const AudioIODeviceType* type)
  280. {
  281. return WildcardMatches { getWildcardMatch (type->getDeviceNames (true)),
  282. getWildcardMatch (type->getDeviceNames (false)) };
  283. };
  284. struct SearchResult
  285. {
  286. String type, input, output;
  287. };
  288. const auto result = [&]
  289. {
  290. // First, look for a device type with an input and output which matches the preferred name
  291. for (auto* type : availableDeviceTypes)
  292. {
  293. const auto matches = getMatchesForType (type);
  294. if (matches.input.successful && matches.output.successful)
  295. return SearchResult { type->getTypeName(), matches.input.value, matches.output.value };
  296. }
  297. // No device type has matching ins and outs, so fall back to a device where either the
  298. // input or output match
  299. for (auto* type : availableDeviceTypes)
  300. {
  301. const auto matches = getMatchesForType (type);
  302. if (matches.input.successful || matches.output.successful)
  303. return SearchResult { type->getTypeName(), matches.input.value, matches.output.value };
  304. }
  305. // No devices match the query, so just use the default devices from the current type
  306. return SearchResult { currentDeviceType, {}, {} };
  307. }();
  308. currentDeviceType = result.type;
  309. setup.inputDeviceName = result.input;
  310. setup.outputDeviceName = result.output;
  311. }
  312. insertDefaultDeviceNames (setup);
  313. return setAudioDeviceSetup (setup, false);
  314. }
  315. String AudioDeviceManager::initialiseFromXML (const XmlElement& xml,
  316. bool selectDefaultDeviceOnFailure,
  317. const String& preferredDefaultDeviceName,
  318. const AudioDeviceSetup* preferredSetupOptions)
  319. {
  320. lastExplicitSettings.reset (new XmlElement (xml));
  321. String error;
  322. AudioDeviceSetup setup;
  323. if (preferredSetupOptions != nullptr)
  324. setup = *preferredSetupOptions;
  325. if (xml.getStringAttribute ("audioDeviceName").isNotEmpty())
  326. {
  327. setup.inputDeviceName = setup.outputDeviceName
  328. = xml.getStringAttribute ("audioDeviceName");
  329. }
  330. else
  331. {
  332. setup.inputDeviceName = xml.getStringAttribute ("audioInputDeviceName");
  333. setup.outputDeviceName = xml.getStringAttribute ("audioOutputDeviceName");
  334. }
  335. currentDeviceType = xml.getStringAttribute ("deviceType");
  336. if (findType (currentDeviceType) == nullptr)
  337. {
  338. if (auto* type = findType (setup.inputDeviceName, setup.outputDeviceName))
  339. currentDeviceType = type->getTypeName();
  340. else if (auto* firstType = availableDeviceTypes.getFirst())
  341. currentDeviceType = firstType->getTypeName();
  342. }
  343. setup.bufferSize = xml.getIntAttribute ("audioDeviceBufferSize", setup.bufferSize);
  344. setup.sampleRate = xml.getDoubleAttribute ("audioDeviceRate", setup.sampleRate);
  345. setup.inputChannels .parseString (xml.getStringAttribute ("audioDeviceInChans", "11"), 2);
  346. setup.outputChannels.parseString (xml.getStringAttribute ("audioDeviceOutChans", "11"), 2);
  347. setup.useDefaultInputChannels = ! xml.hasAttribute ("audioDeviceInChans");
  348. setup.useDefaultOutputChannels = ! xml.hasAttribute ("audioDeviceOutChans");
  349. error = setAudioDeviceSetup (setup, true);
  350. if (error.isNotEmpty() && selectDefaultDeviceOnFailure)
  351. error = initialise (numInputChansNeeded, numOutputChansNeeded, nullptr, false, preferredDefaultDeviceName);
  352. midiDeviceInfosFromXml.clear();
  353. enabledMidiInputs.clear();
  354. for (auto* c : xml.getChildWithTagNameIterator ("MIDIINPUT"))
  355. midiDeviceInfosFromXml.add ({ c->getStringAttribute ("name"), c->getStringAttribute ("identifier") });
  356. auto isIdentifierAvailable = [] (const Array<MidiDeviceInfo>& available, const String& identifier)
  357. {
  358. for (auto& device : available)
  359. if (device.identifier == identifier)
  360. return true;
  361. return false;
  362. };
  363. auto getUpdatedIdentifierForName = [&] (const Array<MidiDeviceInfo>& available, const String& name) -> String
  364. {
  365. for (auto& device : available)
  366. if (device.name == name)
  367. return device.identifier;
  368. return {};
  369. };
  370. auto inputs = MidiInput::getAvailableDevices();
  371. for (auto& info : midiDeviceInfosFromXml)
  372. {
  373. if (isIdentifierAvailable (inputs, info.identifier))
  374. {
  375. setMidiInputDeviceEnabled (info.identifier, true);
  376. }
  377. else
  378. {
  379. auto identifier = getUpdatedIdentifierForName (inputs, info.name);
  380. if (identifier.isNotEmpty())
  381. setMidiInputDeviceEnabled (identifier, true);
  382. }
  383. }
  384. MidiDeviceInfo defaultOutputDeviceInfo (xml.getStringAttribute ("defaultMidiOutput"),
  385. xml.getStringAttribute ("defaultMidiOutputDevice"));
  386. auto outputs = MidiOutput::getAvailableDevices();
  387. if (isIdentifierAvailable (outputs, defaultOutputDeviceInfo.identifier))
  388. {
  389. setDefaultMidiOutputDevice (defaultOutputDeviceInfo.identifier);
  390. }
  391. else
  392. {
  393. auto identifier = getUpdatedIdentifierForName (outputs, defaultOutputDeviceInfo.name);
  394. if (identifier.isNotEmpty())
  395. setDefaultMidiOutputDevice (identifier);
  396. }
  397. return error;
  398. }
  399. String AudioDeviceManager::initialiseWithDefaultDevices (int numInputChannelsNeeded,
  400. int numOutputChannelsNeeded)
  401. {
  402. lastExplicitSettings.reset();
  403. return initialise (numInputChannelsNeeded, numOutputChannelsNeeded,
  404. nullptr, false, {}, nullptr);
  405. }
  406. void AudioDeviceManager::insertDefaultDeviceNames (AudioDeviceSetup& setup) const
  407. {
  408. if (auto* type = getCurrentDeviceTypeObject())
  409. {
  410. for (const auto isInput : { false, true })
  411. {
  412. const auto numChannelsNeeded = isInput ? numInputChansNeeded : numOutputChansNeeded;
  413. const auto info = getSetupInfo (setup, isInput);
  414. if (numChannelsNeeded > 0 && info.name.isEmpty())
  415. info.name = type->getDeviceNames (isInput) [type->getDefaultDeviceIndex (isInput)];
  416. }
  417. }
  418. }
  419. std::unique_ptr<XmlElement> AudioDeviceManager::createStateXml() const
  420. {
  421. if (lastExplicitSettings != nullptr)
  422. return std::make_unique<XmlElement> (*lastExplicitSettings);
  423. return {};
  424. }
  425. //==============================================================================
  426. void AudioDeviceManager::scanDevicesIfNeeded()
  427. {
  428. if (listNeedsScanning)
  429. {
  430. listNeedsScanning = false;
  431. createDeviceTypesIfNeeded();
  432. for (auto* type : availableDeviceTypes)
  433. type->scanForDevices();
  434. }
  435. }
  436. AudioIODeviceType* AudioDeviceManager::findType (const String& typeName)
  437. {
  438. scanDevicesIfNeeded();
  439. for (auto* type : availableDeviceTypes)
  440. if (type->getTypeName() == typeName)
  441. return type;
  442. return {};
  443. }
  444. AudioIODeviceType* AudioDeviceManager::findType (const String& inputName, const String& outputName)
  445. {
  446. scanDevicesIfNeeded();
  447. for (auto* type : availableDeviceTypes)
  448. if ((inputName.isNotEmpty() && deviceListContains (type, true, inputName))
  449. || (outputName.isNotEmpty() && deviceListContains (type, false, outputName)))
  450. return type;
  451. return {};
  452. }
  453. AudioDeviceManager::AudioDeviceSetup AudioDeviceManager::getAudioDeviceSetup() const
  454. {
  455. return currentSetup;
  456. }
  457. void AudioDeviceManager::getAudioDeviceSetup (AudioDeviceSetup& setup) const
  458. {
  459. setup = currentSetup;
  460. }
  461. void AudioDeviceManager::deleteCurrentDevice()
  462. {
  463. currentAudioDevice.reset();
  464. currentSetup.inputDeviceName.clear();
  465. currentSetup.outputDeviceName.clear();
  466. }
  467. void AudioDeviceManager::setCurrentAudioDeviceType (const String& type, bool treatAsChosenDevice)
  468. {
  469. for (int i = 0; i < availableDeviceTypes.size(); ++i)
  470. {
  471. if (availableDeviceTypes.getUnchecked(i)->getTypeName() == type
  472. && currentDeviceType != type)
  473. {
  474. if (currentAudioDevice != nullptr)
  475. {
  476. closeAudioDevice();
  477. Thread::sleep (1500); // allow a moment for OS devices to sort themselves out, to help
  478. // avoid things like DirectSound/ASIO clashes
  479. }
  480. currentDeviceType = type;
  481. AudioDeviceSetup s (*lastDeviceTypeConfigs.getUnchecked(i));
  482. insertDefaultDeviceNames (s);
  483. setAudioDeviceSetup (s, treatAsChosenDevice);
  484. sendChangeMessage();
  485. break;
  486. }
  487. }
  488. }
  489. AudioIODeviceType* AudioDeviceManager::getCurrentDeviceTypeObject() const
  490. {
  491. for (auto* type : availableDeviceTypes)
  492. if (type->getTypeName() == currentDeviceType)
  493. return type;
  494. return availableDeviceTypes.getFirst();
  495. }
  496. static void updateSetupChannels (AudioDeviceManager::AudioDeviceSetup& setup, int defaultNumIns, int defaultNumOuts)
  497. {
  498. auto updateChannels = [] (const String& deviceName, BigInteger& channels, int defaultNumChannels)
  499. {
  500. if (deviceName.isEmpty())
  501. {
  502. channels.clear();
  503. }
  504. else if (defaultNumChannels != -1)
  505. {
  506. channels.clear();
  507. channels.setRange (0, defaultNumChannels, true);
  508. }
  509. };
  510. updateChannels (setup.inputDeviceName, setup.inputChannels, setup.useDefaultInputChannels ? defaultNumIns : -1);
  511. updateChannels (setup.outputDeviceName, setup.outputChannels, setup.useDefaultOutputChannels ? defaultNumOuts : -1);
  512. }
  513. String AudioDeviceManager::setAudioDeviceSetup (const AudioDeviceSetup& newSetup,
  514. bool treatAsChosenDevice)
  515. {
  516. jassert (&newSetup != &currentSetup); // this will have no effect
  517. if (newSetup != currentSetup)
  518. sendChangeMessage();
  519. else if (currentAudioDevice != nullptr)
  520. return {};
  521. stopDevice();
  522. if (getCurrentDeviceTypeObject() == nullptr
  523. || (newSetup.inputDeviceName.isEmpty() && newSetup.outputDeviceName.isEmpty()))
  524. {
  525. deleteCurrentDevice();
  526. if (treatAsChosenDevice)
  527. updateXml();
  528. return {};
  529. }
  530. String error;
  531. const auto needsNewDevice = currentSetup.inputDeviceName != newSetup.inputDeviceName
  532. || currentSetup.outputDeviceName != newSetup.outputDeviceName
  533. || currentAudioDevice == nullptr;
  534. if (needsNewDevice)
  535. {
  536. deleteCurrentDevice();
  537. scanDevicesIfNeeded();
  538. auto* type = getCurrentDeviceTypeObject();
  539. for (const auto isInput : { false, true })
  540. {
  541. const auto name = getSetupInfo (newSetup, isInput).name;
  542. if (name.isNotEmpty() && ! deviceListContains (type, isInput, name))
  543. return "No such device: " + name;
  544. }
  545. currentAudioDevice.reset (type->createDevice (newSetup.outputDeviceName, newSetup.inputDeviceName));
  546. if (currentAudioDevice == nullptr)
  547. error = "Can't open the audio device!\n\n"
  548. "This may be because another application is currently using the same device - "
  549. "if so, you should close any other applications and try again!";
  550. else
  551. error = currentAudioDevice->getLastError();
  552. if (error.isNotEmpty())
  553. {
  554. deleteCurrentDevice();
  555. return error;
  556. }
  557. }
  558. currentSetup = newSetup;
  559. if (! currentSetup.useDefaultInputChannels) numInputChansNeeded = currentSetup.inputChannels.countNumberOfSetBits();
  560. if (! currentSetup.useDefaultOutputChannels) numOutputChansNeeded = currentSetup.outputChannels.countNumberOfSetBits();
  561. updateSetupChannels (currentSetup, numInputChansNeeded, numOutputChansNeeded);
  562. if (currentSetup.inputChannels.isZero() && currentSetup.outputChannels.isZero())
  563. {
  564. if (treatAsChosenDevice)
  565. updateXml();
  566. return {};
  567. }
  568. currentSetup.sampleRate = chooseBestSampleRate (currentSetup.sampleRate);
  569. currentSetup.bufferSize = chooseBestBufferSize (currentSetup.bufferSize);
  570. error = currentAudioDevice->open (currentSetup.inputChannels,
  571. currentSetup.outputChannels,
  572. currentSetup.sampleRate,
  573. currentSetup.bufferSize);
  574. if (error.isEmpty())
  575. {
  576. currentDeviceType = currentAudioDevice->getTypeName();
  577. currentAudioDevice->start (callbackHandler.get());
  578. updateCurrentSetup();
  579. for (int i = 0; i < availableDeviceTypes.size(); ++i)
  580. if (availableDeviceTypes.getUnchecked (i)->getTypeName() == currentDeviceType)
  581. *(lastDeviceTypeConfigs.getUnchecked (i)) = currentSetup;
  582. if (treatAsChosenDevice)
  583. updateXml();
  584. }
  585. else
  586. {
  587. deleteCurrentDevice();
  588. }
  589. return error;
  590. }
  591. double AudioDeviceManager::chooseBestSampleRate (double rate) const
  592. {
  593. jassert (currentAudioDevice != nullptr);
  594. auto rates = currentAudioDevice->getAvailableSampleRates();
  595. if (rate > 0 && rates.contains (rate))
  596. return rate;
  597. rate = currentAudioDevice->getCurrentSampleRate();
  598. if (rate > 0 && rates.contains (rate))
  599. return rate;
  600. double lowestAbove44 = 0.0;
  601. for (int i = rates.size(); --i >= 0;)
  602. {
  603. auto sr = rates[i];
  604. if (sr >= 44100.0 && (lowestAbove44 < 1.0 || sr < lowestAbove44))
  605. lowestAbove44 = sr;
  606. }
  607. if (lowestAbove44 > 0.0)
  608. return lowestAbove44;
  609. return rates[0];
  610. }
  611. int AudioDeviceManager::chooseBestBufferSize (int bufferSize) const
  612. {
  613. jassert (currentAudioDevice != nullptr);
  614. if (bufferSize > 0 && currentAudioDevice->getAvailableBufferSizes().contains (bufferSize))
  615. return bufferSize;
  616. return currentAudioDevice->getDefaultBufferSize();
  617. }
  618. void AudioDeviceManager::stopDevice()
  619. {
  620. if (currentAudioDevice != nullptr)
  621. currentAudioDevice->stop();
  622. testSound.reset();
  623. }
  624. void AudioDeviceManager::closeAudioDevice()
  625. {
  626. stopDevice();
  627. currentAudioDevice.reset();
  628. loadMeasurer.reset();
  629. }
  630. void AudioDeviceManager::restartLastAudioDevice()
  631. {
  632. if (currentAudioDevice == nullptr)
  633. {
  634. if (currentSetup.inputDeviceName.isEmpty()
  635. && currentSetup.outputDeviceName.isEmpty())
  636. {
  637. // This method will only reload the last device that was running
  638. // before closeAudioDevice() was called - you need to actually open
  639. // one first, with setAudioDeviceSetup().
  640. jassertfalse;
  641. return;
  642. }
  643. AudioDeviceSetup s (currentSetup);
  644. setAudioDeviceSetup (s, false);
  645. }
  646. }
  647. void AudioDeviceManager::updateXml()
  648. {
  649. lastExplicitSettings.reset (new XmlElement ("DEVICESETUP"));
  650. lastExplicitSettings->setAttribute ("deviceType", currentDeviceType);
  651. lastExplicitSettings->setAttribute ("audioOutputDeviceName", currentSetup.outputDeviceName);
  652. lastExplicitSettings->setAttribute ("audioInputDeviceName", currentSetup.inputDeviceName);
  653. if (currentAudioDevice != nullptr)
  654. {
  655. lastExplicitSettings->setAttribute ("audioDeviceRate", currentAudioDevice->getCurrentSampleRate());
  656. if (currentAudioDevice->getDefaultBufferSize() != currentAudioDevice->getCurrentBufferSizeSamples())
  657. lastExplicitSettings->setAttribute ("audioDeviceBufferSize", currentAudioDevice->getCurrentBufferSizeSamples());
  658. if (! currentSetup.useDefaultInputChannels)
  659. lastExplicitSettings->setAttribute ("audioDeviceInChans", currentSetup.inputChannels.toString (2));
  660. if (! currentSetup.useDefaultOutputChannels)
  661. lastExplicitSettings->setAttribute ("audioDeviceOutChans", currentSetup.outputChannels.toString (2));
  662. }
  663. for (auto& input : enabledMidiInputs)
  664. {
  665. auto* child = lastExplicitSettings->createNewChildElement ("MIDIINPUT");
  666. child->setAttribute ("name", input->getName());
  667. child->setAttribute ("identifier", input->getIdentifier());
  668. }
  669. if (midiDeviceInfosFromXml.size() > 0)
  670. {
  671. // Add any midi devices that have been enabled before, but which aren't currently
  672. // open because the device has been disconnected.
  673. auto availableMidiDevices = MidiInput::getAvailableDevices();
  674. for (auto& d : midiDeviceInfosFromXml)
  675. {
  676. if (! availableMidiDevices.contains (d))
  677. {
  678. auto* child = lastExplicitSettings->createNewChildElement ("MIDIINPUT");
  679. child->setAttribute ("name", d.name);
  680. child->setAttribute ("identifier", d.identifier);
  681. }
  682. }
  683. }
  684. if (defaultMidiOutputDeviceInfo != MidiDeviceInfo())
  685. {
  686. lastExplicitSettings->setAttribute ("defaultMidiOutput", defaultMidiOutputDeviceInfo.name);
  687. lastExplicitSettings->setAttribute ("defaultMidiOutputDevice", defaultMidiOutputDeviceInfo.identifier);
  688. }
  689. }
  690. //==============================================================================
  691. void AudioDeviceManager::addAudioCallback (AudioIODeviceCallback* newCallback)
  692. {
  693. {
  694. const ScopedLock sl (audioCallbackLock);
  695. if (callbacks.contains (newCallback))
  696. return;
  697. }
  698. if (currentAudioDevice != nullptr && newCallback != nullptr)
  699. newCallback->audioDeviceAboutToStart (currentAudioDevice.get());
  700. const ScopedLock sl (audioCallbackLock);
  701. callbacks.add (newCallback);
  702. }
  703. void AudioDeviceManager::removeAudioCallback (AudioIODeviceCallback* callbackToRemove)
  704. {
  705. if (callbackToRemove != nullptr)
  706. {
  707. bool needsDeinitialising = currentAudioDevice != nullptr;
  708. {
  709. const ScopedLock sl (audioCallbackLock);
  710. needsDeinitialising = needsDeinitialising && callbacks.contains (callbackToRemove);
  711. callbacks.removeFirstMatchingValue (callbackToRemove);
  712. }
  713. if (needsDeinitialising)
  714. callbackToRemove->audioDeviceStopped();
  715. }
  716. }
  717. void AudioDeviceManager::audioDeviceIOCallbackInt (const float** inputChannelData,
  718. int numInputChannels,
  719. float** outputChannelData,
  720. int numOutputChannels,
  721. int numSamples,
  722. const AudioIODeviceCallbackContext& context)
  723. {
  724. const ScopedLock sl (audioCallbackLock);
  725. inputLevelGetter->updateLevel (inputChannelData, numInputChannels, numSamples);
  726. if (callbacks.size() > 0)
  727. {
  728. AudioProcessLoadMeasurer::ScopedTimer timer (loadMeasurer, numSamples);
  729. tempBuffer.setSize (jmax (1, numOutputChannels), jmax (1, numSamples), false, false, true);
  730. callbacks.getUnchecked(0)->audioDeviceIOCallbackWithContext (inputChannelData,
  731. numInputChannels,
  732. outputChannelData,
  733. numOutputChannels,
  734. numSamples,
  735. context);
  736. auto** tempChans = tempBuffer.getArrayOfWritePointers();
  737. for (int i = callbacks.size(); --i > 0;)
  738. {
  739. callbacks.getUnchecked(i)->audioDeviceIOCallbackWithContext (inputChannelData,
  740. numInputChannels,
  741. tempChans,
  742. numOutputChannels,
  743. numSamples,
  744. context);
  745. for (int chan = 0; chan < numOutputChannels; ++chan)
  746. {
  747. if (auto* src = tempChans [chan])
  748. if (auto* dst = outputChannelData [chan])
  749. for (int j = 0; j < numSamples; ++j)
  750. dst[j] += src[j];
  751. }
  752. }
  753. }
  754. else
  755. {
  756. for (int i = 0; i < numOutputChannels; ++i)
  757. zeromem (outputChannelData[i], (size_t) numSamples * sizeof (float));
  758. }
  759. if (testSound != nullptr)
  760. {
  761. auto numSamps = jmin (numSamples, testSound->getNumSamples() - testSoundPosition);
  762. auto* src = testSound->getReadPointer (0, testSoundPosition);
  763. for (int i = 0; i < numOutputChannels; ++i)
  764. if (auto* dst = outputChannelData [i])
  765. for (int j = 0; j < numSamps; ++j)
  766. dst[j] += src[j];
  767. testSoundPosition += numSamps;
  768. if (testSoundPosition >= testSound->getNumSamples())
  769. testSound.reset();
  770. }
  771. outputLevelGetter->updateLevel (const_cast<const float**> (outputChannelData), numOutputChannels, numSamples);
  772. }
  773. void AudioDeviceManager::audioDeviceAboutToStartInt (AudioIODevice* const device)
  774. {
  775. loadMeasurer.reset (device->getCurrentSampleRate(),
  776. device->getCurrentBufferSizeSamples());
  777. updateCurrentSetup();
  778. {
  779. const ScopedLock sl (audioCallbackLock);
  780. for (int i = callbacks.size(); --i >= 0;)
  781. callbacks.getUnchecked(i)->audioDeviceAboutToStart (device);
  782. }
  783. sendChangeMessage();
  784. }
  785. void AudioDeviceManager::audioDeviceStoppedInt()
  786. {
  787. sendChangeMessage();
  788. const ScopedLock sl (audioCallbackLock);
  789. loadMeasurer.reset();
  790. for (int i = callbacks.size(); --i >= 0;)
  791. callbacks.getUnchecked(i)->audioDeviceStopped();
  792. }
  793. void AudioDeviceManager::audioDeviceErrorInt (const String& message)
  794. {
  795. const ScopedLock sl (audioCallbackLock);
  796. for (int i = callbacks.size(); --i >= 0;)
  797. callbacks.getUnchecked(i)->audioDeviceError (message);
  798. }
  799. double AudioDeviceManager::getCpuUsage() const
  800. {
  801. return loadMeasurer.getLoadAsProportion();
  802. }
  803. //==============================================================================
  804. void AudioDeviceManager::setMidiInputDeviceEnabled (const String& identifier, bool enabled)
  805. {
  806. if (enabled != isMidiInputDeviceEnabled (identifier))
  807. {
  808. if (enabled)
  809. {
  810. if (auto midiIn = MidiInput::openDevice (identifier, callbackHandler.get()))
  811. {
  812. enabledMidiInputs.push_back (std::move (midiIn));
  813. enabledMidiInputs.back()->start();
  814. }
  815. }
  816. else
  817. {
  818. auto removePredicate = [identifier] (const std::unique_ptr<MidiInput>& in) { return in->getIdentifier() == identifier; };
  819. enabledMidiInputs.erase (std::remove_if (std::begin (enabledMidiInputs), std::end (enabledMidiInputs), removePredicate),
  820. std::end (enabledMidiInputs));
  821. }
  822. updateXml();
  823. sendChangeMessage();
  824. }
  825. }
  826. bool AudioDeviceManager::isMidiInputDeviceEnabled (const String& identifier) const
  827. {
  828. for (auto& mi : enabledMidiInputs)
  829. if (mi->getIdentifier() == identifier)
  830. return true;
  831. return false;
  832. }
  833. void AudioDeviceManager::addMidiInputDeviceCallback (const String& identifier, MidiInputCallback* callbackToAdd)
  834. {
  835. removeMidiInputDeviceCallback (identifier, callbackToAdd);
  836. if (identifier.isEmpty() || isMidiInputDeviceEnabled (identifier))
  837. {
  838. const ScopedLock sl (midiCallbackLock);
  839. midiCallbacks.add ({ identifier, callbackToAdd });
  840. }
  841. }
  842. void AudioDeviceManager::removeMidiInputDeviceCallback (const String& identifier, MidiInputCallback* callbackToRemove)
  843. {
  844. for (int i = midiCallbacks.size(); --i >= 0;)
  845. {
  846. auto& mc = midiCallbacks.getReference (i);
  847. if (mc.callback == callbackToRemove && mc.deviceIdentifier == identifier)
  848. {
  849. const ScopedLock sl (midiCallbackLock);
  850. midiCallbacks.remove (i);
  851. }
  852. }
  853. }
  854. void AudioDeviceManager::handleIncomingMidiMessageInt (MidiInput* source, const MidiMessage& message)
  855. {
  856. if (! message.isActiveSense())
  857. {
  858. const ScopedLock sl (midiCallbackLock);
  859. for (auto& mc : midiCallbacks)
  860. if (mc.deviceIdentifier.isEmpty() || mc.deviceIdentifier == source->getIdentifier())
  861. mc.callback->handleIncomingMidiMessage (source, message);
  862. }
  863. }
  864. //==============================================================================
  865. void AudioDeviceManager::setDefaultMidiOutputDevice (const String& identifier)
  866. {
  867. if (defaultMidiOutputDeviceInfo.identifier != identifier)
  868. {
  869. std::unique_ptr<MidiOutput> oldMidiPort;
  870. Array<AudioIODeviceCallback*> oldCallbacks;
  871. {
  872. const ScopedLock sl (audioCallbackLock);
  873. oldCallbacks.swapWith (callbacks);
  874. }
  875. if (currentAudioDevice != nullptr)
  876. for (int i = oldCallbacks.size(); --i >= 0;)
  877. oldCallbacks.getUnchecked (i)->audioDeviceStopped();
  878. std::swap (oldMidiPort, defaultMidiOutput);
  879. if (identifier.isNotEmpty())
  880. defaultMidiOutput = MidiOutput::openDevice (identifier);
  881. if (defaultMidiOutput != nullptr)
  882. defaultMidiOutputDeviceInfo = defaultMidiOutput->getDeviceInfo();
  883. else
  884. defaultMidiOutputDeviceInfo = {};
  885. if (currentAudioDevice != nullptr)
  886. for (auto* c : oldCallbacks)
  887. c->audioDeviceAboutToStart (currentAudioDevice.get());
  888. {
  889. const ScopedLock sl (audioCallbackLock);
  890. oldCallbacks.swapWith (callbacks);
  891. }
  892. updateXml();
  893. sendSynchronousChangeMessage();
  894. }
  895. }
  896. //==============================================================================
  897. AudioDeviceManager::LevelMeter::LevelMeter() noexcept : level() {}
  898. void AudioDeviceManager::LevelMeter::updateLevel (const float* const* channelData, int numChannels, int numSamples) noexcept
  899. {
  900. if (getReferenceCount() <= 1)
  901. return;
  902. auto localLevel = level.get();
  903. if (numChannels > 0)
  904. {
  905. for (int j = 0; j < numSamples; ++j)
  906. {
  907. float s = 0;
  908. for (int i = 0; i < numChannels; ++i)
  909. s += std::abs (channelData[i][j]);
  910. s /= (float) numChannels;
  911. const float decayFactor = 0.99992f;
  912. if (s > localLevel)
  913. localLevel = s;
  914. else if (localLevel > 0.001f)
  915. localLevel *= decayFactor;
  916. else
  917. localLevel = 0;
  918. }
  919. }
  920. else
  921. {
  922. localLevel = 0;
  923. }
  924. level = localLevel;
  925. }
  926. double AudioDeviceManager::LevelMeter::getCurrentLevel() const noexcept
  927. {
  928. jassert (getReferenceCount() > 1);
  929. return level.get();
  930. }
  931. void AudioDeviceManager::playTestSound()
  932. {
  933. { // cunningly nested to swap, unlock and delete in that order.
  934. std::unique_ptr<AudioBuffer<float>> oldSound;
  935. {
  936. const ScopedLock sl (audioCallbackLock);
  937. std::swap (oldSound, testSound);
  938. }
  939. }
  940. testSoundPosition = 0;
  941. if (currentAudioDevice != nullptr)
  942. {
  943. auto sampleRate = currentAudioDevice->getCurrentSampleRate();
  944. auto soundLength = (int) sampleRate;
  945. double frequency = 440.0;
  946. float amplitude = 0.5f;
  947. auto phasePerSample = MathConstants<double>::twoPi / (sampleRate / frequency);
  948. std::unique_ptr<AudioBuffer<float>> newSound (new AudioBuffer<float> (1, soundLength));
  949. for (int i = 0; i < soundLength; ++i)
  950. newSound->setSample (0, i, amplitude * (float) std::sin (i * phasePerSample));
  951. newSound->applyGainRamp (0, 0, soundLength / 10, 0.0f, 1.0f);
  952. newSound->applyGainRamp (0, soundLength - soundLength / 4, soundLength / 4, 1.0f, 0.0f);
  953. {
  954. const ScopedLock sl (audioCallbackLock);
  955. std::swap (testSound, newSound);
  956. }
  957. }
  958. }
  959. int AudioDeviceManager::getXRunCount() const noexcept
  960. {
  961. auto deviceXRuns = (currentAudioDevice != nullptr ? currentAudioDevice->getXRunCount() : -1);
  962. return jmax (0, deviceXRuns) + loadMeasurer.getXRunCount();
  963. }
  964. //==============================================================================
  965. // Deprecated
  966. void AudioDeviceManager::setMidiInputEnabled (const String& name, const bool enabled)
  967. {
  968. for (auto& device : MidiInput::getAvailableDevices())
  969. {
  970. if (device.name == name)
  971. {
  972. setMidiInputDeviceEnabled (device.identifier, enabled);
  973. return;
  974. }
  975. }
  976. }
  977. bool AudioDeviceManager::isMidiInputEnabled (const String& name) const
  978. {
  979. for (auto& device : MidiInput::getAvailableDevices())
  980. if (device.name == name)
  981. return isMidiInputDeviceEnabled (device.identifier);
  982. return false;
  983. }
  984. void AudioDeviceManager::addMidiInputCallback (const String& name, MidiInputCallback* callbackToAdd)
  985. {
  986. if (name.isEmpty())
  987. {
  988. addMidiInputDeviceCallback ({}, callbackToAdd);
  989. }
  990. else
  991. {
  992. for (auto& device : MidiInput::getAvailableDevices())
  993. {
  994. if (device.name == name)
  995. {
  996. addMidiInputDeviceCallback (device.identifier, callbackToAdd);
  997. return;
  998. }
  999. }
  1000. }
  1001. }
  1002. void AudioDeviceManager::removeMidiInputCallback (const String& name, MidiInputCallback* callbackToRemove)
  1003. {
  1004. if (name.isEmpty())
  1005. {
  1006. removeMidiInputDeviceCallback ({}, callbackToRemove);
  1007. }
  1008. else
  1009. {
  1010. for (auto& device : MidiInput::getAvailableDevices())
  1011. {
  1012. if (device.name == name)
  1013. {
  1014. removeMidiInputDeviceCallback (device.identifier, callbackToRemove);
  1015. return;
  1016. }
  1017. }
  1018. }
  1019. }
  1020. void AudioDeviceManager::setDefaultMidiOutput (const String& name)
  1021. {
  1022. for (auto& device : MidiOutput::getAvailableDevices())
  1023. {
  1024. if (device.name == name)
  1025. {
  1026. setDefaultMidiOutputDevice (device.identifier);
  1027. return;
  1028. }
  1029. }
  1030. }
  1031. //==============================================================================
  1032. //==============================================================================
  1033. #if JUCE_UNIT_TESTS
  1034. class AudioDeviceManagerTests : public UnitTest
  1035. {
  1036. public:
  1037. AudioDeviceManagerTests() : UnitTest ("AudioDeviceManager", UnitTestCategories::audio) {}
  1038. void runTest() override
  1039. {
  1040. beginTest ("When the AudioDeviceSetup has non-empty device names, initialise uses the requested devices");
  1041. {
  1042. AudioDeviceManager manager;
  1043. initialiseManager (manager);
  1044. expectEquals (manager.getAvailableDeviceTypes().size(), 2);
  1045. AudioDeviceManager::AudioDeviceSetup setup;
  1046. setup.outputDeviceName = "z";
  1047. setup.inputDeviceName = "c";
  1048. expect (manager.initialise (2, 2, nullptr, true, String{}, &setup).isEmpty());
  1049. const auto& newSetup = manager.getAudioDeviceSetup();
  1050. expectEquals (newSetup.outputDeviceName, setup.outputDeviceName);
  1051. expectEquals (newSetup.inputDeviceName, setup.inputDeviceName);
  1052. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1053. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 2);
  1054. }
  1055. beginTest ("When the AudioDeviceSetup has empty device names, initialise picks suitable default devices");
  1056. {
  1057. AudioDeviceManager manager;
  1058. initialiseManager (manager);
  1059. AudioDeviceManager::AudioDeviceSetup setup;
  1060. expect (manager.initialise (2, 2, nullptr, true, String{}, &setup).isEmpty());
  1061. const auto& newSetup = manager.getAudioDeviceSetup();
  1062. expectEquals (newSetup.outputDeviceName, String ("x"));
  1063. expectEquals (newSetup.inputDeviceName, String ("a"));
  1064. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1065. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 2);
  1066. }
  1067. beginTest ("When the preferred device name matches an input and an output on the same type, that type is used");
  1068. {
  1069. AudioDeviceManager manager;
  1070. initialiseManagerWithDifferentDeviceNames (manager);
  1071. expect (manager.initialise (2, 2, nullptr, true, "bar *").isEmpty());
  1072. expectEquals (manager.getCurrentAudioDeviceType(), String ("bar"));
  1073. const auto& newSetup = manager.getAudioDeviceSetup();
  1074. expectEquals (newSetup.outputDeviceName, String ("bar out a"));
  1075. expectEquals (newSetup.inputDeviceName, String ("bar in a"));
  1076. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1077. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 2);
  1078. expect (manager.getCurrentAudioDevice() != nullptr);
  1079. }
  1080. beginTest ("When the preferred device name matches either an input and an output, but not both, that type is used");
  1081. {
  1082. AudioDeviceManager manager;
  1083. initialiseManagerWithDifferentDeviceNames (manager);
  1084. expect (manager.initialise (2, 2, nullptr, true, "bar out b").isEmpty());
  1085. expectEquals (manager.getCurrentAudioDeviceType(), String ("bar"));
  1086. const auto& newSetup = manager.getAudioDeviceSetup();
  1087. expectEquals (newSetup.outputDeviceName, String ("bar out b"));
  1088. expectEquals (newSetup.inputDeviceName, String ("bar in a"));
  1089. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1090. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 2);
  1091. expect (manager.getCurrentAudioDevice() != nullptr);
  1092. }
  1093. beginTest ("When the preferred device name does not match any inputs or outputs, defaults are used");
  1094. {
  1095. AudioDeviceManager manager;
  1096. initialiseManagerWithDifferentDeviceNames (manager);
  1097. expect (manager.initialise (2, 2, nullptr, true, "unmatchable").isEmpty());
  1098. expectEquals (manager.getCurrentAudioDeviceType(), String ("foo"));
  1099. const auto& newSetup = manager.getAudioDeviceSetup();
  1100. expectEquals (newSetup.outputDeviceName, String ("foo out a"));
  1101. expectEquals (newSetup.inputDeviceName, String ("foo in a"));
  1102. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1103. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 2);
  1104. expect (manager.getCurrentAudioDevice() != nullptr);
  1105. }
  1106. beginTest ("When first device type has no devices, a device type with devices is used instead");
  1107. {
  1108. AudioDeviceManager manager;
  1109. initialiseManagerWithEmptyDeviceType (manager);
  1110. AudioDeviceManager::AudioDeviceSetup setup;
  1111. expect (manager.initialise (2, 2, nullptr, true, {}, &setup).isEmpty());
  1112. const auto& newSetup = manager.getAudioDeviceSetup();
  1113. expectEquals (newSetup.outputDeviceName, String ("x"));
  1114. expectEquals (newSetup.inputDeviceName, String ("a"));
  1115. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1116. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 2);
  1117. }
  1118. beginTest ("If a device type has been explicitly set to a type with devices, "
  1119. "initialisation should respect this choice");
  1120. {
  1121. AudioDeviceManager manager;
  1122. initialiseManagerWithEmptyDeviceType (manager);
  1123. manager.setCurrentAudioDeviceType (mockBName, true);
  1124. AudioDeviceManager::AudioDeviceSetup setup;
  1125. expect (manager.initialise (2, 2, nullptr, true, {}, &setup).isEmpty());
  1126. expectEquals (manager.getCurrentAudioDeviceType(), mockBName);
  1127. }
  1128. beginTest ("If a device type has been explicitly set to a type without devices, "
  1129. "initialisation should pick a type with devices instead");
  1130. {
  1131. AudioDeviceManager manager;
  1132. initialiseManagerWithEmptyDeviceType (manager);
  1133. manager.setCurrentAudioDeviceType (emptyName, true);
  1134. AudioDeviceManager::AudioDeviceSetup setup;
  1135. expect (manager.initialise (2, 2, nullptr, true, {}, &setup).isEmpty());
  1136. expectEquals (manager.getCurrentAudioDeviceType(), mockAName);
  1137. }
  1138. beginTest ("Carry out a long sequence of configuration changes");
  1139. {
  1140. AudioDeviceManager manager;
  1141. initialiseManagerWithEmptyDeviceType (manager);
  1142. initialiseWithDefaultDevices (manager);
  1143. disableInputChannelsButLeaveDeviceOpen (manager);
  1144. selectANewInputDevice (manager);
  1145. disableInputDevice (manager);
  1146. reenableInputDeviceWithNoChannels (manager);
  1147. enableInputChannels (manager);
  1148. disableInputChannelsButLeaveDeviceOpen (manager);
  1149. switchDeviceType (manager);
  1150. enableInputChannels (manager);
  1151. closeDeviceByRequestingEmptyNames (manager);
  1152. }
  1153. beginTest ("AudioDeviceManager updates its current settings before notifying callbacks when device restarts itself");
  1154. {
  1155. AudioDeviceManager manager;
  1156. auto deviceType = std::make_unique<MockDeviceType> ("foo",
  1157. StringArray { "foo in a", "foo in b" },
  1158. StringArray { "foo out a", "foo out b" });
  1159. auto* ptr = deviceType.get();
  1160. manager.addAudioDeviceType (std::move (deviceType));
  1161. AudioDeviceManager::AudioDeviceSetup setup;
  1162. setup.sampleRate = 48000.0;
  1163. setup.bufferSize = 256;
  1164. setup.inputDeviceName = "foo in a";
  1165. setup.outputDeviceName = "foo out a";
  1166. setup.useDefaultInputChannels = true;
  1167. setup.useDefaultOutputChannels = true;
  1168. manager.setAudioDeviceSetup (setup, true);
  1169. const auto currentSetup = manager.getAudioDeviceSetup();
  1170. expectEquals (currentSetup.sampleRate, setup.sampleRate);
  1171. expectEquals (currentSetup.bufferSize, setup.bufferSize);
  1172. MockCallback callback;
  1173. manager.addAudioCallback (&callback);
  1174. constexpr auto newSr = 10000.0;
  1175. constexpr auto newBs = 1024;
  1176. auto numCalls = 0;
  1177. // Compilers disagree about whether newSr and newBs need to be captured
  1178. callback.aboutToStart = [&]
  1179. {
  1180. ++numCalls;
  1181. const auto current = manager.getAudioDeviceSetup();
  1182. expectEquals (current.sampleRate, newSr);
  1183. expectEquals (current.bufferSize, newBs);
  1184. };
  1185. ptr->restartDevices (newSr, newBs);
  1186. expectEquals (numCalls, 1);
  1187. }
  1188. }
  1189. private:
  1190. void initialiseWithDefaultDevices (AudioDeviceManager& manager)
  1191. {
  1192. manager.initialiseWithDefaultDevices (2, 2);
  1193. const auto& setup = manager.getAudioDeviceSetup();
  1194. expectEquals (setup.inputChannels.countNumberOfSetBits(), 2);
  1195. expectEquals (setup.outputChannels.countNumberOfSetBits(), 2);
  1196. expect (setup.useDefaultInputChannels);
  1197. expect (setup.useDefaultOutputChannels);
  1198. expect (manager.getCurrentAudioDevice() != nullptr);
  1199. }
  1200. void disableInputChannelsButLeaveDeviceOpen (AudioDeviceManager& manager)
  1201. {
  1202. auto setup = manager.getAudioDeviceSetup();
  1203. setup.inputChannels.clear();
  1204. setup.useDefaultInputChannels = false;
  1205. expect (manager.setAudioDeviceSetup (setup, true).isEmpty());
  1206. const auto newSetup = manager.getAudioDeviceSetup();
  1207. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 0);
  1208. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1209. expect (! newSetup.useDefaultInputChannels);
  1210. expect (newSetup.useDefaultOutputChannels);
  1211. expectEquals (newSetup.inputDeviceName, setup.inputDeviceName);
  1212. expectEquals (newSetup.outputDeviceName, setup.outputDeviceName);
  1213. expect (manager.getCurrentAudioDevice() != nullptr);
  1214. }
  1215. void selectANewInputDevice (AudioDeviceManager& manager)
  1216. {
  1217. auto setup = manager.getAudioDeviceSetup();
  1218. setup.inputDeviceName = "b";
  1219. expect (manager.setAudioDeviceSetup (setup, true).isEmpty());
  1220. const auto newSetup = manager.getAudioDeviceSetup();
  1221. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 0);
  1222. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1223. expect (! newSetup.useDefaultInputChannels);
  1224. expect (newSetup.useDefaultOutputChannels);
  1225. expectEquals (newSetup.inputDeviceName, setup.inputDeviceName);
  1226. expectEquals (newSetup.outputDeviceName, setup.outputDeviceName);
  1227. expect (manager.getCurrentAudioDevice() != nullptr);
  1228. }
  1229. void disableInputDevice (AudioDeviceManager& manager)
  1230. {
  1231. auto setup = manager.getAudioDeviceSetup();
  1232. setup.inputDeviceName = "";
  1233. expect (manager.setAudioDeviceSetup (setup, true).isEmpty());
  1234. const auto newSetup = manager.getAudioDeviceSetup();
  1235. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 0);
  1236. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1237. expect (! newSetup.useDefaultInputChannels);
  1238. expect (newSetup.useDefaultOutputChannels);
  1239. expectEquals (newSetup.inputDeviceName, setup.inputDeviceName);
  1240. expectEquals (newSetup.outputDeviceName, setup.outputDeviceName);
  1241. expect (manager.getCurrentAudioDevice() != nullptr);
  1242. }
  1243. void reenableInputDeviceWithNoChannels (AudioDeviceManager& manager)
  1244. {
  1245. auto setup = manager.getAudioDeviceSetup();
  1246. setup.inputDeviceName = "a";
  1247. expect (manager.setAudioDeviceSetup (setup, true).isEmpty());
  1248. const auto newSetup = manager.getAudioDeviceSetup();
  1249. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 0);
  1250. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1251. expect (! newSetup.useDefaultInputChannels);
  1252. expect (newSetup.useDefaultOutputChannels);
  1253. expectEquals (newSetup.inputDeviceName, setup.inputDeviceName);
  1254. expectEquals (newSetup.outputDeviceName, setup.outputDeviceName);
  1255. expect (manager.getCurrentAudioDevice() != nullptr);
  1256. }
  1257. void enableInputChannels (AudioDeviceManager& manager)
  1258. {
  1259. auto setup = manager.getAudioDeviceSetup();
  1260. setup.inputDeviceName = manager.getCurrentDeviceTypeObject()->getDeviceNames (true)[0];
  1261. setup.inputChannels = 3;
  1262. setup.useDefaultInputChannels = false;
  1263. expect (manager.setAudioDeviceSetup (setup, true).isEmpty());
  1264. const auto newSetup = manager.getAudioDeviceSetup();
  1265. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 2);
  1266. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1267. expect (! newSetup.useDefaultInputChannels);
  1268. expect (newSetup.useDefaultOutputChannels);
  1269. expectEquals (newSetup.inputDeviceName, setup.inputDeviceName);
  1270. expectEquals (newSetup.outputDeviceName, setup.outputDeviceName);
  1271. expect (manager.getCurrentAudioDevice() != nullptr);
  1272. }
  1273. void switchDeviceType (AudioDeviceManager& manager)
  1274. {
  1275. const auto oldSetup = manager.getAudioDeviceSetup();
  1276. expectEquals (manager.getCurrentAudioDeviceType(), String (mockAName));
  1277. manager.setCurrentAudioDeviceType (mockBName, true);
  1278. expectEquals (manager.getCurrentAudioDeviceType(), String (mockBName));
  1279. const auto newSetup = manager.getAudioDeviceSetup();
  1280. expect (newSetup.outputDeviceName.isNotEmpty());
  1281. // We had no channels enabled, which means we don't need to open a new input device
  1282. expect (newSetup.inputDeviceName.isEmpty());
  1283. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 0);
  1284. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1285. expect (manager.getCurrentAudioDevice() != nullptr);
  1286. }
  1287. void closeDeviceByRequestingEmptyNames (AudioDeviceManager& manager)
  1288. {
  1289. auto setup = manager.getAudioDeviceSetup();
  1290. setup.inputDeviceName = "";
  1291. setup.outputDeviceName = "";
  1292. expect (manager.setAudioDeviceSetup (setup, true).isEmpty());
  1293. const auto newSetup = manager.getAudioDeviceSetup();
  1294. expectEquals (newSetup.inputChannels.countNumberOfSetBits(), 2);
  1295. expectEquals (newSetup.outputChannels.countNumberOfSetBits(), 2);
  1296. expect (newSetup.inputDeviceName.isEmpty());
  1297. expect (newSetup.outputDeviceName.isEmpty());
  1298. expect (manager.getCurrentAudioDevice() == nullptr);
  1299. }
  1300. const String mockAName = "mockA";
  1301. const String mockBName = "mockB";
  1302. const String emptyName = "empty";
  1303. struct Restartable
  1304. {
  1305. virtual ~Restartable() = default;
  1306. virtual void restart (double newSr, int newBs) = 0;
  1307. };
  1308. class MockDevice : public AudioIODevice,
  1309. private Restartable
  1310. {
  1311. public:
  1312. MockDevice (ListenerList<Restartable>& l, String typeNameIn, String outNameIn, String inNameIn)
  1313. : AudioIODevice ("mock", typeNameIn), listeners (l), outName (outNameIn), inName (inNameIn)
  1314. {
  1315. listeners.add (this);
  1316. }
  1317. ~MockDevice() override
  1318. {
  1319. listeners.remove (this);
  1320. }
  1321. StringArray getOutputChannelNames() override { return { "o1", "o2", "o3" }; }
  1322. StringArray getInputChannelNames() override { return { "i1", "i2", "i3" }; }
  1323. Array<double> getAvailableSampleRates() override { return { 44100.0, 48000.0 }; }
  1324. Array<int> getAvailableBufferSizes() override { return { 128, 256 }; }
  1325. int getDefaultBufferSize() override { return 128; }
  1326. String open (const BigInteger& inputs, const BigInteger& outputs, double sr, int bs) override
  1327. {
  1328. inChannels = inputs;
  1329. outChannels = outputs;
  1330. sampleRate = sr;
  1331. blockSize = bs;
  1332. on = true;
  1333. return {};
  1334. }
  1335. void close() override { on = false; }
  1336. bool isOpen() override { return on; }
  1337. void start (AudioIODeviceCallback* c) override
  1338. {
  1339. callback = c;
  1340. callback->audioDeviceAboutToStart (this);
  1341. playing = true;
  1342. }
  1343. void stop() override
  1344. {
  1345. playing = false;
  1346. callback->audioDeviceStopped();
  1347. }
  1348. bool isPlaying() override { return playing; }
  1349. String getLastError() override { return {}; }
  1350. int getCurrentBufferSizeSamples() override { return blockSize; }
  1351. double getCurrentSampleRate() override { return sampleRate; }
  1352. int getCurrentBitDepth() override { return 16; }
  1353. BigInteger getActiveOutputChannels() const override { return outChannels; }
  1354. BigInteger getActiveInputChannels() const override { return inChannels; }
  1355. int getOutputLatencyInSamples() override { return 0; }
  1356. int getInputLatencyInSamples() override { return 0; }
  1357. private:
  1358. void restart (double newSr, int newBs) override
  1359. {
  1360. stop();
  1361. close();
  1362. open (inChannels, outChannels, newSr, newBs);
  1363. start (callback);
  1364. }
  1365. ListenerList<Restartable>& listeners;
  1366. AudioIODeviceCallback* callback = nullptr;
  1367. String outName, inName;
  1368. BigInteger outChannels, inChannels;
  1369. double sampleRate = 0.0;
  1370. int blockSize = 0;
  1371. bool on = false, playing = false;
  1372. };
  1373. class MockDeviceType : public AudioIODeviceType
  1374. {
  1375. public:
  1376. explicit MockDeviceType (String kind)
  1377. : MockDeviceType (std::move (kind), { "a", "b", "c" }, { "x", "y", "z" }) {}
  1378. MockDeviceType (String kind, StringArray inputNames, StringArray outputNames)
  1379. : AudioIODeviceType (std::move (kind)),
  1380. inNames (std::move (inputNames)),
  1381. outNames (std::move (outputNames)) {}
  1382. ~MockDeviceType() override
  1383. {
  1384. // A Device outlived its DeviceType!
  1385. jassert (listeners.isEmpty());
  1386. }
  1387. void scanForDevices() override {}
  1388. StringArray getDeviceNames (bool isInput) const override
  1389. {
  1390. return getNames (isInput);
  1391. }
  1392. int getDefaultDeviceIndex (bool) const override { return 0; }
  1393. int getIndexOfDevice (AudioIODevice* device, bool isInput) const override
  1394. {
  1395. return getNames (isInput).indexOf (device->getName());
  1396. }
  1397. bool hasSeparateInputsAndOutputs() const override { return true; }
  1398. AudioIODevice* createDevice (const String& outputName, const String& inputName) override
  1399. {
  1400. if (inNames.contains (inputName) || outNames.contains (outputName))
  1401. return new MockDevice (listeners, getTypeName(), outputName, inputName);
  1402. return nullptr;
  1403. }
  1404. // Call this to emulate the device restarting itself with new settings.
  1405. // This might happen e.g. when a user changes the ASIO settings.
  1406. void restartDevices (double newSr, int newBs)
  1407. {
  1408. listeners.call ([&] (auto& l) { return l.restart (newSr, newBs); });
  1409. }
  1410. private:
  1411. const StringArray& getNames (bool isInput) const { return isInput ? inNames : outNames; }
  1412. const StringArray inNames, outNames;
  1413. ListenerList<Restartable> listeners;
  1414. };
  1415. class MockCallback : public AudioIODeviceCallback
  1416. {
  1417. public:
  1418. std::function<void()> callback;
  1419. std::function<void()> aboutToStart;
  1420. std::function<void()> stopped;
  1421. std::function<void()> error;
  1422. void audioDeviceIOCallback (const float**, int, float**, int, int) override { NullCheckedInvocation::invoke (callback); }
  1423. void audioDeviceAboutToStart (AudioIODevice*) override { NullCheckedInvocation::invoke (aboutToStart); }
  1424. void audioDeviceStopped() override { NullCheckedInvocation::invoke (stopped); }
  1425. void audioDeviceError (const String&) override { NullCheckedInvocation::invoke (error); }
  1426. };
  1427. void initialiseManager (AudioDeviceManager& manager)
  1428. {
  1429. manager.addAudioDeviceType (std::make_unique<MockDeviceType> (mockAName));
  1430. manager.addAudioDeviceType (std::make_unique<MockDeviceType> (mockBName));
  1431. }
  1432. void initialiseManagerWithEmptyDeviceType (AudioDeviceManager& manager)
  1433. {
  1434. manager.addAudioDeviceType (std::make_unique<MockDeviceType> (emptyName, StringArray{}, StringArray{}));
  1435. initialiseManager (manager);
  1436. }
  1437. void initialiseManagerWithDifferentDeviceNames (AudioDeviceManager& manager)
  1438. {
  1439. manager.addAudioDeviceType (std::make_unique<MockDeviceType> ("foo",
  1440. StringArray { "foo in a", "foo in b" },
  1441. StringArray { "foo out a", "foo out b" }));
  1442. manager.addAudioDeviceType (std::make_unique<MockDeviceType> ("bar",
  1443. StringArray { "bar in a", "bar in b" },
  1444. StringArray { "bar out a", "bar out b" }));
  1445. }
  1446. };
  1447. static AudioDeviceManagerTests audioDeviceManagerTests;
  1448. #endif
  1449. } // namespace juce