- Home /
 
GetSpectrumData works only for 3 sec of microphone input
Hello. I'm trying to get pitch data from microphone input, like shown in this topic- https://answers.unity.com/questions/157940/getoutputdata-and-getspectrumdata-they-represent-t.html
Briefly:
   void Start()
 `{
   `audio.clip = Microphone.Start(selectedDevice, true, 1, maxFreq);
     audio.Play();
   }
 
  void Update()
  {
     audio.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
     float maxV = 0;
     int maxN = 0;
     for (i = 0; i < qSamples; i++)
     { // find max 
          if (spectrum[i] > maxV && spectrum[i] > threshold)
          {
              maxV = spectrum[i];
              maxN = i; // maxN is the index of max
          }
     }
     float freqN = maxN; // pass the index to a float variable
     if (maxN > 0 && maxN < qSamples - 1)
     { // interpolate index using neighbours
          var dL = spectrum[maxN - 1] / spectrum[maxN];
          var dR = spectrum[maxN + 1] / spectrum[maxN];
          freqN += 0.5f * (dR * dR - dL * dL);
     }
     float pitchValue = freqN * (maxFreq / 2f) / qSamples; // convert index to frequency
   }
 
               It works somehow for about 3 seconds of nonstop sound input (singing or whatever). But then pitchValue becomes zero. I took a look at spectrum array - all values are about zero in that case, so they don't pass threshold filter.
If I stop "singing" in mic, and then start again, pitchValue is again above zero. But again for about 3 seconds, then tuns to zero again. How can I fix that behavior? I use Unity 5.6.0.f3, if that matters.
Thanks everyone!
Your answer
 
             Follow this Question
Related Questions
Generating an Echo effect in Unity 3 Answers
Voice Recording issue ?? 0 Answers
Audio microphone analysis 1 Answer
How do I get the fundamental frequency pitch for audio input? 0 Answers
Custom simulation of Doppler effect 0 Answers