本文介绍了PyAudio 一次播放多个声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 PyAudio 将两种声音混合为一种.我浏览了整个互联网,但没有人回答这个问题.我也在考虑使用 audiolab 或 swMixer,但它们不支持 Python 3.4……我在考虑将它们的字节字符串转换为 numpy 数组,将其与另一个数组合并,然后再转换回字符串.这可能吗?
How can I mix two sounds with PyAudio into one. I browsed all over internet but nobody answered this question. I was also thinking about using audiolab or swMixer, but they dont support Python 3.4... I was thinking about converting they byte string into numpy array, merging it with another, and converting back to by string. Is that possible?
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)
你是我最后的希望,感谢您的任何提前!
You are my last hope, thank you for any advance!
推荐答案
我自己找到了解决方案,而且非常简单.所以我会在这里发布我的代码以帮助其他人:
I found the solution by myself and it was pretty simple. So I will post my code here to help others:
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)
这篇关于PyAudio 一次播放多个声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!