﻿# PortAudio final callbacks: completion does not remove the output-buffer contract

PortAudio final callbacks: completion does not remove the output-buffer contract. A focused voice-infrastructure guide with the documented mechanism, a reproducible validation scenario, evidence and operational limits.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/voice-portaudio-final-callback-output-fill/
Language: en

A PortAudio callback that returns a completion status still has responsibility for its current output buffer. A text-to-speech player often reaches the end of generated audio partway through that buffer. Leaving the rest unwritten can expose stale samples or noise at the exact point where the application believes playback has finished cleanly.

## The audio contract

The callback contract requires the complete output buffer to be filled regardless of the return value. paComplete stops further callback invocation and lets queued output finish; paAbort requests completion as soon as possible. These statuses describe stream control rather than a shorter valid-output length. Their playback consequences differ, but neither grants permission to leave the current output memory uninitialized. The application must separately describe which part contains intended speech and which part is explicit tail padding.

## Implementation details

Copy the remaining valid speech frames and explicitly fill the rest of the requested output region using the declared silence representation. Then return the selected control status. Keep model generation completion, waveform-queue exhaustion and audible playback completion as separate states. A normal graceful finish and a user cancellation can choose different policies, but both must obey buffer initialization. Do not call unsupported stream-control operations from inside the callback; arrange external lifecycle handling through the documented interface and a bounded communication mechanism.

## A constructed validation case

Create a generated waveform ending before the next callback buffer is full. Prefill the test output memory with an identifiable non-silent pattern, then run the final callback. Verify that every requested output sample is written and that only the intended prefix contains speech. Exercise graceful completion and abort separately, recording their chosen semantics. Add an exact-buffer-boundary ending to catch off-by-one queue handling. Acceptance requires deterministic initialized output and distinct completion states, not merely a final boolean saying that generation is done.

## Operational limits

This check does not prove that a device has already played every queued sample. Host buffering and cancellation behavior still need observation under the selected implementation. Keep the buffer contract explicit before measuring final-word truncation or the time between stopping generation and stopping audible speech.

## Primary references

[Portaudio Api](https://portaudio.com/docs/v19-doxydocs/portaudio_8h.html); [Portaudio Callback](https://portaudio.com/docs/v19-doxydocs/writing_a_callback.html).

This is a proposed voice-system validation, not an executed Retrivium benchmark. Preserve the implementation version and unresolved outcomes; interface documentation does not establish the quality or timing of a deployed voice service.

## Related reading

[CosyVoice2 streaming assembly: token offsets and vocoder tails prevent duplicate audio](/journal/fg2-cosyvoice2-streaming-token-offset-vocoder-tail/); [Mimi streaming frames: network chunks must be buffered before codec input](/journal/fg2-mimi-streaming-frame-buffer-contract/).
