1.Pyaudio Profile
PyAudio voice processing Python library that provides rich functionality.
The Pyaud python module can call the computer's microphone or audio recording, audio playback, generated wav
files.
A wave is recorded with a file format standard WINDOWS, extension WAV, the format for the PCM data itself or compression type, belonging to a lossless music format.
3.Pyaudio installation
pip install pyaudio
Tsinghua recommended source Oh, fast
pip install pyaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
4. Use recording Pyaudio
Import the required libraries
import wave import pyaudio def audio_record(out_file, rec_time): The CHUNK = 1024 the FORMAT = pyaudio.paInt16 # 16bit format encoding the CHANNELS. 1 = # Mono the RATE = 16000 # 16000 sampling frequency P = pyaudio.PyAudio () # create an audio stream Stream = p.open (the format the FORMAT =, # audio streams wav format channels = the CHANNELS, # mono Rate = the RATE, # sampling rate 16000 INPUT = True, frames_per_buffer=CHUNK) Print ( " Start Recording ... " ) Frames = [] # recorded in the audio stream # record audio data for I in Range (0, int (the RATE / * the CHUNK rec_time)): data = stream.read(CHUNK) frames.append(data) # Recording is completed stream.stop_stream () stream.close() p.terminate() Print ( " complete ..... " )
It will be packaged into a function, go directly to the called function parameters are the file name and recording time.
5. Play the tape library using Pyaudio
import wave import pyaudio def play(): the chunk = 1024 # 2014kb WF = wave.open (R & lt " file name " , ' RB ' ) p = pyaudio.PyAudio() stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) Data = wf.readframes (the chunk) # read data Print (Data) the while Data = B! '' : # Play stream.write (data) data = wf.readframes(chunk) print('while循环中!') print(data) stream.stop_stream () # stop the flow stream.close () p.terminate () # close PyAudio Play ()
With more content refer to the official documentation ` http://people.csail.mit.edu/hubert/pyaudio/docs/ `