How to Reliably Access USoundWave PCM Data (with or without FAsyncAudioDecompress)

Some progress, this works in the editor but fails in a cooked game since

SoundWave->GetCompressedData

returns NULL

bool InitFromSoundWave(USoundWave* SoundWave)
	{
		if (SoundWave->bProcedural)
		{
			return false;
		}

		if (SoundWave->RawPCMData == NULL)
		{
			FAudioDevice* AudioDevice = GEngine->GetMainAudioDeviceRaw();
			if (AudioDevice)
			{
				EDecompressionType DecompressionType = SoundWave->DecompressionType;
				SoundWave->DecompressionType = DTYPE_Native;

				FName format = AudioDevice->GetRuntimeFormat(SoundWave);

				FByteBulkData* Bulk = SoundWave->GetCompressedData(format, SoundWave->GetPlatformCompressionOverridesForCurrentPlatform());
				if (Bulk)
				{
					SoundWave->InitAudioResource(*Bulk);
	
					if (SoundWave->DecompressionType != DTYPE_RealTime || SoundWave->CachedRealtimeFirstBuffer == nullptr)
					{
						FAsyncAudioDecompress TempDecompress(SoundWave, 128, AudioDevice);
						TempDecompress.StartSynchronousTask();
					}
				}

				SoundWave->DecompressionType = DecompressionType;
			}
		}

		UE_LOG(LogTemp, Warning, TEXT("RawPCMDataSize: %d"), SoundWave->RawPCMDataSize);

		return InitFromShortSamples(TArrayView<SHORT>((SHORT*)SoundWave->RawPCMData, SoundWave->RawPCMDataSize / 2),
			SoundWave->GetSampleRateForCurrentPlatform(), SoundWave->NumChannels);
	}
1 Like