How to determine frequencies present in music file using python

2.3k views Asked by At

I want to determine frequencies present in a music file. It will retrive a chunk of data from music file and print the frequencies present in it. then it will pick another chunk of data. Is it possible to make it using python? I am new in this domain. If anyone could help me to do this I will be highly thankfull to him.My target device is raspberry pi.

import pyaudio
import wave
import numpy as np

chunk = 2048
wf = wave.open('/home/pi/music.wav', 'rb')
swidth = wf.getsampwidth()
RATE = wf.getframerate()
window = np.blackman(chunk)
p = pyaudio.PyAudio()
stream = p.open(format =
    p.get_format_from_width(wf.getsampwidth()),
    channels = wf.getnchannels(),
    rate = RATE,
    output = True)
data = wf.readframes(chunk)
while len(data) != '':
stream.write(data)
indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),\
                             data))*window
fftdata=abs(np.fft.rfft(indata))**2
fftData=abs(np.fft.rfft(indata))**2
# find the maximum
which = fftData[1:].argmax() + 1
# use quadratic interpolation around the max
if which != len(fftData)-1:
    y0,y1,y2 = np.log(fftData[which-1:which+2:])
    x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
    # find the frequency and output it
    thefreq = (which+x1)*RATE/chunk
    print "The freq is %f Hz." % (thefreq)
else:
    thefreq = which*RATE/chunk
    print "The freq is %f Hz." % (thefreq)
# read some more data
  data = wf.readframes(chunk)
if data:
    stream.write(data)
stream.close()
p.terminate()

my above program is working fine for single frequency sample. but for a song it shows an error at the line indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),data))*window . error msg is operands could not be broadcast together with shapes. how to fix the problem??

1

There are 1 answers

6
meneldal On

To determine the frequencies in a signal (for example an audio signal) you can use a Fourier Transform. The most common numerical implementation is called fft (for Fast Fourier Transform).

Python does have what you need for this: Discrete Fourier Transform (numpy.fft).

So the first thing to do is put your data in an array and then send it to the function.

Then you need to do a little math to get the frequency. Thankfully you can look up Wikipedia for converting between time and frequency. Since you didn't say the sampling frequency I can't give you a number but you can follow the formula:

f=1/T*i with T the total time and i the table index.