Continuous Audio Playback with Sounddevice

佐手、 提交于 2019-12-11 03:35:23

问题


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?


回答1:


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?



来源:https://stackoverflow.com/questions/54560927/continuous-audio-playback-with-sounddevice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!