Skip to content

Commit c1a1994

Browse files
committed
Initial import
1 parent 26de6d4 commit c1a1994

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/go-skynet/whisper
2+
3+
go 1.19
4+
5+
require (
6+
github.com/ggerganov/whisper.cpp/bindings/go v0.0.0-20230307193630-09e90680072d
7+
github.com/go-audio/wav v1.1.0
8+
)
9+
10+
require (
11+
github.com/go-audio/audio v1.0.0 // indirect
12+
github.com/go-audio/riff v1.0.0 // indirect
13+
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/ggerganov/whisper.cpp/bindings/go v0.0.0-20230307193630-09e90680072d h1:a+wrVgrr8EGpMmauel61hLJsM7h3LGXwoB2oUdPyr7Y=
3+
github.com/ggerganov/whisper.cpp/bindings/go v0.0.0-20230307193630-09e90680072d/go.mod h1:QIjZ9OktHFG7p+/m3sMvrAJKKdWrr1fZIK0rM6HZlyo=
4+
github.com/go-audio/audio v1.0.0 h1:zS9vebldgbQqktK4H0lUqWrG8P0NxCJVqcj7ZpNnwd4=
5+
github.com/go-audio/audio v1.0.0/go.mod h1:6uAu0+H2lHkwdGsAY+j2wHPNPpPoeg5AaEFh9FlA+Zs=
6+
github.com/go-audio/riff v1.0.0 h1:d8iCGbDvox9BfLagY94fBynxSPHO80LmZCaOsmKxokA=
7+
github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38rVG498=
8+
github.com/go-audio/wav v1.1.0 h1:jQgLtbqBzY7G+BM8fXF7AHUk1uHUviWS4X39d5rsL2g=
9+
github.com/go-audio/wav v1.1.0/go.mod h1:mpe9qfwbScEbkd8uybLuIpTgHyrISw/OTuvjUW2iGtE=
10+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
11+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
12+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

whisper.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package whisper
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
8+
"github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
9+
wav "github.com/go-audio/wav"
10+
)
11+
12+
func sh(c string) (string, error) {
13+
cmd := exec.Command("/bin/sh", "-c", c)
14+
cmd.Env = os.Environ()
15+
o, err := cmd.CombinedOutput()
16+
return string(o), err
17+
}
18+
19+
// AudioToWav converts audio to wav for transcribe. It bashes out to ffmpeg
20+
// TODO: use https://github.com/mccoyst/ogg?
21+
func AudioToWav(src, dst string) error {
22+
out, err := sh(fmt.Sprintf("ffmpeg -i %s -format s16le -ar 16000 -ac 1 -acodec pcm_s16le %s", src, dst))
23+
if err != nil {
24+
return fmt.Errorf("error: %w out: %s", err, out)
25+
}
26+
27+
return nil
28+
}
29+
30+
func Transcribe(modelpath, audiopath, language string) (string, error) {
31+
// Open samples
32+
fh, err := os.Open(audiopath)
33+
if err != nil {
34+
return "", err
35+
}
36+
defer fh.Close()
37+
38+
// Read samples
39+
d := wav.NewDecoder(fh)
40+
buf, err := d.FullPCMBuffer()
41+
if err != nil {
42+
return "", err
43+
}
44+
45+
data := buf.AsFloat32Buffer().Data
46+
47+
// Load the model
48+
model, err := whisper.New(modelpath)
49+
if err != nil {
50+
return "", err
51+
}
52+
defer model.Close()
53+
54+
// Process samples
55+
context, err := model.NewContext()
56+
if err != nil {
57+
return "", err
58+
59+
}
60+
61+
if language != "" {
62+
context.SetLanguage(language)
63+
}
64+
65+
if err := context.Process(data, nil); err != nil {
66+
return "", err
67+
}
68+
69+
text := ""
70+
for {
71+
segment, err := context.NextSegment()
72+
if err != nil {
73+
break
74+
}
75+
text += segment.Text
76+
}
77+
78+
return text, nil
79+
}

0 commit comments

Comments
 (0)