-
Notifications
You must be signed in to change notification settings - Fork 147
/
Coqui.go
executable file
·81 lines (75 loc) · 2.42 KB
/
Coqui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package wirepod_coqui
import (
"fmt"
"log"
"os"
"time"
"github.com/asticode/go-asticoqui"
"github.com/kercre123/wire-pod/chipper/pkg/logger"
sr "github.com/kercre123/wire-pod/chipper/pkg/wirepod/speechrequest"
)
var Name string = "coqui"
// Init should be defined as `func() error`
func Init() error {
logger.Println("Running a Coqui test...")
coquiInstance, _ := asticoqui.New("../stt/model.tflite")
if _, err := os.Stat("../stt/large_vocabulary.scorer"); err == nil {
coquiInstance.EnableExternalScorer("../stt/large_vocabulary.scorer")
} else if _, err := os.Stat("../stt/model.scorer"); err == nil {
coquiInstance.EnableExternalScorer("../stt/model.scorer")
} else {
logger.Println("No .scorer file found.")
}
coquiStream, err := coquiInstance.NewStream()
if err != nil {
log.Fatal(err)
}
pcmBytes, _ := os.ReadFile("./stttest.pcm")
var micData [][]byte
cTime := time.Now()
micData = sr.SplitVAD(pcmBytes)
for _, sample := range micData {
coquiStream.FeedAudioContent(sr.BytesToSamples(sample))
}
res, err := coquiStream.Finish()
tTime := time.Now().Sub(cTime)
if err != nil {
log.Fatal("Failed testing speech to text: ", err)
}
logger.Println("Text:", res)
if tTime.Seconds() > 3 {
logger.Println("Coqui test took a while, performance may be degraded. (" + fmt.Sprint(tTime) + ")")
}
logger.Println("Coqui test successful! (Took " + fmt.Sprint(tTime) + ")")
return nil
}
// STT funcs should be defined as func(sr.SpeechRequest) (string, error)
func STT(req sr.SpeechRequest) (string, error) {
logger.Println("(Bot " + req.Device + ", Coqui) Processing...")
speechIsDone := false
coquiInstance, _ := asticoqui.New("../stt/model.tflite")
if _, err := os.Stat("../stt/large_vocabulary.scorer"); err == nil {
coquiInstance.EnableExternalScorer("../stt/large_vocabulary.scorer")
} else if _, err := os.Stat("../stt/model.scorer"); err == nil {
coquiInstance.EnableExternalScorer("../stt/model.scorer")
} else {
logger.Println("No .scorer file found.")
}
coquiStream, _ := coquiInstance.NewStream()
for {
var chunk []byte
var err error
chunk, err = req.GetNextStreamChunk()
if err != nil {
return "", err
}
coquiStream.FeedAudioContent(sr.BytesToSamples(chunk))
speechIsDone, _ = req.DetectEndOfSpeech()
if speechIsDone {
break
}
}
transcribedText, _ := coquiStream.Finish()
logger.Println("Bot " + req.Device + " Transcribed text: " + transcribedText)
return transcribedText, nil
}