playing sine wave on raspberry pi has 25+ seconds delay before sound starts to play
See original GitHub issueI have been testing sounddevice to play a sine wave (based on the code example in the docs). Everything works - but after I start the output stream, it takes about 25-30 seconds before the sine wave sound starts to play. On a Raspberry pi 3B - I did a clean latest rasbian buster light install - running headless (SSH from mac), and only installed what was necessary for sounddevice and soundfile. Installed python 3.9.4 - but I had the same problem when I was running 3.7 that came with the OS (playing the wave file example). I can use aplay on the RPi to play a sound wave no problem (no delay)
I initially used the code from sine wave example with outputstream(...)
- and but then changed to creating the stream first and start it separately: same result.
I inserted a bunch of calls to time.time() - I can see the stream starts right away but the first call to the callback takes 25-30 seconds.
My Code:
import sounddevice as sd
import numpy as np
start_idx = 0
cnt=1
sd.default.device=0
try:
samplerate = 44100
def callback(outdata, frames, time, status):
if status:
print(status, file=sys.stderr)
global start_idx
global cnt
t = (start_idx + np.arange(frames)) / samplerate
t = t.reshape(-1, 1)
outdata[:] = 1 * np.sin(2 * np.pi * 440 * t)
start_idx += frames
cnt+=1
if cnt>20:
print('count =',cnt)
raise sd.CallbackStop
myStream=sd.OutputStream(device=0, channels=1, callback=callback,
samplerate=samplerate)
myStream.start()
print('stream is active: ',myStream.active). #This shows True
print('cpu load = ',myStream.cpu_load). #This shows 0.0
input() # (I wait until I see count=21 printed in terminal and sound stops in headphones, then hit return )
myStream.stop()
myStream.close()
For info - sd.query_devices() yields:
* 0 bcm2835 Headphones: - (hw:0,0), ALSA (0 in, 8 out)
1 Logitech USB Microphone: Audio (hw:1,0), ALSA (1 in, 0 out)
2 sysdefault, ALSA (0 in, 128 out)
3 output, ALSA (0 in, 8 out)
4 dmix, ALSA (0 in, 2 out)
5 default, ALSA (0 in, 128 out)
any ideas why this happens? could it be a PortAudio issue on the pi - nothing to do with sounddevice?
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (4 by maintainers)
Top GitHub Comments
I believe this can be closed then.
Good news!
solves it 😃