代码:
1 # -*- coding: utf-8 -*- 2 # @Time : 18-10-16 下午12:20 3 # @Author : Felix Wang 4 5 import pyaudio 6 import numpy as np 7 from scipy import fftpack 8 import wave 9 10 11 # 录音 12 # 录音必须安装portaudio模块,否则会报错 13 # http://portaudio.com/docs/v19-doxydocs/compile_linux.html 14 def recording(filename, time=0, threshold=7000): 15 """ 16 :param filename: 文件名 17 :param time: 录音时间,如果指定时间,按时间来录音,默认为自动识别是否结束录音 18 :param threshold: 判断录音结束的阈值 19 :return: 20 """ 21 CHUNK = 1024 # 块大小 22 FORMAT = pyaudio.paInt16 # 每次采集的位数 23 CHANNELS = 1 # 声道数 24 RATE = 16000 # 采样率:每秒采集数据的次数 25 RECORD_SECONDS = time # 录音时间 26 WAVE_OUTPUT_FILENAME = filename # 文件存放位置 27 p = pyaudio.PyAudio() 28 stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) 29 print("* 录音中...") 30 frames = [] 31 if time > 0: 32 for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): 33 data = stream.read(CHUNK) 34 frames.append(data) 35 else: 36 stopflag = 0 37 stopflag2 = 0 38 while True: 39 data = stream.read(CHUNK) 40 rt_data = np.frombuffer(data, np.dtype('<i2')) 41 # print(rt_data*10) 42 # 傅里叶变换 43 fft_temp_data = fftpack.fft(rt_data, rt_data.size, overwrite_x=True) 44 fft_data = np.abs(fft_temp_data)[0:fft_temp_data.size // 2 + 1] 45 46 # 测试阈值,输出值用来判断阈值 47 # print(sum(fft_data) // len(fft_data)) 48 49 # 判断麦克风是否停止,判断说话是否结束,# 麦克风阈值,默认7000 50 if sum(fft_data) // len(fft_data) > threshold: 51 stopflag += 1 52 else: 53 stopflag2 += 1 54 oneSecond = int(RATE / CHUNK) 55 if stopflag2 + stopflag > oneSecond: 56 if stopflag2 > oneSecond // 3 * 2: 57 break 58 else: 59 stopflag2 = 0 60 stopflag = 0 61 frames.append(data) 62 print("* 录音结束") 63 stream.stop_stream() 64 stream.close() 65 p.terminate() 66 with wave.open(WAVE_OUTPUT_FILENAME, 'wb') as wf: 67 wf.setnchannels(CHANNELS) 68 wf.setsampwidth(p.get_sample_size(FORMAT)) 69 wf.setframerate(RATE) 70 wf.writeframes(b''.join(frames)) 71 72 73 recording('ppp.mp3', time=5) # 按照时间来录音,录音5秒 74 recording('ppp.mp3') # 没有声音自动停止,自动停止