Hi everybody,
I want to know if it’s possible to convert voice data recorded from microphone to an actual audio file, like a .wav.
Starting from this thread:
https://answers.unrealengine.com/questions/347976/basic-microphone-input-with-ue4.html
I was able to get audio from the microphne, and reproduce it using a UAudioComponent.
But if I try to save it as a .wav binary file, the file is currupted.
I don’t have experience in audio manipulation/conversion, so someone can explain me how to proceed to obtain a valid .wav (or any other format) file?
here is my code:
TSharedPtr<class IVoiceCapture> VoiceCapture;
TArray<uint8> VoiceCaptureBuffer;
TArray<uint8> VoiceCaptureBufferAppended;
int32 MaxRawCaptureDataSize;
USoundWaveProcedural* SoundWave;
void UVoiceUtils::init() {
VoiceCapture = FVoiceModule::Get().CreateVoiceCapture();
if (VoiceCapture.IsValid())
{
MaxRawCaptureDataSize = VoiceCapture->GetBufferSize();
VoiceCaptureBuffer.Empty(MaxRawCaptureDataSize);
VoiceCaptureBuffer.AddUninitialized(MaxRawCaptureDataSize);
VoiceCaptureBuffer.Empty(MaxRawCaptureDataSize);
VoiceCaptureBufferAppended.AddUninitialized(MaxRawCaptureDataSize);
}
}
void UVoiceUtils::start() {
VoiceCapture->Start();
SoundWave = NewObject<USoundWaveProcedural>();
SoundWave->SampleRate = 16000;
SoundWave->NumChannels = 1;
SoundWave->Duration = INDEFINITELY_LOOPING_DURATION;
SoundWave->SoundGroup = SOUNDGROUP_Voice;
SoundWave->bLooping = false;
SoundWave->bProcedural = true;
SoundWave->Pitch = 0.99f;
SoundWave->Volume = 5.f;
}
void UVoiceUtils::captureAudio() {
if (VoiceCapture.IsValid()) {
uint32 VoiceCaptureBytesAvailable = 0;
EVoiceCaptureState::Type CaptureState = VoiceCapture->GetCaptureState(VoiceCaptureBytesAvailable);
VoiceCaptureBuffer.Reset();
if (CaptureState == EVoiceCaptureState::Ok && VoiceCaptureBytesAvailable > 0)
{
float VoiceCaptureTotalSquared = 0;
VoiceCaptureBuffer.SetNumUninitialized(VoiceCaptureBytesAvailable);
VoiceCapture->GetVoiceData(VoiceCaptureBuffer.GetData(), VoiceCaptureBytesAvailable, VoiceCaptureBytesAvailable);
TArray<uint8> NewAudioBuffer;
NewAudioBuffer.AddUninitialized(VoiceCaptureBytesAvailable);
FMemory::Memcpy(NewAudioBuffer.GetData(), VoiceCaptureBuffer.GetData(), VoiceCaptureBytesAvailable);
VoiceCaptureBufferAppended.Append(NewAudioBuffer);
SoundWave->QueueAudio(VoiceCaptureBuffer.GetData(), VoiceCaptureBytesAvailable);
}
}
}
void UVoiceUtils::stopAndSave(UAudioComponent* VoiceCaptureAudioComponent, FString path, FString &text) {
VoiceCaptureAudioComponent->SetSound(SoundWave);
VoiceCaptureAudioComponent->Play();
VoiceCapture->Stop();
SoundWave->OnGeneratePCMAudio(VoiceCaptureBufferAppended, 16);
UE_LOG(LogTemp, Warning, TEXT("Recording Stopped"));
//SAVING THE DATA TO BINARY
FBufferArchive Binary;
Binary << VoiceCaptureBufferAppended;
FFileHelper::SaveArrayToFile(VoiceCaptureBufferAppended, path);
Binary.FlushCache();
Binary.Empty();
UE_LOG(LogTemp, Warning, TEXT("File Saved"));
return;
}
Thanks!!