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.

1005 lines
34KB

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