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

    编辑:这是特定的录音课程,以便澄清

    1. import threading
    2. import queue
    3. import numpy
    4. import sounddevice as sd
    5. import soundfile as sf
    6. class AudioRecorder():
    7. def __init__(self):
    8. self.open = True
    9. self.file_name = 'name_of_file.wav'
    10. self.channels = 1
    11. self.q = queue.Queue()
    12. # Get samplerate
    13. device_info = sd.query_devices(2, 'input')
    14. self.samplerate = int(device_info['default_samplerate'])
    15. def callback(self, indata, frames, time, status):
    16. # This is called (from a separate thread) for each audio block.
    17. if status:
    18. print(status, file=sys.stderr)
    19. self.q.put(indata.copy())
    20. def record(self):
    21. with sf.SoundFile(self.file_name, mode='x', samplerate=self.samplerate, channels=self.channels) as file:
    22. with sd.InputStream(samplerate=self.samplerate, channels=self.channels, callback=self.callback):
    23. while(self.open == True):
    24. file.write(self.q.get())

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

    作者: Kyle Polley 发布者: 27.11.2018 05:40