convert AudioClip.GetData values into AudioSource.GetOutputData values


you know the functions to get values from the AudioSource like GetOutputData and GetSpectrumData provide information just for the part of the sound that is playing.
But i need the spectrum data for at least every second or better every sample of the given audio.

i heard there’s something with Fast Fourier Transformation (FFT) but i don’t know how to do it.

the only other very messed why i know at this moment is to generate for every second of the given audio a seperate audio clip and play ALL OF THEM at once. than you can analyze them with GetOutputData or GetSpectrumData.
Well i don’t have to say that this’d be not nice.

i hope you can help… i’m helpless

Well, like you have found out already Unity only provides the SpectrumData(FFT) for the currently playing audio. If you want the spectrum data for a certain fixed interval you have to do a FFT manually. There are plenty of FFT libraries or implementations for .NET.

You might want to read about Discrete Fourier Transform to get an overview what it actually is. Calculating a Fourier transform is quite complicated as it makes use of complex numbers. The usual Fourier transform requires an extreme amount of calculations. The “fast Fourier transform” speeds that process up by storing intermediate results for later use.

To get the spectrum at a certain point you have to analyse the samples around that point. That gives you the frequencies within that block of samples. If you want to analyse the whole file you have to repeat the FFT by “sliding” your section (window) you analyse over the data of your file.

Creating a spectrum analysis for every sample wouldn’t make mucb sense since you always get the spectrum of a quite large range of samples. If you just shift one sample you most likely won’t see any changes. Also a single calculation for a point already takes quite some time. A 3 min track has about 8 million samples (@ 44kHz).

GetOutputData just returns the current buffer of the already mixed output of all playing audioclips. If you only play one audioclip (and it’s played in 2d mode) GetOutputData will simply return a fraction of your original AudioClip data. I guess GetSpectrumData simply calculates the FFT from this section.