Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyAudio, how to tell frequency and amplitude while recording?

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?

like image 962
jewz Avatar asked Aug 02 '11 07:08

jewz


People also ask

How do you find audio amplitude?

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.

What are chunks samples and frames when using PyAudio?

"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.

What can PyAudio do?

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.


1 Answers

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()
like image 135
HYRY Avatar answered Sep 28 '22 08:09

HYRY