我想编写一个非常基本的应用程序,将麦克风的音频传递给扬声器。在https://people.csail.mit.edu/hubert/pyaudio/中描述的pyaudio非常简单。PyAudio - 如何将波形文件混合成连续流
def passthrough():
WIDTH = 2
CHANNELS = 1
RATE = 44100
p = pyaudio.PyAudio()
def callback(in_data, frame_count, time_info, status):
return (in_data, pyaudio.paContinue)
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.1)
stream.stop_stream()
stream.close()
p.terminate()
但是现在我尝试在发生事件时将波形文件混合到此流中。这就是我现在被卡住的地方。播放波形文件似乎也很容易。
def play_wave(wav_file):
wf = wave.open(wav_file, 'rb')
sample_width=wf.getsampwidth()
channels=wf.getnchannels()
rate=wf.getframerate()
second=sample_width*channels*rate
def callback(in_data, frame_count, time_info, status):
data = wf.readframes(frame_count)
return (data, pyaudio.paContinue)
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(sample_width),
channels=channels,
rate=int(rate),
output=True,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.1)
stream.stop_stream()
stream.close()
wf.close()
p.terminate()
在这个时候,我有两个问题。
- 怎样波输出混合到连续流
- 我怎样才能触发1.基于事件
希望有人能照亮黑暗的地下室,我在现在的。
编辑:假设波形文件具有相同数量的通道和相同的速率,所以不需要转换。