FSoundWavePCMWriter not working in UE 5.4.3 ?

Hi,

I am getting an audio wav file bytes data from an HTTP response and I am trying to make a SoundWave and play it at runtime.

In order to make the HTTP Request I use the C++ HTTP Module and I get the bytes using FHttpResponsePtr->GetContent().

In order to create the SoundWave I use FWaveModInfo combined with FSoundWavePCMWriter:

USoundWave* WavBytesToSoundWave(const TArray<uint8>& AudioBytes, bool& Success, FString& OutReason)
{
	// from: https://github.com/getnamo/SocketIOClient-Unreal/blob/master/Source/CoreUtility/Private/CUBlueprintLibrary.cpp#L281
	// and:	 https://forums.unrealengine.com/t/loading-raw-wav-in-runtime-into-usoundwave/126017
	// and:  https://medium.com/@bdmccowan/how-to-use-audio-capture-component-and-audio-capture-in-unreal-c-59a78c13221 
	FWaveModInfo WaveModInfo;
	Success = WaveModInfo.ReadWaveInfo(AudioBytes.GetData(), AudioBytes.Num(), &OutReason);

	if(Success)
	{
		UE_LOG(LogTemp, Display, TEXT("NUM SAMPLE IS %d, SAMPLE DATA SIZE IS %d"), WaveModInfo.GetNumSamples(), WaveModInfo.SampleDataSize);
		Audio::FSoundWavePCMWriter Writer;
		
		TArray<int16> ConvertedArray;
		ConvertedArray.Reset(WaveModInfo.GetNumSamples());
		ConvertedArray.AddUninitialized(WaveModInfo.GetNumSamples());
		for(uint32 i = 0; i < WaveModInfo.SampleDataSize; i+=2)
		{
			ConvertedArray[i/2] = static_cast<int16>(WaveModInfo.SampleDataStart[i]) | static_cast<int16>(WaveModInfo.SampleDataStart[i + 1]) << 8;
		}
		Audio::FSampleBuffer Buffer(ConvertedArray.GetData(), WaveModInfo.GetNumSamples(), *WaveModInfo.pChannels, *WaveModInfo.pSamplesPerSec);
		USoundWave* SoundWave = Writer.SynchronouslyWriteSoundWave(Buffer);

		UE_LOG(LogTemp, Display, TEXT("SOUNDWAVE DURATION: %f"), SoundWave->Duration);
		return SoundWave;
	}

	UE_LOG(LogTemp, Error, TEXT("Couldn't convert wav bytes to soundwave: %s"), *OutReason);
	return nullptr;
}

It was working fine with the previous version of Unreal Engine, however with version 5.4 the created SoundWave is only played for a few milliseconds then stops automatically with no error message or crash. I am a bit lost on what to do because there is no error…

This is what I added to try to debug:


UE 5.3 (working):

LogBlueprintUserMessages: [Ariel_Demo_C_1] 129544
LogTemp: Display: NUM SAMPLE IS 64750, SAMPLE DATA SIZE IS 129500
LogTemp: Display: SOUNDWAVE DURATION: 2.697917
LogBlueprintUserMessages: [Ariel_Demo_C_1] PLAYING
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.007892
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.015799
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.023707
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.031614
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.031614
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.039521
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.047429
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.047429
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.055336
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.063243
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.071151
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.079058
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.086965
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.086965
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.094873
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.10278
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.10278
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.110687
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.118595
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.126502
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.134409
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.142317
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.142317
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.150224
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.158131
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.158131
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.166039
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.173946
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.181853
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.189761
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.197668
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.197668
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.205575
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.213483
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.213483
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.22139
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.229297
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.237205
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.245112
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.253019
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.253019
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.260927
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.268834
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.268834
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.276741
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.284649
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.292556
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.300463
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.308371
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.308371
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.316278
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.324185
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.324185
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.332093
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.34
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.347907
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.355815
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.363722
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.363722
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.371629
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.379537
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.387444
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.387444
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.395351
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.403259
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.411166
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.419073
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.419073
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.426981
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.434888
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.442795
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.442795
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.450703
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.45861
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.466517
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.474425
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.474425
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.482332
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.490239
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.498147
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.506054
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.506054
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.513961
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.521869
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.529776
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.529776
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.537683
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.545591
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.553498
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.561405
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.561405
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.569313
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.57722
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.585127
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.585127
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.593035
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.600942
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.608849
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.616757
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.624664
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.624664
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.632571
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.640479
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.640479
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.648386
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.656293
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.664201
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.672108
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.680015
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.680015
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.687923
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.69583
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.69583
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.703737
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.711645
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.719552
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.727459
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.735367
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.735367
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.743274
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.751181
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.751181
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.759089
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.766996
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.774903
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.782811
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.790718
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.790718
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.798625
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.806533
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.806533
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.81444
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.822347
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.830255
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.838162
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.84607
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.84607
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.853977
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.861884
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.861884
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.869792
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.877699
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.885606
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.893514
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.901421
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.901421
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.909328
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.917235
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.917235
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.925143
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.93305
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.940958
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.948865
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.956772
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.956772
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.96468
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.972587
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.980494
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.980494
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.988402
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.996309
LogBlueprintUserMessages: [Ariel_Demo_C_1] 1.0
LogBlueprintUserMessages: [Ariel_Demo_C_1] 1.0
LogBlueprintUserMessages: [Ariel_Demo_C_1] STOPPED

UE 5.4 (not working):

LogBlueprintUserMessages: [Ariel_Demo_C_1] 125944
LogTemp: Display: NUM SAMPLE IS 62950, SAMPLE DATA SIZE IS 125900
LogTemp: Display: SOUNDWAVE DURATION: 2.622917
LogBlueprintUserMessages: [Ariel_Demo_C_1] PLAYING
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.0
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.008118
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.016251
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.024384
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.024384
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.032518
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.040651
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.048785
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.048785
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.056918
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.065052
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.073185
LogBlueprintUserMessages: [Ariel_Demo_C_1] 0.073185
LogBlueprintUserMessages: [Ariel_Demo_C_1] STOPPED

Does someone have encounter the same problem? Does anyone have successfully created a SoundWave from bytes at runtime with Unreal 5.4? I would be very grateful if you could help me!

P.S: For now, I can useUSoundWaveProcedural with QueueAudio() method for UE 5.4. This works well, however this is not optimal since I want to be able to convert back the SoundWave to bytes array later on, and I couldn’t find a way to get the raw PCM data from an USoundWaveProcedural (which is kind of logic because a Procedural Sound does not have Raw Data)…

2 Likes

the same , usoundwave work fine in 5.4.2,but played for a few milliseconds in 5.4.3,i don’t how to fix it.

I have the same problem. When I work In 5.3,all is right.
Then In 5.4.3, just play a shortime, In Editor and in Package.
If I add code in Editor ,it can work well only in Editor.
such as

USoundWave* XXXXAsyncAction::GenerateFromPCM(TArray<uint8> InPCMData)
{
	if (!IsInGameThread())
	{
		AsyncTask(ENamedThreads::GameThread, [InPCMData, this]() {
			GenerateFromPCM(InPCMData);
			});

		return nullptr;
	}


	const int32 ChannelCount = 1;

	const int32 SizeOfSample = 16 / 8;

	const int32 NumSamples = InPCMData.Num() / SizeOfSample;

	const int32 NumFrames = NumSamples / ChannelCount;

	const int32 SampleDataSize = InPCMData.Num();
	FString SoundWaveName = TEXT("SoundWave:") + FGuid::NewGuid().ToString();
	USoundWave* SoundWave = NewObject<USoundWave>(GetTransientPackage(), *SoundWaveName, RF_Public | RF_Standalone);

	TArray<uint8> WavData;

	ConvertPCMToWave(InPCMData, WavData);

#if WITH_EDITOR&& ENGINE_MAJOR_VERSION == 5&& ENGINE_MINOR_VERSION >=1 

	FSharedBuffer Buffer = FSharedBuffer::Clone(WavData.GetData(), WavData.Num());

	SoundWave->RawData.UpdatePayload(Buffer);

#endif  
	FWaveModInfo WaveInfo;

	WaveInfo.ReadWaveInfo(WavData.GetData(), WavData.Num());

	SoundWave->SetSampleRate(*WaveInfo.pSamplesPerSec);

	SoundWave->NumChannels = *WaveInfo.pChannels;

	const int32 BytesDataPerSecond = *WaveInfo.pChannels * (*WaveInfo.pBitsPerSample / 8.f) * *WaveInfo.pSamplesPerSec;

	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);

	SoundWave->SetSoundAssetCompressionType(ESoundAssetCompressionType::PCM);
	SoundWave->SoundWaveDataPtr->InitializeDataFromSoundWave(*SoundWave);

	return SoundWave;


}

It help to work well inEditor.but in package,there are ensure problem and it plays only a short time.
I hope someone can help me.

The regular USoundWave isn’t well-suited for dynamically filling in audio data, but is more geared towards serialization on the editor side for later runtime use, so this runtime sound wave creation behavior can vary between different UE versions. From my experience, it’s common for its behavior to change across versions, including differences in how it handles compressed audio buffers and the ways for getting and populating audio data.

I recommend decoding the audio data to PCM manually and then queuing it into USoundWaveProcedural (which is inherited from USoundWave), as this approach maintains consistent behavior across different engine versions. For example, RuntimeAudioImporter uses the same approach and works consistently and correctly from UE 4.24 through the latest version, 5.4, and it can handle WAV formats. You can check it out here: https://www.fab.com/listings/66e0d72e-982f-4d9e-aaaf-13a1d22efad1