UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
GrainEnvelope.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
7namespace Audio
8{
9 // Utility for managing a grain data (enveloped buffer)
10 namespace Grain
11 {
13
14 // Grain envelope types
25
26 // Utility function generates an envelope with the input array for the number of desired frames
27 static void GenerateEnvelopeData(FEnvelope& InData, const int32 InNumFrames, const EEnvelope InEnvelopeType)
28 {
29 InData.Reset();
30 InData.AddUninitialized(InNumFrames);
31
32 const float N = static_cast<float>(InNumFrames);
33 const float N_1 = N - 1.0f;
34 float n = 0.0f;
35
36 switch (InEnvelopeType)
37 {
39 {
40 const float Denominator = 0.3f * N_1 / 2.0f;
41 for (int32 i = 0; i < InNumFrames; ++i, n += 1.0f)
42 {
43 InData[i] = FMath::Exp(-0.5f * FMath::Pow((n - 0.5f * N_1) / Denominator, 2.0f));
44 }
45 }
46 break;
47
49 {
50 const float A = 0.5f * N_1;
51 for (int32 i = 0; i < InNumFrames; ++i, n += 1.0f)
52 {
53 InData[i] = 1.0f - FMath::Abs((n - A) / A);
54 }
55 }
56 break;
57
59 {
60 for (int32 i = 0; i < InNumFrames; ++i, n += 1.0f)
61 {
62 InData[i] = 1.0f - n / N_1;
63 }
64 }
65 break;
66
68 {
69 for (int32 i = 0; i < InNumFrames; ++i, n += 1.0f)
70 {
71 InData[i] = n / N_1;
72 }
73 }
74 break;
75
77 {
78 for (int32 i = 0; i < InNumFrames; ++i, n += 1.0f)
79 {
80 InData[i] = FMath::Pow((n - N + 1.0f) / N_1, 4.0f);
81 }
82 }
83 break;
84
86 {
87 for (int32 i = 0; i < InNumFrames; ++i, n += 1.0f)
88 {
89 InData[i] = FMath::Pow(n / N_1, 4.0f);
90 }
91 }
92 break;
93
94 case EEnvelope::Hann:
95 {
96 for (int32 i = 0; i < InNumFrames; ++i, n += 1.0f)
97 {
98 InData[i] = 0.5f - 0.5f * FMath::Cos(2.0f * PI * n / N_1);
99 }
100 }
101 break;
102 }
103 }
104
105 // Returns the interpolated envelope value given the fraction through the grain
106 static float GetValue(const FEnvelope& InGrainData, const float InFraction)
107 {
108 const int32 NumFrames = InGrainData.Num();
109 const float Frame = static_cast<float>(InFraction) * (NumFrames - 1);
110 const int32 PrevIndex = static_cast<int32>(Frame);
111 const int32 NextIndex = FMath::Min(NumFrames, PrevIndex + 1);
112 const float AlphaIndex = Frame - static_cast<float>(PrevIndex);
113 const float* GrainEnvelopePtr = InGrainData.GetData();
114 return FMath::Lerp(GrainEnvelopePtr[PrevIndex], GrainEnvelopePtr[NextIndex], AlphaIndex);
115 }
116
117 }
118}
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
#define PI
Definition UnrealMathUtility.h:65
Definition Envelope.h:14
Definition Array.h:670
EEnvelope
Definition GrainEnvelope.h:16
TArray< float > FEnvelope
Definition GrainEnvelope.h:12
NO_LOGGING.
Definition AudioMixerPlatformAndroid.cpp:53
T::FDataType GetValue(const UBlackboardComponent &Blackboard, const FName &Name, FBlackboard::FKey &InOutCachedKey, const typename T::FDataType &DefaultValue)
Definition ValueOrBBKey.h:51
static constexpr UE_FORCEINLINE_HINT T Lerp(const T &A, const T &B, const U &Alpha)
Definition UnrealMathUtility.h:1116