Skip to content

Commit 90e1dce

Browse files
committed
portaudio: selection by name - filter out unusable
do not return not usable deviecs - eg. "Microsoft Sound Mapper" doesn't return "Microsoft Sound Mapper - Input" device if playback wanted but "- Output" one.
1 parent 8039c9d commit 90e1dce

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

src/audio/capture/portaudio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ static void * audio_cap_portaudio_init(struct module *parent, const char *cfg)
203203

204204
// default device
205205
if (strlen(input_device_name) > 0) {
206-
input_device_idx = portaudio_select_device_by_name(input_device_name);
206+
input_device_idx = portaudio_select_device_by_name(
207+
input_device_name, PORTAUDIO_IN);
207208
}
208209
if (input_device_idx == -1) {
209210
log_msg(LOG_LEVEL_NOTICE, MOD_NAME "Using default input audio device: %s\n",

src/audio/playback/portaudio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ audio_play_portaudio_init(const struct audio_playback_opts *opts)
196196
}
197197

198198
if (strlen(output_device_name) > 0) {
199-
output_device_idx = portaudio_select_device_by_name(output_device_name);
199+
output_device_idx = portaudio_select_device_by_name(
200+
output_device_name, PORTAUDIO_OUT);
200201
}
201202

202203
struct state_portaudio_playback *s = calloc(1, sizeof *s);

src/audio/portaudio_common.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ portaudio_get_device_details(PaDeviceIndex device,
9494
return buffer;
9595
}
9696

97+
static bool
98+
has_channels(const PaDeviceInfo *device_info, enum portaudio_device_direction dir)
99+
{
100+
101+
if ((dir == PORTAUDIO_IN && device_info->maxInputChannels == 0) ||
102+
device_info->maxOutputChannels == 0) {
103+
return false;
104+
}
105+
return true;
106+
}
107+
97108
void
98109
portaudio_print_help(enum portaudio_device_direction kind, bool full)
99110
{
@@ -138,8 +149,7 @@ portaudio_print_help(enum portaudio_device_direction kind, bool full)
138149
const char *highlight = TERM_BOLD;
139150
const PaDeviceInfo *device_info = Pa_GetDeviceInfo(i);
140151
// filter out (or differently highlight in verbose mode) unusable devices
141-
if ((device_info->maxInputChannels == 0 && kind == PORTAUDIO_IN) ||
142-
(device_info->maxOutputChannels == 0 && kind == PORTAUDIO_OUT)) {
152+
if (has_channels(device_info, kind)) {
143153
if (log_level < LOG_LEVEL_VERBOSE) {
144154
continue;
145155
} else {
@@ -231,14 +241,19 @@ void portaudio_print_version() {
231241
* @retval -2 device selection failed
232242
* @retval >=0 selected device
233243
*/
234-
int portaudio_select_device_by_name(const char *name) {
244+
int
245+
portaudio_select_device_by_name(const char *name,
246+
enum portaudio_device_direction dir)
247+
{
235248
for (int i = 0; i < Pa_GetDeviceCount(); i++) {
236249
const PaDeviceInfo *device_info = Pa_GetDeviceInfo(i);
250+
if (!has_channels(device_info, dir)) {
251+
continue;
252+
}
237253
if (strstr(device_info->name, name)) {
238254
return i;
239255
}
240256
}
241257
log_msg(LOG_LEVEL_ERROR, MOD_NAME "No device named \"%s\" was found!\n", name);
242258
return -2;
243259
}
244-

src/audio/portaudio_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ const char *portaudio_get_device_name(PaDeviceIndex device);
5656
void audio_portaudio_probe(struct device_info **available_devices, int *count, enum portaudio_device_direction);
5757
void portaudio_print_version(void);
5858
// documented at definition
59-
int portaudio_select_device_by_name(const char *name);
59+
int portaudio_select_device_by_name(const char *name,
60+
enum portaudio_device_direction dir);
6061

6162
#ifdef __cplusplus
6263
}

0 commit comments

Comments
 (0)