PyAudio 一次播放多种声音
PyAudio Play multiple sounds at once
如何使用 PyAudio 将两种声音合二为一。我浏览了整个互联网,但没有人回答这个问题。我也在考虑使用 audiolab 或 swMixer,但它们不支持 Python 3.4...我在考虑将它们的字节字符串转换为 numpy 数组,将其与另一个合并,然后转换回字符串。这可能吗?
wf1 = wave.open("YYY.wav", 'rb')
wf1 = wave.open("XXX.wav", 'rb')
p = pyaudio.PyAudio()
def callback(in_data, frame_count, time_info, status):
data1 = wf1.readframes(frame_count)
data2 = wf2.readframes(frame_count)
#Here i need to mix these two byte strings together
return (data, pyaudio.paContinue)
你是我最后的希望,
感谢您的任何预付款!
我自己找到了解决方案,非常简单。所以我会 post 我的代码在这里帮助别人:
wf1 = wave.open("YYY.wav", 'rb')
wf1 = wave.open("XXX.wav", 'rb')
def callback(in_data, frame_count, time_info, status):
data1 = wf.readframes(frame_count)
data2 = wf1.readframes(frame_count)
decodeddata1 = numpy.fromstring(data1, numpy.int16)
decodeddata2 = numpy.fromstring(data2, numpy.int16)
newdata = (decodeddata1 * 0.5 + decodeddata2* 0.5).astype(numpy.int16)
return (result.tostring(), pyaudio.paContinue)
根据 base Josef 的回答 post 更新版本,检查声音时间是否不同。
def callback(in_data, frame_count, time_info, status):
data1 = wf.readframes(frame_count)
data2 = wf_2.readframes(frame_count)
decodeddata1 = numpy.fromstring(data1, numpy.int16)
decodeddata2 = numpy.fromstring(data2, numpy.int16)
if(int(decodeddata1.size)==0):
newdata = (decodeddata2).astype(numpy.int16)
return (newdata.tostring(), pyaudio.paContinue)
if(int(decodeddata2.size)==0):
newdata = (decodeddata1).astype(numpy.int16)
return (newdata.tostring(), pyaudio.paContinue)
if(int(decodeddata1.size)>int(decodeddata2.size)):
rest = int(decodeddata1.size)-int(decodeddata2.size)
for i in range(1,rest+1):
decodeddata2 = numpy.append(decodeddata2,0)
if(int(decodeddata1.size)<int(decodeddata2.size)):
rest = int(decodeddata2.size)-int(decodeddata1.size)
for i in range(1,rest+1):
decodeddata1 = numpy.append(decodeddata1,0)
newdata = (decodeddata1 + decodeddata2).astype(numpy.int16)
return (newdata.tostring(), pyaudio.paContinue)
如何使用 PyAudio 将两种声音合二为一。我浏览了整个互联网,但没有人回答这个问题。我也在考虑使用 audiolab 或 swMixer,但它们不支持 Python 3.4...我在考虑将它们的字节字符串转换为 numpy 数组,将其与另一个合并,然后转换回字符串。这可能吗?
wf1 = wave.open("YYY.wav", 'rb')
wf1 = wave.open("XXX.wav", 'rb')
p = pyaudio.PyAudio()
def callback(in_data, frame_count, time_info, status):
data1 = wf1.readframes(frame_count)
data2 = wf2.readframes(frame_count)
#Here i need to mix these two byte strings together
return (data, pyaudio.paContinue)
你是我最后的希望, 感谢您的任何预付款!
我自己找到了解决方案,非常简单。所以我会 post 我的代码在这里帮助别人:
wf1 = wave.open("YYY.wav", 'rb')
wf1 = wave.open("XXX.wav", 'rb')
def callback(in_data, frame_count, time_info, status):
data1 = wf.readframes(frame_count)
data2 = wf1.readframes(frame_count)
decodeddata1 = numpy.fromstring(data1, numpy.int16)
decodeddata2 = numpy.fromstring(data2, numpy.int16)
newdata = (decodeddata1 * 0.5 + decodeddata2* 0.5).astype(numpy.int16)
return (result.tostring(), pyaudio.paContinue)
根据 base Josef 的回答 post 更新版本,检查声音时间是否不同。
def callback(in_data, frame_count, time_info, status):
data1 = wf.readframes(frame_count)
data2 = wf_2.readframes(frame_count)
decodeddata1 = numpy.fromstring(data1, numpy.int16)
decodeddata2 = numpy.fromstring(data2, numpy.int16)
if(int(decodeddata1.size)==0):
newdata = (decodeddata2).astype(numpy.int16)
return (newdata.tostring(), pyaudio.paContinue)
if(int(decodeddata2.size)==0):
newdata = (decodeddata1).astype(numpy.int16)
return (newdata.tostring(), pyaudio.paContinue)
if(int(decodeddata1.size)>int(decodeddata2.size)):
rest = int(decodeddata1.size)-int(decodeddata2.size)
for i in range(1,rest+1):
decodeddata2 = numpy.append(decodeddata2,0)
if(int(decodeddata1.size)<int(decodeddata2.size)):
rest = int(decodeddata2.size)-int(decodeddata1.size)
for i in range(1,rest+1):
decodeddata1 = numpy.append(decodeddata1,0)
newdata = (decodeddata1 + decodeddata2).astype(numpy.int16)
return (newdata.tostring(), pyaudio.paContinue)