Hello, I’m having a hard time to get the same value than unity.
The idea is to record the microphone and get the pitch, I was using FFTSharp.
int micPosition = Microphone.GetPosition(m_SelectedMicrophone);
if (micPosition > 0)
{
float[] samples = new float[sampleSize];
int startPosition = micPosition - samples.Length;
if (startPosition < 0)
startPosition = 0;
m_AudioSource.clip.GetData(samples, startPosition);
double[] fftMagnitude = FftSharp.Transform.FFTmagnitude(samples.Select(s => (double)s).ToArray());
double maxMagnitude = fftMagnitude.Max();
int maxIndex = fftMagnitude.ToList().IndexOf(maxMagnitude);
double frequency = (double)maxIndex * AudioSettings.outputSampleRate / sampleSize;
Debug.Log(frequency);
if (frequency > 80 && frequency < 1100)
m_LastPitch = frequency;
else
m_LastPitch = 0;
}
But adding the dsp fft on the channel recording the mircophone on fmod, then asking for the dominant frequency…doesn’t get the same result (like 3800 vs 1000).
I’m going to share the code sample for mircophone recording (which is just the fmod sample)
void Start()
{
fmodSystem = FMODUnity.RuntimeManager.CoreSystem;
/*
Determine latency in samples.
*/
string name = "";
FMOD.SPEAKERMODE speakerMode;
FMOD.DRIVER_STATE driverState;
FMODUnity.RuntimeManager.CoreSystem.getRecordDriverInfo(id, out name, 30, out _, out nativeRate, out speakerMode, out nativeChannels, out driverState);
driftThreshold = (uint)(nativeRate * DRIFT_MS) / 1000;
desiredLatency = (uint)(nativeRate * LATENCY_MS) / 1000;
adjustLatency = desiredLatency;
actualLatency = (int)desiredLatency;
/*
Create user sound to record into, then start recording.
*/
exInfo.cbsize = Marshal.SizeOf(typeof(FMOD.CREATESOUNDEXINFO));
exInfo.numchannels = nativeChannels;
exInfo.format = FMOD.SOUND_FORMAT.PCM16;
exInfo.defaultfrequency = nativeRate;
exInfo.length = (uint)(nativeRate * sizeof(short) * nativeChannels);
FMODUnity.RuntimeManager.CoreSystem.createSound("", FMOD.MODE.LOOP_NORMAL | FMOD.MODE.OPENUSER, ref exInfo, out recSound);
FMODUnity.RuntimeManager.CoreSystem.recordStart(id, recSound, true);
recSound.getLength(out recSoundLength, FMOD.TIMEUNIT.PCM);
}
I even tried to analyze directly the sound object, like I did because I wanted to get the pitch in a file not playing. But nothing works, well I mean, I can’t make anything works.
Any inputs ?
Thanks and have a nice day !