- Home /
convert AudioClip.GetData values into AudioSource.GetOutputData values
hello,
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
Answer by Bunny83 · May 16, 2015 at 01:16 PM
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.
so you say getoutput data is similar to AudioClip.GetData? that'd be much more helpful! its just i printed the values of GetData and GetOutputData. i $$anonymous$$NOW in the song i used must be a lot high numbers because i visualized GetOutputData using this tutorial: http://gamedevelopment.tutsplus.com/tutorials/how-to-use-sound-to-make-3d-geometry-in-unity--cms-20456 my problem is... i did both i calculated the average of all values and i counted the values in 0.1 ranges.
i'm still not sure if i know what exactly i want to use but i'm sure GetOutputData was visualized correctly!
well... of course i can't compare the values because i don't know in what frequence the GetOutputData function returns values...
another little thing is the following... unity seems to mess around with my audio. i used a tool to get an image such like the visualization tutorial i gave you... but in this tool somehow ... shrinking the curves... i'll try to explain by using images. both graphs are from the same audio track.
well sorry for the size but you see there are differences but its definitly the same file!
GetOutputData just returns samples of the pure audio signal. This signal doesn't have one frequency but is a mixture of many. That's what audio is. The Fouier transform actually manages the impossible to actually decompose a section of samples into single frequencies which mixed together would result in the original signal.
The weve form you see in Unity isn't averaged at all. It's simply downscaled. So it's affectively doesn't show all samples in that view. It has to skip some. This file contains 1107168 samples. Since the image representation in Unity has only 381 pixels (at least in your image here) it will skip about 2900 samples. Or to put it the other way round: It only displayes every 2900 sample.
Your's look like you take some kind of average of multiple samples. You also take the absolute value and not the real +- signal.
Any oscillating signal oscillates around a certain level (usually 0). The signal is equivalent to the movement of your speakers membrane. It doesn't only moves outwards (positive signal) but also inwards (negative signal). If no power is supplied the membrane is at it's resting position in the center. See this video or look for others that explain how a speaker works ^^.
As i mentioned on your other question you should read more about signal analysis in general. I suggest you get some audio tools (like Audacity) and zoom in on your file. At the end you will see the how the actual signal looks like. It's not a single frequency but a continous signal which is made up of several frequencies.
well at least GetOutputData returns at least the same pattern like $$anonymous$$P3DirectCut as you see. And its not that i miss some samples in unity image. $$anonymous$$y point is that the "height" of the values isn't big enough. as you see the first 3 bars are at least 1.5 times higher than the rest of the signal. but in unity its all nearly the same. so are the values when i read them out. the values of the right part of the signal are even HIGHER!
okay if i want you to help me i guess i have to describe my whole problem. i want to make a game like audio surf. i got some algorithms in $$anonymous$$d how i can use the values i have to transform them into a road but... i don't know what values to use!
so i putted some $$anonymous$$P3s into audio surf. the road goes up what means that the values reduce. (of course for the audio file i showed you in the image). as far as i analyzed in audio surf: the road straight for some seconds. exactly as long as the first 3 beats are playing. then it goes up. the image in $$anonymous$$P3DirectCut seems to exactly match this values.
so here the question: what function in unity do i use to get the $$anonymous$$P3DirectCut values or at least the equivalent in unity. i'm pretty sure unity messes around with the sound somehow.
AND i need those values not only for the actual playing second i need to know ALL values of the sound before i play the sound. the only function that does this way is AudioClip.GetData so i have to use this to produce the signal visualized by $$anonymous$$P3Cut... you know my problem? >.<
i hope you can help me... i pray! this project is really important for me...
Your answer
![](https://koobas.hobune.stream/wayback/20220613195734im_/https://answers.unity.com/themes/thub/images/avi.jpg)