Files
UnrealEngineUWP/Engine/Source/Runtime/SignalProcessing/Private/BitCrusher.cpp
ryan durand 0f0464a30e Updating copyright for Engine Runtime.
#rnx
#rb none


#ROBOMERGE-OWNER: ryan.durand
#ROBOMERGE-AUTHOR: ryan.durand
#ROBOMERGE-SOURCE: CL 10869210 via CL 10869511 via CL 10869900
#ROBOMERGE-BOT: (v613-10869866)

[CL 10870549 by ryan durand in Main branch]
2019-12-26 14:45:42 -05:00

68 lines
1.6 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "DSP/BitCrusher.h"
namespace Audio
{
FBitCrusher::FBitCrusher()
: SampleRate(0)
, BitDepth(16.0f)
, BitDelta(0.0f)
, Phase(1.0f)
, PhaseDelta(1.0f)
{
BitDelta = 1.0f / FMath::Pow(2.0f, BitDepth);
LastOutput[0] = 0.0f;
LastOutput[1] = 0.0f;
}
FBitCrusher::~FBitCrusher()
{
}
void FBitCrusher::Init(const float InSampleRate, const int32 InNumChannels)
{
SampleRate = InSampleRate;
Phase = 1.0f;
NumChannels = InNumChannels;
}
void FBitCrusher::SetSampleRateCrush(const float InFrequency)
{
PhaseDelta = FMath::Clamp(InFrequency, 1.0f, SampleRate) / SampleRate;
}
void FBitCrusher::SetBitDepthCrush(const float InBitDepth)
{
BitDepth = FMath::Clamp(InBitDepth, 1.0f, 32.0f);
BitDelta = 1.0f / FMath::Pow(2.0f, BitDepth);
}
void FBitCrusher::ProcessAudioFrame(const float* InFrame, float* OutFrame)
{
Phase += PhaseDelta;
if (Phase >= 1.0f)
{
Phase -= 1.0f;
for (int32 ChannelIndex = 0; ChannelIndex < NumChannels; ++ChannelIndex)
{
LastOutput[ChannelIndex] = BitDelta * (float)FMath::FloorToInt(InFrame[ChannelIndex] / BitDelta + 0.5f);
}
}
for (int32 ChannelIndex = 0; ChannelIndex < NumChannels; ++ChannelIndex)
{
OutFrame[ChannelIndex] = LastOutput[ChannelIndex];
}
}
void FBitCrusher::ProcessAudio(const float* InBuffer, const int32 InNumSamples, float* OutBuffer)
{
for (int32 SampleIndex = 0; SampleIndex < InNumSamples; SampleIndex += NumChannels)
{
ProcessAudioFrame(&InBuffer[SampleIndex], &OutBuffer[SampleIndex]);
}
}
}