Victor Pira
Victor Pira

Reputation: 1172

Speeding up the sound processing algorithm

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.

Upvotes: 0

Views: 622

Answers (3)

Michele d&#39;Amico
Michele d&#39;Amico

Reputation: 23711

You are scheduling 1000 callback in tk main thread; for every callback you are using 1 ms delay (after()'s first argument). That means the last loop will start around after 1000 ms (1 second) the first one.

Maybe that is way the loop still takes around a second to complete.

So, try to use after_idle(). I don't think you really need to Speeding up the sound processing algorithm because np is already quite efficient.

[EDIT] Surprise!! you are reading from audio channel at every iteration 1 second 8000 bytes in 16 bits format for a 4000 frame rate. You need a second to have it.

Upvotes: 4

Roland Smith
Roland Smith

Reputation: 43495

Squeezing I/O and calculations into the main loop like you are doing is the classical solution. But there are alternatives.

  1. Do the audio gathering and calculations in a second thread. Since both I/O and numpy should release the GIL, it might be a good alternative here. There is a caveat here. Since GUI toolkits like TKinter are generally not multithread-safe, you should not make Tkinter calls from the second thread. But you could set up a function that is called with after to check the progress of the calculation and update the UI say every 100 ms.

  2. Do the audio gathering and calculations in a different multiprocessing.Process. This makes it completely separate from your GUI. You will have to set up a communication channel like e.g. a Queue to send the pks back to the main process. You should use an after function to check if the Queue has data available and to update the display if so.

Upvotes: 2

Ton Plooij
Ton Plooij

Reputation: 2641

Depending on the OS you're running at you might nog be measuring actual 'wall-clock' time. See here http://pythoncentral.io/measure-time-in-python-time-time-vs-time-clock/ for some details. Note that for python 3.3 time.clock is deprecated and time.process_time() or time.perf_counter() is recommended.

Upvotes: -1

Related Questions