Documentation
¶
Overview ¶
Package turn provides end-of-turn detection: it decides when a user has actually finished speaking, as opposed to merely pausing. The Analyzer interface is the contract; the package ships SmartTurnV3, an ONNX model that predicts turn completion from the audio of the user's turn, as the default.
Unlike a voice activity detector, which reacts to silence, a turn analyzer listens to the shape of speech (intonation, trailing words) to tell a mid-sentence pause from a finished thought. The turntaking package feeds it the user's audio and asks it, when speech stops, whether the turn is over.
Index ¶
- type Analyzer
- type Base
- type EndOfTurnState
- type HTTPConfig
- type HTTPSmartTurn
- func (b HTTPSmartTurn) AnalyzeEndOfTurn() (EndOfTurnState, *MetricsData, error)
- func (b HTTPSmartTurn) AppendAudio(buffer []byte, isSpeech bool) EndOfTurnState
- func (s *HTTPSmartTurn) Cleanup() error
- func (b HTTPSmartTurn) Clear()
- func (b HTTPSmartTurn) Params() Params
- func (b HTTPSmartTurn) SpeechTriggered() bool
- func (b HTTPSmartTurn) UpdateVADStartSecs(secs float64)
- type MetricsData
- type Option
- type Params
- type SmartTurnV3
- func (b SmartTurnV3) AnalyzeEndOfTurn() (EndOfTurnState, *MetricsData, error)
- func (b SmartTurnV3) AppendAudio(buffer []byte, isSpeech bool) EndOfTurnState
- func (s *SmartTurnV3) Cleanup() error
- func (b SmartTurnV3) Clear()
- func (b SmartTurnV3) Params() Params
- func (b SmartTurnV3) SpeechTriggered() bool
- func (b SmartTurnV3) UpdateVADStartSecs(secs float64)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Analyzer ¶
type Analyzer interface {
// SetSampleRate sets the input sample rate in Hz. It is called once before
// analysis begins.
SetSampleRate(sampleRate int)
// AppendAudio adds a chunk of the user's audio along with whether voice
// activity considers it speech. It returns Complete when a non-model
// condition (the silence safety net) ends the turn, otherwise Incomplete.
AppendAudio(buffer []byte, isSpeech bool) EndOfTurnState
// SpeechTriggered reports whether the analyzer has heard speech since the
// turn began, which is what tells a turn that is under way from one that has
// not started. It stays true across a turn the analyzer judged unfinished,
// and Clear puts it back to false.
SpeechTriggered() bool
// AnalyzeEndOfTurn runs the model over the buffered turn audio and returns
// the predicted state, with the metrics of the prediction. The metrics are
// nil when the model did not run, because there was no audio to judge.
AnalyzeEndOfTurn() (EndOfTurnState, *MetricsData, error)
// UpdateVADStartSecs informs the analyzer of the VAD start delay so it can
// align its pre-speech buffering.
UpdateVADStartSecs(secs float64)
// Params are the analysis parameters in force. They are published to the
// pipeline when it starts, so a processor downstream can size its own
// behavior to them and clients and observers can mirror them.
Params() Params
// Clear resets the analyzer to its initial state.
Clear()
// Cleanup releases the resources the analyzer holds, a model session for
// example. The turn strategy driving the analyzer calls it when it is
// cleaned up itself.
//
// The analyzer stays usable afterwards. Cleaning up is not only teardown: a
// turn strategy is cleaned up and set up again whenever the strategies are
// updated, so what Cleanup released is acquired again when the next turn
// needs it.
Cleanup() error
// contains filtered or unexported methods
}
Analyzer decides when a user's turn has ended. An analyzer is built by embedding Base, which supplies the sample rate and the defaults for UpdateVADStartSecs and Cleanup.
type Base ¶ added in v1.0.0
type Base struct {
// contains filtered or unexported fields
}
Base is what every end-of-turn analyzer shares: the sample rate it runs at, and the defaults for the methods an analyzer has no use for. Its zero value is ready to use; embed it in an analyzer.
func (*Base) Cleanup ¶ added in v1.0.0
Cleanup releases nothing: an analyzer holding a model session overrides it.
func (*Base) SampleRate ¶ added in v1.0.0
SampleRate is the input sample rate in Hz, zero until SetSampleRate is called.
func (*Base) SetSampleRate ¶ added in v1.0.0
SetSampleRate sets the input sample rate in Hz.
func (*Base) UpdateVADStartSecs ¶ added in v1.0.0
UpdateVADStartSecs does nothing: an analyzer that sizes its buffering to the VAD start delay overrides it.
type EndOfTurnState ¶
type EndOfTurnState int
EndOfTurnState is the result of end-of-turn analysis.
const ( // Incomplete means the user is likely still speaking or about to continue. Incomplete EndOfTurnState = iota // Complete means the user has finished their turn. Complete )
func (EndOfTurnState) String ¶
func (s EndOfTurnState) String() string
String returns the lowercase state name.
type HTTPConfig ¶ added in v0.2.0
type HTTPConfig struct {
// URL is the endpoint the audio is posted to. Required.
URL string
// Headers are sent on every request, on top of the content type. It is where
// an authorization header belongs.
Headers map[string]string
// Client makes the requests; nil uses a client of its own. The request
// deadline comes from the analysis parameters rather than from the client,
// so a client configured with its own timeout has whichever is shorter apply.
Client *http.Client
// Params configures the analysis; the zero value uses the defaults.
Params *Params
}
HTTPConfig configures an HTTPSmartTurn analyzer.
type HTTPSmartTurn ¶ added in v0.2.0
type HTTPSmartTurn struct {
// contains filtered or unexported fields
}
HTTPSmartTurn is an end-of-turn Analyzer that asks a remote service for the prediction, for a deployment that runs the model somewhere other than in the bot. It buffers and segments the turn's audio exactly as the local analyzer does, and posts that segment for scoring.
A request is given the silence window to answer in, since an answer arriving later is an answer about a turn that is already over. A request that times out, and any other failure, is reported as an unfinished turn: a service that is down leaves every turn to the stop-seconds safety net rather than cutting the user off mid-sentence.
func NewHTTPSmartTurn ¶ added in v0.2.0
func NewHTTPSmartTurn(cfg HTTPConfig) (*HTTPSmartTurn, error)
NewHTTPSmartTurn builds an analyzer backed by a remote scoring service.
func (HTTPSmartTurn) AnalyzeEndOfTurn ¶ added in v0.2.0
func (b HTTPSmartTurn) AnalyzeEndOfTurn() (EndOfTurnState, *MetricsData, error)
AnalyzeEndOfTurn runs the model over the buffered turn and returns the predicted state, with the metrics of the prediction. The metrics are nil when there was no audio to run the model on. On Complete it clears the buffer.
func (HTTPSmartTurn) AppendAudio ¶ added in v0.2.0
func (b HTTPSmartTurn) AppendAudio(buffer []byte, isSpeech bool) EndOfTurnState
AppendAudio buffers a chunk of turn audio and tracks silence. It returns Complete only when accumulated silence crosses the stop-seconds safety net; the model decision happens in AnalyzeEndOfTurn.
func (*HTTPSmartTurn) Cleanup ¶ added in v0.3.0
func (s *HTTPSmartTurn) Cleanup() error
Cleanup releases nothing: the analyzer holds no session of its own, and the client it posts with belongs to whoever configured it.
func (HTTPSmartTurn) Clear ¶ added in v0.2.0
func (b HTTPSmartTurn) Clear()
Clear resets the analyzer to its initial state.
func (HTTPSmartTurn) Params ¶ added in v0.2.0
func (b HTTPSmartTurn) Params() Params
Params are the analysis parameters this analyzer runs with.
func (HTTPSmartTurn) SpeechTriggered ¶ added in v0.2.0
func (b HTTPSmartTurn) SpeechTriggered() bool
SpeechTriggered reports whether speech has been heard since the turn began.
func (HTTPSmartTurn) UpdateVADStartSecs ¶ added in v0.2.0
func (b HTTPSmartTurn) UpdateVADStartSecs(secs float64)
UpdateVADStartSecs stores the VAD start delay used to widen the pre-speech window.
type MetricsData ¶ added in v0.3.0
type MetricsData struct {
// Processor names the analyzer that made the prediction.
Processor string
// Complete is whether the turn was predicted to be finished.
Complete bool
// Probability is the model's confidence that it was finished, in [0,1].
Probability float64
// E2EProcessing is how long the prediction took.
E2EProcessing time.Duration
}
MetricsData is what an end-of-turn model decided about a turn and what it cost to decide. An analyzer reports it only for a prediction the model actually made.
type Params ¶
type Params struct {
// StopSecs is the silence duration that forces a turn complete even if the
// model has not fired, as a safety net.
StopSecs float64
// PreSpeechMs is how much audio before speech onset to include in the
// analyzed segment.
PreSpeechMs float64
// MaxDurationSecs caps the analyzed segment length; only the most recent
// audio is kept.
MaxDurationSecs float64
}
Params configures end-of-turn analysis.
func DefaultParams ¶
func DefaultParams() Params
DefaultParams returns the default analysis parameters.
type SmartTurnV3 ¶
type SmartTurnV3 struct {
// contains filtered or unexported fields
}
SmartTurnV3 is an end-of-turn Analyzer backed by the smart-turn-v3 ONNX model.
func NewSmartTurnV3 ¶
func NewSmartTurnV3(opts ...Option) (*SmartTurnV3, error)
NewSmartTurnV3 loads the embedded smart-turn-v3 model and returns an analyzer. It requires the ONNX runtime to be locatable (see the onnxrt package).
func (SmartTurnV3) AnalyzeEndOfTurn ¶
func (b SmartTurnV3) AnalyzeEndOfTurn() (EndOfTurnState, *MetricsData, error)
AnalyzeEndOfTurn runs the model over the buffered turn and returns the predicted state, with the metrics of the prediction. The metrics are nil when there was no audio to run the model on. On Complete it clears the buffer.
func (SmartTurnV3) AppendAudio ¶
func (b SmartTurnV3) AppendAudio(buffer []byte, isSpeech bool) EndOfTurnState
AppendAudio buffers a chunk of turn audio and tracks silence. It returns Complete only when accumulated silence crosses the stop-seconds safety net; the model decision happens in AnalyzeEndOfTurn.
func (*SmartTurnV3) Cleanup ¶ added in v0.3.0
func (s *SmartTurnV3) Cleanup() error
Cleanup releases the model session, the native resource the analyzer holds. The analyzer stays usable: the session is opened again when the next turn needs it.
func (SmartTurnV3) Clear ¶
func (b SmartTurnV3) Clear()
Clear resets the analyzer to its initial state.
func (SmartTurnV3) Params ¶ added in v0.1.0
func (b SmartTurnV3) Params() Params
Params are the analysis parameters this analyzer runs with.
func (SmartTurnV3) SpeechTriggered ¶ added in v0.1.0
func (b SmartTurnV3) SpeechTriggered() bool
SpeechTriggered reports whether speech has been heard since the turn began.
func (SmartTurnV3) UpdateVADStartSecs ¶
func (b SmartTurnV3) UpdateVADStartSecs(secs float64)
UpdateVADStartSecs stores the VAD start delay used to widen the pre-speech window.