1

I simply want to determine frequencies of a .mp3 or .wav file and print it

import pyaudio
import wave
import numpy as np

chunk = 2048
wf = wave.open('/home/pi/Adhuri.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)

# read some data
data = wf.readframes(chunk)
# play stream and find the frequency of each chunk
while len(data) == chunk*swidth:
    print "ok"
    stream.write(data)
    indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),\
                                     data))*window
    fftData=abs(np.fft.rfft(indata))**2
    which = fftData[1:].argmax() + 1
    if which != len(fftData)-1:
        y0,y1,y2 = np.log(fftData[which-1:which+2:])
        x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
        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()

It does not satisfy while len(data) == chunkswidth: and when i am writing while len(data) != chunkswidth: only then it enters into loop and shows an error message valueerror: operands could not together broadcast with shapes (4096)(2048) . how to solve this problem?

Akash Nil
  • 693
  • 10
  • 26
  • [Here](https://github.com/praveenv253/tuner)'s a link that might help. It prints the dominant frequency by reading chunks of microphone input. You might be able to modify this to read chunks out of a wav or mp3 file instead. – Praveen Jun 05 '15 at 03:29
  • please share me the microphone details. when i am running your program it shows me the error invalid sample rate – Akash Nil Jun 05 '15 at 11:07
  • 1
    possible duplicate of [separate frequencies from music](http://stackoverflow.com/questions/30728737/separate-frequencies-from-music) – tom10 Jun 12 '15 at 16:32

0 Answers0