How do get raw byte and sampling data from a users microphone ?
I have a script here which I intend to use to capture raw audio data and transport it to an rtcWeb socket.
Is there a better way to capture and sample audio from the users microphone ?
#include "MyAudioCaptureActor.h"
#include "Components/AudioCaptureComponent.h"
AMyAudioCaptureActor::AMyAudioCaptureActor()
{
PrimaryActorTick.bCanEverTick = true;
AudioCaptureComponent = CreateDefaultSubobject<UAudioCaptureComponent>(TEXT("AudioCaptureComponent"));
}
void AMyAudioCaptureActor::BeginPlay()
{
Super::BeginPlay();
AudioCaptureComponent->Start();
AudioCaptureComponent->OnAudioCapture().AddUObject(this, &AMyAudioCaptureActor::OnAudioCapture);
}
void AMyAudioCaptureActor::OnAudioCapture(const float* InBuffer, int32 NumFrames, int32 NumChannels, float SampleRate, int32 BufferSize)
{
for (int i = 0; i < NumFrames * NumChannels; ++i)
{
int16 IntAudioData = static_cast<int16>(InBuffer[i] * 32767.0f);
AudioBuffer.Add((uint8) (IntAudioData & 0xff));
AudioBuffer.Add((uint8) ((IntAudioData >> 8) & 0xff));
}
// Stream or process the AudioBuffer here
// For example, you can send AudioBuffer over a network
}
void AMyAudioCaptureActor::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
AudioCaptureComponent->Stop();
}