-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get_microphone(), get_speaker(): Improved comparison between the call parameter "id" and existing device names #147
base: master
Are you sure you want to change the base?
Conversation
Functions _match_device()/_match_soundcard() in the modules soundcard.coreaudio, soundcard.mediafoundation, soundcard.pulseaudio replaced with soundcard.utils.match_device().
- Distinguish between real devices and loopback devices. Try a lookup for real devices first; when one of them matches, return it before trying to find a matching loopback device. - When a device name for a loopback device is also a substring of the name of a real device, the loopback device with the completely matching name is returned. - Symbols with a special meaning in regular expressions are escaped when pattern for regex matching is built. - Call of re.match() replaced with re.search() so that the "search parameter" passed to match_device does not have to match the start of a device name. 2. Obsolete "import re" removed from modules soundcard.coreaudio, soundcard.mediafoundation, soundcard.pulseaudio.
That's a great addition, thank you! And you factored out the common code as well, and added some platform-independent tests! Thank you so much! Here are pairs of device IDs, device names, and isloopback on my Windows box: Mics:
Speakers:
|
And a few id/names from macOS:
Speakers:
|
I think I agree with your assessment that the two-stage search of no-loopbacks before loopbacks is actually overkill. Your first solution with the substring match is perfectly acceptable to me, and much more elegant. I would probably add a note in the docstring of |
- check if an integer ID was provided before attempting to match strings. - re.escape() now used to escape possible special symbols in ID strings instead of checks against a handcrafted list of symbols. - tests: Fake devices with IDs and names as used in MacOS and Windows added.
Thanks for the review! I think I addressed all issues you mentioned. I kept though the "two-stage search" for now as it can in some cases make sense, I believe. Also thanks for the examples of Windows and MacOS IDs/device names. Added to the test data. |
This looks good to me! I'll test this code on Windows and macOS next week, and then we can merge! Thank you very much! |
This PR fixes the problems mentioned in #146 , "Problems with the "fuzzy matcher" in
get_microphone()
.The changes are a bit longer than I expected at first, but anyway:
I moved the functions
_match_device()
from the modulescoreaudio
andmediafoundation
into a new moduleutils
so that the same implementation can be used for all platforms.pulseaudio
has of course a similar change to useutils.match_device()
.Changes to match_device():
match_device()
: Before the function checks if the call parameterid
is a substring of one of the device names, it checks if the call parameter identical with any of the device names. While I also added your suggestion to separate real and loopback devices in name lookups (see below), there still is at least in theory still a corner case without this "name identity lookup": A loopback device could have a name that is the substring of a real microphone. Admitted, not very likely, but you never know ;) If only a substring lookup for the loopback device is done where real devices have precedence, the loopback device would not be returned. (See also the diff of test_soundcard, line 229. That test will fail without the additional "full name check".)Final note: I am not too happy with the tests I wrote: I think it would be nice to have some device names and IDs from Windows and MacOS too in the tests. Problem, as already mentioned in #146 : I have neither a Mac nor a machine with a Windows installation. But I'm happy to add device names from MacOS/Windows if I get some examples.