spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
1.02k stars 149 forks source link

Error starting stream, wait timed out, rpiOS 64-bit #389

Open Gerekt opened 2 years ago

Gerekt commented 2 years ago

Enviroment: Raspberry Pi OS 64 bit Sounddevice version: 1246720 PortAudio V19.6.0-devel, (0.4.4) Python verison: 3.9.2 Platform: Raspberry Pi 4B with IQ audioCodec Zero HAT(Can record and play audio)

Im having a problem when trying to use the HAT for recording (or playing) with the sounddevice library. When trying to run one of the example scripts from the documentation, the following error occurs:

$python3 play_file.py -l
0 vc4-hdmi-1: 
1 IQaudIOCODEC: CODEC HiFi v1.2 (hw:2,0), ALSA (2 in, 2 out)
2 pulse, ALSA(32 in, 32 out)
3 default, ALSA(32 in, 32 out)
$python3 play_file.py -d 1 /home/pi/test3.wav
Expression ' paTimedOut' failed in 'src/os/unix/pa_unix_util.c', line: 387
Expression 'paUnixThread_New( &stream->thread, &CallbackThreadFunc, stream, 1 .,  stream->rtSched ) ' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line:2998
**PortAudioError: Error starting stream: Wait timed out [PaErrorCode -9987]**
import sounddevice as sd
sample_rate = 44100

print("Version: " ,sd.get_portaudio_version())
print("First query: " ,sd.query_devices())

sd.default.device = 1
#sd.default.samplerate = sample_rate
#sd.default.channels = 2

print("Second query: ", sd.query_devices(device = 1, kind='input'))
print("Input settings: " , sd.check_input_settings(device = 1))
try:
    myrecording = sd.rec(int(2 * sample_rate), samplerate = sample_rate, channels = 2)
except ValueError as err:
    print("error", err.args)

Which gives the output:

Version:  (1246720, 'PortAudio V19.6.0-devel, revision 396fe4b6699ae929d3a685b3ef8a7e97396139a4')
First query:    0 vc4-hdmi-1: MAI PCM i2s-hifi-0 (hw:1,0), ALSA (0 in, 2 out)
  1 IQaudIOCODEC: CODEC HiFi v1.2 da7213-hifi-0 (hw:2,0), ALSA (2 in, 2 out)
  2 pulse, ALSA (32 in, 32 out)
* 3 default, ALSA (32 in, 32 out)
* 
Second query:  {'name': 'IQaudIOCODEC: CODEC HiFi v1.2 da7213-hifi-0 (hw:2,0)', 'hostapi': 0, 'max_input_channels': 2, 'max_output_channels': 2, 'default_low_input_latency': 0.005804988662131519, 'default_low_output_latency': 0.005804988662131519, 'default_high_input_latency': 0.034829931972789115, 'default_high_output_latency': 0.034829931972789115, 'default_samplerate': 44100.0}

Input settings:  None

Expression 'paTimedOut' failed in 'src/os/unix/pa_unix_util.c', line: 387
Expression 'PaUnixThread_New( &stream->thread, &CallbackThreadFunc, stream, 1., stream->rtSched )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2998
Traceback (most recent call last):
  File "/home/pi/tensorflowRPI/selectMicrophone.py", line 14, in <module>
    myrecording = sd.rec(int(2 * sample_rate), samplerate = sample_rate, channels = 2)
  File "/home/pi/.local/lib/python3.9/site-packages/sounddevice.py", line 274, in rec
    ctx.start_stream(InputStream, samplerate, ctx.input_channels,
  File "/home/pi/.local/lib/python3.9/site-packages/sounddevice.py", line 2582, in start_stream
    self.stream.start()
  File "/home/pi/.local/lib/python3.9/site-packages/sounddevice.py", line 1107, in start
    _check(err, 'Error starting stream')
  File "/home/pi/.local/lib/python3.9/site-packages/sounddevice.py", line 2741, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error starting stream: Wait timed out [PaErrorCode -9987]
Expression 'pthread_join( self->thread, &pret )' failed in 'src/os/unix/pa_unix_util.c', line: 441
Expression 'PaUnixThread_Terminate( &stream->thread, !abort, &threadRes )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 3102

The most logical explanation would be that i have not configured the IQaudio HAT correctly, however when i record a file using arecord, it functions at it should.

arecord -l
**** List of CAPTURE Hardware Devices ****
card 2: IQaudIOCODEC [IQaudIOCODEC], device 0: IQaudIO CODEC HiFi v1.2 da7213-hifi-0 [IQaudIO CODEC HiFi v1.2 da7213-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
arecord -D "hw:2,0" -f S16_LE -d 10 -V stereo -r 44100 -c 2 githubTest.wav
Recording WAVE 'githubTest.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
+################################# 99%|99%##################################+

The last line with the ###, shows the current output of the microphone. I can see that it works when i talk or touch the mic.

My question is, what is going wrong with the sounddevice library. Why does is not seem to want to start a stream?

mgeier commented 2 years ago

I don't know, those are error messages from the underlying PortAudio library.

You could try the PortAudio example programs to see if they cause the same errors.

You could also try the blocking API instead of the callback API (which is used internally by sd.rec()).

You could also ask the PortAudio community: http://portaudio.com/contacts.html