使用pyaudio的Raspberry pi I2S MEMS麦克风右侧CHN单声道

时间:2018-09-05 10:11:01

标签: python raspberry-pi3 microphone pyaudio digital

我正在使用Adafruit I2S MEMS麦克风分组进行录制。参考https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout?view=all

当我按照下面的图像以单声道配置将麦克风连接到RPI时,我可以使用arecord命令和下面的python代码录制音频

enter image description here

  
    

arecord -D dmic_sv -c2 -r 48000 -f S32_LE -t wav -V mono -v recording.wav

  

Python代码段:

channels = 1,rate = 48000,frames_per_buffer = 2400

def start_recording(self):
        try:
            self.logger.info("start_recording()> enter")
            # Use a stream with a callback in non-blocking mode
            self._stream = self._pa.open(format=pyaudio.paInt32,
                                            channels=self.channels,
                                            rate=self.rate,
                                            input=True,
                                            frames_per_buffer=self.frames_per_buffer,
                                            stream_callback=self.get_callback())
            self._stream.start_stream()
            self.logger.info("start_recording()> exit")
            return self
        except Exception, e:
            self.logger.error("start_recording()>", exc_info = True)

但是,如果我将通道选择引脚连接到逻辑高电压,则可以使用arecord命令录制音频,但是可以使用python代码录制音频。 python代码需要录制右声道单声道音频吗?

1 个答案:

答案 0 :(得分:0)

我做了类似的事情,但是使用了python-sounddevice。这是我的repo

编辑: 这是需要澄清的特定录音课程

import threading
import queue
import numpy
import sounddevice as sd
import soundfile as sf

class AudioRecorder():

    def __init__(self):

        self.open = True
        self.file_name = 'name_of_file.wav'
        self.channels = 1
        self.q = queue.Queue()

        # Get samplerate
        device_info = sd.query_devices(2, 'input')
        self.samplerate = int(device_info['default_samplerate'])

    def callback(self, indata, frames, time, status):

        # This is called (from a separate thread) for each audio block.
        if status:
            print(status, file=sys.stderr)
        self.q.put(indata.copy())

    def record(self):
        with sf.SoundFile(self.file_name, mode='x', samplerate=self.samplerate, channels=self.channels) as file:
            with sd.InputStream(samplerate=self.samplerate, channels=self.channels, callback=self.callback):

                while(self.open == True):
                    file.write(self.q.get())

编辑2:该代码是Python类,它使用I2S麦克风创建音频文件,类似于问题中显示的图像。当值self.open为true时,sounddevice会将音频数据写入队列(def callback),然后将数据写入文件。您所要做的就是切换self.open以开始和停止录制。