目标是同时播放两个声音文件,因此该sounddevice.play
功能不是一个选项。我相信我应该OutputStream
为每个文件制作一个。现在我被困在试图让一个OutputStream
工作。请帮忙。
到目前为止我的代码:
import soundfile as sf
import sounddevice as sd
data, fs = sf.read('sound_file.wav', dtype='float32')
def callback(outdata, frames, time, status):
outdata[:] = data
with sd.OutputStream(callback=callback):
pass
编辑:切换到RawOutputStream
import soundfile as sf
import sounddevice as sd
wf = sf.SoundFile('sound_file.wav')
def callback(outdata, frames, time, status):
data = wf.buffer_read(frames, dtype='float32')
if len(data) <= 0:
raise sd.CallbackAbort
if len(outdata) > len(data):
raise sd.CallbackAbort #wrong obviously
outdata[:] = data
with sd.RawOutputStream(channels=wf.channels,
callback=callback) as stream:
while stream.active:
continue