首页 > 解决方案 > 使用 pyaudio 和 matplotlib 实时创建波形

问题描述

我正在寻找音频输入(麦克风)的实时图表,并使用 matplotlib 在波形图中显示此数据。如何显示 Y 轴上的数据和 X 轴上的时间(秒)?

我对音频处理一无所知,所以我从文档和示例中学到了很多东西,但我仍然不知道如何让图形实时反应。

from scipy.io import wavfile
from matplotlib import pyplot as plt
import numpy as np

# Load the data and calculate the time of each sample
samplerate, data = wavfile.read('audio.wav')
print(samplerate, data)
times = np.arange(len(data))/float(samplerate)

# Make the plot
# You can tweak the figsize (width, height) in inches
plt.figure(figsize=(30, 4))
print(data[:,0])
plt.fill_between(times, data[:,0], data[:,1], color='k') 
plt.xlim(times[0], times[-1])
plt.xlabel('time (s)')
plt.ylabel('amplitude')
# You can set the format by changing the extension
# like .pdf, .svg, .eps
plt.savefig('plot.png', dpi=100)
plt.show()

我希望波形看起来像那样,但是使用我的麦克风而不是 wav 文件。我测试了其他东西,但没有那么结论......

标签: pythonmatplotlibpyaudiowaveform

解决方案


推荐阅读