OpenShot Audio Library | OpenShotAudio  0.3.0
juce_AudioDeviceManager.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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  {
99 
105 
111  double sampleRate = 0;
112 
117  int bufferSize = 0;
118 
125 
130  bool useDefaultInputChannels = true;
131 
138 
143  bool useDefaultOutputChannels = true;
144 
145  bool operator== (const AudioDeviceSetup&) const;
146  bool operator!= (const AudioDeviceSetup&) const;
147  };
148 
149 
150  //==============================================================================
181  String initialise (int numInputChannelsNeeded,
182  int numOutputChannelsNeeded,
183  const XmlElement* savedState,
184  bool selectDefaultDeviceOnFailure,
185  const String& preferredDefaultDeviceName = String(),
186  const AudioDeviceSetup* preferredSetupOptions = nullptr);
187 
189  String initialiseWithDefaultDevices (int numInputChannelsNeeded,
190  int numOutputChannelsNeeded);
191 
200  std::unique_ptr<XmlElement> createStateXml() const;
201 
202  //==============================================================================
206  AudioDeviceSetup getAudioDeviceSetup() const;
207 
213  void getAudioDeviceSetup (AudioDeviceSetup& result) const;
214 
234  String setAudioDeviceSetup (const AudioDeviceSetup& newSetup, bool treatAsChosenDevice);
235 
236 
238  AudioIODevice* getCurrentAudioDevice() const noexcept { return currentAudioDevice.get(); }
239 
243  String getCurrentAudioDeviceType() const { return currentDeviceType; }
244 
249  AudioIODeviceType* getCurrentDeviceTypeObject() const;
250 
258  void setCurrentAudioDeviceType (const String& type, bool treatAsChosenDevice);
259 
264  void closeAudioDevice();
265 
274  void restartLastAudioDevice();
275 
276  //==============================================================================
289  void addAudioCallback (AudioIODeviceCallback* newCallback);
290 
298  void removeAudioCallback (AudioIODeviceCallback* callback);
299 
300  //==============================================================================
305  double getCpuUsage() const;
306 
307  //==============================================================================
327  void setMidiInputDeviceEnabled (const String& deviceIdentifier, bool enabled);
328 
333  bool isMidiInputDeviceEnabled (const String& deviceIdentifier) const;
334 
345  void addMidiInputDeviceCallback (const String& deviceIdentifier,
346  MidiInputCallback* callback);
347 
349  void removeMidiInputDeviceCallback (const String& deviceIdentifier,
350  MidiInputCallback* callback);
351 
352  //==============================================================================
365  void setDefaultMidiOutputDevice (const String& deviceIdentifier);
366 
371  const String& getDefaultMidiOutputIdentifier() const noexcept { return defaultMidiOutputDeviceInfo.identifier; }
372 
378  MidiOutput* getDefaultMidiOutput() const noexcept { return defaultMidiOutput.get(); }
379 
380  //==============================================================================
382  const OwnedArray<AudioIODeviceType>& getAvailableDeviceTypes();
383 
392  virtual void createAudioDeviceTypes (OwnedArray<AudioIODeviceType>& types);
393 
395  void addAudioDeviceType (std::unique_ptr<AudioIODeviceType> newDeviceType);
396 
398  void removeAudioDeviceType (AudioIODeviceType* deviceTypeToRemove);
399 
400  //==============================================================================
406  void playTestSound();
407 
408  //==============================================================================
419  {
420  LevelMeter() noexcept;
421  double getCurrentLevel() const noexcept;
422 
424 
425  private:
426  friend class AudioDeviceManager;
427 
428  Atomic<float> level { 0 };
429  void updateLevel (const float* const*, int numChannels, int numSamples) noexcept;
430  };
431 
437  LevelMeter::Ptr getInputLevelGetter() noexcept { return inputLevelGetter; }
438 
444  LevelMeter::Ptr getOutputLevelGetter() noexcept { return outputLevelGetter; }
445 
446  //==============================================================================
451  CriticalSection& getAudioCallbackLock() noexcept { return audioCallbackLock; }
452 
457  CriticalSection& getMidiCallbackLock() noexcept { return midiCallbackLock; }
458 
459  //==============================================================================
466  int getXRunCount() const noexcept;
467 
468  //==============================================================================
470  void setMidiInputEnabled (const String&, bool);
472  bool isMidiInputEnabled (const String&) const;
474  void addMidiInputCallback (const String&, MidiInputCallback*);
476  void removeMidiInputCallback (const String&, MidiInputCallback*);
478  void setDefaultMidiOutput (const String&);
480  const String& getDefaultMidiOutputName() const noexcept { return defaultMidiOutputDeviceInfo.name; }
481 
482 private:
483  //==============================================================================
484  OwnedArray<AudioIODeviceType> availableDeviceTypes;
485  OwnedArray<AudioDeviceSetup> lastDeviceTypeConfigs;
486 
487  AudioDeviceSetup currentSetup;
488  std::unique_ptr<AudioIODevice> currentAudioDevice;
490  int numInputChansNeeded = 0, numOutputChansNeeded = 2;
491  String preferredDeviceName, currentDeviceType;
492  std::unique_ptr<XmlElement> lastExplicitSettings;
493  mutable bool listNeedsScanning = true;
494  AudioBuffer<float> tempBuffer;
495 
496  struct MidiCallbackInfo
497  {
498  String deviceIdentifier;
499  MidiInputCallback* callback;
500  };
501 
502  Array<MidiDeviceInfo> midiDeviceInfosFromXml;
503  std::vector<std::unique_ptr<MidiInput>> enabledMidiInputs;
504  Array<MidiCallbackInfo> midiCallbacks;
505 
506  MidiDeviceInfo defaultMidiOutputDeviceInfo;
507  std::unique_ptr<MidiOutput> defaultMidiOutput;
508  CriticalSection audioCallbackLock, midiCallbackLock;
509 
510  std::unique_ptr<AudioBuffer<float>> testSound;
511  int testSoundPosition = 0;
512 
513  AudioProcessLoadMeasurer loadMeasurer;
514 
515  LevelMeter::Ptr inputLevelGetter { new LevelMeter() },
516  outputLevelGetter { new LevelMeter() };
517 
518  //==============================================================================
519  class CallbackHandler;
520  std::unique_ptr<CallbackHandler> callbackHandler;
521 
522  void audioDeviceIOCallbackInt (const float** inputChannelData, int totalNumInputChannels,
523  float** outputChannelData, int totalNumOutputChannels, int numSamples);
524  void audioDeviceAboutToStartInt (AudioIODevice*);
525  void audioDeviceStoppedInt();
526  void audioDeviceErrorInt (const String&);
527  void handleIncomingMidiMessageInt (MidiInput*, const MidiMessage&);
528  void audioDeviceListChanged();
529 
530  String restartDevice (int blockSizeToUse, double sampleRateToUse,
531  const BigInteger& ins, const BigInteger& outs);
532  void stopDevice();
533 
534  void updateXml();
535 
536  void createDeviceTypesIfNeeded();
537  void scanDevicesIfNeeded();
538  void deleteCurrentDevice();
539  double chooseBestSampleRate (double preferred) const;
540  int chooseBestBufferSize (int preferred) const;
541  void insertDefaultDeviceNames (AudioDeviceSetup&) const;
542  String initialiseDefault (const String& preferredDefaultDeviceName, const AudioDeviceSetup*);
543  String initialiseFromXML (const XmlElement&, bool selectDefaultDeviceOnFailure,
544  const String& preferredDefaultDeviceName, const AudioDeviceSetup*);
545 
546  AudioIODeviceType* findType (const String& inputName, const String& outputName);
547  AudioIODeviceType* findType (const String& typeName);
548 
549  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioDeviceManager)
550 };
551 
552 } // 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