OpenShot Audio Library | OpenShotAudio  0.6.0
juce_AudioDeviceManager.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 //==============================================================================
66 class JUCE_API AudioDeviceManager : public ChangeBroadcaster
67 {
68 public:
69  //==============================================================================
77 
79  ~AudioDeviceManager() override;
80 
81  //==============================================================================
90  struct JUCE_API AudioDeviceSetup
91  {
98 
103 
109  double sampleRate = 0;
110 
115  int bufferSize = 0;
116 
123 
128  bool useDefaultInputChannels = true;
129 
136 
141  bool useDefaultOutputChannels = true;
142 
143  bool operator== (const AudioDeviceSetup&) const;
144  bool operator!= (const AudioDeviceSetup&) const;
145  };
146 
147 
148  //==============================================================================
182  String initialise (int numInputChannelsNeeded,
183  int numOutputChannelsNeeded,
184  const XmlElement* savedState,
185  bool selectDefaultDeviceOnFailure,
186  const String& preferredDefaultDeviceName = String(),
187  const AudioDeviceSetup* preferredSetupOptions = nullptr);
188 
190  String initialiseWithDefaultDevices (int numInputChannelsNeeded,
191  int numOutputChannelsNeeded);
192 
201  std::unique_ptr<XmlElement> createStateXml() const;
202 
203  //==============================================================================
207  AudioDeviceSetup getAudioDeviceSetup() const;
208 
214  void getAudioDeviceSetup (AudioDeviceSetup& result) const;
215 
239  String setAudioDeviceSetup (const AudioDeviceSetup& newSetup, bool treatAsChosenDevice);
240 
241 
243  AudioIODevice* getCurrentAudioDevice() const noexcept { return currentAudioDevice.get(); }
244 
248  String getCurrentAudioDeviceType() const { return currentDeviceType; }
249 
254  AudioIODeviceType* getCurrentDeviceTypeObject() const;
255 
263  void setCurrentAudioDeviceType (const String& type, bool treatAsChosenDevice);
264 
266  AudioWorkgroup getDeviceAudioWorkgroup() const;
267 
272  void closeAudioDevice();
273 
282  void restartLastAudioDevice();
283 
284  //==============================================================================
297  void addAudioCallback (AudioIODeviceCallback* newCallback);
298 
306  void removeAudioCallback (AudioIODeviceCallback* callback);
307 
308  //==============================================================================
313  double getCpuUsage() const;
314 
315  //==============================================================================
335  void setMidiInputDeviceEnabled (const String& deviceIdentifier, bool enabled);
336 
341  bool isMidiInputDeviceEnabled (const String& deviceIdentifier) const;
342 
353  void addMidiInputDeviceCallback (const String& deviceIdentifier,
354  MidiInputCallback* callback);
355 
357  void removeMidiInputDeviceCallback (const String& deviceIdentifier,
358  MidiInputCallback* callback);
359 
360  //==============================================================================
373  void setDefaultMidiOutputDevice (const String& deviceIdentifier);
374 
379  const String& getDefaultMidiOutputIdentifier() const noexcept { return defaultMidiOutputDeviceInfo.identifier; }
380 
386  MidiOutput* getDefaultMidiOutput() const noexcept { return defaultMidiOutput.get(); }
387 
388  //==============================================================================
390  const OwnedArray<AudioIODeviceType>& getAvailableDeviceTypes();
391 
400  virtual void createAudioDeviceTypes (OwnedArray<AudioIODeviceType>& types);
401 
403  void addAudioDeviceType (std::unique_ptr<AudioIODeviceType> newDeviceType);
404 
406  void removeAudioDeviceType (AudioIODeviceType* deviceTypeToRemove);
407 
408  //==============================================================================
414  void playTestSound();
415 
416  //==============================================================================
427  {
428  LevelMeter() noexcept;
429  double getCurrentLevel() const noexcept;
430 
432 
433  private:
434  friend class AudioDeviceManager;
435 
436  Atomic<float> level { 0 };
437  void updateLevel (const float* const*, int numChannels, int numSamples) noexcept;
438  };
439 
445  LevelMeter::Ptr getInputLevelGetter() noexcept { return inputLevelGetter; }
446 
452  LevelMeter::Ptr getOutputLevelGetter() noexcept { return outputLevelGetter; }
453 
454  //==============================================================================
459  CriticalSection& getAudioCallbackLock() noexcept { return audioCallbackLock; }
460 
465  CriticalSection& getMidiCallbackLock() noexcept { return midiCallbackLock; }
466 
467  //==============================================================================
474  int getXRunCount() const noexcept;
475 
476  //==============================================================================
477  #ifndef DOXYGEN
478  [[deprecated ("Use setMidiInputDeviceEnabled instead.")]]
479  void setMidiInputEnabled (const String&, bool);
480  [[deprecated ("Use isMidiInputDeviceEnabled instead.")]]
481  bool isMidiInputEnabled (const String&) const;
482  [[deprecated ("Use addMidiInputDeviceCallback instead.")]]
483  void addMidiInputCallback (const String&, MidiInputCallback*);
484  [[deprecated ("Use removeMidiInputDeviceCallback instead.")]]
485  void removeMidiInputCallback (const String&, MidiInputCallback*);
486  [[deprecated ("Use setDefaultMidiOutputDevice instead.")]]
487  void setDefaultMidiOutput (const String&);
488  [[deprecated ("Use getDefaultMidiOutputIdentifier instead.")]]
489  const String& getDefaultMidiOutputName() const noexcept { return defaultMidiOutputDeviceInfo.name; }
490  #endif
491 
492 private:
493  //==============================================================================
494  OwnedArray<AudioIODeviceType> availableDeviceTypes;
495  OwnedArray<AudioDeviceSetup> lastDeviceTypeConfigs;
496 
497  AudioDeviceSetup currentSetup;
498  std::unique_ptr<AudioIODevice> currentAudioDevice;
499  Array<AudioIODeviceCallback*> callbacks;
500  int numInputChansNeeded = 0, numOutputChansNeeded = 2;
501  String preferredDeviceName, currentDeviceType;
502  std::unique_ptr<XmlElement> lastExplicitSettings;
503  mutable bool listNeedsScanning = true;
504  AudioBuffer<float> tempBuffer;
505  MidiDeviceListConnection midiDeviceListConnection = MidiDeviceListConnection::make ([this]
506  {
507  midiDeviceListChanged();
508  });
509 
510  struct MidiCallbackInfo
511  {
512  String deviceIdentifier;
513  MidiInputCallback* callback;
514  };
515 
516  Array<MidiDeviceInfo> midiDeviceInfosFromXml;
517  std::vector<std::unique_ptr<MidiInput>> enabledMidiInputs;
518  Array<MidiCallbackInfo> midiCallbacks;
519 
520  MidiDeviceInfo defaultMidiOutputDeviceInfo;
521  std::unique_ptr<MidiOutput> defaultMidiOutput;
522  CriticalSection audioCallbackLock, midiCallbackLock;
523 
524  std::unique_ptr<AudioBuffer<float>> testSound;
525  int testSoundPosition = 0;
526 
527  AudioProcessLoadMeasurer loadMeasurer;
528 
529  LevelMeter::Ptr inputLevelGetter { new LevelMeter() },
530  outputLevelGetter { new LevelMeter() };
531 
532  //==============================================================================
533  class CallbackHandler;
534  std::unique_ptr<CallbackHandler> callbackHandler;
535 
536  void audioDeviceIOCallbackInt (const float* const* inputChannelData,
537  int totalNumInputChannels,
538  float* const* outputChannelData,
539  int totalNumOutputChannels,
540  int numSamples,
541  const AudioIODeviceCallbackContext& context);
542  void audioDeviceAboutToStartInt (AudioIODevice*);
543  void audioDeviceStoppedInt();
544  void audioDeviceErrorInt (const String&);
545  void handleIncomingMidiMessageInt (MidiInput*, const MidiMessage&);
546  void audioDeviceListChanged();
547  void midiDeviceListChanged();
548 
549  String restartDevice (int blockSizeToUse, double sampleRateToUse,
550  const BigInteger& ins, const BigInteger& outs);
551  void stopDevice();
552 
553  void updateXml();
554 
555  void updateCurrentSetup();
556  void createDeviceTypesIfNeeded();
557  void scanDevicesIfNeeded();
558  void deleteCurrentDevice();
559  double chooseBestSampleRate (double preferred) const;
560  int chooseBestBufferSize (int preferred) const;
561  void insertDefaultDeviceNames (AudioDeviceSetup&) const;
562  String initialiseDefault (const String& preferredDefaultDeviceName, const AudioDeviceSetup*);
563  String initialiseFromXML (const XmlElement&, bool selectDefaultDeviceOnFailure,
564  const String& preferredDefaultDeviceName, const AudioDeviceSetup*);
565  void openLastRequestedMidiDevices (const Array<MidiDeviceInfo>&, const MidiDeviceInfo&);
566 
567  AudioIODeviceType* findType (const String& inputName, const String& outputName);
568  AudioIODeviceType* findType (const String& typeName);
569  void pickCurrentDeviceTypeWithDevices();
570 
571  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioDeviceManager)
572 };
573 
574 } // namespace juce
LevelMeter::Ptr getInputLevelGetter() noexcept
AudioIODevice * getCurrentAudioDevice() const noexcept
LevelMeter::Ptr getOutputLevelGetter() noexcept
MidiOutput * getDefaultMidiOutput() const noexcept
const String & getDefaultMidiOutputIdentifier() const noexcept
CriticalSection & getAudioCallbackLock() noexcept
CriticalSection & getMidiCallbackLock() noexcept
static MidiDeviceListConnection make(std::function< void()>)