﻿# Opus packet duration: count decoded samples rather than compressed bytes

Opus packet duration: count decoded samples rather than compressed bytes. 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-opus-packet-samples-not-compressed-bytes/
Language: en

An Opus payload does not have a fixed relationship between byte length and speech duration. A variable bitrate encoder may spend different numbers of bytes on intervals of equal length. A voice inference adapter therefore needs a sample-count contract before it can assemble waveform batches, enforce duration limits, or associate transcription output with the original conversation. Packet size is useful for transport accounting, but it is a different measurement.

## The audio contract

The decoder API exposes opus_packet_get_nb_samples with an explicit sampling frequency, and opus_decoder_get_nb_samples using an existing decoder. These functions inspect packet framing; they do not calculate speech duration from a chosen bitrate. Decoding returns the number of samples per channel actually produced. For interleaved output, the number of scalar values also includes the channel count. Confusing those quantities makes stereo appear longer than mono even when both represent the same time interval.

## Implementation details

Keep compressed length, declared decoder rate, channel layout, predicted samples and returned samples as separate fields. Validate the parser result before allocating or advancing the timeline. Allocate output capacity according to the API's per-channel convention, then use the successful decode result to bound the valid output region. Do not append the unused capacity of a reusable waveform buffer. Duration is returned samples divided by the selected decoder rate, while bytes remain a transport statistic. A failed packet needs an explicit loss policy rather than a fabricated duration inferred from its length.

## A constructed validation case

Construct several valid packets that represent equal time intervals but have different compressed lengths. Feed them through the same decoder configuration and compare parsed sample counts with successful decode returns. Include mono and stereo configurations separately, plus a truncated payload that must follow the error path. The acceptance condition is stable represented duration despite different payload sizes, with the correct number of interleaved scalar values. Preserve the exact packets and decoder revision so a later wrapper change can be checked against the same evidence rather than an estimated average bitrate.

## Operational limits

A structurally readable packet is not a transcript-quality certificate. Container trimming, deliberate discontinuities and transport loss still need their own policies. In a neural voice pipeline, batch size should be based on valid waveform samples after decoding and preparation. Keep network throughput and acoustic duration visible separately when diagnosing why a queue grows.

## Primary references

[Opus Decoder](https://opus-codec.org/docs/opus_api-1.5/group__opus__decoder.html); [Rfc6716](https://www.rfc-editor.org/rfc/rfc6716.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

[Mimi streaming frames: network chunks must be buffered before codec input](/journal/fg2-mimi-streaming-frame-buffer-contract/); [Audio resampling for datasets: keep timestamps aligned when sample counts change](/journal/data-guide-audio-resample-time-labels/).
