separate frequencies from music

678 views Asked by At

i want to print separate frequency present in a music file. if freq amp is grater than threshold. then it will brinted.

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))
   fftdata=abs(np.fft.rfft(indata))**2
   freq1=np.fft.fftfreq(len(indata))
   for i in range(len(freq1)):
       freq = freq1[i]
       print freq
  data = wf.readframes(chunk)

it is printing frequencies 0.something. not getting actual frequiencies

1

There are 1 answers

1
tmdavison On

Your loop over freq1 is wrong. To use i as the index for freq1, try this:

for i in range(len(freq1):
    freq = freq1[i]
    print freq

Or alternatively,

for i in freq1:
    print i