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:右键桌面右下角的小喇叭,打开声音设置
win10下实现PyAudio持续监听并录音(麦克风/扬声器)_第1张图片

step2:往下滚动,打开 声音控制面板
win10下实现PyAudio持续监听并录音(麦克风/扬声器)_第2张图片

step3:在 录制 标签栏下,启动 立体声混音,并且将其设置为 默认设备
win10下实现PyAudio持续监听并录音(麦克风/扬声器)_第3张图片疑难问题请参考

此时用下面的代码,就可以录制扬声器的声音了。

监听麦克风并录音

import pyaudio
import numpy as np
import wave

def Monitor_MIC(th, filename):
    CHUNK = 512
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 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:
                        break
                else:
                    less = []
            break
    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(frames2))
    wf.close()

你可能感兴趣的:(Python代码实践和编程技巧)