Convert voice recordings to Wav

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!!

Hi baloo887,

You should not use that thread as a reference on how to get audio information. We have a new microphone component in 4.18 in the Audio Capture plugin which should get you started on getting audio from mic input.

To get the raw PCM data, you’ll need to use the new audio mixer and write either a submix effect or a source effect. Then, once you get the PCM data, you’ll need to properly serialize out a .wav file. Your code here appears to be just feeding an audio component to a raw binary serializer, which is absolutely not going to work. There’s some example code you can check out to see how to write a .wav file elsewhere in UE4 (check out the audio recording manager), but there’s nothing in 4.18 that takes raw PCM data and writes it directly to a file. If you must use 4.18, just google .wav file format and follow some tutorials. It’s not hard, but it’s certainly more work than serializing an audio component into binary data.

Hi Minus_Kelvin, thanks so much for the hints! I’ll gonna try this tomorrow.