Files
UnrealEngineUWP/Engine/Source/Runtime/SignalProcessing/Private/IAudioEncoder.cpp
Josh Markiewicz d79515867d Copying //UE4/Dev-Online to Dev-Main (//UE4/Dev-Main)
- Up to CL8320930 from DevOnline and 8311605 Merge Down from Main
- skipped some Fortnite content/plugins/code where it tried to reintegrate files that had been moved pending investigation
#rb none

[CL 8321295 by Josh Markiewicz in Main branch]
2019-08-26 18:35:22 -04:00

96 lines
2.7 KiB
C++

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#include "DSP/Encoders/IAudioEncoder.h"
Audio::IAudioEncoder::IAudioEncoder(uint32 AudioBufferSlack, uint32 DataBufferSlack /*= 4096*/)
: UncompressedAudioBuffer(AudioBufferSlack)
, CompressedDataBuffer(DataBufferSlack)
{
}
Audio::IAudioEncoder::~IAudioEncoder()
{
}
bool Audio::IAudioEncoder::PushAudio(const float* InBuffer, int32 NumSamples, bool bEncodeIfPossible /*= true*/)
{
if (UncompressedAudioBuffer.Remainder() < (uint32) NumSamples)
{
ensureAlwaysMsgf(false, TEXT("Not enough space. Please construct IAudioEncoder with a larager value for AudioBufferSlack"));
return false;
}
else
{
UncompressedAudioBuffer.Push(InBuffer, NumSamples);
}
if (bEncodeIfPossible)
{
return EncodeIfPossible();
}
else
{
return true;
}
}
int32 Audio::IAudioEncoder::PopData(uint8* OutData, int32 DataSize)
{
int32 Result = CompressedDataBuffer.Pop(OutData, DataSize);
// If result is negative, it indicates how many samples short we are
// of the requested amount.
return Result;
}
bool Audio::IAudioEncoder::EncodeIfPossible()
{
while (UncompressedAudioBuffer.Num() > SamplesRequiredPerEncode())
{
CurrentAudioBuffer.Reset();
CurrentAudioBuffer.AddUninitialized(SamplesRequiredPerEncode());
UncompressedAudioBuffer.Pop(CurrentAudioBuffer.GetData(), CurrentAudioBuffer.Num());
CurrentCompressedBuffer.Reset();
if (!EncodeChunk(CurrentAudioBuffer, CurrentCompressedBuffer))
{
ensureAlwaysMsgf(false, TEXT("Encode failed!"));
return false;
}
else
{
int32 Result = CompressedDataBuffer.Push(CurrentCompressedBuffer.GetData(), CurrentCompressedBuffer.Num());
ensureAlwaysMsgf(Result == CurrentCompressedBuffer.Num(), TEXT("Compressed Data Buffer full. Please construct IAudioEncoder with a larger value for DataBufferSlack or call PopData more often."));
break;
}
}
return true;
}
int64 Audio::IAudioEncoder::Finalize()
{
// Encode all remaining uncompressed audio:
EncodeIfPossible();
CurrentCompressedBuffer.Reset();
EndFile(CurrentCompressedBuffer);
int32 Remainder = CompressedDataBuffer.Push(CurrentCompressedBuffer.GetData(), CurrentCompressedBuffer.Num());
if (Remainder < 0)
{
ensureAlwaysMsgf(false, TEXT("Insufficient slack for header! Please use a larger value for DataBufferSlack."));
return Remainder;
}
return CompressedDataBuffer.Num();
}
void Audio::IAudioEncoder::Init(const FSoundQualityInfo& InQualityInfo)
{
StartFile(InQualityInfo, CurrentCompressedBuffer);
int32 Remainder = CompressedDataBuffer.Push(CurrentCompressedBuffer.GetData(), CurrentCompressedBuffer.Num());
checkf(Remainder > 0, TEXT("Insufficient slack for header! Please use a larger value for DataBufferSlack."));
}