Hi everyone! For a research project, I am making a VR Runner game with a musical instrument as input instead of controllers. I am developing for Quest 3 using the Unity VR Core.
I have succeeded in using the microphone class and doing a Fast Fourier Transform on the input to extract the frequencies present in the audio input. I used basic math, no libraries at first, but Iam not happy with the accuracy it provides. I tried to use GetSpectrumData but this expects some arguments that I don't know what they are. I have tried a few things, but it doesn't do anything. My own function at least seemed to work and provided a stable-ish note when I started singing, but it was off by a few tones.
here are the basic FFT fnuction I made:
public static void CalculateFFT(float[] data, float[] output)
{
// Perform a basic FFT operation (replace with a library if needed for better accuracy)
int n = data.Length;
for (int i = 0; i < output.Length; i++)
{
float real = 0;
float imag = 0;
for (int j = 0; j < n; j++)
{
float angle = -2.0f * Mathf.PI * i * j / n;
real += data[j] * Mathf.Cos(angle);
imag += data[j] * Mathf.Sin(angle);
}
output[i] = Mathf.Sqrt(real * real + imag * imag); // Magnitude
}
}