From fee63d712f2512966d22b82ec4b1f8d63f447f03 Mon Sep 17 00:00:00 2001 From: Nickolay Shmyrev Date: Sat, 10 Jul 2021 23:44:15 +0200 Subject: [PATCH] Add go bindings --- go/example/main.go | 36 +++++++++++++++++++++++++++ go/vosk.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 go/example/main.go create mode 100644 go/vosk.go diff --git a/go/example/main.go b/go/example/main.go new file mode 100644 index 00000000..c658e11a --- /dev/null +++ b/go/example/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "flag" + "os" + ".." +) + +func main() { + var filename string + flag.StringVar(&filename, "f", "", "file to transcribe") + flag.Parse() + model, err := vosk.NewModel("model") + rec, err := vosk.NewRecognizer(model) + + file, err := os.Open(filename) + if err != nil { + panic(err) + } + defer file.Close() + + fileinfo, err := file.Stat() + if err != nil { + panic(err) + } + + filesize := fileinfo.Size() + buffer := make([]byte, filesize) + + _, err = file.Read(buffer) + if err != nil { + panic(err) + } + + println(vosk.VoskFinalResult(rec, buffer)) +} diff --git a/go/vosk.go b/go/vosk.go new file mode 100644 index 00000000..25008093 --- /dev/null +++ b/go/vosk.go @@ -0,0 +1,62 @@ +package vosk + +// #cgo CPPFLAGS: -I ${SRCDIR}/../src +// #cgo LDFLAGS: -L ${SRCDIR}/../src -lvosk -ldl -lpthread +// #include +// #include +import "C" + +// VoskModel contains a reference to the C VoskModel +type VoskModel struct { + model *C.struct_VoskModel +} + +// VoskSpkModel contains a reference to the C VoskSpkModel +type VoskSpkModel struct { + spkModel *C.struct_VoskSpkModel +} + +// VoskRecognizer contains a reference to the C VoskRecognizer +type VoskRecognizer struct { + rec *C.struct_VoskRecognizer +} + +func VoskFinalResult(recognizer *VoskRecognizer, buffer []byte) string { + cbuf := C.CBytes(buffer) + defer C.free(cbuf) + _ = C.vosk_recognizer_accept_waveform(recognizer.rec, (*C.char)(cbuf), C.int(len(buffer))) + result := C.GoString(C.vosk_recognizer_final_result(recognizer.rec)) + return result +} + +// NewModel creates a new VoskModel instance +func NewModel(modelPath string) (*VoskModel, error) { + var internal *C.struct_VoskModel + internal = C.vosk_model_new(C.CString(modelPath)) + model := &VoskModel{model: internal} + return model, nil +} + +// NewRecognizer creates a new VoskRecognizer instance +func NewRecognizer(model *VoskModel) (*VoskRecognizer, error) { + var internal *C.struct_VoskRecognizer + internal = C.vosk_recognizer_new(model.model, 16000.0) + rec := &VoskRecognizer{rec: internal} + return rec, nil +} + +func freeModel(model *VoskModel) { + C.vosk_model_free(model.model) +} + +func freeRecognizer(recognizer *VoskRecognizer) { + C.vosk_recognizer_free(recognizer.rec) +} + +// NewSpkModel creates a new VoskSpkModel instance +func NewSpkModel(spkModelPath string) (*VoskSpkModel, error) { + var internal *C.struct_VoskSpkModel + internal = C.vosk_spk_model_new(C.CString(spkModelPath)) + spkModel := &VoskSpkModel{spkModel: internal} + return spkModel, nil +}