diff --git a/RuntimeAudioImporter/Source/RuntimeAudioImporter/Private/RuntimeAudioImporterLibrary.cpp b/RuntimeAudioImporter/Source/RuntimeAudioImporter/Private/RuntimeAudioImporterLibrary.cpp index fc73cae..47f72db 100644 --- a/RuntimeAudioImporter/Source/RuntimeAudioImporter/Private/RuntimeAudioImporterLibrary.cpp +++ b/RuntimeAudioImporter/Source/RuntimeAudioImporter/Private/RuntimeAudioImporterLibrary.cpp @@ -11,11 +11,23 @@ #include "OpusCompressor.h" #include "AdvancedPCMCompressor.h" +#include "PreimportedSoundAsset.h" + +#include "Misc/FileHelper.h" + URuntimeAudioImporterLibrary* URuntimeAudioImporterLibrary::CreateRuntimeAudioImporter() { URuntimeAudioImporterLibrary* Importer = NewObject(); - Importer->AddToRoot(); + Importer->bUseOfAdvancedBuffersInfo = false; + return Importer; +} + +URuntimeAudioImporterLibrary* URuntimeAudioImporterLibrary::CreateAdvancedRuntimeAudioImporter( + const bool UseOfAdvancedBufferInfo) +{ + URuntimeAudioImporterLibrary* Importer = NewObject(); + Importer->bUseOfAdvancedBuffersInfo = UseOfAdvancedBufferInfo; return Importer; } @@ -24,9 +36,6 @@ void URuntimeAudioImporterLibrary::ImportAudioFromFile( { if (!FPaths::FileExists(FilePath)) { - // Trying to get a file inside packaged build - IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile(); - OnResult_Internal(nullptr, ETranscodingStatus::AudioDoesNotExist); return; } @@ -47,6 +56,7 @@ void URuntimeAudioImporterLibrary::ImportAudioFromFile( ImportAudioFromBuffer(AudioBuffer, Format, BuffersDetails); } + void URuntimeAudioImporterLibrary::ImportAudioFromPreimportedSound(UPreimportedSoundAsset* PreimportedSoundAssetRef, const FBuffersDetailsStruct& BuffersDetails) { @@ -145,7 +155,8 @@ USoundWave* URuntimeAudioImporterLibrary::DefineSoundWave() OnProgress_Internal(45); // Whether to fill the compressed buffer or not. If not filled, it is necessary to fill Raw (Wave) data so that the engine will automatically generate compressed data. - if (BuffersDetailsInfo.CompressionFormat == ECompressionFormat::RawData) + if (BuffersDetailsInfo.CompressionFormat == ECompressionFormat::RawData || (bUseOfAdvancedBuffersInfo && + AdvancedBuffersInfo.FillRawData)) { // Callback Dispatcher OnProgress OnProgress_Internal(60); @@ -157,8 +168,11 @@ USoundWave* URuntimeAudioImporterLibrary::DefineSoundWave() return nullptr; } - // free unneeded PCM Data - FMemory::Free(TranscodingFillInfo.PCMInfo.RawPCMData); + if (!bUseOfAdvancedBuffersInfo || (bUseOfAdvancedBuffersInfo && !AdvancedBuffersInfo.FillPCMData)) + { + // free unneeded PCM Data + FMemory::Free(TranscodingFillInfo.PCMInfo.RawPCMData); + } // Callback Dispatcher OnProgress OnProgress_Internal(75); @@ -166,16 +180,18 @@ USoundWave* URuntimeAudioImporterLibrary::DefineSoundWave() // Fill Raw (Wave) Data FillRawWaveData(SoundWaveRef); + // Fill PCM Data if needed + if (bUseOfAdvancedBuffersInfo && AdvancedBuffersInfo.FillPCMData) + { + FillPCMData(SoundWaveRef); + } + // Callback Dispatcher OnProgress OnProgress_Internal(95); return SoundWaveRef; } - /* We don't need to fill RawPCMData buffer since it is not used at runtime during audio playback. But in case you want to fill this buffer - uncomment the code below (you should also remove some RawPCMData cleanups in code before it is executed) - sw->RawPCMData = static_cast(FMemory::Malloc(sw->RawPCMDataSize)); - FMemory::Memmove(sw->RawPCMData, TranscodingFillInfo.PCMInfo.RawPCMData, sw->RawPCMDataSize); - */ // Set a new sound compression quality SoundWaveRef->CompressionQuality = BuffersDetailsInfo.SoundCompressionQuality; @@ -186,20 +202,30 @@ USoundWave* URuntimeAudioImporterLibrary::DefineSoundWave() // Callback Dispatcher OnProgress OnProgress_Internal(60); - // Transcode Raw PCM data to Compressed data - if (!TranscodePCMToCompressedData(SoundWaveRef)) + if (!bUseOfAdvancedBuffersInfo || (bUseOfAdvancedBuffersInfo && AdvancedBuffersInfo.FillCompressedData && + AdvancedBuffersInfo.CompressionFormat != ECompressionFormat::RawData)) { - return nullptr; + // Transcode Raw PCM data to Compressed data + if (!TranscodePCMToCompressedData(SoundWaveRef)) + { + return nullptr; + } + + // Callback Dispatcher OnProgress + OnProgress_Internal(75); + + SoundWaveRef->SetPrecacheState(ESoundWavePrecacheState::NotStarted); + + // Fill the compressed data + FillCompressedData(SoundWaveRef); + + // Fill PCM Data if needed + if (bUseOfAdvancedBuffersInfo && AdvancedBuffersInfo.FillPCMData) + { + FillPCMData(SoundWaveRef); + } } - // Callback Dispatcher OnProgress - OnProgress_Internal(75); - - SoundWaveRef->SetPrecacheState(ESoundWavePrecacheState::NotStarted); - - // Fill the compressed data - FillCompressedData(SoundWaveRef); - // Callback Dispatcher OnProgress OnProgress_Internal(95); @@ -227,6 +253,9 @@ void URuntimeAudioImporterLibrary::FillRawWaveData(USoundWave* SoundWaveRef) con FMemory::Memmove(SoundWaveRef->RawData.Realloc(TranscodingFillInfo.WAVInfo.WaveDataSize), TranscodingFillInfo.WAVInfo.WaveData, TranscodingFillInfo.WAVInfo.WaveDataSize); SoundWaveRef->RawData.Unlock(); + + UE_LOG(LogTemp, Warning, TEXT("RawData has been filled successfully, Raw data size: %d"), + TranscodingFillInfo.WAVInfo.WaveDataSize); } void URuntimeAudioImporterLibrary::FillCompressedData(USoundWave* SoundWaveRef) @@ -247,6 +276,18 @@ void URuntimeAudioImporterLibrary::FillCompressedData(USoundWave* SoundWaveRef) CompressedBulkData->Unlock(); SoundWaveRef->SetPrecacheState(ESoundWavePrecacheState::Done); + + UE_LOG(LogTemp, Warning, TEXT("Compressed data has been filled successfully, Cmpressed data size: %d"), + TranscodingFillInfo.CompressedInfo.CompressedFormatData.Num()); +} + +void URuntimeAudioImporterLibrary::FillPCMData(USoundWave* SoundWaveRef) +{ + SoundWaveRef->RawPCMData = static_cast(FMemory::Malloc(SoundWaveRef->RawPCMDataSize)); + FMemory::Memmove(SoundWaveRef->RawPCMData, TranscodingFillInfo.PCMInfo.RawPCMData, + SoundWaveRef->RawPCMDataSize); + UE_LOG(LogTemp, Warning, TEXT("PCM Data has been filled successfully, PCMData size: %d"), + TranscodingFillInfo.PCMInfo.RawPCMDataSize); } bool URuntimeAudioImporterLibrary::TranscodePCMToCompressedData(USoundWave* SoundWaveToGetData) @@ -258,6 +299,11 @@ bool URuntimeAudioImporterLibrary::TranscodePCMToCompressedData(USoundWave* Soun // Sound quality information for compression purposes FSoundQualityInfo SoundQualityInfo = GenerateSoundQualityInfo(SoundWaveToGetData); + if (bUseOfAdvancedBuffersInfo) + { + BuffersDetailsInfo.CompressionFormat = AdvancedBuffersInfo.CompressionFormat; + } + switch (BuffersDetailsInfo.CompressionFormat) { case OggVorbis: @@ -315,9 +361,12 @@ bool URuntimeAudioImporterLibrary::TranscodePCMToCompressedData(USoundWave* Soun } } - // When the compressed data is generated, RawPCMData is no longer needed. - RawPCMArray.Empty(); - FMemory::Free(TranscodingFillInfo.PCMInfo.RawPCMData); + if (!bUseOfAdvancedBuffersInfo || (bUseOfAdvancedBuffersInfo && !AdvancedBuffersInfo.FillPCMData)) + { + // When the compressed data is generated, RawPCMData is no longer needed + RawPCMArray.Empty(); + FMemory::Free(TranscodingFillInfo.PCMInfo.RawPCMData); + } // Callback Dispatcher OnProgress OnProgress_Internal(85); diff --git a/RuntimeAudioImporter/Source/RuntimeAudioImporter/Public/RuntimeAudioImporterLibrary.h b/RuntimeAudioImporter/Source/RuntimeAudioImporter/Public/RuntimeAudioImporterLibrary.h index 5d202ef..013358a 100644 --- a/RuntimeAudioImporter/Source/RuntimeAudioImporter/Public/RuntimeAudioImporterLibrary.h +++ b/RuntimeAudioImporter/Source/RuntimeAudioImporter/Public/RuntimeAudioImporterLibrary.h @@ -5,8 +5,6 @@ #include "Sound/SoundWave.h" #include "Async/Async.h" -#include "PreimportedSoundAsset.h" - #include "RuntimeAudioImporterLibrary.generated.h" /** Possible audio importing results */ @@ -79,7 +77,7 @@ enum ECompressionFormat }; /** Information about which buffers to fill */ -USTRUCT(BlueprintType) +USTRUCT(BlueprintType, Category = "RuntimeAudioImporter") struct FBuffersDetailsStruct { GENERATED_BODY() @@ -155,9 +153,28 @@ struct FTranscodingFillStruct FCompressedStruct CompressedInfo; }; +/** Advanced CPP only structure which contains information about which buffers to fill */ +struct FAdvancedBuffersStruct +{ + /** Needed compression format. Do not select RawData here! If you want to fill Raw (Wav) Data, check the "FillRawData" parameter */ + TEnumAsByte CompressionFormat; + + /** Whether to fill the compressed data or not */ + bool FillCompressedData = false; + + /** Whether to fill PCM Data or not */ + bool FillPCMData = false; + + /** Whether to fill Raw (Wav) data */ + bool FillRawData = false; +}; + // Forward declaration of the FSoundQualityInfo structure struct FSoundQualityInfo; +// Forward declaration of the UPreimportedSoundAsset class +class UPreimportedSoundAsset; + /** * Declare delegate which will be called during the transcoding process * @@ -201,6 +218,10 @@ public: /** Buffers details info. CPP use only */ FBuffersDetailsStruct BuffersDetailsInfo = FBuffersDetailsStruct(); + /** Advanced buffer details info. CPP use only (even as an input parameter) */ + FAdvancedBuffersStruct AdvancedBuffersInfo = FAdvancedBuffersStruct(); + bool bUseOfAdvancedBuffersInfo = false; + /** * Instantiates a RuntimeAudioImporter object * @@ -211,6 +232,14 @@ public: "RuntimeAudioImporter") static URuntimeAudioImporterLibrary* CreateRuntimeAudioImporter(); + /** + * If you want to select a separate formats to fill (using of "FAdvancedBuffersStruct"), then use this method in CPP + * + * @param UseOfAdvancedBufferInfo Whether to use the advanced buffer info or not + */ + UFUNCTION() + static URuntimeAudioImporterLibrary* CreateAdvancedRuntimeAudioImporter(const bool UseOfAdvancedBufferInfo); + /** * Transcode audio file to SoundWave object * @@ -296,6 +325,13 @@ private: */ void FillCompressedData(USoundWave* SoundWaveRef); + /** + * Fill SoundWave PCM data buffer + * + * @param SoundWaveRef SoundWave object reference + */ + void FillPCMData(USoundWave* SoundWaveRef); + /** * Transcode Audio from PCM to 16-bit WAV data *