|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the YUP library. |
| 5 | + Copyright (c) 2026 - kunitoki@gmail.com |
| 6 | +
|
| 7 | + YUP is an open source library subject to open-source licensing. |
| 8 | +
|
| 9 | + The code included in this file is provided under the terms of the ISC license |
| 10 | + http://www.isc.org/downloads/software-support-policy/isc-license. Permission |
| 11 | + to use, copy, modify, and/or distribute this software for any purpose with or |
| 12 | + without fee is hereby granted provided that the above copyright notice and |
| 13 | + this permission notice appear in all copies. |
| 14 | +
|
| 15 | + YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER |
| 16 | + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE |
| 17 | + DISCLAIMED. |
| 18 | +
|
| 19 | + ============================================================================== |
| 20 | +*/ |
| 21 | + |
| 22 | +#pragma once |
| 23 | + |
| 24 | +namespace yup |
| 25 | +{ |
| 26 | + |
| 27 | +//============================================================================== |
| 28 | +/** |
| 29 | + Fixed-size circular (ring) buffer for efficient sample history access. |
| 30 | +
|
| 31 | + Provides O(1) push and random-read operations. Logically, index 0 refers to |
| 32 | + the oldest element and index BufferSize - 1 to the most recently pushed one. |
| 33 | +
|
| 34 | + Intended as a building block for block-based DSP algorithms that require a |
| 35 | + sliding window of sample history, such as polyphase resampling and FIR |
| 36 | + convolution. |
| 37 | +
|
| 38 | + @tparam SampleType Element type stored in the buffer. |
| 39 | + @tparam BufferSize Compile-time capacity (must be > 0). |
| 40 | +*/ |
| 41 | +template <typename SampleType, int BufferSize> |
| 42 | +class CircularBuffer |
| 43 | +{ |
| 44 | +public: |
| 45 | + static_assert (BufferSize > 0, "CircularBuffer BufferSize must be greater than zero"); |
| 46 | + |
| 47 | + //============================================================================== |
| 48 | + /** Default constructor. */ |
| 49 | + CircularBuffer() noexcept |
| 50 | + { |
| 51 | + buffer.fill (SampleType {}); |
| 52 | + } |
| 53 | + |
| 54 | + /** Constructs a buffer with all entries initialised to initValue. */ |
| 55 | + explicit CircularBuffer (SampleType initValue) noexcept |
| 56 | + { |
| 57 | + buffer.fill (initValue); |
| 58 | + } |
| 59 | + |
| 60 | + //============================================================================== |
| 61 | + /** Inserts value, overwriting the oldest entry and advancing the write pointer. */ |
| 62 | + forcedinline void push (SampleType value) noexcept |
| 63 | + { |
| 64 | + buffer[static_cast<std::size_t> (oldestIndex)] = value; |
| 65 | + |
| 66 | + if (++oldestIndex == BufferSize) |
| 67 | + oldestIndex = 0; |
| 68 | + } |
| 69 | + |
| 70 | + /** Returns the element at logical index (0 = oldest, BufferSize-1 = newest). */ |
| 71 | + forcedinline SampleType& operator[] (int index) noexcept |
| 72 | + { |
| 73 | + return buffer[static_cast<std::size_t> ((oldestIndex + index) % BufferSize)]; |
| 74 | + } |
| 75 | + |
| 76 | + /** Returns the element at logical index (0 = oldest, BufferSize-1 = newest). */ |
| 77 | + const forcedinline SampleType& operator[] (int index) const noexcept |
| 78 | + { |
| 79 | + return buffer[static_cast<std::size_t> ((oldestIndex + index) % BufferSize)]; |
| 80 | + } |
| 81 | + |
| 82 | + //============================================================================== |
| 83 | + /** Resets all entries to zero and rewinds the write pointer. */ |
| 84 | + void clear() noexcept |
| 85 | + { |
| 86 | + buffer.fill (SampleType {}); |
| 87 | + oldestIndex = 0; |
| 88 | + } |
| 89 | + |
| 90 | +private: |
| 91 | + std::array<SampleType, static_cast<std::size_t> (BufferSize)> buffer {}; |
| 92 | + int oldestIndex = 0; |
| 93 | +}; |
| 94 | + |
| 95 | +} // namespace yup |
0 commit comments