Skip to content

Commit

Permalink
Consider the audio file path in GetAudioHeaderInfoFromFile to avoid u…
Browse files Browse the repository at this point in the history
…nnecessary codec checks when possible (#72)
  • Loading branch information
gtreshchev committed Aug 11, 2024
1 parent ef551e5 commit 5bea8f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
37 changes: 30 additions & 7 deletions Source/RuntimeAudioImporter/Private/RuntimeAudioUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TArray<ERuntimeAudioFormat> URuntimeAudioUtilities::GetAudioFormatsAdvanced(cons

void URuntimeAudioUtilities::GetAudioHeaderInfoFromFile(const FString& FilePath, const FOnGetAudioHeaderInfoResult& Result)
{
GetAudioHeaderInfoFromFile(FilePath, FOnGetAudioHeaderInfoResultNative::CreateLambda([Result](bool bSucceeded, const FRuntimeAudioHeaderInfo& HeaderInfo)
GetAudioHeaderInfoFromFile(FilePath, FOnGetAudioHeaderInfoResultNative::CreateLambda([Result](bool bSucceeded, FRuntimeAudioHeaderInfo HeaderInfo)
{
Result.ExecuteIfBound(bSucceeded, HeaderInfo);
}));
Expand All @@ -40,9 +40,9 @@ void URuntimeAudioUtilities::GetAudioHeaderInfoFromFile(const FString& FilePath,
{
auto ExecuteResult = [Result](bool bSucceeded, FRuntimeAudioHeaderInfo&& HeaderInfo)
{
AsyncTask(ENamedThreads::GameThread, [Result, bSucceeded, HeaderInfo = MoveTemp(HeaderInfo)]()
AsyncTask(ENamedThreads::GameThread, [Result, bSucceeded, HeaderInfo = MoveTemp(HeaderInfo)]() mutable
{
Result.ExecuteIfBound(bSucceeded, HeaderInfo);
Result.ExecuteIfBound(bSucceeded, MoveTemp(HeaderInfo));
});
};

Expand All @@ -53,9 +53,32 @@ void URuntimeAudioUtilities::GetAudioHeaderInfoFromFile(const FString& FilePath,
return;
}

GetAudioHeaderInfoFromBuffer(MoveTemp(AudioBuffer), FOnGetAudioHeaderInfoResultNative::CreateLambda([Result](bool bSucceeded, const FRuntimeAudioHeaderInfo& HeaderInfo)
{
Result.ExecuteIfBound(bSucceeded, HeaderInfo);
FRuntimeCodecFactory CodecFactory;
TArray<FBaseRuntimeCodec*> RuntimeCodecs = CodecFactory.GetCodecs(FilePath);

FRuntimeBulkDataBuffer<uint8> BulkAudioData = FRuntimeBulkDataBuffer<uint8>(AudioBuffer);

FEncodedAudioStruct EncodedData;
EncodedData.AudioData = MoveTemp(BulkAudioData);
for (FBaseRuntimeCodec* RuntimeCodec : RuntimeCodecs)
{
EncodedData.AudioFormat = RuntimeCodec->GetAudioFormat();

FRuntimeAudioHeaderInfo HeaderInfo;
if (!RuntimeCodec->GetHeaderInfo(MoveTemp(EncodedData), HeaderInfo))
{
continue;
}

ExecuteResult(true, MoveTemp(HeaderInfo));
return;
}
}

GetAudioHeaderInfoFromBuffer(MoveTemp(AudioBuffer), FOnGetAudioHeaderInfoResultNative::CreateLambda([Result](bool bSucceeded, FRuntimeAudioHeaderInfo HeaderInfo) mutable
{
Result.ExecuteIfBound(bSucceeded, MoveTemp(HeaderInfo));
}));
});
}
Expand All @@ -74,9 +97,9 @@ void URuntimeAudioUtilities::GetAudioHeaderInfoFromBuffer(TArray64<uint8> AudioD
{
auto ExecuteResult = [Result](bool bSucceeded, FRuntimeAudioHeaderInfo&& HeaderInfo)
{
AsyncTask(ENamedThreads::GameThread, [Result, bSucceeded, HeaderInfo = MoveTemp(HeaderInfo)]()
AsyncTask(ENamedThreads::GameThread, [Result, bSucceeded, HeaderInfo = MoveTemp(HeaderInfo)]() mutable
{
Result.ExecuteIfBound(bSucceeded, HeaderInfo);
Result.ExecuteIfBound(bSucceeded, MoveTemp(HeaderInfo));
});
};

Expand Down
2 changes: 1 addition & 1 deletion Source/RuntimeAudioImporter/Public/RuntimeAudioUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "RuntimeAudioUtilities.generated.h"

/** Static delegate broadcasting the result of retrieving audio header info */
DECLARE_DELEGATE_TwoParams(FOnGetAudioHeaderInfoResultNative, bool, const FRuntimeAudioHeaderInfo&);
DECLARE_DELEGATE_TwoParams(FOnGetAudioHeaderInfoResultNative, bool, FRuntimeAudioHeaderInfo);

/** Dynamic delegate broadcasting the result of retrieving audio header info */
DECLARE_DYNAMIC_DELEGATE_TwoParams(FOnGetAudioHeaderInfoResult, bool, bSucceeded, const FRuntimeAudioHeaderInfo&, HeaderInfo);
Expand Down

0 comments on commit 5bea8f0

Please sign in to comment.