3

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?

MikeS159
  • 1,884
  • 3
  • 29
  • 54

1 Answers1

3

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?

Matthias
  • 4,524
  • 2
  • 31
  • 50
  • When I use the wire.py example I call it using `python3 wire.py -i 2 -o 1 -c 2 -s 44100` which doesn't provide and audio output. If I use the -l argument with value 1 I heard the mic audio with a 1s delay, but there is a noticeable pause ever ~5s. I can ignore -l and use -b 8192 which gives lower latency playback, but still with the noticeable gaps ~5/6s. The only error I get is when I press return to end I see `WARNING:root:input overflow, output underflow` – MikeS159 Feb 07 '19 at 11:17
  • Using the above code but replacing the stream as you suggested I get the following errors: `sd.OutputStream.write(myrec)` gives: TypeError: write() missing 1 required positional argument: 'data' and trying: `stream = sd.OutputStream(samplerate=fs, channels=2, dtype='float32')` `stream.write(myrec)` gives the error: sounddevice.PortAudioError: Stream is stopped [PaErrorCode -9983] – MikeS159 Feb 07 '19 at 11:20
  • The pauses you are describing are strange, I don't know what's going wrong there. For the other problem, you'll have to `start()` the stream (either explicitly or in a `with` statement). – Matthias Feb 07 '19 at 16:23
  • Ahhh, of course. That worked and it sounds like there is no gap. The example code in the docs still isn't working, but at least I can play my audio buffers without gaps. Thanks. – MikeS159 Feb 07 '19 at 17:30
  • the pauses seem to only happen on the Linux VM I was working in, otherwise it works properly. – MikeS159 Feb 09 '19 at 17:19