- Home /
get spectrum data returns low values
I'm calling GetSpectrumData from an AudioListener component to analyse a sound. See reference at http://unity3d.com/support/documentation/ScriptReference/AudioListener.GetSpectrumData.html
The array should be filled with floats from 0.0 to 1.0 (I guess) but the strange thing is that if I call Debug.Log and print the values of the samples, the result is a very small number: something like 0.02 in the low frequencies and something like 9E-06 in the high frequencies. I expected this values to be different from low to high frequencies because sound power has a logarithmic decay over the frequency spectrum, but WHY AM I READING SO SMALL VALUES?
Is this normal behaviour? What should I do with that to scale it to something useful in a range from 0 to 1 across all frequencies?
Thanks in advance to anyone willing to clarify this
Here's an example code, attach it to the camera, drag the camera audio listener on it and create an audio source and then check the console
var listener : AudioListener;
var bufSize : int;
var samples : float[];
function Start () {
//take the smallest buffer
//just not to flood Debug.Log but this
//something like 1024
bufSize = 64;
samples = new float[64];
//call the function every tenth of a second
InvokeRepeating("readSound", 0.0, 1.0 / 10.0);
}
function readSound() {
//0 means left channel in a stereo file
listener.GetSpectrumData (samples, 0, FFTWindow.BlackmanHarris);
for (var i : int = 0; i < bufSize; i++) {
Debug.Log("sample: " + i + " value: " + samples[i]);
}
}
function Update () {
}
Answer by drudiverse · Dec 01, 2014 at 08:23 AM
check out aldonetto's example code for get spectrum data. it's good code. the results will be correct.
FFT can be very low values, especially if the audio source is weak. the values are probably square values, not linear, i.e. they are exponential so the lowest values are very very low, you have to square them or do a RMS value of them. see aldonetto's post for percise info's http://answers.unity3d.com/questions/157940/getoutputdata-and-getspectrumdata-they-represent-t.html