summaryrefslogtreecommitdiff
path: root/src/audio/audio.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio/audio.cc')
-rw-r--r--src/audio/audio.cc31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/audio/audio.cc b/src/audio/audio.cc
index 3b7b7fd..6e1e9b7 100644
--- a/src/audio/audio.cc
+++ b/src/audio/audio.cc
@@ -109,10 +109,12 @@ void audio_render_ahead(float music_time, float dt) {
}
g_pending_samples = remaining;
- // Notify backend
+ // Notify backend (for testing/tracking)
+#if !defined(STRIP_ALL)
if (g_audio_backend != nullptr) {
g_audio_backend->on_frames_rendered(written / RING_BUFFER_CHANNELS);
}
+#endif
}
// If still have pending samples, buffer is full - wait for consumption
@@ -127,25 +129,30 @@ void audio_render_ahead(float music_time, float dt) {
// Stop if buffer is full enough
if (buffered_time >= target_lookahead) break;
- // Check if buffer has space for this chunk
+ // Check available space and render chunk that fits
const int available_space = g_ring_buffer.available_write();
- if (available_space < chunk_samples) {
- // Buffer is too full, wait for audio callback to consume more
+ if (available_space == 0) {
+ // Buffer is completely full, wait for audio callback to consume
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;
+
// Allocate temporary buffer (stereo)
- float* temp_buffer = new float[chunk_samples];
+ float* temp_buffer = new float[actual_samples];
// Render audio from synth (advances synth state incrementally)
- synth_render(temp_buffer, chunk_frames);
+ synth_render(temp_buffer, actual_frames);
// Write to ring buffer
- const int written = g_ring_buffer.write(temp_buffer, chunk_samples);
+ const int written = g_ring_buffer.write(temp_buffer, actual_samples);
// If partial write, save remaining samples to pending buffer
- if (written < chunk_samples) {
- const int remaining = chunk_samples - written;
+ 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];
@@ -155,14 +162,16 @@ void audio_render_ahead(float music_time, float dt) {
}
// Notify backend of frames rendered (count frames sent to synth)
+#if !defined(STRIP_ALL)
if (g_audio_backend != nullptr) {
- g_audio_backend->on_frames_rendered(chunk_frames);
+ g_audio_backend->on_frames_rendered(actual_frames);
}
+#endif
delete[] temp_buffer;
// If we couldn't write everything, stop and retry next frame
- if (written < chunk_samples) break;
+ if (written < actual_samples) break;
}
}