Error Importing WAV audio at runtime

Hey there, I really need some assistance on this - it’s imperative that my game is able to import WAV audio data at runtime.

So far, everything works in the editor. The problem is this error on a packaged build:

Here is the code:



USoundWave* SpeechSystem::GetSoundWaveFromFile(const FString& filePath)
{
	USoundWave* sw = NewObject<USoundWave>(USoundWave::StaticClass());

	if (!sw)
		return nullptr;

	TArray < uint8 > rawFile;

	FFileHelper::LoadFileToArray(rawFile, filePath.GetCharArray().GetData());
	FWaveModInfo WaveInfo;

	if (WaveInfo.ReadWaveInfo(rawFile.GetData(), rawFile.Num()))
	{
		sw->InvalidateCompressedData();

		sw->RawData.Lock(LOCK_READ_WRITE);
		void* LockedData = sw->RawData.Realloc(rawFile.Num());
		FMemory::Memcpy(LockedData, rawFile.GetData(), rawFile.Num());
		sw->RawData.Unlock();
		
		int32 DurationDiv = *WaveInfo.pChannels * *WaveInfo.pBitsPerSample * *WaveInfo.pSamplesPerSec;
		if (DurationDiv)
		{
			sw->Duration = *WaveInfo.pWaveDataSize * 8.0f / DurationDiv;
		}
		else
		{
			sw->Duration = 0.0f;
		}
		sw->SampleRate = *WaveInfo.pSamplesPerSec;
		sw->NumChannels = *WaveInfo.pChannels;
		sw->RawPCMDataSize = WaveInfo.SampleDataSize;
		sw->SoundGroup = ESoundGroup::SOUNDGROUP_Default;

	}
	else {
		return nullptr;
	}

	return sw;
}


Searching for the error message I got comes up with literally nothing, so I figured I would post here. Any ideas?

I have no idea, just a small note though: you initialize sw and allocate a new object to it, however, when the method doesn’t find the info you need, it returns null, but doesn’t destroy sw, perhaps you should do it, to avoid memory leaks?

Thanks for the tip, . I’m still new to c++ after coming from c#. Question though, isn’t this stack allocation - meaning the memory will be freed once it’s taken off the stack?

You will have to de-allocate everything you initialize using the “new” keyword. UE4 does include its own garbage engine, but it wont apply in this case.
For stack allocation you have to use alloca(object) (or _alloca don’t quite remember sorry) be careful though, keep in mind it will be deleted once you reach the end of the method, not when it goes out of scope.

e.g.



void Foo::DoSmth()
{
   char* mychar = malloca(5);

  (....)

  return;
  //malloca gets deleted here
}

void Foo::DoSmthElse()
{
   for(int i=1; i< YourCondition; ++i)
   {
    char* mychar = malloca(i);
   }

  (....)

  return;
  //malloca will ONLY delete the last memory allocation for mychar, so all the other instantiations (memory allocations) will still be there = memory leak
}



Hope that helps :slight_smile:

Can I bump this since I posted it on the weekend? It’d be nice to get some staff insight on the error message.

Alternatively, is there another method of getting external WAV audio to play within the engine? It seems like this should be something in-built, like Unity’s WWW class.