In unity5, I call GetSpectrumData in Update function. I see it returns null ( an array of zeros) sometimes.
To find the problem, I played a 1000Hz sound to fixed the input and make a log file of GetSpectrumData’s returns values.
In this way, it should return an array with same values each time.
But surprisingly in my PC, after every 125 calls, there is “exactly” one null (an array of zeros) returned!
And in my Tablet (Samsung android), in every 125 calls, there are “exactly” 14 nulls (array of zeros) returned!
Honestly - it sounds directly related to the hardware your running it on. Could you post the code possibly… The best option is to try and optimise it perhaps. But, without seeing it, I can’t tell.
Cheers bud
Gruffy
private int amountfreq = 120;
private float[] freq = new float[1024];
private float allfreqvalue;
int GetMaxFreq()
{
for (int s=0; s<amountfreq; s++)
freq~~=0;~~
~~ allfreqvalue = 0;~~
~~ int x = 0;~~
~~ mic.GetSpectrumData (freq, 0, FFTWindow.BlackmanHarris);~~
~~ for (int s=0; s<amountfreq; s++)~~ allfreqvalue = allfreqvalue + freq ; ~~ return x; }~~ As you see this function returns the dominant frequency (which is an integer number between 0 and 1024). It also calculates a summation of all spectrum values (as allfreqvalue). It means from the spectrum we have the “index of maximum value” and “its integral”. I call it in Update function and write these 2 values in a log file: int maxfreq; maxfreq = GetMaxFreq (); ~~allfreqvalue = allfreqvalue * 300.0f;// to make it bigger only ~~ System.IO.File.AppendAllText (Application.persistentDataPath + “/somewhere.txt”, maxfreq.ToString () + “,” + maxfreqvalue.ToString () + “;”);
Log file shows; every 125 frame the GetSpectrumData returns some 0s. (the maxfreq becomes 0 and the allfreqvalue becomes very low for a while), in my PC and my Tablet the frequent on this happening is exactly same; “once in 125 frame”! The only different is, in my PC this situation (returning 0) take 2 or 3 frame and then it “recovers” to its normal functionality , but in my tablet it takes about 14 frame of returning 0 followed by about 20 frame of low value of maxfreq - longer recovery but with same period! I played a fixed frequency sound and put it through microphone line to my devices, to fix the input… but the result has not changed! Also I call the GetMaxFreq() using InvokeRepeating seperate from Update function, to avoid animation delay (of course my project has only 3 game objects and it is quite empty!), but again, there is no difference, every 3.12s it returns 0 for a while, no matter what is the repeatRate and no matter what is the device!
See below graphs: ![alt text][1] the top graph is maxfreq values in my pc when I played a fixed frequency sound with repeatRate of 0.08, the bottom graph is allfreqvale in my pc from same log file as above ![alt text][2] and here, the top one is maxfreq from my tablet, as you see it takes more sample to back to normal, and the other one is allfreqvalu form same logfile as above, (I have played a fixed frequency whistle), as you see it takes more time to “recover”! Because the “frequency” of returning 0 is same in both PC and Tablet (both 3.12s), it should not has any things to do with hardware. Of course the “recovery time” is obviously related to hardware! So it is complicated …. [1]: /storage/temp/54117-12.png [2]: /storage/temp/54118-34.png