From f1fcaaf387e22e2470254515a5c09e91dce0bebf Mon Sep 17 00:00:00 2001 From: blattm Date: Fri, 23 Jul 2021 20:48:20 +0200 Subject: [PATCH 1/4] Allow exact matches to include the hostapi's name --- sounddevice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sounddevice.py b/sounddevice.py index 9998264..5e598ec 100644 --- a/sounddevice.py +++ b/sounddevice.py @@ -2778,7 +2778,7 @@ def _get_device_id(id_or_query_string, kind, raise_on_error=False): pos += len(substring) else: matches.append((id, full_string)) - if device_string.lower() == query_string: + if query_string in [device_string.lower(), full_string.lower()]: exact_device_matches.append(id) if kind is None: From 7c36abcabd0b0cfe7510ddb6e855bdf61d694714 Mon Sep 17 00:00:00 2001 From: blattm Date: Sat, 24 Jul 2021 13:56:47 +0200 Subject: [PATCH 2/4] Allow matching device name exactly, hostapi fuzzy --- sounddevice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sounddevice.py b/sounddevice.py index 5e598ec..72a3730 100644 --- a/sounddevice.py +++ b/sounddevice.py @@ -2778,7 +2778,7 @@ def _get_device_id(id_or_query_string, kind, raise_on_error=False): pos += len(substring) else: matches.append((id, full_string)) - if query_string in [device_string.lower(), full_string.lower()]: + if query_string[:len(device_string)].lower() == device_string.lower(): exact_device_matches.append(id) if kind is None: From 2eac0e7b187e07d85a6afe68c64847c2f1cf1dca Mon Sep 17 00:00:00 2001 From: blattm Date: Sat, 24 Jul 2021 13:57:50 +0200 Subject: [PATCH 3/4] Allow exact matches without ALSA identifier --- sounddevice.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sounddevice.py b/sounddevice.py index 72a3730..52254f8 100644 --- a/sounddevice.py +++ b/sounddevice.py @@ -53,6 +53,7 @@ import atexit as _atexit import os as _os import platform as _platform +import re as _re import sys as _sys from ctypes.util import find_library as _find_library from _sounddevice import ffi as _ffi @@ -2737,6 +2738,10 @@ def _check(err, msg=''): raise PortAudioError(errormsg, err) +def _remove_alsa_id(device_string): + return _re.sub(" \(hw:[0-9]+,[0-9]+\)$", "", device_string) + + def _get_device_id(id_or_query_string, kind, raise_on_error=False): """Return device ID given space-separated substrings.""" assert kind in ('input', 'output', None) @@ -2770,6 +2775,7 @@ def _get_device_id(id_or_query_string, kind, raise_on_error=False): exact_device_matches = [] for id, device_string, hostapi_string in device_list: full_string = device_string + ', ' + hostapi_string + device_string = _remove_alsa_id(device_string) pos = 0 for substring in substrings: pos = full_string.lower().find(substring, pos) From f397c805f347f92014f7abd944f4357fd2962b77 Mon Sep 17 00:00:00 2001 From: blattm Date: Sat, 24 Jul 2021 14:07:08 +0200 Subject: [PATCH 4/4] Fix PEP 8 issues --- sounddevice.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sounddevice.py b/sounddevice.py index 52254f8..676e335 100644 --- a/sounddevice.py +++ b/sounddevice.py @@ -2739,7 +2739,7 @@ def _check(err, msg=''): def _remove_alsa_id(device_string): - return _re.sub(" \(hw:[0-9]+,[0-9]+\)$", "", device_string) + return _re.sub(r" \(hw:[0-9]+,[0-9]+\)$", "", device_string) def _get_device_id(id_or_query_string, kind, raise_on_error=False): @@ -2784,7 +2784,10 @@ def _get_device_id(id_or_query_string, kind, raise_on_error=False): pos += len(substring) else: matches.append((id, full_string)) - if query_string[:len(device_string)].lower() == device_string.lower(): + if ( + query_string[: len(device_string)].lower() + == device_string.lower() + ): exact_device_matches.append(id) if kind is None: