From 6cb272b8401e8ecb63d6cef84406610469390dc9 Mon Sep 17 00:00:00 2001 From: Respirant Date: Tue, 29 Dec 2020 14:01:16 +0300 Subject: [PATCH] function to destroy audio and change memory deallocation --- .../Private/RuntimeAudioImporterBPLibrary.cpp | 19 +++++++++++++++++++ .../Public/RuntimeAudioImporterBPLibrary.h | 5 ++++- .../RuntimeAudioImporter.Build.cs | 6 +----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/RuntimeAudioImporter/Source/RuntimeAudioImporter/Private/RuntimeAudioImporterBPLibrary.cpp b/RuntimeAudioImporter/Source/RuntimeAudioImporter/Private/RuntimeAudioImporterBPLibrary.cpp index 254ccd7..2553cab 100644 --- a/RuntimeAudioImporter/Source/RuntimeAudioImporter/Private/RuntimeAudioImporterBPLibrary.cpp +++ b/RuntimeAudioImporter/Source/RuntimeAudioImporter/Private/RuntimeAudioImporterBPLibrary.cpp @@ -79,6 +79,23 @@ class USoundWave* URuntimeAudioImporterBPLibrary::GetSoundWaveObject(const uint8 return sw; } +/* Destroy USoundWave static object */ +void URuntimeAudioImporterBPLibrary::DestroySoundWave(USoundWave* ReadySoundWave, bool& SuccessfullyDestroyed) { + if (IsValid(ReadySoundWave) && ReadySoundWave->IsReadyForFinishDestroy()) { + FMemory::Free(ReadySoundWave->RawPCMData); + + ReadySoundWave->RawData.RemoveBulkData(); + ReadySoundWave->InvalidateCompressedData(); + ReadySoundWave->FreeResources(); + ReadySoundWave->RemoveAudioResource(); + ReadySoundWave->ConditionalBeginDestroy(); + SuccessfullyDestroyed = true; + } + else { + SuccessfullyDestroyed = false; + } +} + #include "ThirdParty/dr_wav.h" #include "ThirdParty/dr_mp3.h" @@ -125,7 +142,9 @@ class USoundWave* URuntimeAudioImporterBPLibrary::GetUSoundWaveFromAudioFile_Int USoundWave* ReadyUSoundWave = GetSoundWaveObject((const uint8*)pWavFileData, wavFileSize, status); // A ready USoundWave static object + drwav_free(pSampleData, NULL); drwav_free(pWavFileData, NULL); + return ReadyUSoundWave; } diff --git a/RuntimeAudioImporter/Source/RuntimeAudioImporter/Public/RuntimeAudioImporterBPLibrary.h b/RuntimeAudioImporter/Source/RuntimeAudioImporter/Public/RuntimeAudioImporterBPLibrary.h index dd48789..d4e0554 100644 --- a/RuntimeAudioImporter/Source/RuntimeAudioImporter/Public/RuntimeAudioImporterBPLibrary.h +++ b/RuntimeAudioImporter/Source/RuntimeAudioImporter/Public/RuntimeAudioImporterBPLibrary.h @@ -4,7 +4,7 @@ #include "Kismet/BlueprintFunctionLibrary.h" -#include "Runtime/Engine/Classes/Sound/SoundWave.h" +#include "Sound/SoundWave.h" #include "Developer/TargetPlatform/Public/Interfaces/IAudioFormat.h" @@ -47,6 +47,9 @@ public: UFUNCTION(BlueprintCallable, meta = (Keywords = "Importer, Transcoder, Converter, MP3, FLAC, WAV", ToolTip = "Transcode audio file to USoundWave static object"), Category = "RuntimeAudioImporter") static class USoundWave* GetSoundWaveFromAudioFile(const FString & filePath, TEnumAsByte < AudioFormat > Format, TEnumAsByte < TranscodingStatus > & status, bool DefineFormatAutomatically); + UFUNCTION(BlueprintCallable, meta = (Keywords = "MP3, FLAC, WAV, Destroy", ToolTip = "After the Soundwave is no longer needed, you need to call this function to free memory"), Category = "RuntimeAudioImporter") + static void DestroySoundWave(USoundWave * ReadySoundWave, bool& SuccessfullyDestroyed); + private: /* Main internal get USoundWave from audio file */ diff --git a/RuntimeAudioImporter/Source/RuntimeAudioImporter/RuntimeAudioImporter.Build.cs b/RuntimeAudioImporter/Source/RuntimeAudioImporter/RuntimeAudioImporter.Build.cs index 50add96..1dcbb9f 100644 --- a/RuntimeAudioImporter/Source/RuntimeAudioImporter/RuntimeAudioImporter.Build.cs +++ b/RuntimeAudioImporter/Source/RuntimeAudioImporter/RuntimeAudioImporter.Build.cs @@ -19,11 +19,7 @@ public class RuntimeAudioImporter : ModuleRules } ); - PrivateIncludePaths.AddRange( - new string[] { - // ... add other private include paths required here ... - } - ); + //PrivateIncludePaths.Add(Path.Combine(ModuleDirectory, "ThirdParty")); PublicDependencyModuleNames.AddRange(