我在尝试使用 PyAudio 回调模式时遇到了类似的问题,但我的要求是:
- 使用立体声输出(2 个通道)。
- 实时处理。
- 使用任意脉冲响应处理输入信号,这可能会在处理过程中发生变化。
几次尝试后我成功了,这是我的代码片段(基于此处找到的 PyAudio 示例):
import pyaudio
import scipy.signal as ss
import numpy as np
import librosa
track1_data, track1_rate = librosa.load('path/to/wav/track1', sr=44.1e3, dtype=np.float64)
track2_data, track2_rate = librosa.load('path/to/wav/track2', sr=44.1e3, dtype=np.float64)
track3_data, track3_rate = librosa.load('path/to/wav/track3', sr=44.1e3, dtype=np.float64)
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
count = 0
IR_left = first_IR_left # Replace for actual IR
IR_right = first_IR_right # Replace for actual IR
# define callback (2)
def callback(in_data, frame_count, time_info, status):
global count
track1_frame = track1_data[frame_count*count : frame_count*(count+1)]
track2_frame = track2_data[frame_count*count : frame_count*(count+1)]
track3_frame = track3_data[frame_count*count : frame_count*(count+1)]
track1_left = ss.fftconvolve(track1_frame, IR_left)
track1_right = ss.fftconvolve(track1_frame, IR_right)
track2_left = ss.fftconvolve(track2_frame, IR_left)
track2_right = ss.fftconvolve(track2_frame, IR_right)
track3_left = ss.fftconvolve(track3_frame, IR_left)
track3_right = ss.fftconvolve(track3_frame, IR_right)
track_left = 1/3 * track1_left + 1/3 * track2_left + 1/3 * track3_left
track_right = 1/3 * track1_right + 1/3 * track2_right + 1/3 * track3_right
ret_data = np.empty((track_left.size + track_right.size), dtype=track1_left.dtype)
ret_data[1::2] = br_left
ret_data[0::2] = br_right
ret_data = ret_data.astype(np.float32).tostring()
count += 1
return (ret_data, pyaudio.paContinue)
# open stream using callback (3)
stream = p.open(format=pyaudio.paFloat32,
channels=2,
rate=int(track1_rate),
output=True,
stream_callback=callback,
frames_per_buffer=2**16)
# start the stream (4)
stream.start_stream()
# wait for stream to finish (5)
while_count = 0
while stream.is_active():
while_count += 1
if while_count % 3 == 0:
IR_left = first_IR_left # Replace for actual IR
IR_right = first_IR_right # Replace for actual IR
elif while_count % 3 == 1:
IR_left = second_IR_left # Replace for actual IR
IR_right = second_IR_right # Replace for actual IR
elif while_count % 3 == 2:
IR_left = third_IR_left # Replace for actual IR
IR_right = third_IR_right # Replace for actual IR
time.sleep(10)
# stop stream (6)
stream.stop_stream()
stream.close()
# close PyAudio (7)
p.terminate()
以下是对上述代码的一些重要反思:
- 使用
librosa
而不是 wave 允许我使用 numpy 数组进行处理,这比来自wave.readframes
.
- 您设置的数据类型必须与字节
p.open(format=
的格式匹配。ret_data
PyAudiofloat32
最多可以使用。
- 偶数索引字节
ret_data
进入右侧耳机,奇数索引字节进入左侧耳机。
澄清一下,这段代码将三个轨道的混合发送到立体声输出音频,每 10 秒它会改变脉冲响应,从而应用滤波器。我用它来测试我正在开发的 3d 音频应用程序,以及头部相关脉冲响应 (HRIR) 的脉冲响应,它每 10 秒改变一次声音的位置。
编辑:
此代码有一个问题:输出具有与帧大小相对应的频率噪声(帧大小较小时频率较高)。我通过手动重叠和添加帧来解决这个问题。基本上,ss.oaconvolve
返回了一个 size 的数组track_frame.size + IR.size - 1
,所以我将该数组分成第一个track_frame.size
元素(然后用于ret_data
),然后是IR.size - 1
我为以后保存的最后一个元素。然后将这些保存的元素添加到IR.size - 1
下一帧的第一个元素中。第一帧添加零。