I'm using the below code in Unreal Engine 4 to capture microphone input and get the resulting FFT.
I'm having trouble calculating the frequency based on this data.
I've tried finding the max amplitude and taking that as the frequency, but that doesn't seem to be correct.
// Additional includes:
#include "Voice.h"
#include "OnlineSubsystemUtils.h"
// New class member:
TSharedPtr<class IVoiceCapture> voiceCapture;
// Initialisation:
voiceCapture = FVoiceModule::Get().CreateVoiceCapture();
voiceCapture->Start();
// Capturing samples:
uint32 bytesAvailable = 0;
EVoiceCaptureState::Type captureState = voiceCapture->GetCaptureState(bytesAvailable);
if (captureState == EVoiceCaptureState::Ok && bytesAvailable > 0)
{
uint8 buf[maxBytes];
memset(buf, 0, maxBytes);
uint32 readBytes = 0;
voiceCapture->GetVoiceData(buf, maxBytes, readBytes);
uint32 samples = readBytes / 2;
float* sampleBuf = new float[samples];
int16_t sample;
for (uint32 i = 0; i < samples; i++)
{
sample = (buf[i * 2 + 1] << 8) | buf[i * 2];
sampleBuf[i] = float(sample) / 32768.0f;
}
// Do fun stuff here
delete[] sampleBuf;
}