python pyaudio实时读取音频数据并展示波形图
下面代码可以驱动电脑接受声音数据,并实时展示音波图:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pyaudio
import wave
import os
import operator
from functools import reduceclass SubplotAnimation(animation.TimedAnimation):def __init__(self, static=False, path=None):"""音频波形动态显示,实时显示波形:param static: 是否为静态模式:param path: wav 文件路径"""self.static = staticif static and os.path.isfile(path):self.stream = wave.open(path)# 采样频率self.rate = self.stream.getparams()[2]self.chunk = self.rate / 2self.read = self.stream.readframeselse:self.rate = 8000 # 采样率self.chunk = 200 # 语音块大小self.deviceindex = 0 # 录音设备编号p = pyaudio.PyAudio()# frames_per_buffer=self.chunk 设置音频流的缓冲区大小,即每次从音频设备读取的数据块的大小self.stream = p.open(format=pyaudio.paInt16, channels=1, rate=self.rate,input_device_index=self.deviceindex,input=True, frames_per_buffer=self.chunk)self.read = self.stream.readself.chunknum = 40 # 同屏显示语音块的数量self.voicedatas = []self.zero = [0 for i in range(self.chunk)]for index in range(self.chunknum):self.voicedatas.insert(0, self.zero)# 定义波形显示图的横纵坐标大小及类别并选用读取实时音频数据方式显示,设定更新间隔def start(self):fig = plt.figure()ax1 = fig.add_subplot(1, 1, 1)ax1.set_xlabel("t")ax1.set_ylabel("x")ax1.set_xlim(0, 1.0 * (self.chunknum * self.chunk - 1) / self.rate)ax1.set_ylim(-16000, 16000)self.line1, = ax1.plot([], [], lw=2)# 更新间隔/msinterval = int(1000 * self.chunk / self.rate)animation.TimedAnimation.__init__(self, fig, interval=interval, blit=True)# 初始化绘图,l.set_data([], []) 将绘图数据清空,使得动画开始时图像为空白状态def _init_draw(self):lines = [self.line1]for l in lines:l.set_data([], [])def new_frame_seq(self):return iter(range(self.chunk))def _draw_frame(self, framedata):if self.static:# 读取静态wav文件波形y = np.fromstring(self.read(self.chunk / 2 + 1), dtype=np.int16)[:-1]else:# 实时读取声频data = np.fromstring(self.read(self.chunk, exception_on_overflow=False), dtype=np.int16)data = data.tolist()self.voicedatas.append(data)self.voicedatas.pop(0)y = reduce(operator.add, self.voicedatas) # 将 self.voicedatas 中的 40 个列表数据全部放在 1 个列表中x = np.linspace(0, 1.0 * (self.chunknum * self.chunk - 1) / self.rate, self.chunknum * self.chunk)# 画波形图self.line1.set_data(x, y)ani = SubplotAnimation()
ani.start()
plt.show()