Documentation
¶
Overview ¶
Package cueclip keeps rendered cue clips: the short pieces of speech a realtime session plays for a cue, such as a guardrail's message. A clip is rendered once and kept in a Cache under a content Key, so sessions that need the same clip (the same text, voice, source and format) play it without rendering it again.
The cue stage (runtime/pipeline/stage) reads and writes the cache; a host chooses it: NewMemoryCache for one process, NewDirCache for a directory that can be filled offline and checked in, or its own Cache over a shared store.
Index ¶
Constants ¶
const DefaultCacheBytes = 64 << 20
DefaultCacheBytes is the size of the process-wide in-memory cache a cue stage uses when it is given none.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
Get(ctx context.Context, key Key) (Clip, bool, error)
Put(ctx context.Context, key Key, clip Clip) error
}
Cache stores rendered clips by content key, so sessions that need the same clip render it once. Implementations must be safe for concurrent use. Get returns a miss as (zero, false, nil); an error is a cache fault, which the cue stage logs and treats as a miss.
The cue stage renders through a single-flight group within its process, so one process renders a key once at a time. A store shared across processes (many pods serving the same pack) does not get that across processes: each process can render a key once before the store holds it. An implementation that wants no duplicate writes should make Put put-if-absent (Redis SET NX, a blob store's create-if-absent).
A cached clip is ready to play at the format in its key, so a hit needs no processing. Its bytes are shared: no one modifies them.
func NewDirCache ¶
NewDirCache is a Cache over dir. Each clip is two files named by the key's hex: <key>.pcm (raw PCM16LE) and <key>.json, a sidecar naming the text, voice, source and format, so the directory can be read and checked into a repo:
{"text": "...", "voice": "alloy", "source": "tts:openai",
"encoding": "pcm16", "sample_rate": 24000, "channels": 1, "duration_ms": 1830,
"sha256": "..."}
Writes are atomic (temp file and rename, .pcm first, .json last), so a .pcm without a .json, what a write cut short leaves, is a miss. Many writers, in one process or several, can share a directory. Get is a miss for a sidecar that is not the key's own (its text, voice, source or format does not make the key) and for a .pcm the sidecar does not vouch for (another length, or another digest). Put keeps only a clip Get would return, so it errors on one whose Meta does not make its key.
A writable cache creates dir and sweeps temp files a crashed writer left more than an hour ago; a ReadOnly one needs dir to exist and changes nothing in it.
func NewMemoryCache ¶
NewMemoryCache is an in-process Cache that keeps at most maxBytes of clip audio, evicting the least recently used clips first. A clip larger than maxBytes is not kept.
type Clip ¶
type Clip struct {
// PCM is the audio, PCM16 little-endian in Format.
PCM []byte
Format Format
// Meta says where the clip came from. A cache that keeps clips where a
// person can read them (the directory cache's sidecar) records it; it is
// not part of what plays.
Meta Meta
}
Clip is a rendered clip, ready to play: trimmed, faded out and in Format.
type Key ¶
type Key [32]byte
Key identifies a rendered clip by what determines its audio.
func KeyFor ¶
KeyFor is the key of the clip that says text in voice from the source kind sourceKind ("tts:<service name>", "session:<family>" or "earcon"), in format. It is SHA-256 over the text (trimmed and in Unicode NFC, otherwise exact: punctuation changes how it is said), the voice, the source kind and the format. No model name goes into it: a model change behind the same source and voice keeps the key, so a host that wants fresh clips after one clears its cache.