Game freezing on loading wav/ogg file at runtime

Hi, I need to load and play custom audio files at runtime for a VR game I’m working on where players can load music of their own choice into the game and play them. I’ve gotten the wav and ogg files to load and play at runtime (thanks to this post) but encountered an issue with the game freezing for few seconds (most likely the game thread getting blocked). For wav files, on playing the loaded file, the log showed:

“LogAudioDerivedData: Display: Building compressed audio format OGG hash OGG wave SoundWave_1…”

Wav file was getting compressed to ogg and then getting played which was blocking the game thread (from what i gather but I can be wrong)
But, now I’m loading ogg file and the game is still freezing for few seconds (less than the wav file though). I’m loading the file asynchronously and and then passing it to an actor class that has an audio component. The loaded file is being assigned to the audio component’s sound property. No freezes till this point. Game freezes when the execution hits the “Play” node of the audio component.
Can someone tell how to overcome this game freezing? Thanks! (Using 4.23.1)

How do you load the .wav file, you better show your code

Was loading wav file using a different code but switched to using ogg format (it’s already compressed and takes less time to play than wav) as wav file was getting decompressed on playing it the first time which caused the freezing for long depending on the length of the audio. Using ogg also causes a small freeze but its 1-2 seconds at max compared to wav taking 8-10 seconds. I want to avoid this small hiccup when playing. Here’s the code for loading ogg at runtime:



USoundWave* sw = NewObject<USoundWave>(USoundWave::StaticClass());

    if (!sw)
    {
       UE_LOG(LogTemp, Warning, TEXT("Sound Wave Object Not Valid"));
    }

    sw->SoundGroup = ESoundGroup::SOUNDGROUP_Default;
    TArray < uint8 > rawFile;

    //FilePath is a string variable that holds the path to the ogg file including filename and extension
    bool loaded = FFileHelper::LoadFileToArray(rawFile, FilePath.GetCharArray().GetData());

    if (loaded)
    {
        FByteBulkData* bulkData = &sw->CompressedFormatData.GetFormat(TEXT("OGG"));
        bulkData->Lock(LOCK_READ_WRITE);
        FMemory::Memcpy(bulkData->Realloc(rawFile.Num()), rawFile.GetData(), rawFile.Num());
        bulkData->Unlock();
        sw->NumChannels = 2;
        //Setting duration manually for the time being, need to figure out a way to do it dynamically
        sw->Duration = 225.0f;
        UE_LOG(LogTemp, Warning, TEXT("OGG File Loaded Successfully"));
    }
    else
    {
       UE_LOG(LogTemp, Warning, TEXT("OGG File Could Not Be Loaded"));
    }


You do this in the game thread, so at the time of working with a large amount of data in memory, there is a freeze.

You can look at the source code of my plugin to see how I did it: GitHub - gtreshchev/RuntimeAudioImporter: Runtime Audio Importer plugin for Unreal Engine. Importing audio of various formats at runtime.