I have a bunch of Sound Waves (mono, 16 bit, 16k sample rate), and I need to access the PCM data of these Sound Waves in C++.
I did some looking around, but couldn’t really find any way to do this, or even any recent discussion on this (see references below).
The USoundWave.RawPCMData and USoundWave.RawPCMDataSize are exactly what I need, the problem is they are often nullptr and 0 even when there is audio in the Sound Wave. Also, unfortunately, there doesn’t seem to be any getter for the PCM data which would handle preparation of that data for you.
I got excited when I saw the suggestion to use the FAudioThumbnail::GenerateWaveformPreview method, which uses FAsyncAudioDecompress (in file AudioTrackEditor.cpp). In the example there is first a check to see if decompression is required, then decompress if necessary, then access the raw PCM data - exactly what I need! Unfortunately, when I tried to implement this it seems to do nothing, and I’m still getting nullptr and 0 for the raw PCM data. Am I doing something wrong? How can I get this working? (my implementation below)
void UMyClass::UseCannedAudioData(USoundWave* CannedAudio)
{
if (CannedAudio->RawPCMData == nullptr) {
FAsyncAudioDecompress TempDecompress(CannedAudio);
TempDecompress.StartSynchronousTask();
}
UE_LOG(LogTemp, Warning, TEXT("UseCannedAudioData, RawPCMDataSize: %d"), CannedAudio->RawPCMDataSize);
TArray<uint8> AudioBuffer(CannedAudio->RawPCMData, CannedAudio->RawPCMDataSize);
VoiceData = AudioBuffer;
UE_LOG(LogTemp, Warning, TEXT("UseCannedAudioData, voice data size: %d"), VoiceData.Num());
}
Also, in case I can’t get this working, is there any other way to access the audio data? It seems like this shouldn’t be so difficult, but I’ve been stuck on it for days. Please help!
References:
Post from 2015 saying there is no way to do this:
Post from 2014 forums, saying to use the FAudioThumbnail::GenerateWaveformPreview method:
https://forums.unrealengine.com/development-discussion/c-gameplay-programming/19191-accessing-soundwave-pcm-data-from-c-code
Same post but on answer hub instead, explaining the unreliability of USoundWave.RawPCMData: