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
 
#pragma once
 
#include <vector>
#include "ModulatedAllpass.h"
#include "RandomBuffer.h"
 
 
#ifndef BUFFER_SIZE
#define BUFFER_SIZE 1024
#endif
 
 
 
namespace ReverbHallRoom
{
    class AllpassDiffuser
    {
    public:
        static const int MaxStageCount = 12;
 
    private:
        int samplerate;
 
        ModulatedAllpass filters[MaxStageCount];
        int delay;
        float modRate;
        std::vector<float> seedValues;
        int seed;
        float crossSeed;
 
    public:
        int Stages;
 
        AllpassDiffuser()
        {
            crossSeed = 0.0;
            seed = 23456;
            UpdateSeeds();
            Stages = 1;
 
            SetSamplerate(48000);
        }
 
        int GetSamplerate()
        {
            return samplerate;
        }
 
        void SetSamplerate(int samplerate)
        {
            this->samplerate = samplerate;
            SetModRate(modRate);
        }
 
        void SetSeed(int seed)
        {
            this->seed = seed;
            UpdateSeeds();
        }
 
        void SetCrossSeed(float crossSeed)
        {
            this->crossSeed = crossSeed;
            UpdateSeeds();
        }
 
 
        bool GetModulationEnabled()
        {
            return filters[0].ModulationEnabled;
        }
 
        void SetModulationEnabled(bool value)
        {
            for (int i = 0; i < MaxStageCount; i++)
                filters[i].ModulationEnabled = value;
 
        }
 
        void SetInterpolationEnabled(bool enabled)
        {
            for (int i = 0; i < MaxStageCount; i++)
                filters[i].InterpolationEnabled = enabled;
        }
 
        void SetDelay(int delaySamples)
        {
            delay = delaySamples;
            Update();
        }
 
        void SetFeedback(float feedback)
        {
            for (int i = 0; i < MaxStageCount; i++)
                filters[i].Feedback = feedback;
        }
 
        void SetModAmount(float amount)
        {
            for (int i = 0; i < MaxStageCount; i++)
                filters[i].ModAmount = amount * (0.85 + 0.3 * seedValues[MaxStageCount + i]);
        }
 
        void SetModRate(float rate)
        {
            modRate = rate;
 
            for (int i = 0; i < MaxStageCount; i++)
                filters[i].ModRate = rate * (0.85 + 0.3 * seedValues[MaxStageCount * 2 + i]) / samplerate;
        }
 
        void Process(float* input, float* output, int bufSize)
        {
            float tempBuffer[BUFFER_SIZE];
 
            filters[0].Process(input, tempBuffer, bufSize);
 
            for (int i = 1; i < Stages; i++)
                filters[i].Process(tempBuffer, tempBuffer, bufSize);
            
            Utils::Copy(output, tempBuffer, bufSize);
        }
 
        void ClearBuffers()
        {
            for (int i = 0; i < MaxStageCount; i++)
                filters[i].ClearBuffers();
        }
 
    private:
        void Update()
        {
            for (int i = 0; i < MaxStageCount; i++)
            {
                auto r = seedValues[i];
                auto d = std::pow(10, r) * 0.1; // 0.1 ... 1.0
                filters[i].SampleDelay = (int)(delay * d);
            }
        }
 
        void UpdateSeeds()
        {
            this->seedValues = RandomBuffer::Generate(seed, MaxStageCount * 3, crossSeed);
            Update();
        }
 
    };
}