开发者

PyAudio, how to tell frequency and amplitude while recording?

开发者 https://www.devze.com 2023-03-25 03:04 出处:网络
I\'ve used the PyAudio default recording example, and added numpy and scipy. I can 开发者_如何学编程only use scipy.io.wavefile.read(\'FILE.wav\'), after recording the file, however, and it also gives

I've used the PyAudio default recording example, and added numpy and scipy. I can 开发者_如何学编程only use scipy.io.wavefile.read('FILE.wav'), after recording the file, however, and it also gives me this random tuple, eg: (44100, array([[ 0, 0], [-2, 0], [ 0, -2], ..., [-2, -2], [ 1, 3], [ 2, -1]], dtype=int16)). What does this array give me and do you know how else you can get the frequency/amplitude of each frame of a wav file, preferably while recording?


The array is not random data, it's the wave data of your stereo sound, and 44100 is the sampling rate. use the following code to plot the wave of left channel:

import scipy.io.wavfile as wavfile
import numpy as np
import pylab as pl
rate, data = wavfile.read('FILE.wav')
t = np.arange(len(data[:,0]))*1.0/rate
pl.plot(t, data[:,0])
pl.show()

To get the frequency and amplitude of you wave, do FFT. Following code plot the power of every frequency bin:

p = 20*np.log10(np.abs(np.fft.rfft(data[:2048, 0])))
f = np.linspace(0, rate/2.0, len(p))
pl.plot(f, p)
pl.xlabel("Frequency(Hz)")
pl.ylabel("Power(dB)")
pl.show()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号