【发布时间】:2018-01-23 20:29:15
【问题描述】:
我想编写一个非常基本的应用程序,将音频从麦克风传递到扬声器。如https://people.csail.mit.edu/hubert/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. 基于事件
希望有人可以点亮我现在所在的黑暗地下室。
编辑:假设波形文件具有相同数量的通道和相同的速率,因此无需转换。
【问题讨论】: