/
cheetah_native.go
250 lines (192 loc) · 6.29 KB
/
cheetah_native.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
// 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
/*
#cgo linux LDFLAGS: -ldl
#cgo darwin LDFLAGS: -ldl
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#else
#include <dlfcn.h>
#endif
static void *open_dl(const char *dl_path) {
#if defined(_WIN32) || defined(_WIN64)
return LoadLibrary((LPCSTR) dl_path);
#else
return dlopen(dl_path, RTLD_NOW);
#endif
}
static void *load_symbol(void *handle, const char *symbol) {
#if defined(_WIN32) || defined(_WIN64)
return GetProcAddress((HMODULE) handle, symbol);
#else
return dlsym(handle, symbol);
#endif
}
typedef int32_t (*pv_cheetah_sample_rate_func)();
int32_t pv_cheetah_sample_rate_wrapper(void *f) {
return ((pv_cheetah_sample_rate_func) f)();
}
typedef int32_t (*pv_cheetah_frame_length_func)();
int32_t pv_cheetah_frame_length_wrapper(void* f) {
return ((pv_cheetah_frame_length_func) f)();
}
typedef char* (*pv_cheetah_version_func)();
char* pv_cheetah_version_wrapper(void* f) {
return ((pv_cheetah_version_func) f)();
}
typedef int32_t (*pv_cheetah_init_func)(
const char *access_key,
const char *model_path,
float endpoint_duration_sec,
bool enable_automatic_punctuation,
void **object);
int32_t pv_cheetah_init_wrapper(
void *f,
const char *access_key,
const char *model_path,
float endpoint_duration_sec,
bool enable_automatic_punctuation,
void **object) {
return ((pv_cheetah_init_func) f)(
access_key,
model_path,
endpoint_duration_sec,
enable_automatic_punctuation,
object);
}
typedef int32_t (*pv_cheetah_process_func)(
void *object,
const int16_t *pcm,
char **transcript,
bool *is_endpoint);
int32_t pv_cheetah_process_wrapper(
void *f,
void *object,
const int16_t *pcm,
char **transcript,
bool *is_endpoint) {
return ((pv_cheetah_process_func) f)(
object,
pcm,
transcript,
is_endpoint);
}
typedef int32_t (*pv_cheetah_flush_func)(
void *object,
char **transcript);
int32_t pv_cheetah_flush_wrapper(
void *f,
void *object,
char **transcript) {
return ((pv_cheetah_flush_func) f)(
object,
transcript);
}
typedef void (*pv_cheetah_delete_func)(void *);
void pv_cheetah_delete_wrapper(void *f, void *object) {
return ((pv_cheetah_delete_func) f)(object);
}
*/
import "C"
import (
"unsafe"
)
type nativeCheetahInterface interface {
nativeInit(*Cheetah)
nativeProcess(*Cheetah, []int)
nativeFlush(*Cheetah, string)
nativeDelete(*Cheetah)
nativeSampleRate()
nativeVersion()
}
type nativeCheetahType struct {
libraryPath unsafe.Pointer
pv_cheetah_init_ptr unsafe.Pointer
pv_cheetah_process_ptr unsafe.Pointer
pv_cheetah_flush_ptr unsafe.Pointer
pv_cheetah_delete_ptr unsafe.Pointer
pv_cheetah_version_ptr unsafe.Pointer
pv_cheetah_frame_length_ptr unsafe.Pointer
pv_sample_rate_ptr unsafe.Pointer
}
func (nc *nativeCheetahType) nativeInit(cheetah *Cheetah) (status PvStatus) {
var (
accessKeyC = C.CString(cheetah.AccessKey)
modelPathC = C.CString(cheetah.ModelPath)
libraryPathC = C.CString(cheetah.LibraryPath)
endpointDurationC = C.float(cheetah.EndpointDuration)
enableAutomaticPunctuationC = C.bool(cheetah.EnableAutomaticPunctuation)
)
defer C.free(unsafe.Pointer(accessKeyC))
defer C.free(unsafe.Pointer(modelPathC))
defer C.free(unsafe.Pointer(libraryPathC))
nc.libraryPath = C.open_dl(libraryPathC)
nc.pv_cheetah_init_ptr = C.load_symbol(nc.libraryPath, C.CString("pv_cheetah_init"))
nc.pv_cheetah_process_ptr = C.load_symbol(nc.libraryPath, C.CString("pv_cheetah_process"))
nc.pv_cheetah_flush_ptr = C.load_symbol(nc.libraryPath, C.CString("pv_cheetah_flush"))
nc.pv_cheetah_delete_ptr = C.load_symbol(nc.libraryPath, C.CString("pv_cheetah_delete"))
nc.pv_cheetah_version_ptr = C.load_symbol(nc.libraryPath, C.CString("pv_cheetah_version"))
nc.pv_cheetah_frame_length_ptr = C.load_symbol(nc.libraryPath, C.CString("pv_cheetah_frame_length"))
nc.pv_sample_rate_ptr = C.load_symbol(nc.libraryPath, C.CString("pv_sample_rate"))
var ret = C.pv_cheetah_init_wrapper(
nc.pv_cheetah_init_ptr,
accessKeyC,
modelPathC,
endpointDurationC,
enableAutomaticPunctuationC,
&cheetah.handle)
return PvStatus(ret)
}
func (nc *nativeCheetahType) nativeDelete(cheetah *Cheetah) {
C.pv_cheetah_delete_wrapper(
nc.pv_cheetah_delete_ptr,
cheetah.handle)
}
func (nc *nativeCheetahType) nativeProcess(cheetah *Cheetah, pcm []int16) (status PvStatus, transcript string, isEndpoint bool) {
var transcriptPtr unsafe.Pointer
var ret = C.pv_cheetah_process_wrapper(nc.pv_cheetah_process_ptr,
cheetah.handle,
(*C.int16_t)(unsafe.Pointer(&pcm[0])),
(**C.char)(unsafe.Pointer(&transcriptPtr)),
(*C.bool)(unsafe.Pointer(&isEndpoint)))
if (PvStatus(ret) != SUCCESS) {
return PvStatus(ret), "", false
}
transcript = C.GoString((*C.char)(transcriptPtr))
C.free(transcriptPtr)
return PvStatus(ret), transcript, isEndpoint
}
func (nc *nativeCheetahType) nativeFlush(cheetah *Cheetah) (status PvStatus, transcript string) {
var transcriptPtr unsafe.Pointer
var ret = C.pv_cheetah_flush_wrapper(nc.pv_cheetah_flush_ptr,
cheetah.handle,
(**C.char)(unsafe.Pointer(&transcriptPtr)))
if (PvStatus(ret) != SUCCESS) {
return PvStatus(ret), ""
}
transcript = C.GoString((*C.char)(transcriptPtr))
C.free(transcriptPtr)
return PvStatus(ret), transcript
}
func (nc nativeCheetahType) nativeSampleRate() (sampleRate int) {
return int(C.pv_cheetah_sample_rate_wrapper(nc.pv_sample_rate_ptr))
}
func (nc nativeCheetahType) nativeFrameLength() (frameLength int) {
return int(C.pv_cheetah_frame_length_wrapper(nc.pv_cheetah_frame_length_ptr))
}
func (nc nativeCheetahType) nativeVersion() (version string) {
return C.GoString(C.pv_cheetah_version_wrapper(nc.pv_cheetah_version_ptr))
}