python 通过麦克风录音 生成wav文件的方法

yipeiwu_com5年前Python基础

如下所示:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
########################################################################
# 
# Copyright (c) 2017 aibot.me, Inc. All Rights Reserved
# 
########################################################################
 
"""
File: gen_wav.py
Date: 2017/03/24 12:36:27
Brief: 通过麦克风录音 生成 wav文件
"""

import os
import sys
import wave
import numpy as np 
from datetime import datetime
from pyaudio import PyAudio, paInt16



class GenAudio(object):
    def __init__(self):
        self.num_samples = 2000    #pyaudio内置缓冲大小
        self.sampling_rate = 8000  #取样频率
        self.level = 1500          #声音保存的阈值
        self.count_num = 20        #count_num个取样之内出现COUNT_NUM个大于LEVEL的取样则记录声音
        self.save_length = 8       #声音记录的最小长度:save_length * num_samples 个取样
        self.time_count = 8        #录音时间,单位s
        self.voice_string = []

    
    #保存文件
    def save_wav(self, filename):
        wf = wave.open(filename, 'wb') 
        wf.setnchannels(1) 
        wf.setsampwidth(2) 
        wf.setframerate(self.sampling_rate) 
        wf.writeframes(np.array(self.voice_string).tostring())
        wf.close()
    
    
    def read_audio(self):
        pa = PyAudio() 
        stream = pa.open(format=paInt16, channels=1, rate=self.sampling_rate, input=True, 
                frames_per_buffer=self.num_samples) 
        
        save_count = 0
        save_buffer = [] 
        time_count = self.time_count

        while True:
            time_count -= 1
            
            # 读入num_samples个取样
            string_audio_data = stream.read(self.num_samples)     
            # 将读入的数据转换为数组
            audio_data = np.fromstring(string_audio_data, dtype = np.short)
            #计算大于 level 的取样的个数
            large_sample_count = np.sum(audio_data > self.level)
            
            print(np.max(audio_data)),  "large_sample_count=>", large_sample_count

            # 如果个数大于COUNT_NUM,则至少保存SAVE_LENGTH个块
            if large_sample_count > self.count_num:
                save_count = self.save_length
            else: 
                save_count -= 1
            if save_count < 0:
                save_count = 0
            
            if save_count > 0:
                save_buffer.append(string_audio_data)
            else:
                if len(save_buffer) > 0:
                    self.voice_string = save_buffer
                    save_buffer = [] 
                    print("Recode a piece of  voice successfully!")
                    return True
            
            if time_count == 0: 
                if len(save_buffer) > 0:
                    self.voice_string = save_buffer
                    save_buffer = []
                    print("Recode a piece of  voice successfully!")
                    return True
                else:
                    return False
        return True




if __name__ == "__main__":
    r = GenAudio()
    r.read_audio()
    r.save_wav("./test.wav")

以上这篇python 通过麦克风录音 生成wav文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python+selenium实现截图图片并保存截取的图片

这篇文章介绍如何利用Selenium的方法进行截图,在测试过程中,是有必要截图,特别是遇到错误的时候进行截图。在selenium for Python中主要有三个截图方法,我们挑选其中最...

Windows下实现Python2和Python3两个版共存的方法

一直用的是python2,从python 2.3到python 2.7.6, 出于想了解python3的新特性,又安装了python3.3.3. 用了才发现蛮方便的。python的各个版...

python 处理微信对账单数据的实例代码

下面一段代码给大家介绍python 处理微信对账单数据,具体代码如下所示: #下载对账单并存储到数据库 @app.route("/bill/<string:date>",...

python实现对图片进行旋转,放缩,裁剪的功能

先说明下,我这是对某个目录下的图片名称进行操作,该目录下的图片名称为1.jpg,2.jpg。。。。。这样类似的图片名。 1.旋转 # -*-coding:utf-8-*- from...

django开发post接口简单案例,获取参数值的方法

django开发post接口简单案例,获取参数值的方法

项目环境:python3.6,django2.1 接口功能: 将传入参数a和b字符串相加,返回结果 1.新建一个django项目 # 新建一个名为Post的项目 django-adm...