Skip to content

Commit e2d98a2

Browse files
committed
portaudio_select_device_by_name: improve selection
return available device in this order: 1. exact match; if not found, then: 2. prefix match; if not found, then: 3. any substr match
1 parent 90e1dce commit e2d98a2

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/audio/portaudio_common.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,29 @@ int
245245
portaudio_select_device_by_name(const char *name,
246246
enum portaudio_device_direction dir)
247247
{
248+
int prefix_idx = -1; // dev idx starting with name
249+
int substr_idx = -1; // dev idx with name as substr
248250
for (int i = 0; i < Pa_GetDeviceCount(); i++) {
249251
const PaDeviceInfo *device_info = Pa_GetDeviceInfo(i);
250252
if (!has_channels(device_info, dir)) {
251253
continue;
252254
}
253-
if (strstr(device_info->name, name)) {
255+
if (strcmp(device_info->name, name) == 0) { // exact match
254256
return i;
255257
}
258+
if (strstr(device_info->name, name)) {
259+
substr_idx = i;
260+
if (strstr(device_info->name, name) ==
261+
device_info->name) {
262+
prefix_idx = i;
263+
}
264+
}
265+
}
266+
if (prefix_idx != -1) {
267+
return prefix_idx;
268+
}
269+
if (substr_idx != -1) {
270+
return substr_idx;
256271
}
257272
log_msg(LOG_LEVEL_ERROR, MOD_NAME "No device named \"%s\" was found!\n", name);
258273
return -2;

0 commit comments

Comments
 (0)