Skip to content

Commit

Permalink
Merge pull request Kitt-AI#136 from brentnd/master
Browse files Browse the repository at this point in the history
Add Go support
  • Loading branch information
xuchen authored Feb 27, 2017
2 parents fc9265f + adbb40a commit 2f0342e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/Go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"io/ioutil"
"unsafe"
"os"

"github.com/Kitt-AI/snowboy/swig/Go"
)

func main() {
if len(os.Args) < 3 {
fmt.Printf("usage: %s <keyword.umdl> <wav file>\n", os.Args[0])
return
}
fmt.Printf("Snowboy detecting keyword in %s\n", os.Args[2])
detector := snowboydetect.NewSnowboyDetect("../../resources/common.res", os.Args[1])
detector.SetSensitivity("0.5")
detector.SetAudioGain(1)
defer snowboydetect.DeleteSnowboyDetect(detector)

dat, err := ioutil.ReadFile(os.Args[2])
if err != nil {
panic(err)
}

ptr := snowboydetect.SwigcptrInt16_t(unsafe.Pointer(&dat[0]))
res := detector.RunDetection(ptr, len(dat) / 2 /* len of int16 */)
if res == -2 {
fmt.Println("Snowboy detected silence")
} else if res == -1 {
fmt.Println("Snowboy detection returned error")
} else if res == 0 {
fmt.Println("Snowboy detected nothing")
} else {
fmt.Println("Snowboy detected keyword ", res)
}
}
40 changes: 40 additions & 0 deletions examples/Go/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Dependencies

### Swig
http://www.swig.org/

### Go Package
```
go get github.com/Kitt-AI/snowboy/swig/Go
```

## Building

```
go build -o snowboy main.go
```

## Running

```
./snowboy <keyword.umdl> <wav file>
```

### Examples
Cmd:
`./snowboy ../../resources/snowboy.umdl ../../resources/snowboy.wav`

Output:
```
Snowboy detecting keyword in ../../resources/snowboy.wav
Snowboy detected keyword 1
```

Cmd:
`./snowboy ../../resources/alexa.umdl ../../resources/snowboy.wav`

Output:
```
Snowboy detecting keyword in ../../resources/snowboy.wav
Snowboy detected nothing
```
9 changes: 9 additions & 0 deletions swig/Go/snowboy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package snowboydetect

/*
#cgo CXXFLAGS: -std=c++11
#cgo linux,amd64 LDFLAGS: -lcblas -L${SRCDIR}/../../lib/ubuntu64 -lsnowboy-detect
#cgo linux,arm LDFLAGS: -lcblas -L${SRCDIR}/../../lib/rpi -lsnowboy-detect
#cgo darwin LDFLAGS: -lcblas -L${SRCDIR}/../../lib/osx -lsnowboy-detect
*/
import "C"
13 changes: 13 additions & 0 deletions swig/Go/snowboydetect.swigcxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// swig/Go/snowboydetect.swigcxx

%module snowboydetect

// Suppress SWIG warnings.
#pragma SWIG nowarn=SWIGWARN_PARSE_NESTED_CLASS
%include "std_string.i"

%{
#include "../../include/snowboy-detect.h"
%}

%include "../../include/snowboy-detect.h"

0 comments on commit 2f0342e

Please sign in to comment.