Python PyAudio 正在将所有数据处理到一个通道中

Posted

技术标签:

【中文标题】Python PyAudio 正在将所有数据处理到一个通道中【英文标题】:Python PyAudio is Processing all data into one channel 【发布时间】:2019-12-11 14:41:02 【问题描述】:

我正在使用 pyaudio 使用此功能从双声道声卡记录数据

 def record(self):
        '''
        Record Function reads from stream with configured soundcard and stores items in an array
        uses callback function as can be told to stop recording during stream. After ending writes contents 
        to wav file
        '''
        wf = wave.open('audiooutput.wav', 'wb')
        wf.setnchannels(2)
        wf.setsampwidth(pyaudio.get_sample_size(pyaudio.paInt16))
        wf.setframerate(44100)
        p = pyaudio.PyAudio()
        frames = []
        # sub function checks the queue for a message to stop recording
        def check_for_recordstop():
            try:
                message = self.my_queue.get(timeout = 0.1)
            except:
                return 
            if message == None:
                pass
            elif message.payload == "Stop":
                self.confirm_message_recieved(message)
                stream.stop_stream()
        #subfunction callback         
        def callback(in_data, frame_count, time_info, status):
            if stream.is_active():
                frames.append(in_data)
                return (in_data, pyaudio.paContinue)
            else:
                frames.append(in_data)
                return (in_data, pyaudio.paComplete)

        stream = p.open(format=pyaudio.get_sample_size(pyaudio.paInt16),
                        channels= 2,
                        rate=44100,
                        input=True,
                        frames_per_buffer=1024,
                        input_device_index=1,
                        stream_callback = callback)

        self.logger.info("Recording")
        stream.start_stream() # callback is run on a new thread when start_stream() is triggered

        while stream.is_active(): #Loop to keep thread alive while callback is running

            time.sleep(0.1)
            check_for_recordstop()
        print("done")    

        stream.close()
        wf.writeframes(b''.join(frames))
        wf.close()
        p.terminate()

但是,当我大胆地查看这些数据时,我最终不会看到 2 通道流,它看起来像这样

但是当使用这样的函数时

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                input_device_index=1,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open("pyaudoutput.wav", 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

音频数据按预期输出,请参阅此处

我似乎无法从第二个函数中获得第一个函数来产生所需的结果! 我需要为我的用例使用 pyaudio 的回调功能,但无法将两个通道分开。任何建议都会很棒!

【问题讨论】:

【参考方案1】:

我使用样本大小而不是样本宽度打开流

format=pyaudio.get_sample_size(pyaudio.paInt16),

应该是

format=pyaudio.get_format_from_width(wf.getsampwidth()),

使用我写的文件中的样本宽度来解决问题

【讨论】:

以上是关于Python PyAudio 正在将所有数据处理到一个通道中的主要内容,如果未能解决你的问题,请参考以下文章

如何选择使用哪个设备录制(Python PyAudio)

使用 Python 的 pyaudio(portaudio 绑定)列出所有音频设备

Python PyAudio 安装问题(使用 PortAudio)

pyaudio录音不写文件

使用 PyAudio 将多个图层添加到录音中

使用一个 pyaudio 流进行数据读取和写入