| | |
| | | |
| | | const ( |
| | | asrSampleRate = 16000 |
| | | xasrHeadPaddingSamples = asrSampleRate / 4 |
| | | xasrTailPaddingSamples = asrSampleRate + asrSampleRate/2 |
| | | xasrReleaseTailDelay = 300 * time.Millisecond |
| | | ) |
| | | |
| | | var xasrTailPadding = make([]float32, xasrTailPaddingSamples) |
| | | var ( |
| | | xasrHeadPadding = make([]float32, xasrHeadPaddingSamples) |
| | | xasrTailPadding = make([]float32, xasrTailPaddingSamples) |
| | | ) |
| | | |
| | | type sherpaEngine struct { |
| | | recognizer *sherpa.OfflineRecognizer |
| | |
| | | engine *xasrStreamingEngine |
| | | stream *sherpa.OnlineStream |
| | | lastText string |
| | | hasAcceptedSamples bool |
| | | finished bool |
| | | } |
| | | |
| | |
| | | if s.stream == nil || s.finished { |
| | | return s.lastText, nil |
| | | } |
| | | if !s.hasAcceptedSamples { |
| | | samples = xasrSamplesWithHeadPadding(samples) |
| | | s.hasAcceptedSamples = true |
| | | } |
| | | |
| | | s.engine.mu.Lock() |
| | | defer s.engine.mu.Unlock() |
| | |
| | | return nextText |
| | | } |
| | | |
| | | func xasrSamplesWithHeadPadding(samples []float32) []float32 { |
| | | padded := make([]float32, 0, len(xasrHeadPadding)+len(samples)) |
| | | padded = append(padded, xasrHeadPadding...) |
| | | padded = append(padded, samples...) |
| | | return padded |
| | | } |
| | | |
| | | func (e *sherpaEngine) HardwareInfo() string { |
| | | return e.hwInfo |
| | | } |
| | |
| | | return xasrReleaseTailDelay |
| | | } |
| | | |
| | | func (e *xasrStreamingEngine) HoldPreCaptureEnabled() bool { |
| | | return true |
| | | } |
| | | |
| | | func (e *sherpaEngine) Close() { |
| | | if e.recognizer != nil { |
| | | sherpa.DeleteOfflineRecognizer(e.recognizer) |