Skip to content

Commit 7918bca

Browse files
committed
improved circular buffer implementation
1 parent 5d81c95 commit 7918bca

File tree

2 files changed

+141
-182
lines changed

2 files changed

+141
-182
lines changed
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
11
#pragma once
22
#include <vector>
33

4-
// forward declarations
54
class Filter;
65

7-
// if any function calls are given length or delay arguments that are larger
8-
// than the capacity of the buffer, they may result in undefined behavior
96
class CircularBuffer
107
{
118
public:
12-
// === Lifecycle ==========================================================
9+
// Lifecycle
1310
CircularBuffer();
1411
CircularBuffer(size_t capacity);
1512

16-
// === Add Samples ========================================================
13+
// Add Samples
1714
void addSample(const float sample);
18-
void addSamples(const float* samples, size_t length);
19-
void addSamplesRamped(const float* samples, size_t length);
15+
void addSamples(const float* samples, size_t numToAdd);
16+
void addSamplesRamped(const float* samples, size_t numToAdd);
2017

21-
// === Get Samples ========================================================
18+
// Get Samples
2219
float getSample(size_t delay);
2320
void getSamples(size_t delay, float* output, size_t length);
24-
void sumWithSamples
25-
(size_t delay, float* output, size_t len, float gain = 1.0f);
26-
void sumWithSamplesRamped
27-
(size_t delay, float* output, size_t len, float start = 0, float end = 1);
21+
void sumWithSamples(size_t delay, float* output, size_t length,
22+
float gain = 1.0f);
23+
void sumWithSamplesRamped(size_t delay, float* output, size_t length,
24+
float startGain = 0, float endGain = 1);
2825

29-
// === Manipulate Samples =================================================
30-
void applyGainToSamples(size_t delay, size_t len, float gain);
31-
void applyGainToSamples(size_t delay, size_t len, float start, float end);
32-
void applyFilterToSamples(size_t delay, size_t len, Filter* filter);
26+
// Manipulate Samples
27+
void applyGainToSamples(size_t delay, size_t length, float gain);
28+
void applyGainToSamples(size_t delay, size_t length, float startGain,
29+
float endGain);
30+
void applyFilterToSamples(size_t delay, size_t length, Filter* filter);
3331

34-
// === Other Operations ===================================================
32+
// Other Operations
3533
void clear();
3634
void resize(size_t newLength);
3735
void resize(double sampleRate, float maxDelaySeconds);
3836

3937
private:
4038
std::vector<float> buffer;
41-
size_t leastRecentSample;
42-
size_t numSamples;
39+
size_t sampleCount;
40+
41+
size_t capacityMask(size_t sample);
4342
};

0 commit comments

Comments
 (0)