chenlh
2026-03-10 0f65a1a9267b8a7ab4678ef20b07532e4c8377ca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
 
#pragma once
 
#include "ModulatedAllpass.h"
#include "Utils.h"
#include <cmath>
 
namespace ReverbHallRoom
{
    class ModulatedAllpass
    {
    public:
        static const int DelayBufferSize = 19200; // 100ms at 192Khz
        static const int ModulationUpdateRate = 8;
 
    private:
        //float delayBuffer[DelayBufferSize] = { 0 };
        std::vector<float> delayBuffer;
 
        int index;
        uint64_t samplesProcessed;
 
        float modPhase;
        int delayA;
        int delayB;
        float gainA;
        float gainB;
 
    public:
 
        int SampleDelay;
        float Feedback;
        float ModAmount;
        float ModRate;
 
        bool InterpolationEnabled;
        bool ModulationEnabled;
 
        ModulatedAllpass() : delayBuffer(DelayBufferSize, 0)
        {
            index = DelayBufferSize - 1;
            samplesProcessed = 0;
 
            modPhase = 0.01 + 0.98 * std::rand() / (float)RAND_MAX;
            delayA = 0;
            delayB = 0;
            gainA = 0;
            gainB = 0;
 
            SampleDelay = 100;
            Feedback = 0.5;
            ModAmount = 0.0;
            ModRate = 0.0;
 
            InterpolationEnabled = true;
            ModulationEnabled = true;
            Update();
        }
 
        void ClearBuffers()
        {
            Utils::ZeroBuffer(delayBuffer.data(), DelayBufferSize);
        }
 
        void Process(float* input, float* output, int sampleCount)
        {
            if (ModulationEnabled)
                ProcessWithMod(input, output, sampleCount);
            else
                ProcessNoMod(input, output, sampleCount);
        }
 
    private:
        void ProcessNoMod(float* input, float* output, int sampleCount)
        {
            auto delayedIndex = index - SampleDelay;
            if (delayedIndex < 0) delayedIndex += DelayBufferSize;
 
            for (int i = 0; i < sampleCount; i++)
            {
                auto bufOut = delayBuffer[delayedIndex];
                auto inVal = input[i] + bufOut * Feedback;
 
                delayBuffer[index] = inVal;
                output[i] = bufOut - inVal * Feedback;
 
                index++;
                delayedIndex++;
                if (index >= DelayBufferSize) index -= DelayBufferSize;
                if (delayedIndex >= DelayBufferSize) delayedIndex -= DelayBufferSize;
                samplesProcessed++;
            }
        }
 
        void ProcessWithMod(float* input, float* output, int sampleCount)
        {
            for (int i = 0; i < sampleCount; i++)
            {
                if (samplesProcessed >= ModulationUpdateRate)
                {
                    Update();
                    samplesProcessed = 0;
                }
 
                float bufOut;
 
                if (InterpolationEnabled)
                {
                    int idxA = index - delayA;
                    int idxB = index - delayB;
                    idxA += DelayBufferSize * (idxA < 0); // modulo
                    idxB += DelayBufferSize * (idxB < 0); // modulo
 
                    bufOut = delayBuffer[idxA] * gainA + delayBuffer[idxB] * gainB;
                }
                else
                {
                    int idxA = index - delayA;
                    idxA += DelayBufferSize * (idxA < 0); // modulo
                    bufOut = delayBuffer[idxA];
                }
 
                auto inVal = input[i] + bufOut * Feedback;
                delayBuffer[index] = inVal;
                output[i] = bufOut - inVal * Feedback;
 
                index++;
                if (index >= DelayBufferSize) index -= DelayBufferSize;
                samplesProcessed++;
            }
        }
 
        inline float Get(int delay)
        {
            int idx = index - delay;
            if (idx < 0)
                idx += DelayBufferSize;
 
            return delayBuffer[idx];
        }
 
        void Update()
        {
            modPhase += ModRate * ModulationUpdateRate;
            if (modPhase > 1)
                modPhase = std::fmod(modPhase, 1.0);
 
            auto mod = std::sinf(modPhase * 2 * M_PI);
 
            if (ModAmount >= SampleDelay) // don't modulate to negative value
                ModAmount = SampleDelay - 1;
 
 
            auto totalDelay = SampleDelay + ModAmount * mod;
 
            if (totalDelay <= 0) // should no longer be required
                totalDelay = 1;
 
            delayA = (int)totalDelay;
            delayB = (int)totalDelay + 1;
 
            auto partial = totalDelay - delayA;
 
            gainA = 1 - partial;
            gainB = partial;
        }
    };
}