Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Attributes | List of all members
DeviceManager Class Referencefinal

A singleton that manages the audio devices known to Audacity. More...

#include <DeviceManager.h>

Inheritance diagram for DeviceManager:
[legend]
Collaboration diagram for DeviceManager:
[legend]

Public Member Functions

void Rescan ()
 
std::chrono::duration< float > GetTimeSinceRescan ()
 
DeviceSourceMapGetDefaultOutputDevice (int hostIndex)
 
DeviceSourceMapGetDefaultInputDevice (int hostIndex)
 
const std::vector< DeviceSourceMap > & GetInputDeviceMaps ()
 
const std::vector< DeviceSourceMap > & GetOutputDeviceMaps ()
 
- Public Member Functions inherited from Observer::Publisher< Message, NotifyAll >
template<typename Alloc = std::allocator<Record>>
 Publisher (ExceptionPolicy *pPolicy=nullptr, Alloc a={})
 Constructor supporting type-erased custom allocation/deletion. More...
 
 Publisher (Publisher &&)=default
 
Publisheroperator= (Publisher &&)=default
 
Subscription Subscribe (Callback callback)
 Connect a callback to the Publisher; later-connected are called earlier. More...
 
template<typename Object , typename Return , typename... Args>
Subscription Subscribe (Object &obj, Return(Object::*callback)(Args...))
 Overload of Subscribe takes an object and pointer-to-member-function. More...
 

Static Public Member Functions

static DeviceManagerInstance ()
 Gets the singleton instance. More...
 

Protected Member Functions

 DeviceManager ()
 
 ~DeviceManager ()
 
void Init ()
 
DeviceSourceMapGetDefaultDevice (int hostIndex, int isInput)
 
- Protected Member Functions inherited from Observer::Publisher< Message, NotifyAll >
CallbackReturn Publish (const Message &message)
 Send a message to connected callbacks. More...
 

Protected Attributes

bool m_inited
 
std::vector< DeviceSourceMapmInputDeviceSourceMaps
 
std::vector< DeviceSourceMapmOutputDeviceSourceMaps
 

Static Protected Attributes

static DeviceManager dm
 

Private Attributes

std::chrono::time_point< std::chrono::steady_clock > mRescanTime
 

Additional Inherited Members

- Public Types inherited from Observer::Publisher< Message, NotifyAll >
using message_type = Message
 
using CallbackReturn = std::conditional_t< NotifyAll, void, bool >
 
using Callback = std::function< CallbackReturn(const Message &) >
 Type of functions that can be connected to the Publisher. More...
 
- Static Public Attributes inherited from Observer::Publisher< Message, NotifyAll >
static constexpr bool notifies_all = NotifyAll
 

Detailed Description

A singleton that manages the audio devices known to Audacity.

Definition at line 42 of file DeviceManager.h.

Constructor & Destructor Documentation

◆ DeviceManager()

DeviceManager::DeviceManager ( )
protected

Definition at line 305 of file DeviceManager.cpp.

308: DeviceChangeHandler()
309#endif
310#endif
311{
312 m_inited = false;
313 mRescanTime = std::chrono::steady_clock::now();
314}
std::chrono::time_point< std::chrono::steady_clock > mRescanTime
Definition: DeviceManager.h:74

◆ ~DeviceManager()

DeviceManager::~DeviceManager ( )
protected

Definition at line 316 of file DeviceManager.cpp.

317{
318
319}

Member Function Documentation

◆ GetDefaultDevice()

DeviceSourceMap * DeviceManager::GetDefaultDevice ( int  hostIndex,
int  isInput 
)
protected

Definition at line 62 of file DeviceManager.cpp.

63{
64 if (hostIndex < 0 || hostIndex >= Pa_GetHostApiCount()) {
65 return NULL;
66 }
67
68 const struct PaHostApiInfo *apiinfo = Pa_GetHostApiInfo(hostIndex); // get info on API
69 std::vector<DeviceSourceMap> & maps = isInput ? mInputDeviceSourceMaps : mOutputDeviceSourceMaps;
70 size_t i;
71 int targetDevice = isInput ? apiinfo->defaultInputDevice : apiinfo->defaultOutputDevice;
72
73 for (i = 0; i < maps.size(); i++) {
74 if (maps[i].deviceIndex == targetDevice)
75 return &maps[i];
76 }
77
78 wxLogDebug(wxT("GetDefaultDevice() no default device"));
79 return NULL;
80}
wxT("CloseDown"))
std::vector< DeviceSourceMap > mOutputDeviceSourceMaps
Definition: DeviceManager.h:89
std::vector< DeviceSourceMap > mInputDeviceSourceMaps
Definition: DeviceManager.h:88

References mInputDeviceSourceMaps, mOutputDeviceSourceMaps, and wxT().

Referenced by GetDefaultInputDevice(), and GetDefaultOutputDevice().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDefaultInputDevice()

DeviceSourceMap * DeviceManager::GetDefaultInputDevice ( int  hostIndex)

Definition at line 86 of file DeviceManager.cpp.

87{
88 return GetDefaultDevice(hostIndex, 1);
89}
DeviceSourceMap * GetDefaultDevice(int hostIndex, int isInput)

References GetDefaultDevice().

Referenced by DevicePrefs::OnHost(), AudioSetupToolBar::UpdatePrefs(), and DeviceToolBar::UpdatePrefs().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDefaultOutputDevice()

DeviceSourceMap * DeviceManager::GetDefaultOutputDevice ( int  hostIndex)

Definition at line 82 of file DeviceManager.cpp.

83{
84 return GetDefaultDevice(hostIndex, 0);
85}

References GetDefaultDevice().

Referenced by DevicePrefs::OnHost(), and DeviceToolBar::UpdatePrefs().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetInputDeviceMaps()

const std::vector< DeviceSourceMap > & DeviceManager::GetInputDeviceMaps ( )

Definition at line 38 of file DeviceManager.cpp.

39{
40 if (!m_inited)
41 Init();
43}

References Init(), m_inited, and mInputDeviceSourceMaps.

Referenced by DeviceToolBar::ChangeDevice(), AudioSetupToolBar::ChangeDeviceLabel(), AudioSetupToolBar::FillHostDevices(), DeviceToolBar::FillHostDevices(), AudioSetupToolBar::FillHosts(), DeviceToolBar::FillHosts(), AudioSetupToolBar::FillInputChannels(), DeviceToolBar::FillInputChannels(), DevicePrefs::OnHost(), AudioSetupToolBar::UpdatePrefs(), and DeviceToolBar::UpdatePrefs().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetOutputDeviceMaps()

const std::vector< DeviceSourceMap > & DeviceManager::GetOutputDeviceMaps ( )

Definition at line 44 of file DeviceManager.cpp.

45{
46 if (!m_inited)
47 Init();
49}

References Init(), m_inited, and mOutputDeviceSourceMaps.

Referenced by DeviceToolBar::ChangeDevice(), AudioSetupToolBar::ChangeDeviceLabel(), AudioSetupToolBar::FillHostDevices(), DeviceToolBar::FillHostDevices(), AudioSetupToolBar::FillHosts(), DeviceToolBar::FillHosts(), DevicePrefs::OnHost(), AudioSetupToolBar::UpdatePrefs(), and DeviceToolBar::UpdatePrefs().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTimeSinceRescan()

std::chrono::duration< float > DeviceManager::GetTimeSinceRescan ( )

Definition at line 297 of file DeviceManager.cpp.

297 {
298 auto now = std::chrono::steady_clock::now();
299 auto dur = std::chrono::duration_cast<std::chrono::duration<float>>(now - mRescanTime);
300 return dur;
301}

References mRescanTime.

Referenced by AudioIO::StartPortAudioStream().

Here is the caller graph for this function:

◆ Init()

void DeviceManager::Init ( )
protected

Does an initial scan. Called by GetInputDeviceMaps and GetOutputDeviceMaps when needed.

Definition at line 321 of file DeviceManager.cpp.

322{
323 Rescan();
324
325#if defined(EXPERIMENTAL_DEVICE_CHANGE_HANDLER)
326#if defined(HAVE_DEVICE_CHANGE)
327 DeviceChangeHandler::Enable(true);
328#endif
329#endif
330}

References Rescan().

Referenced by GetInputDeviceMaps(), and GetOutputDeviceMaps().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Instance()

DeviceManager * DeviceManager::Instance ( )
static

◆ Rescan()

void DeviceManager::Rescan ( )

Gets a NEW list of devices by terminating and restarting portaudio Assumes that DeviceManager is only used on the main thread.

Definition at line 238 of file DeviceManager.cpp.

239{
240 // get rid of the previous scan info
241 this->mInputDeviceSourceMaps.clear();
242 this->mOutputDeviceSourceMaps.clear();
243
244 // if we are doing a second scan then restart portaudio to get NEW devices
245 if (m_inited) {
246 // check to see if there is a stream open - can happen if monitoring,
247 // but otherwise Rescan() should not be available to the user.
248 auto gAudioIO = AudioIOBase::Get();
249 if (gAudioIO) {
250 if (gAudioIO->IsMonitoring())
251 {
252 using namespace std::chrono;
253 gAudioIO->StopStream();
254 while (gAudioIO->IsBusy())
255 std::this_thread::sleep_for(100ms);
256 }
257 }
258
259 // restart portaudio - this updates the device list
260 // FIXME: TRAP_ERR restarting PortAudio
261 Pa_Terminate();
262 Pa_Initialize();
263 }
264
265 // FIXME: TRAP_ERR PaErrorCode not handled in ReScan()
266 int nDevices = Pa_GetDeviceCount();
267
268 //The hierarchy for devices is Host/device/source.
269 //Some newer systems aggregate this.
270 //So we need to call port mixer for every device to get the sources
271 for (int i = 0; i < nDevices; i++) {
272 const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
273 if (info->maxOutputChannels > 0) {
274 AddSources(i, info->defaultSampleRate, &mOutputDeviceSourceMaps, 0);
275 }
276
277 if (info->maxInputChannels > 0) {
278#ifdef __WXMSW__
279#if !defined(EXPERIMENTAL_FULL_WASAPI)
280 if (Pa_GetHostApiInfo(info->hostApi)->type != paWASAPI ||
281 PaWasapi_IsLoopback(i) > 0)
282#endif
283#endif
284 AddSources(i, info->defaultSampleRate, &mInputDeviceSourceMaps, 1);
285 }
286 }
287
288 // If this was not an initial scan update each device toolbar.
289 if ( m_inited )
291
292 m_inited = true;
293 mRescanTime = std::chrono::steady_clock::now();
294}
static void AddSources(int deviceIndex, int rate, std::vector< DeviceSourceMap > *maps, int isInput)
static AudioIOBase * Get()
Definition: AudioIOBase.cpp:94
CallbackReturn Publish(const Message &message)
Send a message to connected callbacks.
Definition: Observer.h:207

References AddSources(), AudioIOBase::Get(), m_inited, mInputDeviceSourceMaps, mOutputDeviceSourceMaps, mRescanTime, Observer::Publisher< Message, NotifyAll >::Publish(), and Rescan.

Referenced by Init(), AudioSetupToolBar::OnAudioDeviceRescan(), and anonymous_namespace{TransportMenus.cpp}::OnRescanDevices().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ dm

DeviceManager DeviceManager::dm
staticprotected

Definition at line 91 of file DeviceManager.h.

Referenced by Instance().

◆ m_inited

bool DeviceManager::m_inited
protected

Definition at line 86 of file DeviceManager.h.

Referenced by GetInputDeviceMaps(), GetOutputDeviceMaps(), and Rescan().

◆ mInputDeviceSourceMaps

std::vector<DeviceSourceMap> DeviceManager::mInputDeviceSourceMaps
protected

Definition at line 88 of file DeviceManager.h.

Referenced by GetDefaultDevice(), GetInputDeviceMaps(), and Rescan().

◆ mOutputDeviceSourceMaps

std::vector<DeviceSourceMap> DeviceManager::mOutputDeviceSourceMaps
protected

Definition at line 89 of file DeviceManager.h.

Referenced by GetDefaultDevice(), GetOutputDeviceMaps(), and Rescan().

◆ mRescanTime

std::chrono::time_point<std::chrono::steady_clock> DeviceManager::mRescanTime
private

Definition at line 74 of file DeviceManager.h.

Referenced by GetTimeSinceRescan(), and Rescan().


The documentation for this class was generated from the following files: