minor design changes

This commit is contained in:
Respirant
2021-01-28 09:01:59 +01:00
parent dd7fbcd5c6
commit 203af789c5
2 changed files with 32 additions and 44 deletions
@@ -1,7 +1,6 @@
// Respirant 2020. // Respirant 2020.
#include "RuntimeAudioImporterLibrary.h" #include "RuntimeAudioImporterLibrary.h"
#include "RuntimeAudioImporter.h"
URuntimeAudioImporterLibrary::URuntimeAudioImporterLibrary(const FObjectInitializer& ObjectInitializer) URuntimeAudioImporterLibrary::URuntimeAudioImporterLibrary(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer) : Super(ObjectInitializer)
@@ -9,19 +8,15 @@ URuntimeAudioImporterLibrary::URuntimeAudioImporterLibrary(const FObjectInitiali
} }
URuntimeAudioImporterLibrary::~URuntimeAudioImporterLibrary()
{
}
URuntimeAudioImporterLibrary* URuntimeAudioImporterLibrary::CreateRuntimeAudioImporter() URuntimeAudioImporterLibrary* URuntimeAudioImporterLibrary::CreateRuntimeAudioImporter()
{ {
URuntimeAudioImporterLibrary* Importer = NewObject<URuntimeAudioImporterLibrary>(); URuntimeAudioImporterLibrary* Importer = NewObject<URuntimeAudioImporterLibrary>();
Importer->AddToRoot();
return Importer; return Importer;
} }
URuntimeAudioImporterLibrary* URuntimeAudioImporterLibrary::ImportAudioFromFile(const FString& filePath, TEnumAsByte < AudioFormat > Format, bool DefineFormatAutomatically) URuntimeAudioImporterLibrary* URuntimeAudioImporterLibrary::ImportAudioFromFile(const FString& filePath, TEnumAsByte < AudioFormat > Format, bool DefineFormatAutomatically)
{ {
AddToRoot();
AsyncTask(ENamedThreads::AnyThread, [=]() { AsyncTask(ENamedThreads::AnyThread, [=]() {
GetUSoundWaveFromAudioFile_Internal(filePath, Format, DefineFormatAutomatically); GetUSoundWaveFromAudioFile_Internal(filePath, Format, DefineFormatAutomatically);
}); });
@@ -40,8 +35,8 @@ bool URuntimeAudioImporterLibrary::DestroySoundWave(USoundWave* ReadySoundWave)
ReadySoundWave->RawData.RemoveBulkData(); ReadySoundWave->RawData.RemoveBulkData();
ReadySoundWave->RemoveAudioResource(); ReadySoundWave->RemoveAudioResource();
AsyncTask(ENamedThreads::AudioThread, [=]() { AsyncTask(ENamedThreads::AudioThread, [=]() {
ReadySoundWave->InvalidateCompressedData(true); ReadySoundWave->InvalidateCompressedData(true);
}); });
//Finish USoundWave destroy (complete object deletion as UObject) //Finish USoundWave destroy (complete object deletion as UObject)
ReadySoundWave->IsReadyForFinishDestroy(); //This is not needed for verification, but for the initial removal ReadySoundWave->IsReadyForFinishDestroy(); //This is not needed for verification, but for the initial removal
@@ -141,7 +136,7 @@ URuntimeAudioImporterLibrary* URuntimeAudioImporterLibrary::GetUSoundWaveFromAud
OnResult_Internal(nullptr, Status); OnResult_Internal(nullptr, Status);
} }
else { else {
if (TranscodeAudioFileToPCMData(this, TCHAR_TO_ANSI(*filePath), Format, Status, framesToWrite, pSampleData, channels, sampleRate) == false) { if (TranscodeAudioFileToPCMData(TCHAR_TO_ANSI(*filePath), Format, Status, framesToWrite, pSampleData, channels, sampleRate) == false) {
FMemory::Free(pSampleData); FMemory::Free(pSampleData);
// Callback Dispatcher OnResult // Callback Dispatcher OnResult
@@ -151,14 +146,14 @@ URuntimeAudioImporterLibrary* URuntimeAudioImporterLibrary::GetUSoundWaveFromAud
void* WaveData; void* WaveData;
size_t WaveDataSize; size_t WaveDataSize;
if (TranscodePCMToWAVData(this, *&WaveData, *&WaveDataSize, framesToWrite, *&pSampleData, channels, sampleRate) == false) { if (TranscodePCMToWAVData(*&WaveData, *&WaveDataSize, framesToWrite, *&pSampleData, channels, sampleRate) == false) {
FMemory::Free(pSampleData); FMemory::Free(pSampleData);
// Callback Dispatcher OnResult // Callback Dispatcher OnResult
OnResult_Internal(nullptr, Status); OnResult_Internal(nullptr, Status);
} }
else { else {
USoundWave* ReadyUSoundWave = GetSoundWaveObject(this, (const uint8*)WaveData, WaveDataSize, Status); // A ready USoundWave static object USoundWave* ReadyUSoundWave = GetSoundWaveObject((const uint8*)WaveData, WaveDataSize, Status); // A ready USoundWave static object
/* Free temporary transcoding data */ /* Free temporary transcoding data */
FMemory::Free(WaveData); FMemory::Free(WaveData);
@@ -172,14 +167,14 @@ URuntimeAudioImporterLibrary* URuntimeAudioImporterLibrary::GetUSoundWaveFromAud
OnResult_Internal(ReadyUSoundWave, Status); OnResult_Internal(ReadyUSoundWave, Status);
} }
} }
} }
} }
return this; return this;
} }
class USoundWave* URuntimeAudioImporterLibrary::GetSoundWaveObject(URuntimeAudioImporterLibrary* RuntimeAudioImporterObjRef, const uint8* WaveData, int32 WaveDataSize, TEnumAsByte < TranscodingStatus >& Status) class USoundWave* URuntimeAudioImporterLibrary::GetSoundWaveObject(const uint8* WaveData, int32 WaveDataSize, TEnumAsByte < TranscodingStatus >& Status)
{ {
USoundWave* sw = NewObject<USoundWave>(USoundWave::StaticClass()); USoundWave* sw = NewObject<USoundWave>(USoundWave::StaticClass());
if (!sw) { if (!sw) {
@@ -190,12 +185,12 @@ class USoundWave* URuntimeAudioImporterLibrary::GetSoundWaveObject(URuntimeAudio
FString ErrorReason; FString ErrorReason;
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(65); OnProgress_Internal(65);
if (WaveInfo.ReadWaveInfo(WaveData, WaveDataSize, &ErrorReason)) if (WaveInfo.ReadWaveInfo(WaveData, WaveDataSize, &ErrorReason))
{ {
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(75); OnProgress_Internal(75);
if (*WaveInfo.pBitsPerSample != 16) { if (*WaveInfo.pBitsPerSample != 16) {
Status = TranscodingStatus::UnsupportedBitDepth; Status = TranscodingStatus::UnsupportedBitDepth;
@@ -208,7 +203,7 @@ class USoundWave* URuntimeAudioImporterLibrary::GetSoundWaveObject(URuntimeAudio
sw->RawData.Unlock(); sw->RawData.Unlock();
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(80); OnProgress_Internal(80);
int32 DurationDiv = *WaveInfo.pChannels * *WaveInfo.pBitsPerSample * *WaveInfo.pSamplesPerSec; int32 DurationDiv = *WaveInfo.pChannels * *WaveInfo.pBitsPerSample * *WaveInfo.pSamplesPerSec;
if (DurationDiv) if (DurationDiv)
@@ -227,7 +222,7 @@ class USoundWave* URuntimeAudioImporterLibrary::GetSoundWaveObject(URuntimeAudio
sw->SoundGroup = ESoundGroup::SOUNDGROUP_Default; sw->SoundGroup = ESoundGroup::SOUNDGROUP_Default;
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(85); OnProgress_Internal(85);
} }
else { else {
if (ErrorReason.Equals(TEXT("Invalid WAVE file."), ESearchCase::CaseSensitive)) { if (ErrorReason.Equals(TEXT("Invalid WAVE file."), ESearchCase::CaseSensitive)) {
@@ -248,12 +243,12 @@ class USoundWave* URuntimeAudioImporterLibrary::GetSoundWaveObject(URuntimeAudio
sw->RawPCMData = (uint8*)FMemory::Malloc(sw->RawPCMDataSize); sw->RawPCMData = (uint8*)FMemory::Malloc(sw->RawPCMDataSize);
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(90); OnProgress_Internal(90);
FMemory::Memcpy(sw->RawPCMData, WaveInfo.SampleDataStart, sw->RawPCMDataSize); FMemory::Memcpy(sw->RawPCMData, WaveInfo.SampleDataStart, sw->RawPCMDataSize);
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(95); OnProgress_Internal(95);
if (!sw) { if (!sw) {
Status = TranscodingStatus::InvalidUSoundWave; Status = TranscodingStatus::InvalidUSoundWave;
@@ -263,9 +258,9 @@ class USoundWave* URuntimeAudioImporterLibrary::GetSoundWaveObject(URuntimeAudio
return sw; return sw;
} }
bool URuntimeAudioImporterLibrary::TranscodeAudioFileToPCMData(URuntimeAudioImporterLibrary* RuntimeAudioImporterObjRef, const char* filePath, TEnumAsByte < AudioFormat > Format, TEnumAsByte < TranscodingStatus >& Status, uint64& framesToWrite, int16_t*& pSampleData, uint32& channels, uint32& sampleRate) bool URuntimeAudioImporterLibrary::TranscodeAudioFileToPCMData(const char* filePath, TEnumAsByte < AudioFormat > Format, TEnumAsByte < TranscodingStatus >& Status, uint64& framesToWrite, int16_t*& pSampleData, uint32& channels, uint32& sampleRate)
{ {
RuntimeAudioImporterObjRef->OnProgress_Internal(5); OnProgress_Internal(5);
switch (Format) switch (Format)
{ {
case AudioFormat::MP3: case AudioFormat::MP3:
@@ -283,17 +278,17 @@ bool URuntimeAudioImporterLibrary::TranscodeAudioFileToPCMData(URuntimeAudioImpo
} }
else { else {
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(10); OnProgress_Internal(10);
pSampleData = (int16_t*)FMemory::Malloc((size_t)drmp3_get_pcm_frame_count(&mp3) * mp3.channels * sizeof(int16_t)); pSampleData = (int16_t*)FMemory::Malloc((size_t)drmp3_get_pcm_frame_count(&mp3) * mp3.channels * sizeof(int16_t));
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(15); OnProgress_Internal(15);
framesToWrite = drmp3_read_pcm_frames_s16(&mp3, drmp3_get_pcm_frame_count(&mp3), pSampleData); framesToWrite = drmp3_read_pcm_frames_s16(&mp3, drmp3_get_pcm_frame_count(&mp3), pSampleData);
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(35); OnProgress_Internal(35);
channels = mp3.channels; channels = mp3.channels;
sampleRate = mp3.sampleRate; sampleRate = mp3.sampleRate;
@@ -317,17 +312,17 @@ bool URuntimeAudioImporterLibrary::TranscodeAudioFileToPCMData(URuntimeAudioImpo
} }
else { else {
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(10); OnProgress_Internal(10);
pSampleData = (int16_t*)FMemory::Malloc((size_t)wav.totalPCMFrameCount * wav.channels * sizeof(int16_t)); pSampleData = (int16_t*)FMemory::Malloc((size_t)wav.totalPCMFrameCount * wav.channels * sizeof(int16_t));
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(15); OnProgress_Internal(15);
framesToWrite = drwav_read_pcm_frames_s16(&wav, wav.totalPCMFrameCount, pSampleData); framesToWrite = drwav_read_pcm_frames_s16(&wav, wav.totalPCMFrameCount, pSampleData);
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(35); OnProgress_Internal(35);
channels = wav.channels; channels = wav.channels;
sampleRate = wav.sampleRate; sampleRate = wav.sampleRate;
@@ -351,17 +346,17 @@ bool URuntimeAudioImporterLibrary::TranscodeAudioFileToPCMData(URuntimeAudioImpo
} }
else { else {
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(10); OnProgress_Internal(10);
pSampleData = (int16_t*)FMemory::Malloc((size_t)pFlac->totalPCMFrameCount * pFlac->channels * sizeof(int16_t)); pSampleData = (int16_t*)FMemory::Malloc((size_t)pFlac->totalPCMFrameCount * pFlac->channels * sizeof(int16_t));
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(15); OnProgress_Internal(15);
framesToWrite = drflac_read_pcm_frames_s16(pFlac, pFlac->totalPCMFrameCount, pSampleData); framesToWrite = drflac_read_pcm_frames_s16(pFlac, pFlac->totalPCMFrameCount, pSampleData);
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(35); OnProgress_Internal(35);
channels = pFlac->channels; channels = pFlac->channels;
sampleRate = pFlac->sampleRate; sampleRate = pFlac->sampleRate;
@@ -379,7 +374,7 @@ bool URuntimeAudioImporterLibrary::TranscodeAudioFileToPCMData(URuntimeAudioImpo
} }
} }
bool URuntimeAudioImporterLibrary::TranscodePCMToWAVData(URuntimeAudioImporterLibrary* RuntimeAudioImporterObjRef, void*& WaveData, size_t& WaveDataSize, uint64 framesToWrite, int16_t*& pSampleData, uint32 channels, uint32 sampleRate) { bool URuntimeAudioImporterLibrary::TranscodePCMToWAVData(void*& WaveData, size_t& WaveDataSize, uint64 framesToWrite, int16_t*& pSampleData, uint32 channels, uint32 sampleRate) {
drwav wavEncode; drwav wavEncode;
drwav_data_format format; drwav_data_format format;
format.container = drwav_container_riff; format.container = drwav_container_riff;
@@ -398,12 +393,12 @@ bool URuntimeAudioImporterLibrary::TranscodePCMToWAVData(URuntimeAudioImporterLi
} }
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(40); OnProgress_Internal(40);
drwav_write_pcm_frames(&wavEncode, framesToWrite, pSampleData); drwav_write_pcm_frames(&wavEncode, framesToWrite, pSampleData);
// Callback Dispatcher OnProgress (not completely accurate implementation) // Callback Dispatcher OnProgress (not completely accurate implementation)
RuntimeAudioImporterObjRef->OnProgress_Internal(60); OnProgress_Internal(60);
drwav_uninit(&wavEncode); drwav_uninit(&wavEncode);
return true; return true;
@@ -2,7 +2,6 @@
#pragma once #pragma once
#include "Sound/SoundWave.h" #include "Sound/SoundWave.h"
#include "Async/Async.h"
#include "RuntimeAudioImporterLibrary.generated.h" #include "RuntimeAudioImporterLibrary.generated.h"
/** /**
@@ -70,9 +69,6 @@ public:
UPROPERTY(BlueprintAssignable, Category = "RuntimeAudioImporter") UPROPERTY(BlueprintAssignable, Category = "RuntimeAudioImporter")
FOnResult OnResult; FOnResult OnResult;
URuntimeAudioImporterLibrary();
~URuntimeAudioImporterLibrary();
/** /**
* Instantiates a RuntimeAudioImporter object. * Instantiates a RuntimeAudioImporter object.
* *
@@ -115,7 +111,7 @@ public:
private: private:
/** /**
* Internal main audio transcoding function * Internal main audio transcoding function
* *
* @param filePath Path to the audio file to import * @param filePath Path to the audio file to import
* @param Format Audio file format (extension) * @param Format Audio file format (extension)
* @param DefineFormatAutomatically Whether to define format (extension) automatically or not * @param DefineFormatAutomatically Whether to define format (extension) automatically or not
@@ -126,18 +122,16 @@ private:
/** /**
* Get USoundWave static object reference from 16-bit WAV data * Get USoundWave static object reference from 16-bit WAV data
* *
* @param RuntimeAudioImporterObjRef Reference to the created RuntimeAudioImporter object
* @param WaveData Reference to memory location of 16-bit WAV byte data * @param WaveData Reference to memory location of 16-bit WAV byte data
* @param WaveDataSize Memory size allocated for 16-bit WAV byte data * @param WaveDataSize Memory size allocated for 16-bit WAV byte data
* @param Status Reference to TranscodingStatus Enumerator in case an error occurs * @param Status Reference to TranscodingStatus Enumerator in case an error occurs
* @return Returns a ready USoundWave object * @return Returns a ready USoundWave object
*/ */
static class USoundWave* GetSoundWaveObject(URuntimeAudioImporterLibrary * RuntimeAudioImporterObjRef, const uint8 * WaveData, int32 WaveDataSize, TEnumAsByte < TranscodingStatus > & Status); class USoundWave* GetSoundWaveObject(const uint8 * WaveData, int32 WaveDataSize, TEnumAsByte < TranscodingStatus > & Status);
/** /**
* Transcode Audio from File to PCM Data * Transcode Audio from File to PCM Data
* *
* @param RuntimeAudioImporterObjRef Reference to the created RuntimeAudioImporter object
* @param filePath Path to the audio file from which to receive PCM data * @param filePath Path to the audio file from which to receive PCM data
* @param Format Format of the audio file (e.g. mp3. flac, etc) * @param Format Format of the audio file (e.g. mp3. flac, etc)
* @param Status Reference to TranscodingStatus Enumerator in case an error occurs * @param Status Reference to TranscodingStatus Enumerator in case an error occurs
@@ -147,12 +141,11 @@ private:
* @param sampleRate Sample rate of the audio * @param sampleRate Sample rate of the audio
* @return Whether the transcoding was successful or not * @return Whether the transcoding was successful or not
*/ */
static bool TranscodeAudioFileToPCMData(URuntimeAudioImporterLibrary * RuntimeAudioImporterObjRef, const char* filePath, TEnumAsByte < AudioFormat > Format, TEnumAsByte < TranscodingStatus > & Status, uint64 & framesToWrite, int16_t * &pSampleData, uint32 & channels, uint32 & sampleRate); bool TranscodeAudioFileToPCMData(const char* filePath, TEnumAsByte < AudioFormat > Format, TEnumAsByte < TranscodingStatus > & Status, uint64 & framesToWrite, int16_t * &pSampleData, uint32 & channels, uint32 & sampleRate);
/** /**
* Transcode Audio from PCM to 16-bit WAV data * Transcode Audio from PCM to 16-bit WAV data
* *
* @param RuntimeAudioImporterObjRef Reference to the created RuntimeAudioImporter object
* @param WaveData Reference to memory location of 16-bit WAV byte data * @param WaveData Reference to memory location of 16-bit WAV byte data
* @param WaveDataSize Memory size allocated for 16-bit WAV byte data * @param WaveDataSize Memory size allocated for 16-bit WAV byte data
* @param framesToWrite PCM frames which will be written for WAV data * @param framesToWrite PCM frames which will be written for WAV data
@@ -161,7 +154,7 @@ private:
* @param sampleRate Sample rate of the audio * @param sampleRate Sample rate of the audio
* @return Whether the transcoding was successful or not * @return Whether the transcoding was successful or not
*/ */
static bool TranscodePCMToWAVData(URuntimeAudioImporterLibrary* RuntimeAudioImporterObjRef, void*& WaveData, size_t & WaveDataSize, uint64 framesToWrite, int16_t * &pSampleData, uint32 channels, uint32 sampleRate); bool TranscodePCMToWAVData(void*& WaveData, size_t & WaveDataSize, uint64 framesToWrite, int16_t * &pSampleData, uint32 channels, uint32 sampleRate);
/** /**
* @param filePath File path, where to find the format (by extension) * @param filePath File path, where to find the format (by extension)