UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
GrainDelay.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "DSP/GrainEnvelope.h"
8
9namespace Audio
10{
11 namespace GrainDelay
12 {
14 {
15 public:
16 SIGNALPROCESSING_API FGrainDelay(const float InSampleRate, const float InMaxDelaySeconds = 2.0f);
18
20
21 // Helpers to clamp audio
22 SIGNALPROCESSING_API float GetGrainDelayClamped(const float InDelay) const;
27
28 // Dynamically sets the max grains
30
31 // Sets the grain envelope
33
34 // Sets the feedback amount (how much output grain audio feeds into the delay line)
36
37 // Sets the base pitch shift ratio of all grains
39
40 // Spawns a new grain with the given parameters
41 SIGNALPROCESSING_API void SpawnGrain(const float InDelay, const float InDuration, const float InPitchShiftRatioOffset);
42
43 // Synthesize audio in the given frame range from input audio. Writes to OutAudioBuffer
44 SIGNALPROCESSING_API void SynthesizeAudio(const int32 StartFrame, const int32 EndFrame, const float* InAudioBuffer, float* OutAudioBuffer);
45
46 private:
47 // Synthesize a single frame of audio
48 float SynthesizeFrame(const Audio::FDelay& InDelayLine);
49
50 void InitDynamicsProcessor();
51
52 // Simple grain data structure for tracking grain state
53 struct FGrain
54 {
55 // Where the grain is currently tapped from the delay line
56 float DelayTapPositionMilliseconds = 0.0f;
57
58 // The current frame the grain is in it's duration
59 float NumFramesRendered = 0.0f;
60
61 // The total number of frames this grain will render for
62 float DurationFrames = 0.0f;
63
64 // The unique randomized pitch scale, so we can modulate the base pitch scale
65 float PitchShiftRatioOffset = 1.0f;
66
67 // The tap delay pitch shifter which manages taps from the shared delay line for each grain
68 FTapDelayPitchShifter PitchShifter;
69
70 // Helper function for computing the phaser frequency and phaser increment
71 void SetGrainPitchShiftRatio(const float InPitchShiftRatioBase, const float InSampleRate)
72 {
73 const float DurationMilliseconds = 1000.0f * DurationFrames / InSampleRate;
74 PitchShifter.SetDelayLength(DurationMilliseconds);
75 PitchShifter.SetPitchShiftRatio(InPitchShiftRatioBase * PitchShiftRatioOffset);
76 }
77 };
78
79 // Free grain indices in the grain pool
80 TArray<int32> FreeGrains;
81
82 // Active grain indices in the grain pool
83 TArray<int32> ActiveGrains;
84
85 // The actual grain pool. Reuses tracking memory for grain lifetimes.
86 TArray<FGrain> GrainPool;
87
88 // The shared envelope value. Recomputed when a new envelope is set.
89 Grain::FEnvelope GrainEnvelope;
91
92 // A pitch shifting delay line
93 TArray<FTapDelayPitchShifter> DelayPitchShifters;
94
95 // The shared delay lines per channel
96 FDelay GrainDelayLine;
97
98 // The current feedback amount
99 float FeedbackAmount = 0.0f;
100
101 // Cached value of last rendered frame. Used for feedback
102 float LastFrame = 0.0f;
103
104 // Dynamics processor for the grain synthesis
105 FDynamicsProcessor DynamicsProcessor;
106
107 // The maximum grains to have rendering
108 int32 MaxGrains = 16;
109
110 // Sample rate track to easily deal with pitch scale calculations
111 float SampleRate = 0.0f;
112
113 // The base pitch shift ratio (pitch scale) of all grains in the grain delay
114 float PitchShiftRatioBase = 1.0f;
115
116 float MaxDelaySeconds = 2.0f;
117 };
118 }
119}
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 Delay.h:13
Definition DynamicsProcessor.h:37
Definition TapDelayPitchShifter.h:13
SIGNALPROCESSING_API void SetPitchShiftRatio(const float InPitchShiftRatio)
Definition TapDelayPitchShifter.cpp:69
SIGNALPROCESSING_API void SetDelayLength(const float InDelayLength)
Definition TapDelayPitchShifter.cpp:47
Definition GrainDelay.h:14
SIGNALPROCESSING_API float GetGrainDurationClamped(const float InDuration) const
Definition GrainDelay.cpp:53
SIGNALPROCESSING_API float GetGrainDelayRatioClamped(const float InGrainDelayRatio) const
Definition GrainDelay.cpp:58
SIGNALPROCESSING_API void SetMaxGrains(const int32 InMaxGrains)
Definition GrainDelay.cpp:73
SIGNALPROCESSING_API float GetGrainDelayClamped(const float InDelay) const
Definition GrainDelay.cpp:48
SIGNALPROCESSING_API ~FGrainDelay()
Definition GrainDelay.cpp:33
SIGNALPROCESSING_API float GetGrainPitchShiftFrameRatio(const float InPitchShift) const
Definition GrainDelay.cpp:68
SIGNALPROCESSING_API void SetGrainBasePitchShiftRatio(const float InPitchRatioBase)
Definition GrainDelay.cpp:92
SIGNALPROCESSING_API void SetGrainEnvelope(const Audio::Grain::EEnvelope InGrainEnvelope)
Definition GrainDelay.cpp:78
SIGNALPROCESSING_API void SetFeedbackAmount(float InFeedbackAmount)
Definition GrainDelay.cpp:87
SIGNALPROCESSING_API float GetGrainPitchShiftClamped(const float InPitchShift) const
Definition GrainDelay.cpp:63
SIGNALPROCESSING_API void SynthesizeAudio(const int32 StartFrame, const int32 EndFrame, const float *InAudioBuffer, float *OutAudioBuffer)
Definition GrainDelay.cpp:183
SIGNALPROCESSING_API void SpawnGrain(const float InDelay, const float InDuration, const float InPitchShiftRatioOffset)
Definition GrainDelay.cpp:108
SIGNALPROCESSING_API void Reset()
Definition GrainDelay.cpp:37
Definition Array.h:670
EEnvelope
Definition GrainEnvelope.h:16
NO_LOGGING.
Definition AudioMixerPlatformAndroid.cpp:53