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 EndOfTurnState
- type HTTPConfig
- type HTTPSmartTurn
- func (b HTTPSmartTurn) AnalyzeEndOfTurn() (EndOfTurnState, float64, error)
- func (b HTTPSmartTurn) AppendAudio(buffer []byte, isSpeech bool) EndOfTurnState
- func (b HTTPSmartTurn) Clear()
- func (s *HTTPSmartTurn) Close() error
- func (b HTTPSmartTurn) Params() Params
- func (b HTTPSmartTurn) SetSampleRate(sampleRate int)
- func (b HTTPSmartTurn) SpeechTriggered() bool
- func (b HTTPSmartTurn) UpdateVADStartSecs(secs float64)
- type Params
- type SmartTurnV3
- func (b SmartTurnV3) AnalyzeEndOfTurn() (EndOfTurnState, float64, error)
- func (b SmartTurnV3) AppendAudio(buffer []byte, isSpeech bool) EndOfTurnState
- func (b SmartTurnV3) Clear()
- func (s *SmartTurnV3) Close() error
- func (b SmartTurnV3) Params() Params
- func (b SmartTurnV3) SetSampleRate(sampleRate int)
- func (b SmartTurnV3) SpeechTriggered() bool
- func (b SmartTurnV3) UpdateVADStartSecs(secs float64)
- type TurnOption
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 and the model's completion probability in [0,1].
AnalyzeEndOfTurn() (EndOfTurnState, float64, 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()
// Close releases any resources (for example a model session).
Close() error
}
Analyzer decides when a user's turn has ended.
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, float64, error)
AnalyzeEndOfTurn runs the model over the buffered turn and returns the predicted state and completion probability. 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) Clear ¶ added in v0.2.0
func (b HTTPSmartTurn) Clear()
Clear resets the analyzer to its initial state.
func (*HTTPSmartTurn) Close ¶ added in v0.2.0
func (s *HTTPSmartTurn) Close() error
Close releases nothing: the analyzer holds no session of its own, and the client it posts with belongs to whoever configured it.
func (HTTPSmartTurn) Params ¶ added in v0.2.0
func (b HTTPSmartTurn) Params() Params
Params are the analysis parameters this analyzer runs with.
func (HTTPSmartTurn) SetSampleRate ¶ added in v0.2.0
func (b HTTPSmartTurn) SetSampleRate(sampleRate int)
SetSampleRate sets the input sample rate.
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 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 ...TurnOption) (*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, float64, error)
AnalyzeEndOfTurn runs the model over the buffered turn and returns the predicted state and completion probability. 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) 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) SetSampleRate ¶
func (b SmartTurnV3) SetSampleRate(sampleRate int)
SetSampleRate sets the input sample rate.
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.