The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

998 lines
33KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. AudioDeviceManager::AudioDeviceSetup::AudioDeviceSetup()
  20. : sampleRate (0),
  21. bufferSize (0),
  22. useDefaultInputChannels (true),
  23. useDefaultOutputChannels (true)
  24. {
  25. }
  26. bool AudioDeviceManager::AudioDeviceSetup::operator== (const AudioDeviceManager::AudioDeviceSetup& other) const
  27. {
  28. return outputDeviceName == other.outputDeviceName
  29. && inputDeviceName == other.inputDeviceName
  30. && sampleRate == other.sampleRate
  31. && bufferSize == other.bufferSize
  32. && inputChannels == other.inputChannels
  33. && useDefaultInputChannels == other.useDefaultInputChannels
  34. && outputChannels == other.outputChannels
  35. && useDefaultOutputChannels == other.useDefaultOutputChannels;
  36. }
  37. bool AudioDeviceManager::AudioDeviceSetup::operator!= (const AudioDeviceManager::AudioDeviceSetup& other) const
  38. {
  39. return ! operator== (other);
  40. }
  41. //==============================================================================
  42. class AudioDeviceManager::CallbackHandler : public AudioIODeviceCallback,
  43. public MidiInputCallback,
  44. public AudioIODeviceType::Listener
  45. {
  46. public:
  47. CallbackHandler (AudioDeviceManager& adm) noexcept : owner (adm) {}
  48. private:
  49. void audioDeviceIOCallback (const float** ins, int numIns, float** outs, int numOuts, int numSamples) override
  50. {
  51. owner.audioDeviceIOCallbackInt (ins, numIns, outs, numOuts, numSamples);
  52. }
  53. void audioDeviceAboutToStart (AudioIODevice* device) override
  54. {
  55. owner.audioDeviceAboutToStartInt (device);
  56. }
  57. void audioDeviceStopped() override
  58. {
  59. owner.audioDeviceStoppedInt();
  60. }
  61. void audioDeviceError (const String& message) override
  62. {
  63. owner.audioDeviceErrorInt (message);
  64. }
  65. void handleIncomingMidiMessage (MidiInput* source, const MidiMessage& message) override
  66. {
  67. owner.handleIncomingMidiMessageInt (source, message);
  68. }
  69. void audioDeviceListChanged() override
  70. {
  71. owner.audioDeviceListChanged();
  72. }
  73. AudioDeviceManager& owner;
  74. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CallbackHandler)
  75. };
  76. //==============================================================================
  77. AudioDeviceManager::AudioDeviceManager()
  78. {
  79. callbackHandler.reset (new CallbackHandler (*this));
  80. }
  81. AudioDeviceManager::~AudioDeviceManager()
  82. {
  83. currentAudioDevice.reset();
  84. defaultMidiOutput.reset();
  85. }
  86. //==============================================================================
  87. void AudioDeviceManager::createDeviceTypesIfNeeded()
  88. {
  89. if (availableDeviceTypes.size() == 0)
  90. {
  91. OwnedArray<AudioIODeviceType> types;
  92. createAudioDeviceTypes (types);
  93. for (auto* t : types)
  94. addAudioDeviceType (t);
  95. types.clear (false);
  96. if (auto* first = availableDeviceTypes.getFirst())
  97. currentDeviceType = first->getTypeName();
  98. }
  99. }
  100. const OwnedArray<AudioIODeviceType>& AudioDeviceManager::getAvailableDeviceTypes()
  101. {
  102. scanDevicesIfNeeded();
  103. return availableDeviceTypes;
  104. }
  105. void AudioDeviceManager::audioDeviceListChanged()
  106. {
  107. if (currentAudioDevice != nullptr)
  108. {
  109. currentSetup.sampleRate = currentAudioDevice->getCurrentSampleRate();
  110. currentSetup.bufferSize = currentAudioDevice->getCurrentBufferSizeSamples();
  111. currentSetup.inputChannels = currentAudioDevice->getActiveInputChannels();
  112. currentSetup.outputChannels = currentAudioDevice->getActiveOutputChannels();
  113. }
  114. sendChangeMessage();
  115. }
  116. //==============================================================================
  117. static void addIfNotNull (OwnedArray<AudioIODeviceType>& list, AudioIODeviceType* const device)
  118. {
  119. if (device != nullptr)
  120. list.add (device);
  121. }
  122. void AudioDeviceManager::createAudioDeviceTypes (OwnedArray<AudioIODeviceType>& list)
  123. {
  124. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_WASAPI (false));
  125. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_WASAPI (true));
  126. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_DirectSound());
  127. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_ASIO());
  128. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_CoreAudio());
  129. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_iOSAudio());
  130. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_ALSA());
  131. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_JACK());
  132. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_OpenSLES());
  133. addIfNotNull (list, AudioIODeviceType::createAudioIODeviceType_Android());
  134. }
  135. void AudioDeviceManager::addAudioDeviceType (AudioIODeviceType* newDeviceType)
  136. {
  137. if (newDeviceType != nullptr)
  138. {
  139. jassert (lastDeviceTypeConfigs.size() == availableDeviceTypes.size());
  140. availableDeviceTypes.add (newDeviceType);
  141. lastDeviceTypeConfigs.add (new AudioDeviceSetup());
  142. newDeviceType->addListener (callbackHandler.get());
  143. }
  144. }
  145. static bool deviceListContains (AudioIODeviceType* type, bool isInput, const String& name)
  146. {
  147. for (auto& deviceName : type->getDeviceNames (isInput))
  148. if (deviceName.trim().equalsIgnoreCase (name.trim()))
  149. return true;
  150. return false;
  151. }
  152. //==============================================================================
  153. String AudioDeviceManager::initialise (const int numInputChannelsNeeded,
  154. const int numOutputChannelsNeeded,
  155. const XmlElement* const xml,
  156. const bool selectDefaultDeviceOnFailure,
  157. const String& preferredDefaultDeviceName,
  158. const AudioDeviceSetup* preferredSetupOptions)
  159. {
  160. scanDevicesIfNeeded();
  161. numInputChansNeeded = numInputChannelsNeeded;
  162. numOutputChansNeeded = numOutputChannelsNeeded;
  163. if (xml != nullptr && xml->hasTagName ("DEVICESETUP"))
  164. return initialiseFromXML (*xml, selectDefaultDeviceOnFailure,
  165. preferredDefaultDeviceName, preferredSetupOptions);
  166. return initialiseDefault (preferredDefaultDeviceName, preferredSetupOptions);
  167. }
  168. String AudioDeviceManager::initialiseDefault (const String& preferredDefaultDeviceName,
  169. const AudioDeviceSetup* preferredSetupOptions)
  170. {
  171. AudioDeviceSetup setup;
  172. if (preferredSetupOptions != nullptr)
  173. {
  174. setup = *preferredSetupOptions;
  175. }
  176. else if (preferredDefaultDeviceName.isNotEmpty())
  177. {
  178. for (auto* type : availableDeviceTypes)
  179. {
  180. for (auto& out : type->getDeviceNames (false))
  181. {
  182. if (out.matchesWildcard (preferredDefaultDeviceName, true))
  183. {
  184. setup.outputDeviceName = out;
  185. break;
  186. }
  187. }
  188. for (auto& in : type->getDeviceNames (true))
  189. {
  190. if (in.matchesWildcard (preferredDefaultDeviceName, true))
  191. {
  192. setup.inputDeviceName = in;
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. insertDefaultDeviceNames (setup);
  199. return setAudioDeviceSetup (setup, false);
  200. }
  201. String AudioDeviceManager::initialiseFromXML (const XmlElement& xml,
  202. bool selectDefaultDeviceOnFailure,
  203. const String& preferredDefaultDeviceName,
  204. const AudioDeviceSetup* preferredSetupOptions)
  205. {
  206. lastExplicitSettings.reset (new XmlElement (xml));
  207. String error;
  208. AudioDeviceSetup setup;
  209. if (preferredSetupOptions != nullptr)
  210. setup = *preferredSetupOptions;
  211. if (xml.getStringAttribute ("audioDeviceName").isNotEmpty())
  212. {
  213. setup.inputDeviceName = setup.outputDeviceName
  214. = xml.getStringAttribute ("audioDeviceName");
  215. }
  216. else
  217. {
  218. setup.inputDeviceName = xml.getStringAttribute ("audioInputDeviceName");
  219. setup.outputDeviceName = xml.getStringAttribute ("audioOutputDeviceName");
  220. }
  221. currentDeviceType = xml.getStringAttribute ("deviceType");
  222. if (findType (currentDeviceType) == nullptr)
  223. {
  224. if (auto* type = findType (setup.inputDeviceName, setup.outputDeviceName))
  225. currentDeviceType = type->getTypeName();
  226. else if (auto* firstType = availableDeviceTypes.getFirst())
  227. currentDeviceType = firstType->getTypeName();
  228. }
  229. setup.bufferSize = xml.getIntAttribute ("audioDeviceBufferSize", setup.bufferSize);
  230. setup.sampleRate = xml.getDoubleAttribute ("audioDeviceRate", setup.sampleRate);
  231. setup.inputChannels .parseString (xml.getStringAttribute ("audioDeviceInChans", "11"), 2);
  232. setup.outputChannels.parseString (xml.getStringAttribute ("audioDeviceOutChans", "11"), 2);
  233. setup.useDefaultInputChannels = ! xml.hasAttribute ("audioDeviceInChans");
  234. setup.useDefaultOutputChannels = ! xml.hasAttribute ("audioDeviceOutChans");
  235. error = setAudioDeviceSetup (setup, true);
  236. midiInsFromXml.clear();
  237. forEachXmlChildElementWithTagName (xml, c, "MIDIINPUT")
  238. midiInsFromXml.add (c->getStringAttribute ("name"));
  239. for (auto& m : MidiInput::getDevices())
  240. setMidiInputEnabled (m, midiInsFromXml.contains (m));
  241. if (error.isNotEmpty() && selectDefaultDeviceOnFailure)
  242. error = initialise (numInputChansNeeded, numOutputChansNeeded,
  243. nullptr, false, preferredDefaultDeviceName);
  244. setDefaultMidiOutput (xml.getStringAttribute ("defaultMidiOutput"));
  245. return error;
  246. }
  247. String AudioDeviceManager::initialiseWithDefaultDevices (int numInputChannelsNeeded,
  248. int numOutputChannelsNeeded)
  249. {
  250. lastExplicitSettings.reset();
  251. return initialise (numInputChannelsNeeded, numOutputChannelsNeeded,
  252. nullptr, false, {}, nullptr);
  253. }
  254. void AudioDeviceManager::insertDefaultDeviceNames (AudioDeviceSetup& setup) const
  255. {
  256. if (auto* type = getCurrentDeviceTypeObject())
  257. {
  258. if (setup.outputDeviceName.isEmpty())
  259. setup.outputDeviceName = type->getDeviceNames (false) [type->getDefaultDeviceIndex (false)];
  260. if (setup.inputDeviceName.isEmpty())
  261. setup.inputDeviceName = type->getDeviceNames (true) [type->getDefaultDeviceIndex (true)];
  262. }
  263. }
  264. XmlElement* AudioDeviceManager::createStateXml() const
  265. {
  266. return createCopyIfNotNull (lastExplicitSettings.get());
  267. }
  268. //==============================================================================
  269. void AudioDeviceManager::scanDevicesIfNeeded()
  270. {
  271. if (listNeedsScanning)
  272. {
  273. listNeedsScanning = false;
  274. createDeviceTypesIfNeeded();
  275. for (auto* type : availableDeviceTypes)
  276. type->scanForDevices();
  277. }
  278. }
  279. AudioIODeviceType* AudioDeviceManager::findType (const String& typeName)
  280. {
  281. scanDevicesIfNeeded();
  282. for (auto* type : availableDeviceTypes)
  283. if (type->getTypeName() == typeName)
  284. return type;
  285. return {};
  286. }
  287. AudioIODeviceType* AudioDeviceManager::findType (const String& inputName, const String& outputName)
  288. {
  289. scanDevicesIfNeeded();
  290. for (auto* type : availableDeviceTypes)
  291. if ((inputName.isNotEmpty() && deviceListContains (type, true, inputName))
  292. || (outputName.isNotEmpty() && deviceListContains (type, false, outputName)))
  293. return type;
  294. return {};
  295. }
  296. void AudioDeviceManager::getAudioDeviceSetup (AudioDeviceSetup& setup) const
  297. {
  298. setup = currentSetup;
  299. }
  300. void AudioDeviceManager::deleteCurrentDevice()
  301. {
  302. currentAudioDevice.reset();
  303. currentSetup.inputDeviceName.clear();
  304. currentSetup.outputDeviceName.clear();
  305. }
  306. void AudioDeviceManager::setCurrentAudioDeviceType (const String& type, bool treatAsChosenDevice)
  307. {
  308. for (int i = 0; i < availableDeviceTypes.size(); ++i)
  309. {
  310. if (availableDeviceTypes.getUnchecked(i)->getTypeName() == type
  311. && currentDeviceType != type)
  312. {
  313. if (currentAudioDevice != nullptr)
  314. {
  315. closeAudioDevice();
  316. Thread::sleep (1500); // allow a moment for OS devices to sort themselves out, to help
  317. // avoid things like DirectSound/ASIO clashes
  318. }
  319. currentDeviceType = type;
  320. AudioDeviceSetup s (*lastDeviceTypeConfigs.getUnchecked(i));
  321. insertDefaultDeviceNames (s);
  322. setAudioDeviceSetup (s, treatAsChosenDevice);
  323. sendChangeMessage();
  324. break;
  325. }
  326. }
  327. }
  328. AudioIODeviceType* AudioDeviceManager::getCurrentDeviceTypeObject() const
  329. {
  330. for (auto* type : availableDeviceTypes)
  331. if (type->getTypeName() == currentDeviceType)
  332. return type;
  333. return availableDeviceTypes.getFirst();
  334. }
  335. String AudioDeviceManager::setAudioDeviceSetup (const AudioDeviceSetup& newSetup,
  336. bool treatAsChosenDevice)
  337. {
  338. jassert (&newSetup != &currentSetup); // this will have no effect
  339. if (newSetup == currentSetup && currentAudioDevice != nullptr)
  340. return {};
  341. if (! (newSetup == currentSetup))
  342. sendChangeMessage();
  343. stopDevice();
  344. auto newInputDeviceName (numInputChansNeeded == 0 ? String() : newSetup.inputDeviceName);
  345. auto newOutputDeviceName (numOutputChansNeeded == 0 ? String() : newSetup.outputDeviceName);
  346. String error;
  347. auto* type = getCurrentDeviceTypeObject();
  348. if (type == nullptr || (newInputDeviceName.isEmpty() && newOutputDeviceName.isEmpty()))
  349. {
  350. deleteCurrentDevice();
  351. if (treatAsChosenDevice)
  352. updateXml();
  353. return {};
  354. }
  355. if (currentSetup.inputDeviceName != newInputDeviceName
  356. || currentSetup.outputDeviceName != newOutputDeviceName
  357. || currentAudioDevice == nullptr)
  358. {
  359. deleteCurrentDevice();
  360. scanDevicesIfNeeded();
  361. if (newOutputDeviceName.isNotEmpty() && ! deviceListContains (type, false, newOutputDeviceName))
  362. return "No such device: " + newOutputDeviceName;
  363. if (newInputDeviceName.isNotEmpty() && ! deviceListContains (type, true, newInputDeviceName))
  364. return "No such device: " + newInputDeviceName;
  365. currentAudioDevice.reset (type->createDevice (newOutputDeviceName, newInputDeviceName));
  366. if (currentAudioDevice == nullptr)
  367. error = "Can't open the audio device!\n\n"
  368. "This may be because another application is currently using the same device - "
  369. "if so, you should close any other applications and try again!";
  370. else
  371. error = currentAudioDevice->getLastError();
  372. if (error.isNotEmpty())
  373. {
  374. deleteCurrentDevice();
  375. return error;
  376. }
  377. if (newSetup.useDefaultInputChannels)
  378. {
  379. inputChannels.clear();
  380. inputChannels.setRange (0, numInputChansNeeded, true);
  381. }
  382. if (newSetup.useDefaultOutputChannels)
  383. {
  384. outputChannels.clear();
  385. outputChannels.setRange (0, numOutputChansNeeded, true);
  386. }
  387. if (newInputDeviceName.isEmpty()) inputChannels.clear();
  388. if (newOutputDeviceName.isEmpty()) outputChannels.clear();
  389. }
  390. if (! newSetup.useDefaultInputChannels) inputChannels = newSetup.inputChannels;
  391. if (! newSetup.useDefaultOutputChannels) outputChannels = newSetup.outputChannels;
  392. currentSetup = newSetup;
  393. currentSetup.sampleRate = chooseBestSampleRate (newSetup.sampleRate);
  394. currentSetup.bufferSize = chooseBestBufferSize (newSetup.bufferSize);
  395. error = currentAudioDevice->open (inputChannels,
  396. outputChannels,
  397. currentSetup.sampleRate,
  398. currentSetup.bufferSize);
  399. if (error.isEmpty())
  400. {
  401. currentDeviceType = currentAudioDevice->getTypeName();
  402. currentAudioDevice->start (callbackHandler.get());
  403. currentSetup.sampleRate = currentAudioDevice->getCurrentSampleRate();
  404. currentSetup.bufferSize = currentAudioDevice->getCurrentBufferSizeSamples();
  405. currentSetup.inputChannels = currentAudioDevice->getActiveInputChannels();
  406. currentSetup.outputChannels = currentAudioDevice->getActiveOutputChannels();
  407. for (int i = 0; i < availableDeviceTypes.size(); ++i)
  408. if (availableDeviceTypes.getUnchecked (i)->getTypeName() == currentDeviceType)
  409. *(lastDeviceTypeConfigs.getUnchecked (i)) = currentSetup;
  410. if (treatAsChosenDevice)
  411. updateXml();
  412. }
  413. else
  414. {
  415. deleteCurrentDevice();
  416. }
  417. return error;
  418. }
  419. double AudioDeviceManager::chooseBestSampleRate (double rate) const
  420. {
  421. jassert (currentAudioDevice != nullptr);
  422. auto rates = currentAudioDevice->getAvailableSampleRates();
  423. if (rate > 0 && rates.contains (rate))
  424. return rate;
  425. rate = currentAudioDevice->getCurrentSampleRate();
  426. if (rate > 0 && rates.contains (rate))
  427. return rate;
  428. double lowestAbove44 = 0.0;
  429. for (int i = rates.size(); --i >= 0;)
  430. {
  431. auto sr = rates[i];
  432. if (sr >= 44100.0 && (lowestAbove44 < 1.0 || sr < lowestAbove44))
  433. lowestAbove44 = sr;
  434. }
  435. if (lowestAbove44 > 0.0)
  436. return lowestAbove44;
  437. return rates[0];
  438. }
  439. int AudioDeviceManager::chooseBestBufferSize (int bufferSize) const
  440. {
  441. jassert (currentAudioDevice != nullptr);
  442. if (bufferSize > 0 && currentAudioDevice->getAvailableBufferSizes().contains (bufferSize))
  443. return bufferSize;
  444. return currentAudioDevice->getDefaultBufferSize();
  445. }
  446. void AudioDeviceManager::stopDevice()
  447. {
  448. if (currentAudioDevice != nullptr)
  449. currentAudioDevice->stop();
  450. testSound.reset();
  451. }
  452. void AudioDeviceManager::closeAudioDevice()
  453. {
  454. stopDevice();
  455. currentAudioDevice.reset();
  456. cpuUsageMs = 0;
  457. }
  458. void AudioDeviceManager::restartLastAudioDevice()
  459. {
  460. if (currentAudioDevice == nullptr)
  461. {
  462. if (currentSetup.inputDeviceName.isEmpty()
  463. && currentSetup.outputDeviceName.isEmpty())
  464. {
  465. // This method will only reload the last device that was running
  466. // before closeAudioDevice() was called - you need to actually open
  467. // one first, with setAudioDevice().
  468. jassertfalse;
  469. return;
  470. }
  471. AudioDeviceSetup s (currentSetup);
  472. setAudioDeviceSetup (s, false);
  473. }
  474. }
  475. void AudioDeviceManager::updateXml()
  476. {
  477. lastExplicitSettings.reset (new XmlElement ("DEVICESETUP"));
  478. lastExplicitSettings->setAttribute ("deviceType", currentDeviceType);
  479. lastExplicitSettings->setAttribute ("audioOutputDeviceName", currentSetup.outputDeviceName);
  480. lastExplicitSettings->setAttribute ("audioInputDeviceName", currentSetup.inputDeviceName);
  481. if (currentAudioDevice != nullptr)
  482. {
  483. lastExplicitSettings->setAttribute ("audioDeviceRate", currentAudioDevice->getCurrentSampleRate());
  484. if (currentAudioDevice->getDefaultBufferSize() != currentAudioDevice->getCurrentBufferSizeSamples())
  485. lastExplicitSettings->setAttribute ("audioDeviceBufferSize", currentAudioDevice->getCurrentBufferSizeSamples());
  486. if (! currentSetup.useDefaultInputChannels)
  487. lastExplicitSettings->setAttribute ("audioDeviceInChans", currentSetup.inputChannels.toString (2));
  488. if (! currentSetup.useDefaultOutputChannels)
  489. lastExplicitSettings->setAttribute ("audioDeviceOutChans", currentSetup.outputChannels.toString (2));
  490. }
  491. for (int i = 0; i < enabledMidiInputs.size(); ++i)
  492. lastExplicitSettings->createNewChildElement ("MIDIINPUT")
  493. ->setAttribute ("name", enabledMidiInputs[i]->getName());
  494. if (midiInsFromXml.size() > 0)
  495. {
  496. // Add any midi devices that have been enabled before, but which aren't currently
  497. // open because the device has been disconnected.
  498. const StringArray availableMidiDevices (MidiInput::getDevices());
  499. for (int i = 0; i < midiInsFromXml.size(); ++i)
  500. if (! availableMidiDevices.contains (midiInsFromXml[i], true))
  501. lastExplicitSettings->createNewChildElement ("MIDIINPUT")
  502. ->setAttribute ("name", midiInsFromXml[i]);
  503. }
  504. if (defaultMidiOutputName.isNotEmpty())
  505. lastExplicitSettings->setAttribute ("defaultMidiOutput", defaultMidiOutputName);
  506. }
  507. //==============================================================================
  508. void AudioDeviceManager::addAudioCallback (AudioIODeviceCallback* newCallback)
  509. {
  510. {
  511. const ScopedLock sl (audioCallbackLock);
  512. if (callbacks.contains (newCallback))
  513. return;
  514. }
  515. if (currentAudioDevice != nullptr && newCallback != nullptr)
  516. newCallback->audioDeviceAboutToStart (currentAudioDevice.get());
  517. const ScopedLock sl (audioCallbackLock);
  518. callbacks.add (newCallback);
  519. }
  520. void AudioDeviceManager::removeAudioCallback (AudioIODeviceCallback* callbackToRemove)
  521. {
  522. if (callbackToRemove != nullptr)
  523. {
  524. bool needsDeinitialising = currentAudioDevice != nullptr;
  525. {
  526. const ScopedLock sl (audioCallbackLock);
  527. needsDeinitialising = needsDeinitialising && callbacks.contains (callbackToRemove);
  528. callbacks.removeFirstMatchingValue (callbackToRemove);
  529. }
  530. if (needsDeinitialising)
  531. callbackToRemove->audioDeviceStopped();
  532. }
  533. }
  534. void AudioDeviceManager::audioDeviceIOCallbackInt (const float** inputChannelData,
  535. int numInputChannels,
  536. float** outputChannelData,
  537. int numOutputChannels,
  538. int numSamples)
  539. {
  540. const ScopedLock sl (audioCallbackLock);
  541. inputLevelGetter->updateLevel (inputChannelData, numInputChannels, numSamples);
  542. outputLevelGetter->updateLevel (const_cast<const float**> (outputChannelData), numOutputChannels, numSamples);
  543. if (callbacks.size() > 0)
  544. {
  545. auto callbackStartTime = Time::getMillisecondCounterHiRes();
  546. tempBuffer.setSize (jmax (1, numOutputChannels), jmax (1, numSamples), false, false, true);
  547. callbacks.getUnchecked(0)->audioDeviceIOCallback (inputChannelData, numInputChannels,
  548. outputChannelData, numOutputChannels, numSamples);
  549. auto** tempChans = tempBuffer.getArrayOfWritePointers();
  550. for (int i = callbacks.size(); --i > 0;)
  551. {
  552. callbacks.getUnchecked(i)->audioDeviceIOCallback (inputChannelData, numInputChannels,
  553. tempChans, numOutputChannels, numSamples);
  554. for (int chan = 0; chan < numOutputChannels; ++chan)
  555. {
  556. if (auto* src = tempChans [chan])
  557. if (auto* dst = outputChannelData [chan])
  558. for (int j = 0; j < numSamples; ++j)
  559. dst[j] += src[j];
  560. }
  561. }
  562. auto msTaken = Time::getMillisecondCounterHiRes() - callbackStartTime;
  563. const double filterAmount = 0.2;
  564. cpuUsageMs += filterAmount * (msTaken - cpuUsageMs);
  565. if (msTaken > msPerBlock)
  566. xruns++;
  567. }
  568. else
  569. {
  570. for (int i = 0; i < numOutputChannels; ++i)
  571. zeromem (outputChannelData[i], sizeof (float) * (size_t) numSamples);
  572. }
  573. if (testSound != nullptr)
  574. {
  575. auto numSamps = jmin (numSamples, testSound->getNumSamples() - testSoundPosition);
  576. auto* src = testSound->getReadPointer (0, testSoundPosition);
  577. for (int i = 0; i < numOutputChannels; ++i)
  578. for (int j = 0; j < numSamps; ++j)
  579. outputChannelData [i][j] += src[j];
  580. testSoundPosition += numSamps;
  581. if (testSoundPosition >= testSound->getNumSamples())
  582. testSound.reset();
  583. }
  584. }
  585. void AudioDeviceManager::audioDeviceAboutToStartInt (AudioIODevice* const device)
  586. {
  587. cpuUsageMs = 0;
  588. xruns = 0;
  589. auto sampleRate = device->getCurrentSampleRate();
  590. auto blockSize = device->getCurrentBufferSizeSamples();
  591. if (sampleRate > 0.0 && blockSize > 0)
  592. {
  593. msPerBlock = 1000.0 * blockSize / sampleRate;
  594. timeToCpuScale = (msPerBlock > 0.0) ? (1.0 / msPerBlock) : 0.0;
  595. }
  596. {
  597. const ScopedLock sl (audioCallbackLock);
  598. for (int i = callbacks.size(); --i >= 0;)
  599. callbacks.getUnchecked(i)->audioDeviceAboutToStart (device);
  600. }
  601. sendChangeMessage();
  602. }
  603. void AudioDeviceManager::audioDeviceStoppedInt()
  604. {
  605. cpuUsageMs = 0;
  606. timeToCpuScale = 0;
  607. xruns = 0;
  608. sendChangeMessage();
  609. const ScopedLock sl (audioCallbackLock);
  610. for (int i = callbacks.size(); --i >= 0;)
  611. callbacks.getUnchecked(i)->audioDeviceStopped();
  612. }
  613. void AudioDeviceManager::audioDeviceErrorInt (const String& message)
  614. {
  615. const ScopedLock sl (audioCallbackLock);
  616. for (int i = callbacks.size(); --i >= 0;)
  617. callbacks.getUnchecked(i)->audioDeviceError (message);
  618. }
  619. double AudioDeviceManager::getCpuUsage() const
  620. {
  621. return jlimit (0.0, 1.0, timeToCpuScale * cpuUsageMs);
  622. }
  623. //==============================================================================
  624. void AudioDeviceManager::setMidiInputEnabled (const String& name, const bool enabled)
  625. {
  626. if (enabled != isMidiInputEnabled (name))
  627. {
  628. if (enabled)
  629. {
  630. auto index = MidiInput::getDevices().indexOf (name);
  631. if (index >= 0)
  632. {
  633. if (auto* midiIn = MidiInput::openDevice (index, callbackHandler.get()))
  634. {
  635. enabledMidiInputs.add (midiIn);
  636. midiIn->start();
  637. }
  638. }
  639. }
  640. else
  641. {
  642. for (int i = enabledMidiInputs.size(); --i >= 0;)
  643. if (enabledMidiInputs[i]->getName() == name)
  644. enabledMidiInputs.remove (i);
  645. }
  646. updateXml();
  647. sendChangeMessage();
  648. }
  649. }
  650. bool AudioDeviceManager::isMidiInputEnabled (const String& name) const
  651. {
  652. for (auto* mi : enabledMidiInputs)
  653. if (mi->getName() == name)
  654. return true;
  655. return false;
  656. }
  657. void AudioDeviceManager::addMidiInputCallback (const String& name, MidiInputCallback* callbackToAdd)
  658. {
  659. removeMidiInputCallback (name, callbackToAdd);
  660. if (name.isEmpty() || isMidiInputEnabled (name))
  661. {
  662. const ScopedLock sl (midiCallbackLock);
  663. MidiCallbackInfo mc;
  664. mc.deviceName = name;
  665. mc.callback = callbackToAdd;
  666. midiCallbacks.add (mc);
  667. }
  668. }
  669. void AudioDeviceManager::removeMidiInputCallback (const String& name, MidiInputCallback* callbackToRemove)
  670. {
  671. for (int i = midiCallbacks.size(); --i >= 0;)
  672. {
  673. auto& mc = midiCallbacks.getReference(i);
  674. if (mc.callback == callbackToRemove && mc.deviceName == name)
  675. {
  676. const ScopedLock sl (midiCallbackLock);
  677. midiCallbacks.remove (i);
  678. }
  679. }
  680. }
  681. void AudioDeviceManager::handleIncomingMidiMessageInt (MidiInput* source, const MidiMessage& message)
  682. {
  683. if (! message.isActiveSense())
  684. {
  685. const ScopedLock sl (midiCallbackLock);
  686. for (auto& mc : midiCallbacks)
  687. if (mc.deviceName.isEmpty() || mc.deviceName == source->getName())
  688. mc.callback->handleIncomingMidiMessage (source, message);
  689. }
  690. }
  691. //==============================================================================
  692. void AudioDeviceManager::setDefaultMidiOutput (const String& deviceName)
  693. {
  694. if (defaultMidiOutputName != deviceName)
  695. {
  696. Array<AudioIODeviceCallback*> oldCallbacks;
  697. {
  698. const ScopedLock sl (audioCallbackLock);
  699. oldCallbacks.swapWith (callbacks);
  700. }
  701. if (currentAudioDevice != nullptr)
  702. for (int i = oldCallbacks.size(); --i >= 0;)
  703. oldCallbacks.getUnchecked(i)->audioDeviceStopped();
  704. defaultMidiOutput.reset();
  705. defaultMidiOutputName = deviceName;
  706. if (deviceName.isNotEmpty())
  707. defaultMidiOutput.reset (MidiOutput::openDevice (MidiOutput::getDevices().indexOf (deviceName)));
  708. if (currentAudioDevice != nullptr)
  709. for (auto* c : oldCallbacks)
  710. c->audioDeviceAboutToStart (currentAudioDevice.get());
  711. {
  712. const ScopedLock sl (audioCallbackLock);
  713. oldCallbacks.swapWith (callbacks);
  714. }
  715. updateXml();
  716. sendChangeMessage();
  717. }
  718. }
  719. //==============================================================================
  720. AudioDeviceManager::LevelMeter::LevelMeter() noexcept : level() {}
  721. void AudioDeviceManager::LevelMeter::updateLevel (const float* const* channelData, int numChannels, int numSamples) noexcept
  722. {
  723. if (getReferenceCount() <= 1)
  724. return;
  725. auto localLevel = level.get();
  726. if (numChannels > 0)
  727. {
  728. for (int j = 0; j < numSamples; ++j)
  729. {
  730. float s = 0;
  731. for (int i = 0; i < numChannels; ++i)
  732. s += std::abs (channelData[i][j]);
  733. s /= (float) numChannels;
  734. const float decayFactor = 0.99992f;
  735. if (s > localLevel)
  736. localLevel = s;
  737. else if (localLevel > 0.001f)
  738. localLevel *= decayFactor;
  739. else
  740. localLevel = 0;
  741. }
  742. }
  743. else
  744. {
  745. localLevel = 0;
  746. }
  747. level = localLevel;
  748. }
  749. double AudioDeviceManager::LevelMeter::getCurrentLevel() const noexcept
  750. {
  751. jassert (getReferenceCount() > 1);
  752. return level.get();
  753. }
  754. void AudioDeviceManager::playTestSound()
  755. {
  756. { // cunningly nested to swap, unlock and delete in that order.
  757. ScopedPointer<AudioBuffer<float>> oldSound;
  758. {
  759. const ScopedLock sl (audioCallbackLock);
  760. std::swap (oldSound, testSound);
  761. }
  762. }
  763. testSoundPosition = 0;
  764. if (currentAudioDevice != nullptr)
  765. {
  766. auto sampleRate = currentAudioDevice->getCurrentSampleRate();
  767. auto soundLength = (int) sampleRate;
  768. double frequency = 440.0;
  769. float amplitude = 0.5f;
  770. auto phasePerSample = MathConstants<double>::twoPi / (sampleRate / frequency);
  771. auto* newSound = new AudioBuffer<float> (1, soundLength);
  772. for (int i = 0; i < soundLength; ++i)
  773. newSound->setSample (0, i, amplitude * (float) std::sin (i * phasePerSample));
  774. newSound->applyGainRamp (0, 0, soundLength / 10, 0.0f, 1.0f);
  775. newSound->applyGainRamp (0, soundLength - soundLength / 4, soundLength / 4, 1.0f, 0.0f);
  776. const ScopedLock sl (audioCallbackLock);
  777. testSound.reset (newSound);
  778. }
  779. }
  780. int AudioDeviceManager::getXRunCount() const noexcept
  781. {
  782. auto deviceXRuns = (currentAudioDevice != nullptr ? currentAudioDevice->getXRunCount() : -1);
  783. return (deviceXRuns >= 0 ? deviceXRuns : xruns);
  784. }
  785. } // namespace juce