- Home /
getspectrumdata 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 () {
}