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 ¶
- type Accumulator
- type Buffer
- func (buffer *Buffer) Close() error
- func (buffer *Buffer) Descriptor() v1.Descriptor
- func (buffer *Buffer) EagerEndOfTurn() bool
- func (buffer *Buffer) Finalize(ctx context.Context, sourceSample uint64) (v1.PerceptionRevision, error)
- func (buffer *Buffer) ProviderInvocationCount() uint64
- func (buffer *Buffer) ProviderRuntimeMetrics() ProviderMetrics
- func (buffer *Buffer) PushFrame(ctx context.Context, frame v1.AudioFrame) ([]v1.PerceptionRevision, error)
- func (buffer *Buffer) SpeechEndpointed() bool
- func (buffer *Buffer) Stats() Stats
- type Config
- type ProviderMetrics
- type ReusableBuffer
- type RuntimeSnapshot
- type Stats
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.
func (*Accumulator) Wrap ¶ added in v0.2.0
func (accumulator *Accumulator) Wrap(config Config) (v1.PerceptionProvider, error)
Wrap buffers a recogniser, reusing it across utterances when it supports that and owning one utterance when it does not.
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 ¶
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 ¶
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) EagerEndOfTurn ¶ added in v0.2.0
EagerEndOfTurn forwards the wrapped recogniser's eager end of turn (Flux's EagerEndOfTurn), for the same reason as SpeechEndpointed.
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 ¶
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) SpeechEndpointed ¶ added in v0.2.0
SpeechEndpointed forwards the wrapped recogniser's own end of speech (Nova-3's speech_final, Flux's EndOfTurn).
The buffer is deployment policy, not a recogniser, and the runtime reads these optional signals from whatever it holds. A signal the buffer does not pass on reaches nothing and fails nothing: the served room wrapped every recogniser here, so a recogniser-owned end of turn was configured, wired, tested with an unwrapped fake, and never once reached the graph.
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 ReusableBuffer ¶ added in v0.2.0
type ReusableBuffer struct {
*Buffer
// contains filtered or unexported fields
}
ReusableBuffer is a Buffer around a recogniser that keeps its connection across utterances (api/v1.UtteranceReusable).
A plain Buffer owns one utterance and closes the recogniser when it ends, which is right for a batch endpoint and wrong for a streaming one: the served room wrapped every recogniser in a plain Buffer, so each utterance reconnected to Deepgram before its first partial - measured at 0.3 to 2.3 s for Flux, with the recogniser element blocked for all of it, a later utterance's audio piling up behind, and a turn end that could no longer arrive before the gate's silence.
It is a separate type rather than a method on Buffer because the runtime decides reuse from the methods a provider has, and a batch recogniser wrapped in a Buffer must go on being closed.
func (*ReusableBuffer) EndUtterance ¶ added in v0.2.0
func (buffer *ReusableBuffer) EndUtterance() error
EndUtterance ends the utterance in the recogniser and readies the buffer for the next one. Counters stay cumulative and fold into the accumulator once, when the session closes the buffer, so a snapshot never loses an utterance between a fold and a reset.
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.