目标是同时播放两个声音文件,因此该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
import soundfile as sf
import sounddevice as sd
import threading
def _play(sound):
event =threading.Event()
def callback(outdata, frames, time, status):
data = wf.buffer_read(frames, dtype='float32')
if len(outdata) > len(data):
outdata[:len(data)] = data
outdata[len(data):] = b'\x00' * (len(outdata) - len(data))
raise sd.CallbackStop
else:
outdata[:] = data
with sf.SoundFile(sound) as wf:
stream = sd.RawOutputStream(samplerate=wf.samplerate,
channels=wf.channels,
callback=callback,
blocksize=1024,
finished_callback=event.set)
with stream:
event.wait()
def _playsound(sound):
new_thread = threading.Thread(target=_play, args=(sound,))
new_thread.start()
_playsound('sounds_file.wav')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句