asrbuffer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: Apache-2.0 Imports: 9 Imported by: 1 module, 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 asrbuffer decouples the realtime scheduler cadence from an ASR provider's minimum useful audio chunk size.

A Buffer accepts small, contiguous PCM16 frames at the interaction cadence (for example, 50 or 80 milliseconds), combines them into exact provider chunks (for example, 200 milliseconds), and implements the unchanged api/v1.PerceptionProvider interface. The wrapper is internal middleware: it neither adds an OpenAI Realtime event nor changes the wrapped descriptor.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Accumulator

type Accumulator struct {
	// contains filtered or unexported fields
}

Accumulator folds per-utterance counters into one process-level total.

The recogniser is a factory and a Buffer owns exactly one utterance, so the counters a Buffer carries describe a few seconds of one session and then go out of scope with it. That is the right lifetime for the buffer and the wrong one for an operator, who is watching for a recogniser that is getting slower across every utterance rather than within one. This owns the number that survives the boundary.

A tracked buffer folds itself in exactly once, when it closes. Until then it is read in place rather than waited for, because an utterance still in flight is the one most likely to be the slow one, and a signal that only appears after the endpoint would go quiet during precisely the stall it exists to report.

func NewAccumulator

func NewAccumulator() *Accumulator

NewAccumulator returns an accumulator with no utterances folded in.

func (*Accumulator) New

func (accumulator *Accumulator) New(config Config) (*Buffer, error)

New creates a Buffer whose counters fold into this accumulator when it closes.

It is deliberately a second constructor rather than a Config field on the first. A benchmark builds a Buffer in order to read that one buffer's numbers, and enrolling it in a process-level total would leave the harness measuring a figure that other sessions also move. Deployment opts in; a measurement does not.

A nil accumulator returns an ordinary untracked buffer, so a caller holding one that was never configured needs no branch of its own.

func (*Accumulator) Snapshot

func (accumulator *Accumulator) Snapshot() RuntimeSnapshot

Snapshot totals every utterance this process has run: those already folded, plus those still open.

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer owns one utterance. Calls are serialized because the wrapped provider is stateful and a failed remote update makes retry ambiguous.

func New

func New(config Config) (*Buffer, error)

New validates middleware configuration and returns a fresh utterance buffer. MinimumChunk is explicit so a provider upgrade cannot silently change the scheduling experiment.

func (*Buffer) Close

func (buffer *Buffer) Close() error

Close releases the wrapped recogniser.

The buffer is what the runtime holds, so it is what the runtime can close, and a recogniser that owns a connection is unreachable behind it otherwise. Delegating rather than implementing means a stateless provider still needs nothing, and an abandoned utterance on a streaming one does not strand its socket.

func (*Buffer) Descriptor

func (buffer *Buffer) Descriptor() v1.Descriptor

Descriptor implements api/v1.PerceptionProvider. Buffering is deployment policy, not provider identity, so the wrapped descriptor is returned with a defensive copy of its capability map.

func (*Buffer) Finalize

func (buffer *Buffer) Finalize(ctx context.Context, sourceSample uint64) (v1.PerceptionRevision, error)

Finalize flushes a short terminal remainder before finalizing the wrapped provider. A partial revision from that flush is superseded immediately by the returned final revision and is therefore not separately exposed.

func (*Buffer) ProviderInvocationCount

func (buffer *Buffer) ProviderInvocationCount() uint64

ProviderInvocationCount returns the number of completed PushFrame attempts at the wrapped provider boundary. Benchmark drivers use before/after snapshots to distinguish cheap scheduler ticks from actual decode opportunities, including a terminal failed attempt.

func (*Buffer) ProviderRuntimeMetrics

func (buffer *Buffer) ProviderRuntimeMetrics() ProviderMetrics

ProviderRuntimeMetrics returns monotonic provider-boundary counters and timing for process-level aggregation.

func (*Buffer) PushFrame

func (buffer *Buffer) PushFrame(ctx context.Context, frame v1.AudioFrame) ([]v1.PerceptionRevision, error)

PushFrame accepts one scheduler frame and may make zero or more provider calls. Each provider frame reaches the current fixed or adaptive threshold, except the terminal remainder flushed by Finalize.

func (*Buffer) Stats

func (buffer *Buffer) Stats() Stats

Stats returns an immutable snapshot.

type Config

type Config struct {
	Provider           v1.PerceptionProvider
	MinimumChunk       time.Duration
	MaximumChunk       time.Duration
	MaxInputFrameBytes int
}

Config binds a provider to a minimum chunk duration. MaximumChunk equal to zero selects fixed cadence. A larger value enables revision-adaptive backoff: changed ASR output resets to MinimumChunk, while an unchanged provider advance doubles the next chunk up to MaximumChunk. The policy uses only the presence of a typed revision, never transcript content. MaxInputFrameBytes is an admission bound; zero selects 4 MiB.

type ProviderMetrics

type ProviderMetrics struct {
	AdvanceInvocations   uint64
	AdvanceFailures      uint64
	AdvanceElapsedNS     uint64
	AdvanceMaxElapsedNS  uint64
	FinalizeInvocations  uint64
	FinalizeFailures     uint64
	FinalizeElapsedNS    uint64
	FinalizeMaxElapsedNS uint64
}

ProviderMetrics is the monotonic subset used by process-level benchmark telemetry. It contains no transcript or audio content.

func (ProviderMetrics) MeanAdvanceNS

func (metrics ProviderMetrics) MeanAdvanceNS() uint64

MeanAdvanceNS is the average time one provider advance took. It is the degradation signal: a maximum moves on one bad call and stays there, while a mean that is climbing says the recogniser is slowing down generally. Zero invocations report zero rather than dividing.

func (ProviderMetrics) MeanFinalizeNS

func (metrics ProviderMetrics) MeanFinalizeNS() uint64

MeanFinalizeNS is the same average for finalisation, which is measured separately because it is one call per utterance rather than one per chunk.

type RuntimeSnapshot

type RuntimeSnapshot struct {
	ProviderMetrics
	// Utterances is how many buffers have closed and folded in.
	Utterances uint64
	// InFlight is how many are still open and contributing in place.
	InFlight int
}

RuntimeSnapshot is a process-level view of the recogniser boundary. It contains counters and timing only: no transcript, no audio, no identity.

type Stats

type Stats struct {
	InputFrames          uint64 `json:"input_frames"`
	InputSamples         uint64 `json:"input_samples"`
	ProviderAttempts     uint64 `json:"provider_attempts"`
	ProviderChunks       uint64 `json:"provider_chunks"`
	ProviderFailures     uint64 `json:"provider_failures"`
	ProviderSamples      uint64 `json:"provider_samples"`
	ProviderElapsedNS    uint64 `json:"provider_elapsed_ns"`
	ProviderMaxElapsedNS uint64 `json:"provider_max_elapsed_ns"`
	FinalizeAttempts     uint64 `json:"finalize_attempts"`
	FinalizeFailures     uint64 `json:"finalize_failures"`
	FinalizeElapsedNS    uint64 `json:"finalize_elapsed_ns"`
	FinalizeMaxElapsedNS uint64 `json:"finalize_max_elapsed_ns"`
	PendingSamples       uint64 `json:"pending_samples"`
	MinimumChunkSamples  uint64 `json:"minimum_chunk_samples,omitempty"`
	MaximumChunkSamples  uint64 `json:"maximum_chunk_samples,omitempty"`
	CurrentChunkSamples  uint64 `json:"current_chunk_samples,omitempty"`
	Policy               string `json:"policy"`
	SampleRateHz         uint32 `json:"sample_rate_hz,omitempty"`
	Finalized            bool   `json:"finalized"`
}

Stats is a point-in-time, secret-free snapshot. ProviderChunks counts successful calls to the wrapped provider's PushFrame; ProviderAttempts and timing include failures. Finalization is measured separately.

Jump to

Keyboard shortcuts

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