forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceManager.cpp
More file actions
356 lines (294 loc) · 10.2 KB
/
DeviceManager.cpp
File metadata and controls
356 lines (294 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2010 Audacity Team
Michael Chinen
******************************************************************/
#include "Audacity.h" // for USE_* macros
#include "DeviceManager.h"
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#include "portaudio.h"
#ifdef __WXMSW__
#include "pa_win_wasapi.h"
#endif
#ifdef USE_PORTMIXER
#include "portmixer.h"
#endif
#ifndef WX_PRECOMP
#include <wx/choice.h>
#include <wx/event.h>
#include <wx/intl.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/statbmp.h>
#include <wx/tooltip.h>
#endif
#include "Project.h"
#include "AudioIOBase.h"
#include "DeviceChange.h" // for HAVE_DEVICE_CHANGE
wxDEFINE_EVENT(EVT_RESCANNED_DEVICES, wxCommandEvent);
DeviceManager DeviceManager::dm;
/// Gets the singleton instance
DeviceManager* DeviceManager::Instance()
{
return &dm;
}
const std::vector<DeviceSourceMap> &DeviceManager::GetInputDeviceMaps()
{
if (!m_inited)
Init();
return mInputDeviceSourceMaps;
}
const std::vector<DeviceSourceMap> &DeviceManager::GetOutputDeviceMaps()
{
if (!m_inited)
Init();
return mOutputDeviceSourceMaps;
}
wxString MakeDeviceSourceString(const DeviceSourceMap *map)
{
wxString ret;
ret = map->deviceString;
if (map->totalSources > 1)
ret += wxT(": ") + map->sourceString;
return ret;
}
DeviceSourceMap* DeviceManager::GetDefaultDevice(int hostIndex, int isInput)
{
if (hostIndex < 0 || hostIndex >= Pa_GetHostApiCount()) {
return NULL;
}
const struct PaHostApiInfo *apiinfo = Pa_GetHostApiInfo(hostIndex); // get info on API
std::vector<DeviceSourceMap> & maps = isInput ? mInputDeviceSourceMaps : mOutputDeviceSourceMaps;
size_t i;
int targetDevice = isInput ? apiinfo->defaultInputDevice : apiinfo->defaultOutputDevice;
for (i = 0; i < maps.size(); i++) {
if (maps[i].deviceIndex == targetDevice)
return &maps[i];
}
wxLogDebug(wxT("GetDefaultDevice() no default device"));
return NULL;
}
DeviceSourceMap* DeviceManager::GetDefaultOutputDevice(int hostIndex)
{
return GetDefaultDevice(hostIndex, 0);
}
DeviceSourceMap* DeviceManager::GetDefaultInputDevice(int hostIndex)
{
return GetDefaultDevice(hostIndex, 1);
}
//--------------- Device Enumeration --------------------------
//Port Audio requires we open the stream with a callback or a lot of devices will fail
//as this means open in blocking mode, so we use a dummy one.
static int DummyPaStreamCallback(
const void *WXUNUSED(input), void * WXUNUSED(output),
unsigned long WXUNUSED(frameCount),
const PaStreamCallbackTimeInfo* WXUNUSED(timeInfo),
PaStreamCallbackFlags WXUNUSED(statusFlags),
void *WXUNUSED(userData) )
{
return 0;
}
static void FillHostDeviceInfo(DeviceSourceMap *map, const PaDeviceInfo *info, int deviceIndex, int isInput)
{
wxString hostapiName = wxSafeConvertMB2WX(Pa_GetHostApiInfo(info->hostApi)->name);
wxString infoName = wxSafeConvertMB2WX(info->name);
map->deviceIndex = deviceIndex;
map->hostIndex = info->hostApi;
map->deviceString = infoName;
map->hostString = hostapiName;
map->numChannels = isInput ? info->maxInputChannels : info->maxOutputChannels;
}
static void AddSourcesFromStream(int deviceIndex, const PaDeviceInfo *info, std::vector<DeviceSourceMap> *maps, PaStream *stream)
{
#ifdef USE_PORTMIXER
int i;
#endif
DeviceSourceMap map;
map.sourceIndex = -1;
map.totalSources = 0;
// Only inputs have sources, so we call FillHostDeviceInfo with a 1 to indicate this
FillHostDeviceInfo(&map, info, deviceIndex, 1);
#ifdef USE_PORTMIXER
PxMixer *portMixer = Px_OpenMixer(stream, 0);
if (!portMixer) {
maps->push_back(map);
return;
}
//if there is only one source, we don't need to concatenate the source
//or enumerate, because it is something meaningless like 'master'
//(as opposed to 'mic in' or 'line in'), and the user doesn't have any choice.
//note that some devices have no input sources at all but are still valid.
//the behavior we do is the same for 0 and 1 source cases.
map.totalSources = Px_GetNumInputSources(portMixer);
#endif
if (map.totalSources <= 1) {
map.sourceIndex = 0;
maps->push_back(map);
}
#ifdef USE_PORTMIXER
else {
//open up a stream with the device so portmixer can get the info out of it.
for (i = 0; i < map.totalSources; i++) {
map.sourceIndex = i;
map.sourceString = wxString(wxSafeConvertMB2WX(Px_GetInputSourceName(portMixer, i)));
maps->push_back(map);
}
}
Px_CloseMixer(portMixer);
#endif
}
static bool IsInputDeviceAMapperDevice(const PaDeviceInfo *info)
{
// For Windows only, portaudio returns the default mapper object
// as the first index after a NEW hostApi index is detected (true for MME and DS)
// this is a bit of a hack, but there's no other way to find out which device is a mapper,
// I've looked at string comparisons, but if the system is in a different language this breaks.
#ifdef __WXMSW__
static int lastHostApiTypeId = -1;
int hostApiTypeId = Pa_GetHostApiInfo(info->hostApi)->type;
if(hostApiTypeId != lastHostApiTypeId &&
(hostApiTypeId == paMME || hostApiTypeId == paDirectSound)) {
lastHostApiTypeId = hostApiTypeId;
return true;
}
#endif
return false;
}
static void AddSources(int deviceIndex, int rate, std::vector<DeviceSourceMap> *maps, int isInput)
{
int error = 0;
DeviceSourceMap map;
const PaDeviceInfo *info = Pa_GetDeviceInfo(deviceIndex);
// This tries to open the device with the samplerate worked out above, which
// will be the highest available for play and record on the device, or
// 44.1kHz if the info cannot be fetched.
PaStream *stream = NULL;
PaStreamParameters parameters;
parameters.device = deviceIndex;
parameters.sampleFormat = paFloat32;
parameters.hostApiSpecificStreamInfo = NULL;
parameters.channelCount = 1;
// If the device is for input, open a stream so we can use portmixer to query
// the number of inputs. We skip this for outputs because there are no 'sources'
// and some platforms (e.g. XP) have the same device for input and output, (while
// Vista/Win7 separate these into two devices with the same names (but different
// portaudio indices)
// Also, for mapper devices we don't want to keep any sources, so check for it here
if (isInput && !IsInputDeviceAMapperDevice(info)) {
if (info)
parameters.suggestedLatency = info->defaultLowInputLatency;
else
parameters.suggestedLatency = 10.0;
error = Pa_OpenStream(&stream,
¶meters,
NULL,
rate, paFramesPerBufferUnspecified,
paClipOff | paDitherOff,
DummyPaStreamCallback, NULL);
}
if (stream && !error) {
AddSourcesFromStream(deviceIndex, info, maps, stream);
Pa_CloseStream(stream);
} else {
map.sourceIndex = -1;
map.totalSources = 0;
FillHostDeviceInfo(&map, info, deviceIndex, isInput);
maps->push_back(map);
}
if(error) {
wxLogDebug(wxT("PortAudio stream error creating device list: ") +
map.hostString + wxT(":") + map.deviceString + wxT(": ") +
wxString(wxSafeConvertMB2WX(Pa_GetErrorText((PaError)error))));
}
}
/// Gets a NEW list of devices by terminating and restarting portaudio
/// Assumes that DeviceManager is only used on the main thread.
void DeviceManager::Rescan()
{
// get rid of the previous scan info
this->mInputDeviceSourceMaps.clear();
this->mOutputDeviceSourceMaps.clear();
// if we are doing a second scan then restart portaudio to get NEW devices
if (m_inited) {
// check to see if there is a stream open - can happen if monitoring,
// but otherwise Rescan() should not be available to the user.
auto gAudioIO = AudioIOBase::Get();
if (gAudioIO) {
if (gAudioIO->IsMonitoring())
{
gAudioIO->StopStream();
while (gAudioIO->IsBusy())
wxMilliSleep(100);
}
}
// restart portaudio - this updates the device list
// FIXME: TRAP_ERR restarting PortAudio
Pa_Terminate();
Pa_Initialize();
}
// FIXME: TRAP_ERR PaErrorCode not handled in ReScan()
int nDevices = Pa_GetDeviceCount();
//The hierarchy for devices is Host/device/source.
//Some newer systems aggregate this.
//So we need to call port mixer for every device to get the sources
for (int i = 0; i < nDevices; i++) {
const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
if (info->maxOutputChannels > 0) {
AddSources(i, info->defaultSampleRate, &mOutputDeviceSourceMaps, 0);
}
if (info->maxInputChannels > 0) {
#ifdef __WXMSW__
#if !defined(EXPERIMENTAL_FULL_WASAPI)
if (Pa_GetHostApiInfo(info->hostApi)->type != paWASAPI ||
PaWasapi_IsLoopback(i) > 0)
#endif
#endif
AddSources(i, info->defaultSampleRate, &mInputDeviceSourceMaps, 1);
}
}
// If this was not an initial scan update each device toolbar.
if ( m_inited ) {
wxCommandEvent e{ EVT_RESCANNED_DEVICES };
wxTheApp->ProcessEvent( e );
}
m_inited = true;
mRescanTime = std::chrono::steady_clock::now();
}
float DeviceManager::GetTimeSinceRescan() {
auto now = std::chrono::steady_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::duration<float>>(now - mRescanTime);
return dur.count();
}
//private constructor - Singleton.
DeviceManager::DeviceManager()
#if defined(EXPERIMENTAL_DEVICE_CHANGE_HANDLER)
#if defined(HAVE_DEVICE_CHANGE)
: DeviceChangeHandler()
#endif
#endif
{
m_inited = false;
mRescanTime = std::chrono::steady_clock::now();
}
DeviceManager::~DeviceManager()
{
}
void DeviceManager::Init()
{
Rescan();
#if defined(EXPERIMENTAL_DEVICE_CHANGE_HANDLER)
#if defined(HAVE_DEVICE_CHANGE)
DeviceChangeHandler::Enable(true);
#endif
#endif
}
#if defined(EXPERIMENTAL_DEVICE_CHANGE_HANDLER)
#if defined(HAVE_DEVICE_CHANGE)
void DeviceManager::DeviceChangeNotification()
{
Rescan();
return;
}
#endif
#endif