pyaudio可用于将音频存储为流对象。
在Windows中可以安装pyaudio作为python -m pip install pyaudio
下面是从pyaudio site截取的示例这需要从音频麦克风5秒的持续时间则存储音频流作为对象和后面立即播放。
您可以修改以存储不同持续时间的流对象,然后操作并播放它。 小心:持续时间增加会增加内存需求。
"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).
"""
import pyaudio
CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK)
print("* recording")
for i in range(0, int(RATE/CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK) #read audio stream
stream.write(data, CHUNK) #play back audio stream
print("* done")
stream.stop_stream()
stream.close()
p.terminate()