0

使用 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)

问候,托斯滕

4

1 回答 1

0

据我所知,您不能使用 PyAudio 直接打开文件,但是可以组合 pyaudio 和 wave 模块,以防您的应用程序将处理后的声音重新路由到 SoundFlower。

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
于 2015-05-04T14:11:59.143 回答