# _*_ Coding:utf-8 _*_
__author__ = ' Pythonwu '
__date__ = "2018/7/19 17:39"
Import Pyaudio
Import Wave #使用wave模式
CHUNK = 1024x768 #缓存区设置 1024 bytes
format = pyaudio.paint16 # Quantitative formatting of sampled values
CHANNELS = 2
Rate = 44100
Record_seconds = 5
Wave_output_filename = "Output.wav"
p = Pyaudio. Pyaudio ()
stream = P.open (Format=format,
Channels=channels,
Rate=rate,
Input=true,
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 ()
Stream.Close ()
P.terminate ()
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 ()
Python calls the sound card to record sound