- Home /
Audio.GetSpectrumData() returns NaN
This is driving me nuts and I'm not sure if it's just a windows 8.1 bug or something, but audio.GetSpectrumData() sometimes returns NaN, and sometimes works with the exact same code. I am so confused.
using UnityEngine;
using System.Collections;
public class AudioSpectrumExample : MonoBehaviour {
void Update() {
float[] spectrum = new float[256];
audio.GetSpectrumData( spectrum, 0, FFTWindow.Hamming);
Debug.Log (spectrum [54]);
int i = 1;
while (i < 255) {
Debug.DrawLine(new Vector3(i - 1, spectrum[i] + 10, 0), new Vector3(i, spectrum[i + 1] + 10, 0), Color.red);
Debug.DrawLine(new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2), new Vector3(i, Mathf.Log(spectrum[i]) + 10, 2), Color.cyan);
Debug.DrawLine(new Vector3(Mathf.Log(i - 1), spectrum[i - 1] - 10, 1), new Vector3(Mathf.Log(i), spectrum[i] - 10, 1), Color.green);
Debug.DrawLine(new Vector3(Mathf.Log(i - 1), Mathf.Log(spectrum[i - 1]), 3), new Vector3(Mathf.Log(i), Mathf.Log(spectrum[i]), 3), Color.yellow);
i++;
}
}
}
This line: Debug.Log (spectrum [54), shows me the NaN.
NaN
UnityEngine.Debug:Log(Object)
AudioSpectrumExample:Update() (at Assets/Scripts/MusicVisualizer/AudioSpectrumExample.cs:8)
Thanks for any help!! (And yes I do have an audio source attached to the same game object that's running that script.)
Hello bud, did you sort your issue, or have you taken a look at the answer posted below. This explains your issue, I hope that clears things up for you. Please mark as answer, unless you feel the problem is not sorted, in which case return here to continue through to a solution for you. Take care bud. Gruffy
Answer by Gruffy · Apr 02, 2014 at 08:50 PM
Hey bud, you need to add an audio source component to your GameObject holding this script.
You must then supply that AudioSource with an AudioClip (wav,mp3, ogg file).
These are why your above script(taken from Unity docs) is not working, I just copied your script and added the AudioSource compoent to the object with script and imported a quick track...all working fine. Check you console, when you have done the above and see the Spectrum data recorded in there at runtime.
Image showing it working Take care bud Gruffy
Hey Gruffy! Thanks so much for the detailed response, but unfortunately I think the issue is weirder than that :P In my case I do have an Audio Source attached which I've tried with both an ogg and an mp3.
What's frustrating is I just fired up my test so that I could post a screenshot of it to share, but now it's working. However I'm certain that eventually it will stop working and start outputting those NaN's again. When it does I'll post the screenshot.
Thanks again for the response!
cool man, when it happens do chuck it up here, because I just copied your code from above and console gave tge same errors as you stated and the audio source and clip were what console specified were resolution, these were added including a wav file and it worked, no code changes or anything.. strange one bud, so yeah interested tobsee it when it happens. Anywaym if u like I can zip up a package to show what im saying, it may be cheaper file size wise though for u to add ur own audioclip to the audiosource component in the scene I woukd be sending etc, your call bossman take care bud gruffy
Ok thanks again for the help! I did just repro my issue (image attached), but then when I recreated a new project from scratch - probably identical to the one that you made - yet again the problem went away. Something sooooooooo strange is going on.
To be totally clear, you were able to fix the errors by adding the AudioSource and .wav file to the game object that had the AudioSpectrumExample component?
Here's my failure case example:
here you go bud, you will have to add your own music (wav, ogg, mp3) in yourself due to file size , but simply unzip the zip, extract package and import to a new or open project.
Find the scene and double click it, then go to the GameObject childed to the $$anonymous$$ain Camera, on that gameobject add a song into the AudioSource component there. Press Play. Stop and Press Play again, as the first run might be slow to catch up on analysis etc, will be fine for second time round though etc.
You should see you have the same code and should also have the same(or similar at least) setup, the only other thing I can suggest is that your audio may need optimizing like making sur eit is a 2d sound, that it is set to loop and play on awake etc, all inside the AudioSource component attributes in inspector, excpet for changin to 2d Sound, that`s done through the audio inspector for the clip you are using, by clicking the audioclip n all that. Anyway take care bud. hope that helps in some way to solve this for you. ...& remember, this package needs an audio clip due to file size restrictions on here, sorry. :) Gruffy
Hey Gruffy!
So I had a chance to try his out and all seems good. It continues to be super strange - I did have one hiccup where my code went back to NaN (not your code), but after doing more investigation it simply fixed itself again. I'm going to keep an eye on this for a few more days. If I doesn't come back, I suppose I'll just mark this thread as answered. Gonna wait a bit on that just to make sure - still feels like there could be a deeper issue with windows 8.1 or something.
Ok thanks again for the help, i owe you one!