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?
Finding the amplitude If you want the amplitude of the signal at any given point, then all you have to do is take the absolute value of one sample in the array of samples, i.e. to get the amplitude of the 3rd sample from your audio block data.
"RATE" is the number of samples collected per second. "CHUNK" is the number of frames in the buffer. Each frame will have 2 samples as "CHANNELS=2". Size of each sample is 2 bytes, calculated using the function: pyaudio.
PyAudio provides Python bindings for PortAudio v19, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms, such as GNU/Linux, Microsoft Windows, and Apple macOS.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With