Audacity  3.0.3
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 ()
 
float GetTimeSinceRescan ()
 
DeviceSourceMapGetDefaultOutputDevice (int hostIndex)
 
DeviceSourceMapGetDefaultInputDevice (int hostIndex)
 
const std::vector< DeviceSourceMap > & GetInputDeviceMaps ()
 
const std::vector< DeviceSourceMap > & GetOutputDeviceMaps ()
 

Static Public Member Functions

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

Protected Member Functions

 DeviceManager ()
 
 ~DeviceManager ()
 
void Init ()
 
DeviceSourceMapGetDefaultDevice (int hostIndex, int isInput)
 

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
 

Detailed Description

A singleton that manages the audio devices known to Audacity.

Definition at line 49 of file DeviceManager.h.

Constructor & Destructor Documentation

◆ DeviceManager()

DeviceManager::DeviceManager ( )
protected

Definition at line 316 of file DeviceManager.cpp.

319 : DeviceChangeHandler()
320 #endif
321 #endif
322 {
323  m_inited = false;
324  mRescanTime = std::chrono::steady_clock::now();
325 }

References m_inited, and mRescanTime.

◆ ~DeviceManager()

DeviceManager::~DeviceManager ( )
protected

Definition at line 327 of file DeviceManager.cpp.

328 {
329 
330 }

Member Function Documentation

◆ GetDefaultDevice()

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

Definition at line 64 of file DeviceManager.cpp.

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

References mInputDeviceSourceMaps, and mOutputDeviceSourceMaps.

Referenced by GetDefaultInputDevice(), and GetDefaultOutputDevice().

Here is the caller graph for this function:

◆ GetDefaultInputDevice()

DeviceSourceMap * DeviceManager::GetDefaultInputDevice ( int  hostIndex)

Definition at line 88 of file DeviceManager.cpp.

89 {
90  return GetDefaultDevice(hostIndex, 1);
91 }

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:

◆ GetDefaultOutputDevice()

DeviceSourceMap * DeviceManager::GetDefaultOutputDevice ( int  hostIndex)

Definition at line 84 of file DeviceManager.cpp.

85 {
86  return GetDefaultDevice(hostIndex, 0);
87 }

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 40 of file DeviceManager.cpp.

41 {
42  if (!m_inited)
43  Init();
45 }

References Init(), m_inited, and mInputDeviceSourceMaps.

Referenced by DeviceToolBar::ChangeDevice(), DeviceToolBar::FillHostDevices(), DeviceToolBar::FillHosts(), DeviceToolBar::FillInputChannels(), DevicePrefs::OnHost(), 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 46 of file DeviceManager.cpp.

47 {
48  if (!m_inited)
49  Init();
51 }

References Init(), m_inited, and mOutputDeviceSourceMaps.

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

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

◆ GetTimeSinceRescan()

float DeviceManager::GetTimeSinceRescan ( )

Definition at line 308 of file DeviceManager.cpp.

308  {
309  auto now = std::chrono::steady_clock::now();
310  auto dur = std::chrono::duration_cast<std::chrono::duration<float>>(now - mRescanTime);
311  return dur.count();
312 }

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 332 of file DeviceManager.cpp.

333 {
334  Rescan();
335 
336 #if defined(EXPERIMENTAL_DEVICE_CHANGE_HANDLER)
337 #if defined(HAVE_DEVICE_CHANGE)
338  DeviceChangeHandler::Enable(true);
339 #endif
340 #endif
341 }

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

Gets the singleton instance.

Definition at line 35 of file DeviceManager.cpp.

36 {
37  return &dm;
38 }

References dm.

Referenced by DeviceToolBar::ChangeDevice(), DeviceToolBar::DeviceToolBar(), DeviceToolBar::FillHostDevices(), DeviceToolBar::FillHosts(), DeviceToolBar::FillInputChannels(), DevicePrefs::OnHost(), TransportActions::Handler::OnRescanDevices(), AudioIO::StartPortAudioStream(), and DeviceToolBar::UpdatePrefs().

Here is the caller graph for this function:

◆ 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 247 of file DeviceManager.cpp.

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

References AddSources(), AudioIOBase::Get(), m_inited, mInputDeviceSourceMaps, mOutputDeviceSourceMaps, and mRescanTime.

Referenced by Init(), and TransportActions::Handler::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 98 of file DeviceManager.h.

Referenced by Instance().

◆ m_inited

bool DeviceManager::m_inited
protected

Definition at line 93 of file DeviceManager.h.

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

◆ mInputDeviceSourceMaps

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

Definition at line 95 of file DeviceManager.h.

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

◆ mOutputDeviceSourceMaps

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

Definition at line 96 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 81 of file DeviceManager.h.

Referenced by DeviceManager(), GetTimeSinceRescan(), and Rescan().


The documentation for this class was generated from the following files:
DeviceManager::dm
static DeviceManager dm
Definition: DeviceManager.h:98
DeviceManager::Rescan
void Rescan()
Definition: DeviceManager.cpp:247
DeviceManager::mInputDeviceSourceMaps
std::vector< DeviceSourceMap > mInputDeviceSourceMaps
Definition: DeviceManager.h:95
DeviceManager::m_inited
bool m_inited
Definition: DeviceManager.h:93
DeviceManager::GetDefaultDevice
DeviceSourceMap * GetDefaultDevice(int hostIndex, int isInput)
Definition: DeviceManager.cpp:64
MyEvent
Definition: Prefs.cpp:77
DeviceManager::Init
void Init()
Definition: DeviceManager.cpp:332
DeviceManager::mOutputDeviceSourceMaps
std::vector< DeviceSourceMap > mOutputDeviceSourceMaps
Definition: DeviceManager.h:96
AudioIOBase::Get
static AudioIOBase * Get()
Definition: AudioIOBase.cpp:89
AddSources
static void AddSources(int deviceIndex, int rate, std::vector< DeviceSourceMap > *maps, int isInput)
Definition: DeviceManager.cpp:182
DeviceManager::mRescanTime
std::chrono::time_point< std::chrono::steady_clock > mRescanTime
Definition: DeviceManager.h:81