summaryrefslogtreecommitdiff
path: root/src/audio/ring_buffer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio/ring_buffer.cc')
-rw-r--r--src/audio/ring_buffer.cc63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/audio/ring_buffer.cc b/src/audio/ring_buffer.cc
index 25cf853..ab51d6b 100644
--- a/src/audio/ring_buffer.cc
+++ b/src/audio/ring_buffer.cc
@@ -2,8 +2,11 @@
// It implements a lock-free ring buffer for audio streaming.
#include "ring_buffer.h"
+#include "util/debug.h"
#include <algorithm>
#include <cstring>
+#include <cstdlib> // for abort()
+#include <cstdio> // for fprintf()
AudioRingBuffer::AudioRingBuffer()
: capacity_(RING_BUFFER_CAPACITY_SAMPLES),
@@ -48,16 +51,42 @@ int AudioRingBuffer::write(const float* samples, int count) {
}
const int write = write_pos_.load(std::memory_order_acquire);
+
+ // BOUNDS CHECK: Validate write position
+ if (write < 0 || write >= capacity_) {
+ fprintf(stderr, "FATAL: write_pos out of bounds! write=%d, capacity=%d\n",
+ write, capacity_);
+ abort();
+ }
+
const int space_to_end = capacity_ - write;
if (to_write <= space_to_end) {
// 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",
+ write, to_write, capacity_);
+ abort();
+ }
memcpy(&buffer_[write], samples, to_write * sizeof(float));
write_pos_.store((write + to_write) % capacity_, std::memory_order_release);
} else {
// 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",
+ write, space_to_end, capacity_);
+ abort();
+ }
memcpy(&buffer_[write], samples, space_to_end * sizeof(float));
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",
+ remainder, capacity_);
+ abort();
+ }
memcpy(&buffer_[0], samples + space_to_end, remainder * sizeof(float));
write_pos_.store(remainder, std::memory_order_release);
}
@@ -71,16 +100,42 @@ int AudioRingBuffer::read(float* samples, int count) {
if (to_read > 0) {
const int read = read_pos_.load(std::memory_order_acquire);
+
+ // BOUNDS CHECK: Validate read position
+ if (read < 0 || read >= capacity_) {
+ fprintf(stderr, "FATAL: read_pos out of bounds! read=%d, capacity=%d\n",
+ read, capacity_);
+ abort();
+ }
+
const int space_to_end = capacity_ - read;
if (to_read <= space_to_end) {
// 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",
+ read, to_read, capacity_);
+ abort();
+ }
memcpy(samples, &buffer_[read], to_read * sizeof(float));
read_pos_.store((read + to_read) % capacity_, std::memory_order_release);
} else {
// 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",
+ read, space_to_end, capacity_);
+ abort();
+ }
memcpy(samples, &buffer_[read], space_to_end * sizeof(float));
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",
+ remainder, capacity_);
+ abort();
+ }
memcpy(samples + space_to_end, &buffer_[0], remainder * sizeof(float));
read_pos_.store(remainder, std::memory_order_release);
}
@@ -90,6 +145,14 @@ int AudioRingBuffer::read(float* samples, int count) {
// Fill remainder with silence if not enough samples available
if (to_read < 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);
+ }
+#endif /* defined(DEBUG_LOG_RING_BUFFER) */
memset(samples + to_read, 0, (count - to_read) * sizeof(float));
}