win10下实现PyAudio持续监听并录音(麦克风/扬声器)
Pyaudio默认是通过麦克风录音,扬声器播放。
实现在win10下,录制扬声器的声音需要设置默认音频设备(否则要修改pyaudio的代码)
pyaudio对象结构如下:
def __init__(self,PA_manager,rate,channels,format,input=False,output=False,input_device_index=None,output_device_index=None,frames_per_buffer=1024,start=True,input_host_api_specific_stream_info=None,output_host_api_specific_stream_info=None,stream_callback=None):
实现:在win10下,录制扬声器的声音
step1:右键桌面右下角的小喇叭,打开声音设置
step2:往下滚动,打开 声音控制面板
step3:在 录制 标签栏下,启动 立体声混音,并且将其设置为 默认设备
疑难问题请参考
此时用下面的代码,就可以录制扬声器的声音了。
监听麦克风并录音
import pyaudio
import numpy as np
import wavedef Monitor_MIC(th, filename):CHUNK = 512FORMAT = pyaudio.paInt16CHANNELS = 1RATE = 16000 #录音时的采样率WAVE_OUTPUT_FILENAME = filename + ".wav"p = pyaudio.PyAudio()stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK)frames = []while (True):#print("ready for recording" + str(time.localtime(time.time()).tm_sec))for i in range(0, 5):data = stream.read(CHUNK)frames.append(data)audio_data = np.fromstring(data, dtype=np.short)temp = np.max(audio_data)if temp > th :print("detected a signal")print('current threshold:',temp)less = []frames2 = []while (True):print("recording")for i in range(0, 30):data2 = stream.read(CHUNK)frames2.append(data2)audio_data2 = np.fromstring(data2, dtype=np.short)temp2 = np.max(audio_data2)if temp2 < th:less.append(-1)print("below threshold, counting: ", less)#如果有连续15个循环的点,都不是声音信号,就认为音频结束了if len(less) == 15:breakelse:less = []breakstream.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(frames2))wf.close()