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

WASAPI streams cannot from a thread different to the thread the pyaudio interface was created in #23

Open
4 of 8 tasks
NSGauge opened this issue Nov 30, 2024 · 1 comment

Comments

@NSGauge
Copy link

NSGauge commented Nov 30, 2024

What problem are you facing?

  • audio isn`t recorded
  • audio is recorded with artifacts
  • problem with "silence"
  • other

What is the cause of the error (in your opinion)?

  • PyAudio\PortAudio bug
  • I just need help(or answer)

Before creating, complete the checklist:

If the problem is related to the inability to record sound from speakers,
then before creating this issue, try to first record audio using the provided examples
(for example, simple_recording_app.py)

Start your issue here 👇

Script that has examples mentioned below and demonstrates the bug

  • Trying to open a WASAPI stream using a PyAudio instance created in a different thread causes an unanticipated host error (example 2)
  • This issue only applies to WASAPI interfaces (example 3).
  • Creating the stream from the same thread as the PyAudio instance was works fine (example 1 and 4)
@s0d3s
Copy link
Owner

s0d3s commented Dec 4, 2024

Hi🖐 Thanks for your time and attention to this repo!
But unfortunately, this issue relates to the limitations of "original" PyAudio design of wrapping PortAudio lib, and fix it here will be incongruously. PyAudio has or may have issues with multi-threading, depends on the target drivers which will be used. And here np guarantee that any of "APIs" will work in this mode in 100% cases.

So to use PyAudio(or in our case -WPatch) in multi-threading environment, you must use a little trick: do not share data between threads - pass it instead.

Simplest way to do it - is using pipes or queues:

  • Open PyAudio session and record audio stream in one thread
  • Pass recorded data to the queue(which is shared with another thread)
  • Read data from the queue in another thread

You can use:

  • queue (bult-in python lib)
  • janus (lib to exchange data between async and multi-threading environments)
  • or other implementations or even just "pipes" may work well

This approach was tested on my projects and was working.
Thanks again for your experiment, and sorry that it won't result in code changes

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

No branches or pull requests

2 participants