Skip to content

RSDK-13297 Update latency param for audio playback#29

Open
oliviamiller wants to merge 3 commits intoviam-modules:mainfrom
oliviamiller:fix-latency
Open

RSDK-13297 Update latency param for audio playback#29
oliviamiller wants to merge 3 commits intoviam-modules:mainfrom
oliviamiller:fix-latency

Conversation

@oliviamiller
Copy link
Collaborator

@oliviamiller oliviamiller commented Mar 9, 2026

Summary

  • Investigated alternatives to sleep_for(latency_) at the end of play() for blocking until audio playback is complete. PortAudio's callback API does not expose a "finished playing" signal. Since we keep the stream running at all times, theres no better way to determine when audio is playing vs not.
  • When investigating I realized that the latency param had a slight issue, we should use the actual output latency of the stream rather than using the suggested latency passed to Pa_OpenStream. PortAudio may choose a different latency than the one requested.
  • Renamed latency_seconds to suggested_latency_seconds in StreamParams to make clear this is a request to PortAudio, not the actual latency selected.
  • Extracted a get_stream_latency helper in audio_utils.hpp used by both speaker and microphone to read actual latency (output or input) after stream open.
  • Added unit tests for get_stream_latency.

Approvals
1 approval needed.

@oliviamiller oliviamiller requested review from SebastianMunozP, hexbabe and seanavery and removed request for hexbabe March 9, 2026 20:17
inline double get_stream_latency(PaStream* stream, const StreamParams& params, const audio::portaudio::PortAudioInterface* pa = nullptr) {
audio::portaudio::RealPortAudio real_pa;
const audio::portaudio::PortAudioInterface& audio_interface = pa ? *pa : real_pa;
const PaStreamInfo* stream_info = audio_interface.getStreamInfo(stream);
Copy link

@seanavery seanavery Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Do we need null check on the stream arg?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not neccessary since in in pa_getstreaminfo docs:
If the stream parameter is invalid, or an error is encountered, the function returns NULL.
so its covered by check below.

// Returns the actual latency reported by PortAudio after stream open (inputLatency for
// input streams, outputLatency for output streams). Falls back to suggested_latency_seconds
// if stream info is unavailable.
inline double get_stream_latency(PaStream* stream, const StreamParams& params, const audio::portaudio::PortAudioInterface* pa = nullptr) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] why are audio_utils inline fns in a header file instead of breaking out into a cpp file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the other functions in this file are templates which must be defined in a header file. This function could be moved to a .cpp file but I dont think its necessary rn.

@oliviamiller oliviamiller requested a review from seanavery March 10, 2026 18:50
Copy link

@seanavery seanavery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants