Skip to content
Advertisement

Continuous Audio Playback with Sounddevice

I am writing a program to stream audio over a network, so I have a thread to record data and one to send it. When testing the audio has noticeable gaps. I beleive this is due to the sounddevice.play() function, the example below has the same problem.

import sounddevice as sd

len = 5
fs = 44100
sd.default.device = [2,1]

myrec=sd.rec(int(fs*len), samplerate=fs, channels=2, blocking=True) #fill an array with some sound
while True:
    sd.play(myrec, blocking=True)
    #loop plays 5 second audio clip with slight gaps

The gaps coincide with the play length, so it appears to be caused by a delay in the play function. In continious audio this becomes very noticeable and annoying. The same thing also happens in the documentation audio passthrough example here.

Is there any was to make the playback continuous?

Advertisement

Answer

The function sd.play() is not meant to be used repeatedly in rapid succession. Internally, it each time creates an sd.OutputStream, plays the audio data and closes the stream again. Because of opening and closing the stream, gaps will occur. This is expected.

For continuous playback you should use the sd.OutputStream.write() function or, even better, an sd.OutputStream with a custom callback function (as shown in some of the example programs).

The same thing also happens in the documentation audio passthrough example here.

This must have a different reason. This should work, and it works fine for me. Can you please describe the problem in more detail? Are there any messages printed to the terminal?

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement