summaryrefslogtreecommitdiff
path: root/src/audio
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio')
-rw-r--r--src/audio/audio.cc79
-rw-r--r--src/audio/ring_buffer.cc26
-rw-r--r--src/audio/ring_buffer.h10
3 files changed, 90 insertions, 25 deletions
diff --git a/src/audio/audio.cc b/src/audio/audio.cc
index 2d667bc..2f485a6 100644
--- a/src/audio/audio.cc
+++ b/src/audio/audio.cc
@@ -125,44 +125,73 @@ void audio_render_ahead(float music_time, float dt) {
break;
}
- // Determine how much we can actually render
- // Render the smaller of: desired chunk size OR available space
- const int actual_samples =
- (available_space < chunk_samples) ? available_space : chunk_samples;
- const int actual_frames = actual_samples / RING_BUFFER_CHANNELS;
+ // Get direct write pointer from ring buffer
+ int available_for_write = 0;
+ float* write_ptr = g_ring_buffer.get_write_region(&available_for_write);
- // Allocate temporary buffer (stereo)
- float* temp_buffer = new float[actual_samples];
+ if (available_for_write == 0) {
+ break; // Buffer full, wait for consumption
+ }
- // Render audio from synth (advances synth state incrementally)
- synth_render(temp_buffer, actual_frames);
+ // Clamp to desired chunk size
+ const int actual_samples = (available_for_write < chunk_samples)
+ ? available_for_write
+ : chunk_samples;
+ const int actual_frames = actual_samples / RING_BUFFER_CHANNELS;
- // Write to ring buffer
- const int written = g_ring_buffer.write(temp_buffer, actual_samples);
+ // Render directly to ring buffer (NO COPY, NO ALLOCATION)
+ synth_render(write_ptr, actual_frames);
- // If partial write, save remaining samples to pending buffer
- if (written < actual_samples) {
- const int remaining = actual_samples - written;
- if (remaining <= MAX_PENDING_SAMPLES) {
- for (int i = 0; i < remaining; ++i) {
- g_pending_buffer[i] = temp_buffer[written + i];
- }
- g_pending_samples = remaining;
- }
+ // Apply clipping in-place (Phase 2: ensure samples stay in [-1.0, 1.0])
+ for (int i = 0; i < actual_samples; ++i) {
+ if (write_ptr[i] > 1.0f)
+ write_ptr[i] = 1.0f;
+ if (write_ptr[i] < -1.0f)
+ write_ptr[i] = -1.0f;
}
- // Notify backend of frames rendered (count frames sent to synth)
+ // Commit written data atomically
+ g_ring_buffer.commit_write(actual_samples);
+
+ // Notify backend of frames rendered
#if !defined(STRIP_ALL)
if (g_audio_backend != nullptr) {
g_audio_backend->on_frames_rendered(actual_frames);
}
#endif
- delete[] temp_buffer;
+ // Handle wrap-around: if we wanted more samples but ring wrapped,
+ // get a second region and render remaining chunk
+ if (actual_samples < chunk_samples) {
+ int second_avail = 0;
+ float* second_ptr = g_ring_buffer.get_write_region(&second_avail);
+ if (second_avail > 0) {
+ const int remaining_samples = chunk_samples - actual_samples;
+ const int second_samples = (second_avail < remaining_samples)
+ ? second_avail
+ : remaining_samples;
+ const int second_frames = second_samples / RING_BUFFER_CHANNELS;
- // If we couldn't write everything, stop and retry next frame
- if (written < actual_samples)
- break;
+ synth_render(second_ptr, second_frames);
+
+ // Apply clipping to wrap-around region
+ for (int i = 0; i < second_samples; ++i) {
+ if (second_ptr[i] > 1.0f)
+ second_ptr[i] = 1.0f;
+ if (second_ptr[i] < -1.0f)
+ second_ptr[i] = -1.0f;
+ }
+
+ g_ring_buffer.commit_write(second_samples);
+
+ // Notify backend of additional frames
+#if !defined(STRIP_ALL)
+ if (g_audio_backend != nullptr) {
+ g_audio_backend->on_frames_rendered(second_frames);
+ }
+#endif
+ }
+ }
}
}
diff --git a/src/audio/ring_buffer.cc b/src/audio/ring_buffer.cc
index 7cedb56..30566c9 100644
--- a/src/audio/ring_buffer.cc
+++ b/src/audio/ring_buffer.cc
@@ -152,3 +152,29 @@ void AudioRingBuffer::clear() {
// Note: Don't reset total_read_ - it tracks absolute playback time
memset(buffer_, 0, sizeof(buffer_));
}
+
+float* AudioRingBuffer::get_write_region(int* out_available_samples) {
+ const int write = write_pos_.load(std::memory_order_acquire);
+ const int avail = available_write();
+
+ // Return linear region (less than available if wraps around)
+ const int space_to_end = capacity_ - write;
+ *out_available_samples = std::min(avail, space_to_end);
+
+ return &buffer_[write];
+}
+
+void AudioRingBuffer::commit_write(int num_samples) {
+ const int write = write_pos_.load(std::memory_order_acquire);
+
+ // BOUNDS CHECK
+ FATAL_CHECK(write < 0 || write + num_samples > capacity_,
+ "commit_write out of bounds: write=%d, num_samples=%d, "
+ "capacity=%d\n",
+ write, num_samples, capacity_);
+
+ // Advance write position atomically
+ write_pos_.store((write + num_samples) % capacity_,
+ std::memory_order_release);
+ total_written_.fetch_add(num_samples, std::memory_order_release);
+}
diff --git a/src/audio/ring_buffer.h b/src/audio/ring_buffer.h
index 80b375f..524cb29 100644
--- a/src/audio/ring_buffer.h
+++ b/src/audio/ring_buffer.h
@@ -50,6 +50,16 @@ class AudioRingBuffer {
// Clear buffer (for seeking)
void clear();
+ // Two-phase write API (for zero-copy direct writes)
+ // Get direct pointer to writable region in ring buffer
+ // Returns pointer to linear region and sets out_available_samples
+ // NOTE: May return less than total available space if wrap-around occurs
+ float* get_write_region(int* out_available_samples);
+
+ // Commit written samples (advances write_pos atomically)
+ // FATAL ERROR if num_samples exceeds region from get_write_region()
+ void commit_write(int num_samples);
+
private:
float buffer_[RING_BUFFER_CAPACITY_SAMPLES];
int capacity_; // Total capacity in samples