Hello everyone.
I need to convert base64 string to audio. I get this string from blueprint and I use this string inside cpp code. I want to convert and play this sound. But I do not know how to do this. I tried convert to soundwave but it did not work. I search some codes but they didn’t work. Can anyone know sth about this situation? Where is my mistake? Here is my codes
USoundWave* AConvertSound::WavDataToSoundwave(const FString& Base64String)
{
TArray<uint8> Data;
FBase64::Decode(Base64String, Data);
FWaveModInfo WaveInfo;
if (WaveInfo.ReadWaveInfo(Data.GetData(), Data.Num()))
{
USoundWave* SoundWave = NewObject<USoundWave>();
SoundWave->SetSampleRate(*WaveInfo.pSamplesPerSec);
SoundWave->NumChannels = *WaveInfo.pChannels;
const int32 BytesDataPerSecond = *WaveInfo.pChannels * (*WaveInfo.pBitsPerSample / 8.f) * *WaveInfo.pSamplesPerSec;
if (BytesDataPerSecond)
{
SoundWave->Duration = WaveInfo.SampleDataSize / BytesDataPerSecond;
}
SoundWave->RawPCMDataSize = WaveInfo.SampleDataSize;
SoundWave->RawPCMData = static_cast<uint8*>(FMemory::Malloc(WaveInfo.SampleDataSize));
FMemory::Memcpy(SoundWave->RawPCMData, WaveInfo.SampleDataStart, WaveInfo.SampleDataSize);
return SoundWave;
}
else
{
return nullptr;
}
}
void AConvertSound::PlayBase64Sound(const FString& Base64String)
{
USoundWave* SoundWave = WavDataToSoundwave(Base64String);
if (SoundWave)
{
UE_LOG(LogTemp, Error, TEXT("If state"));
UGameplayStatics::PlaySound2D(this, SoundWave);
}
else
{
UE_LOG(LogTemp, Error, TEXT("Sound can not created!"));
}
}
Thanks.