Skip to content

Conversation

roderickvd
Copy link
Member

Configure CoreAudio device buffer to ensure applications receive callbacks with exactly the buffer size they requested. This provides the same predictable buffer behavior that PR #1013 proposes for ALSA.

Behavior

  • Before: Requesting 256 frames → receiving 512 frame callbacks, ~512 frame latency
  • After: Requesting 256 frames → receiving 256 frame callbacks, ~256 frame latency

Latency is determined by the callback buffer size before transfer to the device hardware buffer. CoreAudio's buffer frame size corresponds to ALSA's "period" concept, with CoreAudio automatically implementing double buffering.

CoreAudio automatically delivers callbacks with buffer_size = 2 * device_buffer_size.
To ensure applications receive callbacks of the size they requested, configure
device_buffer_size = requested_buffer_size / 2.

This provides:
- Predictable callback buffer sizes matching application requests
- Efficient double-buffering (device buffer + callback buffer)
- Low latency determined by the device buffer size
@roderickvd roderickvd changed the title fix(coreaudio): implement predictable buffer sizes refactor(coreaudio): implement predictable buffer sizes Sep 27, 2025
@roderickvd roderickvd removed the bug label Sep 27, 2025
@roderickvd roderickvd changed the title refactor(coreaudio): implement predictable buffer sizes refactor(coreaudio): optimize buffer configuration Sep 27, 2025
- Query actual device buffer size for accurate latency on macOS and iOS
- Use device buffer size instead of callback buffer size when available
- Add documentation and tests clarifying CoreAudio buffer model and behavior
Extract common stream format and buffer size configuration into
configure_stream_format_and_buffer. Move callback variable setup to
setup_callback_vars for code reuse and clarity.
@roderickvd roderickvd requested a review from Copilot September 28, 2025 20:45
Copilot

This comment was marked as resolved.

@roderickvd
Copy link
Member Author

Another part of the campaign on consistent buffer sizes. When done, I'm thinking of doing an alpha release and reaching out on Discord.

@roderickvd roderickvd merged commit 06e2f9d into master Sep 28, 2025
27 of 28 checks passed
@roderickvd roderickvd deleted the feat/coreaudio-predictable-buffers branch September 28, 2025 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant