diff options
Diffstat (limited to 'src/audio/ring_buffer.cc')
| -rw-r--r-- | src/audio/ring_buffer.cc | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/src/audio/ring_buffer.cc b/src/audio/ring_buffer.cc index ab51d6b..b30ebbb 100644 --- a/src/audio/ring_buffer.cc +++ b/src/audio/ring_buffer.cc @@ -4,14 +4,12 @@ #include "ring_buffer.h" #include "util/debug.h" #include <algorithm> +#include <cstdio> // for fprintf() +#include <cstdlib> // for abort() #include <cstring> -#include <cstdlib> // for abort() -#include <cstdio> // for fprintf() AudioRingBuffer::AudioRingBuffer() - : capacity_(RING_BUFFER_CAPACITY_SAMPLES), - write_pos_(0), - read_pos_(0), + : capacity_(RING_BUFFER_CAPACITY_SAMPLES), write_pos_(0), read_pos_(0), total_read_(0) { memset(buffer_, 0, sizeof(buffer_)); } @@ -25,7 +23,7 @@ int AudioRingBuffer::available_write() const { const int read = read_pos_.load(std::memory_order_acquire); if (write >= read) { - return capacity_ - (write - read) - 1; // -1 to avoid full/empty ambiguity + return capacity_ - (write - read) - 1; // -1 to avoid full/empty ambiguity } else { return read - write - 1; } @@ -65,7 +63,8 @@ int AudioRingBuffer::write(const float* samples, int count) { // Write in one chunk // BOUNDS CHECK if (write < 0 || write + to_write > capacity_) { - fprintf(stderr, "BOUNDS ERROR in write(): write=%d, to_write=%d, capacity=%d\n", + fprintf(stderr, + "BOUNDS ERROR in write(): write=%d, to_write=%d, capacity=%d\n", write, to_write, capacity_); abort(); } @@ -75,7 +74,9 @@ int AudioRingBuffer::write(const float* samples, int count) { // Write in two chunks (wrap around) // BOUNDS CHECK - first chunk if (write < 0 || write + space_to_end > capacity_) { - fprintf(stderr, "BOUNDS ERROR in write() chunk1: write=%d, space_to_end=%d, capacity=%d\n", + fprintf(stderr, + "BOUNDS ERROR in write() chunk1: write=%d, space_to_end=%d, " + "capacity=%d\n", write, space_to_end, capacity_); abort(); } @@ -83,7 +84,8 @@ int AudioRingBuffer::write(const float* samples, int count) { const int remainder = to_write - space_to_end; // BOUNDS CHECK - second chunk if (remainder < 0 || remainder > capacity_) { - fprintf(stderr, "BOUNDS ERROR in write() chunk2: remainder=%d, capacity=%d\n", + fprintf(stderr, + "BOUNDS ERROR in write() chunk2: remainder=%d, capacity=%d\n", remainder, capacity_); abort(); } @@ -114,7 +116,8 @@ int AudioRingBuffer::read(float* samples, int count) { // Read in one chunk // BOUNDS CHECK if (read < 0 || read + to_read > capacity_) { - fprintf(stderr, "BOUNDS ERROR in read(): read=%d, to_read=%d, capacity=%d\n", + fprintf(stderr, + "BOUNDS ERROR in read(): read=%d, to_read=%d, capacity=%d\n", read, to_read, capacity_); abort(); } @@ -124,7 +127,9 @@ int AudioRingBuffer::read(float* samples, int count) { // Read in two chunks (wrap around) // BOUNDS CHECK - first chunk if (read < 0 || read + space_to_end > capacity_) { - fprintf(stderr, "BOUNDS ERROR in read() chunk1: read=%d, space_to_end=%d, capacity=%d\n", + fprintf(stderr, + "BOUNDS ERROR in read() chunk1: read=%d, space_to_end=%d, " + "capacity=%d\n", read, space_to_end, capacity_); abort(); } @@ -132,7 +137,8 @@ int AudioRingBuffer::read(float* samples, int count) { const int remainder = to_read - space_to_end; // BOUNDS CHECK - second chunk if (remainder < 0 || remainder > capacity_) { - fprintf(stderr, "BOUNDS ERROR in read() chunk2: remainder=%d, capacity=%d\n", + fprintf(stderr, + "BOUNDS ERROR in read() chunk2: remainder=%d, capacity=%d\n", remainder, capacity_); abort(); } @@ -148,9 +154,10 @@ int AudioRingBuffer::read(float* samples, int count) { #if defined(DEBUG_LOG_RING_BUFFER) // UNDERRUN DETECTED static int underrun_count = 0; - if (++underrun_count % 10 == 1) { // Log every 10th underrun - DEBUG_RING_BUFFER("UNDERRUN #%d: requested=%d, available=%d, filling %d with silence\n", - underrun_count, count, to_read, count - to_read); + if (++underrun_count % 10 == 1) { // Log every 10th underrun + DEBUG_RING_BUFFER( + "UNDERRUN #%d: requested=%d, available=%d, filling %d with silence\n", + underrun_count, count, to_read, count - to_read); } #endif /* defined(DEBUG_LOG_RING_BUFFER) */ memset(samples + to_read, 0, (count - to_read) * sizeof(float)); |
