我做了类似的事情,但使用python-sounddevice。这是我的回购
编辑:这是特定的录音课程,以便澄清
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
开始和停止录制。
作者: Kyle Polley 发布者: 27.11.2018 05:40