- Home /
GetSpectrumData frequently returns null
Hi every one
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!
is there anyone know why?
@inventor2010 , @CgRobot, @aldonaletto, @Gruffy, @dskillsaw , @LoTek$$anonymous$$, @zach.r.d, @martin101, @Oana, @drudiverse, @roccapl, @benhumphreys, @DannyLZS, @sneftel
Hi guys, would you like to see this question? It is very wired, the GetSpectrumData returns 0 periodically (about every 3.12 seconds / exactly every 125 frames), it continues to return 0 depends on devise (in my PC it returns zero 1 or 2 times every 3.12 seconds, in my tablet it returns 14~15 zeros every 3.12 seconds!)
Thank you..
It's still not clear at all where you actually get the data from. In the comment below you seem to use the $$anonymous$$icrophone API which you did not mention at all in your question. The script you've referenced in that comment doesn't use Spectrum data.
So i would suggest that you edit your actual question and include more details about what you actually want to do, what's your setup and your actual code.
When $$anonymous$$icrophone input is started you have to specify the length of the recording. Also keep in $$anonymous$$d that an FFT analysis needs "enough" data to actually calculate the spectrum. I can't currently test myself since i'm currently not on my PC.
Answer by Gruffy · Sep 10, 2015 at 08:40 PM
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
Thank you very much Gruffy for your answer
here are more details:
Well I use this code to make my project: http://wiki.unity3d.com/index.php/$$anonymous$$ic_Input . I have change it a bit. I add these variables and function:
private int amountfreq = 120;
private float[] freq = new float[1024];
private float allfreqvalue;
int Get$$anonymous$$axFreq()
{
for (int s=0; s<amountfreq; s++)
freq[s]=0;
allfreqvalue = 0;
int x = 0;
mic.GetSpectrumData (freq, 0, FFTWindow.BlackmanHarris);
for (int s=0; s<amountfreq; s++)
allfreqvalue = allfreqvalue + freq [s];
return x;
}
As you see this function returns the do$$anonymous$$ant 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 = Get$$anonymous$$axFreq ();
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 Get$$anonymous$$axFreq() 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:
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
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 ….
Answer by arminms · Sep 12, 2015 at 05:55 PM
Thank you very much Gruffy for your answer
here are more details:
Well I use this code to make my project: http://wiki.unity3d.com/index.php/Mic_Input . I have change it a bit. I add these variables and function:
private int amountfreq = 120;
private float[] freq = new float[1024];
private float allfreqvalue;
int GetMaxFreq()
{
for (int s=0; s<amountfreq; s++)
freq[s]=0;
allfreqvalue = 0;
int x = 0;
mic.GetSpectrumData (freq, 0, FFTWindow.BlackmanHarris);
for (int s=0; s<amountfreq; s++)
allfreqvalue = allfreqvalue + freq [s];
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:
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
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 ….