python实现录音小程序

yipeiwu_com6年前Python基础

本文为大家分享了python实现录音小程序的具体代码,供大家参考,具体内容如下

学习目标:掌握python的pyaudio扩展包和Wave模块录制语音的方法

  • Wav音频:声道数,采样频率,量化位数

python Wav包是自带的,pyaudio需要下载

pip3 install pyaudio

python读Wav文件:

fp=wave.open('','rb')
  nf=fp.getnframes()#获取文件的采样点数量
  print('sampwidth:',fp.getsampwidth())
  print('framerate:',fp.getframerate())
  print('channels:',fp.getnchannels())
  f_len=nf*2#文件长度计算,每个采样2个字节
  audio_data=fp.readframes(nf)

python写Wav文件:

def save_wave_file(filename,data):
  '''save the date to the wavfile'''
  wf=wave.open(filename,'wb')
  wf.setnchannels(channels)#声道
  wf.setsampwidth(sampwidth)#采样字节 1 or 2
  wf.setframerate(framerate)#采样频率 8000 or 16000
  wf.writeframes(b"".join(data))#https://stackoverflow.com/questions/32071536/typeerror-sequence-item-0-expected-str-instance-bytes-found
  wf.close()

利用PyAudio录音:

def my_record():
  pa=PyAudio()
  stream=pa.open(format = paInt16,channels=1,
          rate=framerate,input=True,
          frames_per_buffer=NUM_SAMPLES)
  my_buf=[]
  count=0
  while count<TIME*20:#控制录音时间
    string_audio_data = stream.read(NUM_SAMPLES)#一次性录音采样字节大小
    my_buf.append(string_audio_data)
    count+=1
    print('.')
  save_wave_file('01.wav',my_buf)
  stream.close()

利用PyAudio播放音频

chunk=2014
def play():
  wf=wave.open(r"01.wav",'rb')
  p=PyAudio()
  stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=
  wf.getnchannels(),rate=wf.getframerate(),output=True)
  while True:
    data=wf.readframes(chunk)
    if data=="":break
    stream.write(data)
  stream.close()
  p.terminate()

完整录音播放的demo

import wave
from pyaudio import PyAudio,paInt16

framerate=8000
NUM_SAMPLES=2000
channels=1
sampwidth=2
TIME=2
def save_wave_file(filename,data):
  '''save the date to the wavfile'''
  wf=wave.open(filename,'wb')
  wf.setnchannels(channels)
  wf.setsampwidth(sampwidth)
  wf.setframerate(framerate)
  wf.writeframes(b"".join(data))
  wf.close()

def my_record():
  pa=PyAudio()
  stream=pa.open(format = paInt16,channels=1,
          rate=framerate,input=True,
          frames_per_buffer=NUM_SAMPLES)
  my_buf=[]
  count=0
  while count<TIME*20:#控制录音时间
    string_audio_data = stream.read(NUM_SAMPLES)
    my_buf.append(string_audio_data)
    count+=1
    print('.')
  save_wave_file('01.wav',my_buf)
  stream.close()

chunk=2014
def play():
  wf=wave.open(r"01.wav",'rb')
  p=PyAudio()
  stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=
  wf.getnchannels(),rate=wf.getframerate(),output=True)
  while True:
    data=wf.readframes(chunk)
    if data=="":break
    stream.write(data)
  stream.close()
  p.terminate()

if __name__ == '__main__':
  my_record()
  print('Over!') 
  play()

更多用法详见 pyaudio文档

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python单元和文档测试实例详解

本文实例讲述了Python单元和文档测试。分享给大家供大家参考,具体如下: 单元和文档测试 1、单元测试 单元测试就是用来对一个模块、一个函数或者一个类来进行正确性的检测工作。 1.若...

matplotlib中legend位置调整解析

matplotlib中legend位置调整解析

在画一些曲线图(linecharts)时,常常会出现多条曲线同时画在一张图上面,这时候就需要对不同的曲线进行不同的标注,以使读者能够清晰地知道每条曲线代表的含义。当你画很少的几条曲线时,...

对python中不同模块(函数、类、变量)的调用详解

首先,先介绍两种引入模块的方法。 法一:将整个文件引入 import 文件名 文件名.函数名( ) / 文件名.类名 通过这个方法可以运行另外一个文件里的函数 法二:只引入某个文件中一个...

浅谈flask中的before_request与after_request

本文主要是对flask中的before_request与after_request用法做一个简单的分析,具体实例和介绍如下。 使用before_request 和 after_reque...

python multiprocessing多进程变量共享与加锁的实现

python多进程和多线程是大家会重点了解的部分,因为很多工作如果并没有前后相互依赖关系的话其实顺序并不是非常的重要,采用顺序执行的话就必定会造成无谓的等待,任凭cpu和内存白白浪费,这...