The presentation by Adobe’s Jim Corbett on Alchemy at the Adobe MAX 2008 conference in Milan last Tuesday (2nd of December), got me thinking about something I used to spend quite some time on back in the early 90’s, when I studied Sonology at the Kon. Conservatorium in Den Haag: spectral analysis & (re)synthesis of sound. This is usually done by means of the Fourier Transform, which transforms time-based data such as sound to the frequency domain. The most used algorithm for this is the Fast Fourier Transform, or FFT, which requires the number of samples per transformation to be a power of 2. Usually a block of 1024 samples is used, which equals roughly 50ms of sound at 44.1kHz.
Now the FFT algorithm is quite CPU-intensive, and as Alchemy is put forward as useful for large number-crunching operations, I thought this could be a very interesting case. In order to allow for proper comparison, I decided to first port the FFT code from C to Actionscript, and do some testing. It took some refactoring of the C-code in Actionscript to allow for specific bottlenecks popping up, such as multiple lookup of the same value from a Vector, which turns out to be an expensive operation. Also the calculation of sine & cosine was done beforehand, to allow for a lookup table to be used in the actual FFT algorithm. All in all I managed to gain some 50% speed increase by these optimizations.
To my surprise, a single transformation of one block of 1024 samples takes only 5 ms in Actionscript. Even when performing the necessary after-calculations (normalizing & getting the lenght of vectors, which requires Math.sqrt), a back-and-forth transformation shouldn’t take more than 10 ms, leaving 40ms for doing fun stuff with the data. So even without a C-port it’s possible to do real time audio transformation in the frequency domain.
The first thing to do is plot the data in an image. Right now I’m only posting a screenshot, as I’d like to polish the example a bit more before putting out the actual code, but this is what a frequency plot of a short piece of music looks like:
More about this (including source code) soon!