﻿# CosyVoice2 streaming assembly: token offsets and vocoder tails prevent duplicate audio

Trace CosyVoice2 cumulative speech tokens, token-to-mel offsets, retained vocoder state, and final chunk handling before replacing its streaming assembler.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cosyvoice2-streaming-token-offset-vocoder-tail/
Language: en

## Select the streaming architecture version

The [CosyVoice2 report](https://arxiv.org/abs/2412.10117) describes chunk-aware causal flow matching for streaming and non-streaming synthesis. The [project repository](https://github.com/QwenAudio/CosyVoice) includes multiple model generations, so this guide examines the `CosyVoice2Model` path specifically. A similarly named class in another version can have different chunk or cache behavior. Pin the implementation before porting its streaming assembly into a different server or transport layer.

## Keep accumulated tokens and emitted regions distinct

The [reference serving implementation](https://raw.githubusercontent.com/FunAudioLLM/CosyVoice/main/cosyvoice/cli/model.py) keeps speech tokens for a request and passes a prefix with available lookahead to the flow model. A token offset then selects the newly relevant mel region. Consequently, an input prefix can contain material already represented in earlier output. Concatenating full decoded prefixes as if each were entirely new audio repeats content, while discarding context before decoding can remove information the streaming computation expects.

## Preserve the vocoder tail

The implementation retains mel and source state for the vocoder, uses overlap handling, and withholds a tail on non-final chunks. Finalization handles the remaining material rather than applying the same withholding forever. These details bridge chunk boundaries. They are not transport metadata that can be dropped simply because each returned tensor is a valid waveform. A replacement assembler must reproduce the selected overlap and completion policy or explicitly evaluate its changed boundary behavior.

## Diagnose duplicated or truncated speech

Suppose a websocket port repeats syllables at packet joins or consistently loses the end of a sentence. Save emitted sample ranges, token offsets, finalize decisions, and request identifiers before changing speech weights. Duplication can follow from appending overlapping prefixes; truncation can follow from never flushing withheld material. Those are testable assembly hypotheses, not a claim that every acoustic glitch has one cause. Compare a complete short utterance against the concatenated streamed result and inspect the boundary map.

## Audit request state and complete output

Record model revision, token progression, lookahead policy, offset conversion, vocoder cache ownership, and final flush. Guides to [audio representations](/journal/audio-codec-token-generation/) and [inference evaluation](/journal/inference-benchmark-method/) provide context. Include interrupted and completed utterances when testing a wrapper. The relevant correctness property is a coherent emitted sequence with preserved conditioning boundaries, rather than merely obtaining audio tensors quickly or observing that every individual packet can be played on its own.
