chenlh
2026-03-26 36e42207da4c088b5bfd96f2cfc8944f890440d7
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
 
#pragma once
 
//#include "ModulatedDelay.h"
#include "Utils.h"
#include <stdint.h>
#include <vector>
 
namespace ReverbHallRoom
{
    class ModulatedDelay
    {
    private:
 
        static const int ModulationUpdateRate = 8;
        static const int DelayBufferSize = 192000 * 2;
 
        //float delayBuffer[DelayBufferSize] = { 0 };
        std::vector<float> delayBuffer;
        int writeIndex;
        int readIndexA;
        int readIndexB;
        uint64_t samplesProcessed;
 
        float modPhase;
        float gainA;
        float gainB;
 
    public:
        int SampleDelay;
 
        float ModAmount;
        float ModRate;
 
        ModulatedDelay() : delayBuffer(DelayBufferSize, 0)
        {
            writeIndex = 0;
            readIndexA = 0;
            readIndexB = 0;
            samplesProcessed = 0;
 
            modPhase = 0.01 + 0.98 * (std::rand() / (float)RAND_MAX);
            gainA = 0;
            gainB = 0;
 
            SampleDelay = 100;
            ModAmount = 0.0;
            ModRate = 0.0;
 
            Update();
        }
 
        void Process(float* input, float* output, int bufSize)
        {
            for (int i = 0; i < bufSize; i++)
            {
                if (samplesProcessed >= ModulationUpdateRate)
                {
                    Update();
                    samplesProcessed = 0;
                }
 
                delayBuffer[writeIndex] = input[i];
                output[i] = delayBuffer[readIndexA] * gainA + delayBuffer[readIndexB] * gainB;
 
                writeIndex++;
                readIndexA++;
                readIndexB++;
                if (writeIndex >= DelayBufferSize) writeIndex -= DelayBufferSize;
                if (readIndexA >= DelayBufferSize) readIndexA -= DelayBufferSize;
                if (readIndexB >= DelayBufferSize) readIndexB -= DelayBufferSize;
                samplesProcessed++;
            }
        }
 
        void ClearBuffers()
        {
            //Utils::ZeroBuffer(delayBuffer, DelayBufferSize);
            Utils::ZeroBuffer(delayBuffer.data(), DelayBufferSize);
        }
 
 
    private:
        void Update()
        {
            modPhase += ModRate * ModulationUpdateRate;
            if (modPhase > 1)
                modPhase = fmod(modPhase, 1.0);
 
            auto mod = sinf(modPhase * 2 * M_PI);
            auto totalDelay = SampleDelay + ModAmount * mod;
 
            auto delayA = (int)totalDelay;
            auto delayB = (int)totalDelay + 1;
 
            auto partial = totalDelay - delayA;
 
            gainA = 1 - partial;
            gainB = partial;
 
            readIndexA = writeIndex - delayA;
            readIndexB = writeIndex - delayB;
            if (readIndexA < 0) readIndexA += DelayBufferSize;
            if (readIndexB < 0) readIndexB += DelayBufferSize;
        }
    };
}