I have no clue why the: float val = spectrum[i]; is yielding an Index Out of Bounds of the Array. Please help...! I am a complete beginner in this game.
public class InputScript : MonoBehaviour{
void Start()
{
float[] spectrum = new float[256];
AudioSource aud = GetComponent<AudioSource>();
aud.clip = Microphone.Start(null, true, 10, 44100);
aud.Play();
float maxIndex = 0;
float maxValue = 0.0f;
for (int i = 0; i <= spectrum.Length; i++)
{
float val = spectrum[i];
if (val > maxValue)
{
maxValue = val;
maxIndex = i;
}
}
var freq = maxIndex * AudioSettings.outputSampleRate / 2 / spectrum.Length;
}
void Update()
{
float[] spectrum = new float[256];
AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.Rectangular);
}
}
I should be able to extract the pitch of the input from a microphone.