I use the following code to do some immediate sound processing/analyzing. It works, but really slow (compared to the planned speed). I have added some time markers to find out where the problem is and according to them there shouldn't be any. Typical duration (see below) is <0.01 s for all three computed times but it still takes around a second to complete the loop. Where is the problem?
Edit: Please note, that the time measurement is not the real issue here. To prove that: MyPeaks
basically just finds the maximum of pretty short FFT - nothing expensive. And the problem persists even when these routines are commented out.
- Should I use something different than lambda function to make the cycle?
- Did I make some mistake when starting and recording the stream?
etc.
import pyaudio import struct import mute_alsa import time import numpy as np from Tkinter import * def snd_process(k=0): if k<1000: t0=time.clock() data = stream.read(CHUNK) t1=time.clock() fl=CHUNK int_data = struct.unpack("%sh" %str(fl),data) ft=np.fft.fft(int_data) ft=np.fft.fftshift(ft) ft=np.abs(ft) t2=time.clock() pks=MyPeaks(np.log(ft)) freq_out.configure(text=str(pks)) t3=time.clock() print t1-t0, t2-t1, t3-t2 master.after(1, lambda: snd_process(k+1)) CHUNK = 8000 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 4000 p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) #Tkinter stuff master=Tk() button_play=Button(master, command=snd_process, bg="yellow", text="Analyze") button_play.grid(row=0, column=0) freq_out = Label(master) freq_out.grid(row=0, column=1) freq_out.configure(text='base') mainloop()