Files
UnrealEngineUWP/Engine/Source/Runtime/SignalProcessing/Private/EnvelopeFollower.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

134 lines
3.9 KiB
C++

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#include "DSP/EnvelopeFollower.h"
#include "DSP/Dsp.h"
namespace Audio
{
// see https://en.wikipedia.org/wiki/RC_time_constant
// Time constants indicate how quickly the envelope follower responds to changes in input
static const float AnalogTImeConstant = 1.00239343f;
static const float DigitalTimeConstant = 4.60517019f;
FEnvelopeFollower::FEnvelopeFollower()
: EnvMode(EPeakMode::Peak)
, SampleRate(44100.0f)
, AttackTimeMsec(0.0f)
, AttackTimeSamples(0.0f)
, ReleaseTimeMsec(0.0f)
, ReleaseTimeSamples(0.0f)
, CurrentEnvelopeValue(0.0f)
, bIsAnalog(true)
{
}
FEnvelopeFollower::FEnvelopeFollower(const float InSampleRate, const float InAttackTimeMsec, const float InReleaseTimeMSec, const EPeakMode::Type InMode, const bool bInIsAnalg)
{
Init(InSampleRate, InAttackTimeMsec, InReleaseTimeMSec, InMode, bIsAnalog);
}
FEnvelopeFollower::~FEnvelopeFollower()
{
}
void FEnvelopeFollower::Init(const float InSampleRate, const float InAttackTimeMsec, const float InReleaseTimeMSec, const EPeakMode::Type InMode, const bool bInIsAnalog)
{
SampleRate = InSampleRate;
bIsAnalog = bInIsAnalog;
EnvMode = InMode;
// Set the attack and release times using the default values
SetAttackTime(InAttackTimeMsec);
SetReleaseTime(InReleaseTimeMSec);
}
void FEnvelopeFollower::Reset()
{
CurrentEnvelopeValue = 0.0f;
}
void FEnvelopeFollower::SetAnalog(const bool bInIsAnalog)
{
bIsAnalog = bInIsAnalog;
SetAttackTime(AttackTimeMsec);
SetReleaseTime(ReleaseTimeMsec);
}
void FEnvelopeFollower::SetAttackTime(const float InAttackTimeMsec)
{
AttackTimeMsec = InAttackTimeMsec;
const float TimeConstant = bIsAnalog ? AnalogTImeConstant : DigitalTimeConstant;
AttackTimeSamples = FMath::Exp(-1000.0f * TimeConstant / (AttackTimeMsec * SampleRate));
}
void FEnvelopeFollower::SetReleaseTime(const float InReleaseTimeMsec)
{
ReleaseTimeMsec = InReleaseTimeMsec;
const float TimeConstant = bIsAnalog ? AnalogTImeConstant : DigitalTimeConstant;
ReleaseTimeSamples = FMath::Exp(-1000.0f * TimeConstant / (InReleaseTimeMsec * SampleRate));
}
void FEnvelopeFollower::SetMode(const EPeakMode::Type InMode)
{
EnvMode = InMode;
}
float FEnvelopeFollower::ProcessAudio(const float InAudioSample)
{
// Take the absolute value of the input sample
float Sample = FMath::Abs(InAudioSample);
// If we're not Peak detecting, then square the input
if (EnvMode != EPeakMode::Peak)
{
Sample = Sample * Sample;
}
float TimeSamples = (Sample > CurrentEnvelopeValue) ? AttackTimeSamples : ReleaseTimeSamples;
float NewEnvelopeValue = TimeSamples * (CurrentEnvelopeValue - Sample) + Sample;;
NewEnvelopeValue = Audio::UnderflowClamp(NewEnvelopeValue);
NewEnvelopeValue = FMath::Clamp(NewEnvelopeValue, 0.0f, 1.0f);
// Update and return the envelope value
return CurrentEnvelopeValue = NewEnvelopeValue;
}
float FEnvelopeFollower::ProcessAudioNonClamped(const float InAudioSample)
{
// Take the absolute value of the input sample
float Sample = FMath::Abs(InAudioSample);
// If we're not Peak detecting, then square the input
if (EnvMode != EPeakMode::Peak)
{
Sample = Sample * Sample;
}
float TimeSamples = (Sample > CurrentEnvelopeValue) ? AttackTimeSamples : ReleaseTimeSamples;
float NewEnvelopeValue = TimeSamples * (CurrentEnvelopeValue - Sample) + Sample;;
NewEnvelopeValue = Audio::UnderflowClamp(NewEnvelopeValue);
// Update and return the envelope value
return CurrentEnvelopeValue = NewEnvelopeValue;
}
int16 FEnvelopeFollower::ProcessAudio(const int16 InAudioSample)
{
// Convert to float
float SampleValueFloat = (float)InAudioSample / 32767.0f;
// Process it
float Result = ProcessAudio(SampleValueFloat);
// Convert back to int16
return (int16)(Result * 32767.0f);
}
float FEnvelopeFollower::GetCurrentValue() const
{
return CurrentEnvelopeValue;
}
}