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
#pragma once
 
#include <InterpolatedAllPassFilter.h>
#include <LFO.h>
 
/**
 * First Order All-Pass Filter with modulating delay time
 * 
 */
template <typename SampleType>
class ModulatedAllPassFilter
{
public:
 
    /**
     * @brief Constructor
     */
    ModulatedAllPassFilter();
 
    /**
     * @brief Destructor
     */
    ~ModulatedAllPassFilter();
 
    /**
     * @brief Prepares object for playback
     * 
     * @param sampleRate Current sampling rate
     */
    void prepare(SampleType sampleRate);
 
    /**
     * @brief Sets the delay time in milliseconds 
     * 
     * @param delayInMs Delay time in milliseconds
     */
    void setDelayMs(SampleType delayInMs);
 
    /**
     * @brief Sets the delay time in samples 
     * 
     * @param delayInSamples Delay time in samples
     */
    void setDelayInSamples(SampleType delayInSamples);
 
    /**
     * @brief Sets the amount of feedback.
     * 
     * Values must range between 0 and 1
     * 
     * @param newFeedback Feedback amount
     */
    void setFeedback(SampleType newFeedback);
 
    /**
     * @brief Sets the delay time modulation rate
     * 
     * @param newRate New modulation rate value in Hz
     */
    void setRate(SampleType newRate);
 
    /**
     * @brief Sets the delay time modulation depth
     * 
     * @param newDepth New modulation depth value
     * 
     * Values must range between 0.0 to 1.0
     */    
    void setDepth(SampleType newDepth);
 
    /**
     * @brief Sets the bounds the delay time values can modulate between
     * 
     * @param newWidth New modulation width in samples
     */
    void setWidth(SampleType newWidth);
 
    /**
     * @brief Processes a single sample
     * 
     * @param input Input sample
     */
    SampleType processSample(SampleType input);
 
    /**
     * @brief Processes a memory block that holds audio samples
     * 
     * @param data Memory block start pointer 
     * @param startSample Sample index to start processing from
     * @param endSample Number of samples to process
     */
    void process(SampleType* data, int startSample, int endSample);
 
private:
    SampleType doUnipolarModulationFromMin(SampleType unipolarModulatorValue, SampleType minValue, SampleType maxValue);
    SampleType bipolarToUnipolar(SampleType value);
 
private:
    SampleType sampleRate;
    InterpolatedAllPassFilter<SampleType> allPassFilter;
    LFO lfo;
    SampleType delayTime {0.0}, feedback {0.0}, rate {0.0}, depth {0.0}, width {0.0};
};