mirror of
https://github.com/snakers4/silero-vad.git
synced 2026-02-04 17:39:22 +08:00
Golang example
This commit is contained in:
19
examples/go/README.md
Normal file
19
examples/go/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
## Golang Example
|
||||
|
||||
This is a sample program of how to run speech detection using `silero-vad` from Golang (CGO + ONNX Runtime).
|
||||
|
||||
### Requirements
|
||||
|
||||
- Golang >= v1.21
|
||||
- ONNX Runtime
|
||||
|
||||
### Usage
|
||||
|
||||
```sh
|
||||
go run ./cmd/main.go test.wav
|
||||
```
|
||||
|
||||
> **_Note_**
|
||||
>
|
||||
> Make sure you have the ONNX Runtime library and C headers installed in your path.
|
||||
|
||||
60
examples/go/cmd/main.go
Normal file
60
examples/go/cmd/main.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/streamer45/silero-vad-go/speech"
|
||||
|
||||
"github.com/go-audio/wav"
|
||||
)
|
||||
|
||||
func main() {
|
||||
sd, err := speech.NewDetector(speech.DetectorConfig{
|
||||
ModelPath: "../../files/silero_vad.onnx",
|
||||
SampleRate: 16000,
|
||||
WindowSize: 1536,
|
||||
Threshold: 0.5,
|
||||
MinSilenceDurationMs: 0,
|
||||
SpeechPadMs: 0,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create speech detector: %s", err)
|
||||
}
|
||||
|
||||
f, err := os.Open(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open sample audio file: %s", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
dec := wav.NewDecoder(f)
|
||||
|
||||
if ok := dec.IsValidFile(); !ok {
|
||||
log.Fatalf("invalid WAV file")
|
||||
}
|
||||
|
||||
buf, err := dec.FullPCMBuffer()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to get PCM buffer")
|
||||
}
|
||||
|
||||
pcmBuf := buf.AsFloat32Buffer()
|
||||
|
||||
segments, err := sd.Detect(pcmBuf.Data)
|
||||
if err != nil {
|
||||
log.Fatalf("Detect failed: %s", err)
|
||||
}
|
||||
|
||||
for _, s := range segments {
|
||||
log.Printf("speech starts at %0.2fs", s.SpeechStartAt)
|
||||
if s.SpeechEndAt > 0 {
|
||||
log.Printf("speech ends at %0.2fs", s.SpeechEndAt)
|
||||
}
|
||||
}
|
||||
|
||||
err = sd.Destroy()
|
||||
if err != nil {
|
||||
log.Fatalf("failed to destroy detector: %s", err)
|
||||
}
|
||||
}
|
||||
13
examples/go/go.mod
Normal file
13
examples/go/go.mod
Normal file
@@ -0,0 +1,13 @@
|
||||
module silero
|
||||
|
||||
go 1.21.4
|
||||
|
||||
require (
|
||||
github.com/go-audio/wav v1.1.0
|
||||
github.com/streamer45/silero-vad-go v0.1.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-audio/audio v1.0.0 // indirect
|
||||
github.com/go-audio/riff v1.0.0 // indirect
|
||||
)
|
||||
16
examples/go/go.sum
Normal file
16
examples/go/go.sum
Normal file
@@ -0,0 +1,16 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-audio/audio v1.0.0 h1:zS9vebldgbQqktK4H0lUqWrG8P0NxCJVqcj7ZpNnwd4=
|
||||
github.com/go-audio/audio v1.0.0/go.mod h1:6uAu0+H2lHkwdGsAY+j2wHPNPpPoeg5AaEFh9FlA+Zs=
|
||||
github.com/go-audio/riff v1.0.0 h1:d8iCGbDvox9BfLagY94fBynxSPHO80LmZCaOsmKxokA=
|
||||
github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38rVG498=
|
||||
github.com/go-audio/wav v1.1.0 h1:jQgLtbqBzY7G+BM8fXF7AHUk1uHUviWS4X39d5rsL2g=
|
||||
github.com/go-audio/wav v1.1.0/go.mod h1:mpe9qfwbScEbkd8uybLuIpTgHyrISw/OTuvjUW2iGtE=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/streamer45/silero-vad-go v0.1.0 h1:0nGZ6VT3LKOkBG/w+4kljIB6brxtgQn6YuNjTVYjOQ4=
|
||||
github.com/streamer45/silero-vad-go v0.1.0/go.mod h1:B+2FXs/5fZ6pzl6unUZYhZqkYdOB+3saBVzjOzdZnUs=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
Reference in New Issue
Block a user