So for our Unreal Engine 4 project, we’ve been working on a gameplay mechanic involving microphone input. This is something that hasn’t been very well documented in the community so far. We started an Answer Hub thread about it and have since figured it out, but have seen some others still asking. So, sorry for the delay but here are the relevant excerpts for our implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// 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; } |