使用PyAudio打开从物理声音输入设备传入的输入流非常简单。
我想知道是否有一种方法可以将现有声音文件作为流打开,它具有与声音设备流相同的属性?
而不是
self.p = pyaudio.PyAudio()
self.inStream = self.p.open(format=pyaudio.paInt16,
channels=1,
rate=self.RATE,
input=True,
frames_per_buffer=self.BUFFERSIZE)
我想做
self.p = pyaudio.PyAudio()
self.inStream = self.p.open('testfile.wav',
input=True,
frames_per_buffer=self.BUFFERSIZE)
此致 托
答案 0 :(得分:0)
尝试创建一个适合您需求的帮助程序类,因为PyAudio中不存在符合您要求的帮助程序类。
答案 1 :(得分:0)
据我所知,你无法使用PyAudio直接打开文件,但是如果你的应用程序将已处理的声音重新路由到SoundFlower,可以将pyaudio和wave模块结合起来。
import pyaudio, wave
def open_stream(self):
self.p_audio = pyaudio.PyAudio()
self.file_source = wave.open('input.wav', 'rb')
s_width = self.file_source.getsampwidth()
format = self.p_audio.get_format_from_width(s_width)
channels = self.file_source.getnchannels()
rate = self.file_source.getframerate()
self.stream = self.p_audio.open(format=format,
channels=channels,
rate=rate,
frames_per_buffer=1024,
input=False,
output=True,
output_device_index=detect_build_in_output_device_idx(self.p_audio),
stream_callback=self.__recording_callback)
def __recording_callback(self, in_data, frame_count, time_info, status):
# read frames
in_data = self.file_source.readframes(frame_count)
# decode frames
left, right = audio_decode(in_data, self.file_source.getnchannels())
# process frames
left, right = self.core.process(left, right)
# encode back
processed_data = audio_encode((left, right))
return processed_data, pyaudio.paContinue