UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VoiceDelayBuffer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6
7/************************************************************************/
8/* FVoiceDelayBuffer */
9/* This class provides a simple template circular buffer. */
10/************************************************************************/
11template<class SampleType>
13{
14private:
15 TArray<SampleType> InternalBuffer;
16
17 int32 WriteIndex;
18 int32 ReadIndex;
19
20public:
22 void Init(int32 MaxLength)
23 {
24 check(MaxLength > 1);
25 InternalBuffer.Init(0, MaxLength);
26 WriteIndex = 0;
27 ReadIndex = 0;
28 }
29
31 void SetDelay(int32 NumSamples)
32 {
33 const int32 Delta = WriteIndex - NumSamples;
34 if (Delta < 0)
35 {
36 ReadIndex = InternalBuffer.Num() + Delta;
37 }
38 else
39 {
40 ReadIndex = Delta;
41 }
42 }
43
44 /* Gets the amount of samples on the buffer. */
46 {
47 if (ReadIndex > WriteIndex)
48 {
49 return WriteIndex + InternalBuffer.Num() - ReadIndex;
50 }
51 else
52 {
53 return WriteIndex - ReadIndex;
54 }
55 }
56
58 void PushSample(const SampleType& InSample)
59 {
60 WriteIndex = WriteIndex % InternalBuffer.Num();
61 InternalBuffer[WriteIndex++] = InSample;
62 }
63
64 void PushFrame(const SampleType* InFrame, int32 NumChannels)
65 {
66 for (int32 ChannelIndex = 0; ChannelIndex < NumChannels; ChannelIndex++)
67 {
68 PushSample(InFrame[ChannelIndex]);
69 }
70 }
71
73 SampleType PopSample()
74 {
75 ReadIndex = ReadIndex % InternalBuffer.Num();
76 return InternalBuffer[ReadIndex++];
77 }
78
80 void ProcessSample(const SampleType& InSample, SampleType& OutSample)
81 {
84 }
85
86 /* pop as much audio as possible onto OutBuffer. returns the number of samples copied. */
87 int32 PopBufferedAudio(SampleType* OutBuffer, int32 MaxNumSamples)
88 {
89 ReadIndex = ReadIndex % InternalBuffer.Num();
91
92 if (SamplesToCopy == 0)
93 {
94 return 0;
95 }
96
97 if (ReadIndex <= WriteIndex)
98 {
99 FMemory::Memcpy(OutBuffer, InternalBuffer.GetData() + ReadIndex, SamplesToCopy * sizeof(SampleType));
100 }
101 else
102 {
103 const int32 SamplesUntilEndOfBuffer = InternalBuffer.Num() - ReadIndex;
104 //Copy the rest of the buffer to the end, then the remainder in the beginning.
105 FMemory::Memcpy(OutBuffer, InternalBuffer.GetData() + ReadIndex, SamplesUntilEndOfBuffer * sizeof(SampleType));
106 FMemory::Memcpy(OutBuffer + SamplesUntilEndOfBuffer, InternalBuffer.GetData(), (SamplesToCopy - SamplesUntilEndOfBuffer) * sizeof(SampleType));
107 }
108 ReadIndex = ReadIndex + SamplesToCopy;
109 return SamplesToCopy;
110 }
111};
112
113
114
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition VoiceDelayBuffer.h:13
void SetDelay(int32 NumSamples)
Definition VoiceDelayBuffer.h:31
void PushFrame(const SampleType *InFrame, int32 NumChannels)
Definition VoiceDelayBuffer.h:64
void PushSample(const SampleType &InSample)
Definition VoiceDelayBuffer.h:58
void Init(int32 MaxLength)
Definition VoiceDelayBuffer.h:22
SampleType PopSample()
Definition VoiceDelayBuffer.h:73
int32 PopBufferedAudio(SampleType *OutBuffer, int32 MaxNumSamples)
Definition VoiceDelayBuffer.h:87
void ProcessSample(const SampleType &InSample, SampleType &OutSample)
Definition VoiceDelayBuffer.h:80
int32 GetBufferCount()
Definition VoiceDelayBuffer.h:45
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType * GetData() UE_LIFETIMEBOUND
Definition Array.h:1027
void Init(const ElementType &Element, SizeType Number)
Definition Array.h:3043
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160