Skip to content
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

Added index search for get_microphone() #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yuannan
Copy link

@yuannan yuannan commented Feb 19, 2021

as stated by the docs:

Parameters: | id (int or str) – can be an int index, a backend id, a substring of the speaker name, or a fuzzy-matched pattern for the speaker name.

This is not true and caused me to bash my face on my computer for all of last night before I thought it could be an upstream issue. Please merge this. Thanks.

@yuannan yuannan changed the title Index search for mics with works with ints Added index search for get_microphone() Feb 19, 2021
@yuannan
Copy link
Author

yuannan commented Feb 20, 2021

There is a minor bug with this implementation, sound card IDs change and are not static. This could lead to issues if cards are changed around such as unplugging and plugging a device.

This could be fixed by redefining source_list() as a properly indexed list with the index matching up with the sink number. This could lead to more memory usage but this is at most a few array elements. I'm happy to make this patch as well if you don't want to. I'm just worried that changing this around would mess up your other code.

Copy link
Owner

@bastibe bastibe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Thank you for debugging and fixing this issue. Please have a look at my two comments, before this can be merged.

@@ -369,7 +369,7 @@ def get_microphone(id, include_loopback=False, exclude_monitors=True):
include_loopback = not exclude_monitors

microphones = _pulse.source_list
return _Microphone(id=_match_soundcard(id, microphones, include_loopback)['id'])
return _Microphone(id=_match_soundcard(str(id), microphones, include_loopback)['id'])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the case should not happen here, but later on in _match_soundcard. Unless I am misunderstanding something.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. The crash happens on when it tries to pattern match the int to a str within the if statements.

This mitigation is actually completely pointless now. As with the "naive try return" it will return iff it's an int and it's indexed. Only strings or larger numbers will fail(#125 (comment)). I've removed it for now.

If this bug comes back in the future simply put "id = str(id)" at the start of _match_soundcard().

try:
return soundcards[int(id)]
except (ValueError):
pass
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should probably be something like if int(id) in soundcards instead of a try/catch.

Copy link
Author

@yuannan yuannan Feb 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but that adds a lot more complexity. Like I said I have to change a few thing around first. The current source and sink info doesn't even have numerical IDs (afiak anyway). This would take much more work to first update the info obtained from CFFI and then construct a new soundcards with proper info.

This was just a simply one stop shop hack I put together to test my project.

This should be done for readability and future maintenance however. Gimme the go ahead and I'll try and fix it up once and for all if I get some time this week.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely go ahead and investigate it fully. But take it easy; there's absolutely no rush, so do it on your own time.

Let me know when you think it's ready!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants