chenlh
2026-03-20 f761b6198e2e98f02adb84c83dbf59529e51b2ef
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
#pragma once
 
#include <iostream>
#include <vector>
#include <memory>
#include <cmath>
 
/**
 * Circular buffer that utilizes linear interpolation when reading samples, allowing for fractional delay lines.
 */
template <typename SampleType>
class LinearInterpolationCircularBuffer
{
public:
 
    /**
     * @brief Constructor
     */
    LinearInterpolationCircularBuffer();
 
    /**
     * @brief Destructor
     */
    ~LinearInterpolationCircularBuffer();
 
    /**
     * @brief Prepares buffer for playback
     * 
     * @param sampleRate Current sampling rate
     */
    void prepare(int sampleRate);
 
    void reset();
 
    /**
     * @brief Clears the buffer
     * 
     */
    void clear();
 
    /**
     * @brief Sets buffer size
     * 
     * @param newBufferSize New buffer size in samples
     */
    void setSize(int newBufferSize);
 
    /**
     * @brief Returns buffer size
     */
    int getNumSamples();
 
    /**
     * @brief Sets delay time in samples
     * 
     * @param delayInSamples New delay time in samples
     */
    void setDelayInSamples(SampleType delayInSamples);
 
    /**
     * @brief Sets delay time in milliseconds
     * 
     * @param delayInMs New delay time in milliseconds
     */
    void setDelayInMs(SampleType delayInMs);
 
    /**
     * @brief Processes a single sample
     * 
     * @param input Input sample
     */
    SampleType processSample(SampleType input);
 
    /**
     * @brief Processes a memory block that holds audio samples
     * 
     * @param channelData Memory block start pointer 
     * @param startSample Sample index to start processing from
     * @param endSample Number of samples to process
     */
    void process(SampleType* channelData, int startSample, int endSample);    
 
    /**
     * @brief Returns read pointer index
     */
    int getReadPointerIndex();
 
    /**
     * @brief Returns write pointer index
     */    
    int getWritePointerIndex();
 
    /**
     * @brief Pushes a sample to the buffer and advances the write pointer by an index of 1
     * 
     * @warning Meant to be used in a sample by sample processing context
     * 
     * @param sample input sample
     */
    void pushSample(SampleType sample);
 
    /**
     * @brief Returns a sample from the buffer read position and advances the read pointer by an index of 1
     * 
     * The return value is linearly interpolated
     * 
     * @warning Meant to be used in a sample by sample processing context
     */
    SampleType popSample();
 
private:
    SampleType interpolateSample();
    SampleType limitWithinRange(SampleType lowerLimit, SampleType upperLimit, SampleType value); 
 
private:
 
    SampleType sampleRate;
    std::unique_ptr<std::vector<SampleType>> buffer;
    SampleType delayFrac {0.0}, delay {0.0};
    int readPointer, writePointer, delayInt, bufferSize;    
};