PyAudio在Raspi上用Python录制/捕获和停止/终止

时间:2015-01-27 15:35:53

标签: raspbian python audio

我不是Python的专家,试图通过USB音频设备捕获/录制音频。 它在命令终端上工作正常。 但我想制作一个只录制音频的节目,并在我想要的时候停止播放。

我听说ab8 Pyaudio库有一定的API来执行这项工作(如pyaudio.PyAudio(),pyaudio.Pyaudio.open(),pyaudio.stream,pyaudio.stream.close,pyaudio.PyAudio.terminate()。 ....

有人可以帮助制作一个简单的Python录音程序吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

我只是在命令面前添加相关评论,让我知道如果你想清除更多ab8它

import pyaudio, wave, sys

CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 10


WAVE_OUTPUT_FILENAME = 'Audio_.wav'
p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
               channels = CHANNELS,
               rate = RATE,
               input = True,
               input_device_index = 0,
               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()    # "Stop Audio Recording
stream.close()          # "Close Audio Recording
p.terminate()           # "Audio System Close

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