cueclip

package
v2.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func NewDirCache(dir string, opts ...DirOption) (Cache, error)

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

func NewMemoryCache(maxBytes int64) Cache

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.

func (Clip) Duration

func (c Clip) Duration() time.Duration

Duration is how long the clip plays.

type DirOption

type DirOption func(*dirCache)

DirOption configures NewDirCache.

func ReadOnly

func ReadOnly() DirOption

ReadOnly makes Put of a directory clip cache a no-op that returns nil, so a test or an arena run never writes. A clip the directory lacks is a miss.

type Format

type Format struct {
	SampleRate int
	Channels   int
}

Format is the format of a clip's audio, which is always PCM16 little-endian.

type Key

type Key [32]byte

Key identifies a rendered clip by what determines its audio.

func KeyFor

func KeyFor(text, voice, sourceKind string, format Format) Key

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.

type Meta

type Meta struct {
	Text   string // the text the key was made from
	Voice  string
	Source string // the source kind: "tts:<service>", "session:<family>", "earcon"
}

Meta is what a clip says and who said it: the inputs of its Key besides the format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL