larsimmisch / pyalsaaudio

ALSA wrappers for Python
http://larsimmisch.github.io/pyalsaaudio/
Other
214 stars 74 forks source link

How come this plays wav file when pyaudio couldn't? #127

Closed talksik closed 1 year ago

talksik commented 1 year ago

Hey there, I have been trying to diagnose an issue with pyaudio.

When trying to play back a wav file from online that has a sampling rate of 22050, it gives me an error "invalid sample rate". This is with a raspberry pi zero that has a mic/amp setup. This is with pyaudio. Pyaudio does play files that are 48kHz sampling rate likely because the output device has that rate as a default so it has no issues.

Now, the same audio file played with aplay x.wav works.

Also, the same file with this library (pyalsaaudio) works.

Is this because alsa resamples under the hood and converts to the correct rate? Doesn't pyaudio use alsa under the hood, and so why wouldn't it work with pyaudio?

Thank you!

larsimmisch commented 1 year ago

I'm sure that alsaaudio does not resample under the hood, and portaudio is on top of alsaaudio AFAIK, so this is indeed mystifying. Are you maybe opening a different device on alsaaudio vs. on portaudio?

I'll close this issue, because it's not really a pyalsaaudio issue. Feel free to comment anyway.

talksik commented 1 year ago

Thanks @larsimmisch for the response!

When trying pyalsa with 'default' device and pyaudio with that same device index (4), pyaudio comes out with unanticipated host error while pyalsa still works.

It is odd. There is some check going on in alsa https://github.com/EddieRingle/portaudio/blob/9eb5f0b3d820a81d385504d9c54534abbeea1099/src/hostapi/alsa/pa_linux_alsa.c#L3166

This might be the reason, but I am unable to trace down the root cause.

For more information, if it helps, when I print out my custom device info method, this is what I get. It seems that my sound card (google voice hat) is set for 48kHz. My amp is set for a wide range, but it seems that the kernel can't pick that up or something. The file I am playing is 22050 sampling rate. I am unsure the difference between the sound card, alsaconfig, and other things that are needed to make this work.

image

Any ideas? Appreciate you :)