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.

1011 lines
34KB

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