Loading...
Searching...
No Matches
juce_AudioDeviceManager.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
//==============================================================================
class JUCE_API AudioDeviceManager : public ChangeBroadcaster
{
public:
//==============================================================================
AudioDeviceManager();
~AudioDeviceManager() override;
//==============================================================================
struct JUCE_API AudioDeviceSetup
{
String outputDeviceName;
String inputDeviceName;
double sampleRate = 0;
int bufferSize = 0;
BigInteger inputChannels;
bool useDefaultInputChannels = true;
BigInteger outputChannels;
bool useDefaultOutputChannels = true;
bool operator== (const AudioDeviceSetup&) const;
bool operator!= (const AudioDeviceSetup&) const;
};
//==============================================================================
String initialise (int numInputChannelsNeeded,
int numOutputChannelsNeeded,
const XmlElement* savedState,
bool selectDefaultDeviceOnFailure,
const String& preferredDefaultDeviceName = String(),
const AudioDeviceSetup* preferredSetupOptions = nullptr);
String initialiseWithDefaultDevices (int numInputChannelsNeeded,
int numOutputChannelsNeeded);
std::unique_ptr<XmlElement> createStateXml() const;
//==============================================================================
AudioDeviceSetup getAudioDeviceSetup() const;
void getAudioDeviceSetup (AudioDeviceSetup& result) const;
String setAudioDeviceSetup (const AudioDeviceSetup& newSetup, bool treatAsChosenDevice);
AudioIODevice* getCurrentAudioDevice() const noexcept { return currentAudioDevice.get(); }
String getCurrentAudioDeviceType() const { return currentDeviceType; }
AudioIODeviceType* getCurrentDeviceTypeObject() const;
void setCurrentAudioDeviceType (const String& type, bool treatAsChosenDevice);
AudioWorkgroup getDeviceAudioWorkgroup() const;
void closeAudioDevice();
void restartLastAudioDevice();
//==============================================================================
void addAudioCallback (AudioIODeviceCallback* newCallback);
void removeAudioCallback (AudioIODeviceCallback* callback);
//==============================================================================
double getCpuUsage() const;
//==============================================================================
void setMidiInputDeviceEnabled (const String& deviceIdentifier, bool enabled);
bool isMidiInputDeviceEnabled (const String& deviceIdentifier) const;
void addMidiInputDeviceCallback (const String& deviceIdentifier,
MidiInputCallback* callback);
void removeMidiInputDeviceCallback (const String& deviceIdentifier,
MidiInputCallback* callback);
//==============================================================================
void setDefaultMidiOutputDevice (const String& deviceIdentifier);
const String& getDefaultMidiOutputIdentifier() const noexcept { return defaultMidiOutputDeviceInfo.identifier; }
MidiOutput* getDefaultMidiOutput() const noexcept { return defaultMidiOutput.get(); }
//==============================================================================
const OwnedArray<AudioIODeviceType>& getAvailableDeviceTypes();
virtual void createAudioDeviceTypes (OwnedArray<AudioIODeviceType>& types);
void addAudioDeviceType (std::unique_ptr<AudioIODeviceType> newDeviceType);
void removeAudioDeviceType (AudioIODeviceType* deviceTypeToRemove);
//==============================================================================
void playTestSound();
//==============================================================================
struct LevelMeter : public ReferenceCountedObject
{
LevelMeter() noexcept;
double getCurrentLevel() const noexcept;
using Ptr = ReferenceCountedObjectPtr<LevelMeter>;
private:
friend class AudioDeviceManager;
Atomic<float> level { 0 };
void updateLevel (const float* const*, int numChannels, int numSamples) noexcept;
};
LevelMeter::Ptr getInputLevelGetter() noexcept { return inputLevelGetter; }
LevelMeter::Ptr getOutputLevelGetter() noexcept { return outputLevelGetter; }
//==============================================================================
CriticalSection& getAudioCallbackLock() noexcept { return audioCallbackLock; }
CriticalSection& getMidiCallbackLock() noexcept { return midiCallbackLock; }
//==============================================================================
int getXRunCount() const noexcept;
//==============================================================================
#ifndef DOXYGEN
[[deprecated ("Use setMidiInputDeviceEnabled instead.")]]
void setMidiInputEnabled (const String&, bool);
[[deprecated ("Use isMidiInputDeviceEnabled instead.")]]
bool isMidiInputEnabled (const String&) const;
[[deprecated ("Use addMidiInputDeviceCallback instead.")]]
void addMidiInputCallback (const String&, MidiInputCallback*);
[[deprecated ("Use removeMidiInputDeviceCallback instead.")]]
void removeMidiInputCallback (const String&, MidiInputCallback*);
[[deprecated ("Use setDefaultMidiOutputDevice instead.")]]
void setDefaultMidiOutput (const String&);
[[deprecated ("Use getDefaultMidiOutputIdentifier instead.")]]
const String& getDefaultMidiOutputName() const noexcept { return defaultMidiOutputDeviceInfo.name; }
#endif
private:
//==============================================================================
OwnedArray<AudioIODeviceType> availableDeviceTypes;
OwnedArray<AudioDeviceSetup> lastDeviceTypeConfigs;
AudioDeviceSetup currentSetup;
std::unique_ptr<AudioIODevice> currentAudioDevice;
Array<AudioIODeviceCallback*> callbacks;
int numInputChansNeeded = 0, numOutputChansNeeded = 2;
String preferredDeviceName, currentDeviceType;
std::unique_ptr<XmlElement> lastExplicitSettings;
mutable bool listNeedsScanning = true;
AudioBuffer<float> tempBuffer;
MidiDeviceListConnection midiDeviceListConnection = MidiDeviceListConnection::make ([this]
{
midiDeviceListChanged();
});
struct MidiCallbackInfo
{
String deviceIdentifier;
MidiInputCallback* callback;
};
Array<MidiDeviceInfo> midiDeviceInfosFromXml;
std::vector<std::unique_ptr<MidiInput>> enabledMidiInputs;
Array<MidiCallbackInfo> midiCallbacks;
MidiDeviceInfo defaultMidiOutputDeviceInfo;
std::unique_ptr<MidiOutput> defaultMidiOutput;
CriticalSection audioCallbackLock, midiCallbackLock;
std::unique_ptr<AudioBuffer<float>> testSound;
int testSoundPosition = 0;
AudioProcessLoadMeasurer loadMeasurer;
LevelMeter::Ptr inputLevelGetter { new LevelMeter() },
outputLevelGetter { new LevelMeter() };
//==============================================================================
class CallbackHandler;
std::unique_ptr<CallbackHandler> callbackHandler;
void audioDeviceIOCallbackInt (const float* const* inputChannelData,
int totalNumInputChannels,
float* const* outputChannelData,
int totalNumOutputChannels,
int numSamples,
const AudioIODeviceCallbackContext& context);
void audioDeviceAboutToStartInt (AudioIODevice*);
void audioDeviceStoppedInt();
void audioDeviceErrorInt (const String&);
void handleIncomingMidiMessageInt (MidiInput*, const MidiMessage&);
void audioDeviceListChanged();
void midiDeviceListChanged();
String restartDevice (int blockSizeToUse, double sampleRateToUse,
const BigInteger& ins, const BigInteger& outs);
void stopDevice();
void updateXml();
void updateCurrentSetup();
void createDeviceTypesIfNeeded();
void scanDevicesIfNeeded();
void deleteCurrentDevice();
double chooseBestSampleRate (double preferred) const;
int chooseBestBufferSize (int preferred) const;
void insertDefaultDeviceNames (AudioDeviceSetup&) const;
String initialiseDefault (const String& preferredDefaultDeviceName, const AudioDeviceSetup*);
String initialiseFromXML (const XmlElement&, bool selectDefaultDeviceOnFailure,
const String& preferredDefaultDeviceName, const AudioDeviceSetup*);
void openLastRequestedMidiDevices (const Array<MidiDeviceInfo>&, const MidiDeviceInfo&);
AudioIODeviceType* findType (const String& inputName, const String& outputName);
AudioIODeviceType* findType (const String& typeName);
void pickCurrentDeviceTypeWithDevices();
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioDeviceManager)
};
} // namespace juce
juce::AudioDeviceManager::getDefaultMidiOutput
MidiOutput * getDefaultMidiOutput() const noexcept
Definition juce_AudioDeviceManager.h:386
juce::AudioDeviceManager::getInputLevelGetter
LevelMeter::Ptr getInputLevelGetter() noexcept
Definition juce_AudioDeviceManager.h:445
juce::AudioDeviceManager::getMidiCallbackLock
CriticalSection & getMidiCallbackLock() noexcept
Definition juce_AudioDeviceManager.h:465
juce::AudioDeviceManager::AudioDeviceManager
AudioDeviceManager()
Definition juce_AudioDeviceManager.cpp:113
juce::AudioDeviceManager::getAudioDeviceSetup
AudioDeviceSetup getAudioDeviceSetup() const
Definition juce_AudioDeviceManager.cpp:641
juce::AudioDeviceManager::getDefaultMidiOutputIdentifier
const String & getDefaultMidiOutputIdentifier() const noexcept
Definition juce_AudioDeviceManager.h:379
juce::AudioDeviceManager::setAudioDeviceSetup
String setAudioDeviceSetup(const AudioDeviceSetup &newSetup, bool treatAsChosenDevice)
Definition juce_AudioDeviceManager.cpp:718
juce::AudioDeviceManager::getOutputLevelGetter
LevelMeter::Ptr getOutputLevelGetter() noexcept
Definition juce_AudioDeviceManager.h:452
juce::AudioDeviceManager::getCurrentAudioDevice
AudioIODevice * getCurrentAudioDevice() const noexcept
Definition juce_AudioDeviceManager.h:243
juce::AudioDeviceManager::getCurrentAudioDeviceType
String getCurrentAudioDeviceType() const
Definition juce_AudioDeviceManager.h:248
juce::AudioDeviceManager::initialise
String initialise(int numInputChannelsNeeded, int numOutputChannelsNeeded, const XmlElement *savedState, bool selectDefaultDeviceOnFailure, const String &preferredDefaultDeviceName=String(), const AudioDeviceSetup *preferredSetupOptions=nullptr)
Definition juce_AudioDeviceManager.cpp:292
juce::AudioDeviceManager::getAudioCallbackLock
CriticalSection & getAudioCallbackLock() noexcept
Definition juce_AudioDeviceManager.h:459
juce::AudioDeviceManager::initialiseWithDefaultDevices
String initialiseWithDefaultDevices(int numInputChannelsNeeded, int numOutputChannelsNeeded)
Definition juce_AudioDeviceManager.cpp:497
juce::AudioDeviceManager::createStateXml
std::unique_ptr< XmlElement > createStateXml() const
Definition juce_AudioDeviceManager.cpp:596
juce::AudioIODeviceCallback
Definition juce_AudioIODevice.h:55
juce::AudioIODeviceType
Definition juce_AudioIODeviceType.h:62
juce::AudioIODevice
Definition juce_AudioIODevice.h:151
juce::AudioWorkgroup
Definition juce_AudioWorkgroup.h:142
juce::BigInteger
Definition juce_BigInteger.h:39
juce::ChangeBroadcaster::ChangeBroadcaster
ChangeBroadcaster() noexcept
Definition juce_ChangeBroadcaster.cpp:26
juce::CriticalSection
Definition juce_CriticalSection.h:43
juce::MidiInputCallback
Definition juce_MidiDevices.h:277
juce::MidiOutput
Definition juce_MidiDevices.h:325
juce::OwnedArray
Definition juce_OwnedArray.h:50
juce::ReferenceCountedObjectPtr
Definition juce_ReferenceCountedObject.h:247
juce::ReferenceCountedObject::ReferenceCountedObject
ReferenceCountedObject()=default
juce::String
Definition juce_String.h:53
juce::XmlElement
Definition juce_XmlElement.h:83
juce::Atomic
Definition juce_Atomic.h:42
juce::AudioDeviceManager::AudioDeviceSetup
Definition juce_AudioDeviceManager.h:91
juce::AudioDeviceManager::AudioDeviceSetup::outputDeviceName
String outputDeviceName
Definition juce_AudioDeviceManager.h:97
juce::AudioDeviceManager::AudioDeviceSetup::useDefaultInputChannels
bool useDefaultInputChannels
Definition juce_AudioDeviceManager.h:128
juce::AudioDeviceManager::AudioDeviceSetup::inputDeviceName
String inputDeviceName
Definition juce_AudioDeviceManager.h:102
juce::AudioDeviceManager::AudioDeviceSetup::outputChannels
BigInteger outputChannels
Definition juce_AudioDeviceManager.h:135
juce::AudioDeviceManager::AudioDeviceSetup::sampleRate
double sampleRate
Definition juce_AudioDeviceManager.h:109
juce::AudioDeviceManager::AudioDeviceSetup::inputChannels
BigInteger inputChannels
Definition juce_AudioDeviceManager.h:122
juce::AudioDeviceManager::AudioDeviceSetup::bufferSize
int bufferSize
Definition juce_AudioDeviceManager.h:115
juce::AudioDeviceManager::AudioDeviceSetup::useDefaultOutputChannels
bool useDefaultOutputChannels
Definition juce_AudioDeviceManager.h:141