I’m using getSpectrumData with a float array of size 64 to create a music visualizer for mobile devices. The visualizer runs smoothly and nicely on my PC, but on my iPhone 7 it lags noticeably. I upped the framerate to 100 FPS, which seems to correct the issues, but I don’t want to rely on a device having a capacity of 100 FPS for my application to run.
Is there any way I can reduce the number of calculations involved in getSpectrumData? I’m essentially calling getSpectrumData every frame, and using the values to modify some basic rectangles on screen. Any ideas welcome.
Thanks!
getSpectrumData is by definition a quite heavy method. It calculates the FFT of the current audio buffer. You can increase the performance by using a smaller spectrum array. The FFT algorithm is already a heavyly optimised algorithm. Calculating a “normal” DFT of a relatively large chunk of data would take ages.
I’ve implemented an FFT algorithm myself over here. I have never really done much performance checks. However with my FFT class you can re-use the complex array. You just need to fill it manually with the pcm data. You could use the OnAudioFilterRead callback to get access to the current data buffer. However be warned that this callback is invoked from a different thread, so you want to store the results somewhere and use it from Update.