python-sounddevice icon indicating copy to clipboard operation
python-sounddevice copied to clipboard

stream.time raises when the time is 0.0 (PortAudioError: Error getting stream time.. on RaspberyPI)

Open RmStorm opened this issue 6 months ago • 2 comments

When I run:

        self.stream = sd.InputStream(device=self.device, channels=1, samplerate=self.fs, blocksize=self.block_size, callback=cb)
        self.stream.start()
        self.stream.time

on my laptop it works.. but when I run it on a raspbery pi I get:

│ /home/monil/repos/silo/tools/scripts/.venv/lib/python3.12/site-packages/sounddevice.py:1078 in time                                                                                        │
│                                                                                                                                                                                            │
│   1075 │   │   """                                                                              ╭───────────────────────── locals ──────────────────────────╮                              │
│   1076 │   │   time = _lib.Pa_GetStreamTime(self._ptr)                                          │ self = <sounddevice.InputStream object at 0x7fff5eb3d040> │                              │
│   1077 │   │   if not time:                                                                     │ time = 0.0                                                │                              │
│ ❱ 1078 │   │   │   raise PortAudioError('Error getting stream time')                            ╰───────────────────────────────────────────────────────────╯                              │
│   1079 │   │   return time      

This probably happens because the raspberry is slow and the time is literally 0.0 which is falsy breaking this check.

I think that is the case because adding a tiny sleep(0.1) before reading the property fixes the issue..

RmStorm avatar May 30 '25 11:05 RmStorm

Thanks for reporting this!

the time is literally 0.0 which is falsy breaking this check.

This is intentional, because the docs state: "The stream's current time in seconds, or 0 if an error occurred."

adding a tiny sleep(0.1) before reading the property fixes the issue.

That's interesting, but it sounds like a bug in PortAudio to me.

When Pa_StartStream() returns (which we are waiting for in stream.start()), Pa_GetStreamTime() should return a non-null value.

Would you like to create an issue at https://github.com/PortAudio/portaudio/issues?

mgeier avatar May 30 '25 17:05 mgeier

I guess it would be nice to verify a little more but I don't really have time to do that. I'm just living with the sleep now haha. A cursory glance at portaudio makes it seem plausible though. They don't verify the result !=0 before returning it I think:

https://github.com/PortAudio/portaudio/blob/19640aad8f2f79fb9cd14e20eb1410237b98377f/src/common/pa_front.c#L1610-L1617

I'll make an issue over there and see what comes of it.

RmStorm avatar Jun 02 '25 09:06 RmStorm