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

时间:2019-12-11 14:41:02

标签: python pyaudio recording channels soundcard

我正在使用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通道流,看起来像这样enter image description here

但是当使用这样的函数时

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()

音频数据按预期方式输出,请参见此处enter image description here

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

1 个答案:

答案 0 :(得分:0)

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

Stream

应该是

format=pyaudio.get_sample_size(pyaudio.paInt16),

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