diff --git a/go-api-examples/non-streaming-decode-files/main.go b/go-api-examples/non-streaming-decode-files/main.go index 909b94e0..5373dcf2 100644 --- a/go-api-examples/non-streaming-decode-files/main.go +++ b/go-api-examples/non-streaming-decode-files/main.go @@ -3,12 +3,13 @@ package main import ( "bytes" "encoding/binary" - sherpa "github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx" - flag "github.com/spf13/pflag" - "github.com/youpy/go-wav" "log" "os" "strings" + + sherpa "github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx" + flag "github.com/spf13/pflag" + "github.com/youpy/go-wav" ) func main() { @@ -80,7 +81,16 @@ func main() { log.Println("Decoding done!") result := stream.GetResult() - log.Println(strings.ToLower(result.Text)) + log.Println("Text: " + strings.ToLower(result.Text)) + log.Println("Emotion: " + result.Emotion) + log.Println("Lang: " + result.Lang) + log.Println("Event: " + result.Event) + for _, v := range result.Timestamps { + log.Printf("Timestamp: %+v\n", v) + } + for _, v := range result.Tokens { + log.Println("Token: " + v) + } log.Printf("Wave duration: %v seconds", float32(len(samples))/float32(sampleRate)) } diff --git a/scripts/go/sherpa_onnx.go b/scripts/go/sherpa_onnx.go index d7d79434..f2637c40 100644 --- a/scripts/go/sherpa_onnx.go +++ b/scripts/go/sherpa_onnx.go @@ -440,7 +440,12 @@ type OfflineStream struct { // It contains recognition result of an offline stream. type OfflineRecognizerResult struct { - Text string + Text string + Tokens []string + Timestamps []float32 + Lang string + Emotion string + Event string } // Frees the internal pointer of the recognition to avoid memory leak. @@ -593,7 +598,23 @@ func (s *OfflineStream) GetResult() *OfflineRecognizerResult { defer C.SherpaOnnxDestroyOfflineRecognizerResult(p) result := &OfflineRecognizerResult{} result.Text = C.GoString(p.text) - + result.Lang = C.GoString(p.lang) + result.Emotion = C.GoString(p.emotion) + result.Event = C.GoString(p.event) + n := int(p.count) + result.Tokens = make([]string, n) + tokens := (*[1 << 28]*C.char)(unsafe.Pointer(p.tokens_arr))[:n:n] + for i := 0; i < n; i++ { + result.Tokens[i] = C.GoString(tokens[i]) + } + if p.timestamps == nil { + return result + } + result.Timestamps = make([]float32, n) + timestamps := (*[1 << 28]C.float)(unsafe.Pointer(p.timestamps))[:n:n] + for i := 0; i < n; i++ { + result.Timestamps[i] = float32(timestamps[i]) + } return result }