/
cheetah.go
413 lines (352 loc) · 10.6 KB
/
cheetah.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2022 Picovoice Inc.
//
// You may not use this file except in compliance with the license. A copy of the license is
// located in the "LICENSE" file accompanying this source.
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing permissions and
// limitations under the License.
//
// Go binding for Cheetah Speech-to-Text engine.
package cheetah
import (
"C"
"crypto/sha256"
"embed"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
import "unsafe"
//go:embed embedded
var embeddedFS embed.FS
// PvStatus type
type PvStatus int
// Possible status return codes from the Cheetah library
const (
SUCCESS PvStatus = 0
OUT_OF_MEMORY PvStatus = 1
IO_ERROR PvStatus = 2
INVALID_ARGUMENT PvStatus = 3
STOP_ITERATION PvStatus = 4
KEY_ERROR PvStatus = 5
INVALID_STATE PvStatus = 6
RUNTIME_ERROR PvStatus = 7
ACTIVATION_ERROR PvStatus = 8
ACTIVATION_LIMIT_REACHED PvStatus = 9
ACTIVATION_THROTTLED PvStatus = 10
ACTIVATION_REFUSED PvStatus = 11
)
type CheetahError struct {
StatusCode PvStatus
Message string
MessageStack []string
}
func (e *CheetahError) Error() string {
var message strings.Builder
message.WriteString(fmt.Sprintf("%s: %s", pvStatusToString(e.StatusCode), e.Message))
if len(e.MessageStack) > 0 {
message.WriteString(":")
}
for i, value := range e.MessageStack {
message.WriteString(fmt.Sprintf("\n [%d] %s", i, value))
}
return message.String()
}
// Cheetah struct
type Cheetah struct {
// handle for cheetah instance in C
handle unsafe.Pointer
// AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).
AccessKey string
// Absolute path to the file containing model parameters.
ModelPath string
// Absolute path to Cheetah's dynamic library.
LibraryPath string
// Duration of endpoint in seconds. A speech endpoint is detected when there is a
// chunk of audio (with a duration specified herein) after an utterance without any speech in it. Set to 0
// to disable endpoint detection.
EndpointDuration float32
// Flag to enable automatic punctuation insertion.
EnableAutomaticPunctuation bool
}
// private vars
var (
osName, cpu = getOS()
extractionDir = filepath.Join(os.TempDir(), "cheetah")
defaultModelFile = extractDefaultModel()
defaultLibPath = extractLib()
nativeCheetah = nativeCheetahType{}
)
var (
// FrameLength Number of audio samples per frame.
FrameLength int
// SampleRate Audio sample rate accepted by Picovoice.
SampleRate int
// Version Cheetah version
Version string
)
// NewCheetah returns a Cheetah struct with default parameters
func NewCheetah(accessKey string) Cheetah {
return Cheetah{
AccessKey: accessKey,
ModelPath: defaultModelFile,
LibraryPath: defaultLibPath,
EndpointDuration: 1.0,
EnableAutomaticPunctuation: false,
}
}
// Init function for Cheetah. Must be called before attempting process
func (cheetah *Cheetah) Init() error {
if cheetah.AccessKey == "" {
return &CheetahError{
StatusCode: INVALID_ARGUMENT,
Message: "No AccessKey provided to Cheetah"}
}
if cheetah.ModelPath == "" {
cheetah.ModelPath = defaultModelFile
}
if cheetah.LibraryPath == "" {
cheetah.LibraryPath = defaultLibPath
}
if _, err := os.Stat(cheetah.ModelPath); os.IsNotExist(err) {
return &CheetahError{
StatusCode: INVALID_ARGUMENT,
Message: fmt.Sprintf("Specified model file could not be found at %s", cheetah.ModelPath)}
}
if _, err := os.Stat(cheetah.LibraryPath); os.IsNotExist(err) {
return &CheetahError{
StatusCode: INVALID_ARGUMENT,
Message: fmt.Sprintf("Specified library file could not be found at %s", cheetah.LibraryPath)}
}
if cheetah.EndpointDuration < 0 {
return &CheetahError{
StatusCode: INVALID_ARGUMENT,
Message: "Endpoint duration must be non-negative"}
}
ret := nativeCheetah.nativeInit(cheetah)
if PvStatus(ret) != SUCCESS {
errorStatus, messageStack := nativeCheetah.nativeGetErrorStack()
if errorStatus != SUCCESS {
return &CheetahError{
StatusCode: errorStatus,
Message: "Unable to get Cheetah error state",
}
}
return &CheetahError{
StatusCode: ret,
Message: "Cheetah init failed",
MessageStack: messageStack,
}
}
FrameLength = nativeCheetah.nativeFrameLength()
SampleRate = nativeCheetah.nativeSampleRate()
Version = nativeCheetah.nativeVersion()
return nil
}
// Delete releases resources acquired by Cheetah.
func (cheetah *Cheetah) Delete() error {
if cheetah.handle == nil {
return &CheetahError{
StatusCode: INVALID_STATE,
Message: "Cheetah has not been initialized or has already been deleted"}
}
nativeCheetah.nativeDelete(cheetah)
return nil
}
// Processes a frame of audio and returns newly-transcribed text and a flag indicating if an endpoint has been
// detected. Upon detection of an endpoint, the client may invoke `.Flush()` to retrieve any remaining transcription.
// Returns Any newly-transcribed speech (if none is available then an empty string is returned) and a
// flag indicating if an endpoint has been detected.
func (cheetah *Cheetah) Process(pcm []int16) (string, bool, error) {
if cheetah.handle == nil {
return "", false, &CheetahError{
StatusCode: INVALID_STATE,
Message: "Cheetah has not been initialized or has already been deleted"}
}
if len(pcm) != FrameLength {
return "", false, &CheetahError{
StatusCode: INVALID_ARGUMENT,
Message: fmt.Sprintf("Input data frame size (%d) does not match required size of %d", len(pcm), FrameLength)}
}
ret, transcript, isEndpoint := nativeCheetah.nativeProcess(cheetah, pcm)
if PvStatus(ret) != SUCCESS {
errorStatus, messageStack := nativeCheetah.nativeGetErrorStack()
if errorStatus != SUCCESS {
return "", false, &CheetahError{
StatusCode: errorStatus,
Message: "Unable to get Cheetah error state",
}
}
return "", false, &CheetahError{
StatusCode: ret,
Message: "Cheetah process failed",
MessageStack: messageStack,
}
}
return transcript, isEndpoint, nil
}
// Flush marks the end of the audio stream, flushes internal state of the object, and returns any remaining transcribed text.
// Return any remaining transcribed text. If none is available then an empty string is returned.
func (cheetah *Cheetah) Flush() (string, error) {
if cheetah.handle == nil {
return "", &CheetahError{
StatusCode: INVALID_STATE,
Message: "Cheetah has not been initialized or has already been deleted"}
}
ret, transcript := nativeCheetah.nativeFlush(cheetah)
if PvStatus(ret) != SUCCESS {
errorStatus, messageStack := nativeCheetah.nativeGetErrorStack()
if errorStatus != SUCCESS {
return "", &CheetahError{
StatusCode: errorStatus,
Message: "Unable to get Cheetah error state",
}
}
return "", &CheetahError{
StatusCode: ret,
Message: "Cheetah flush failed",
MessageStack: messageStack,
}
}
return transcript, nil
}
func pvStatusToString(status PvStatus) string {
switch status {
case SUCCESS:
return "SUCCESS"
case OUT_OF_MEMORY:
return "OUT_OF_MEMORY"
case IO_ERROR:
return "IO_ERROR"
case INVALID_ARGUMENT:
return "INVALID_ARGUMENT"
case STOP_ITERATION:
return "STOP_ITERATION"
case KEY_ERROR:
return "KEY_ERROR"
case INVALID_STATE:
return "INVALID_STATE"
case RUNTIME_ERROR:
return "RUNTIME_ERROR"
case ACTIVATION_ERROR:
return "ACTIVATION_ERROR"
case ACTIVATION_LIMIT_REACHED:
return "ACTIVATION_LIMIT_REACHED"
case ACTIVATION_THROTTLED:
return "ACTIVATION_THROTTLED"
case ACTIVATION_REFUSED:
return "ACTIVATION_REFUSED"
default:
return fmt.Sprintf("Unknown error code: %d", status)
}
}
func getOS() (string, string) {
switch os := runtime.GOOS; os {
case "darwin":
return "mac", getMacArch()
case "linux":
osName, cpu := getLinuxDetails()
return osName, cpu
case "windows":
return "windows", "amd64"
default:
log.Fatalf("%s is not a supported OS", os)
return "", ""
}
}
func getMacArch() string {
if runtime.GOARCH == "arm64" {
return "arm64"
} else {
return "x86_64"
}
}
func getLinuxDetails() (string, string) {
var archInfo = ""
if runtime.GOARCH == "amd64" {
return "linux", "x86_64"
} else if runtime.GOARCH == "arm64" {
archInfo = "-aarch64"
}
cmd := exec.Command("cat", "/proc/cpuinfo")
cpuInfo, err := cmd.Output()
if err != nil {
log.Fatalf("Failed to get CPU details: %s", err.Error())
}
var cpuPart = ""
for _, line := range strings.Split(string(cpuInfo), "\n") {
if strings.Contains(line, "CPU part") {
split := strings.Split(line, " ")
cpuPart = strings.ToLower(split[len(split)-1])
break
}
}
switch cpuPart {
case "0xd03":
return "raspberry-pi", "cortex-a53" + archInfo
case "0xd07":
return "jetson", "cortex-a57" + archInfo
case "0xd08":
return "raspberry-pi", "cortex-a72" + archInfo
case "0xd0b":
return "raspberry-pi", "cortex-a76" + archInfo
default:
log.Fatalf("Unsupported CPU:\n%s", cpuPart)
return "", ""
}
}
func extractDefaultModel() string {
modelPath := "embedded/lib/common/cheetah_params.pv"
return extractFile(modelPath, extractionDir)
}
func extractLib() string {
var libPath string
switch os := runtime.GOOS; os {
case "darwin":
libPath = fmt.Sprintf("embedded/lib/%s/%s/libpv_cheetah.dylib", osName, cpu)
case "linux":
if cpu == "" {
libPath = fmt.Sprintf("embedded/lib/%s/libpv_cheetah.so", osName)
} else {
libPath = fmt.Sprintf("embedded/lib/%s/%s/libpv_cheetah.so", osName, cpu)
}
case "windows":
libPath = fmt.Sprintf("embedded/lib/%s/amd64/libpv_cheetah.dll", osName)
default:
log.Fatalf("%s is not a supported OS", os)
}
return extractFile(libPath, extractionDir)
}
func extractFile(srcFile string, dstDir string) string {
bytes, readErr := embeddedFS.ReadFile(srcFile)
if readErr != nil {
log.Fatalf("%v", readErr)
}
srcHash := sha256sumBytes(bytes)
hashedDstDir := filepath.Join(dstDir, srcHash)
extractedFilepath := filepath.Join(hashedDstDir, srcFile)
if _, err := os.Stat(extractedFilepath); errors.Is(err, os.ErrNotExist) {
mkErr := os.MkdirAll(filepath.Dir(extractedFilepath), 0764)
if mkErr != nil {
log.Fatalf("%v", mkErr)
}
writeErr := ioutil.WriteFile(extractedFilepath, bytes, 0764)
if writeErr != nil {
log.Fatalf("%v", writeErr)
}
}
return extractedFilepath
}
func sha256sumBytes(bytes []byte) string {
sum := sha256.Sum256(bytes)
return hex.EncodeToString(sum[:])
}