online_recognizer.dart
8.3 KB
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
// Copyright (c) 2024 Xiaomi Corporation
import 'dart:convert';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import './feature_config.dart';
import './online_stream.dart';
import './sherpa_onnx_bindings.dart';
class OnlineTransducerModelConfig {
const OnlineTransducerModelConfig({
this.encoder = '',
this.decoder = '',
this.joiner = '',
});
@override
String toString() {
return 'OnlineTransducerModelConfig(encoder: $encoder, decoder: $decoder, joiner: $joiner)';
}
final String encoder;
final String decoder;
final String joiner;
}
class OnlineParaformerModelConfig {
const OnlineParaformerModelConfig({this.encoder = '', this.decoder = ''});
@override
String toString() {
return 'OnlineParaformerModelConfig(encoder: $encoder, decoder: $decoder)';
}
final String encoder;
final String decoder;
}
class OnlineZipformer2CtcModelConfig {
const OnlineZipformer2CtcModelConfig({this.model = ''});
@override
String toString() {
return 'OnlineZipformer2CtcModelConfig(model: $model)';
}
final String model;
}
class OnlineModelConfig {
const OnlineModelConfig({
this.transducer = const OnlineTransducerModelConfig(),
this.paraformer = const OnlineParaformerModelConfig(),
this.zipformer2Ctc = const OnlineZipformer2CtcModelConfig(),
required this.tokens,
this.numThreads = 1,
this.provider = 'cpu',
this.debug = true,
this.modelType = '',
});
@override
String toString() {
return 'OnlineModelConfig(transducer: $transducer, paraformer: $paraformer, zipformer2Ctc: $zipformer2Ctc, tokens: $tokens, numThreads: $numThreads, provider: $provider, debug: $debug, modelType: $modelType)';
}
final OnlineTransducerModelConfig transducer;
final OnlineParaformerModelConfig paraformer;
final OnlineZipformer2CtcModelConfig zipformer2Ctc;
final String tokens;
final int numThreads;
final String provider;
final bool debug;
final String modelType;
}
class OnlineCtcFstDecoderConfig {
const OnlineCtcFstDecoderConfig({this.graph = '', this.maxActive = 3000});
@override
String toString() {
return 'OnlineCtcFstDecoderConfig(graph: $graph, maxActive: $maxActive)';
}
final String graph;
final int maxActive;
}
class OnlineRecognizerConfig {
const OnlineRecognizerConfig({
this.feat = const FeatureConfig(),
required this.model,
this.decodingMethod = 'greedy_search',
this.maxActivePaths = 4,
this.enableEndpoint = true,
this.rule1MinTrailingSilence = 2.4,
this.rule2MinTrailingSilence = 1.2,
this.rule3MinUtteranceLength = 20,
this.hotwordsFile = '',
this.hotwordsScore = 1.5,
this.ctcFstDecoderConfig = const OnlineCtcFstDecoderConfig(),
});
@override
String toString() {
return 'OnlineRecognizerConfig(feat: $feat, model: $model, decodingMethod: $decodingMethod, maxActivePaths: $maxActivePaths, enableEndpoint: $enableEndpoint, rule1MinTrailingSilence: $rule1MinTrailingSilence, rule2MinTrailingSilence: $rule2MinTrailingSilence, rule3MinUtteranceLength: $rule3MinUtteranceLength, hotwordsFile: $hotwordsFile, hotwordsScore: $hotwordsScore, ctcFstDecoderConfig: $ctcFstDecoderConfig)';
}
final FeatureConfig feat;
final OnlineModelConfig model;
final String decodingMethod;
final int maxActivePaths;
final bool enableEndpoint;
final double rule1MinTrailingSilence;
final double rule2MinTrailingSilence;
final double rule3MinUtteranceLength;
final String hotwordsFile;
final double hotwordsScore;
final OnlineCtcFstDecoderConfig ctcFstDecoderConfig;
}
class OnlineRecognizerResult {
OnlineRecognizerResult(
{required this.text, required this.tokens, required this.timestamps});
@override
String toString() {
return 'OnlineRecognizerResult(text: $text, tokens: $tokens, timestamps: $timestamps)';
}
final String text;
final List<String> tokens;
final List<double> timestamps;
}
class OnlineRecognizer {
OnlineRecognizer._({required this.ptr, required this.config});
/// The user is responsible to call the OnlineRecognizer.free()
/// method of the returned instance to avoid memory leak.
factory OnlineRecognizer(OnlineRecognizerConfig config) {
final c = calloc<SherpaOnnxOnlineRecognizerConfig>();
c.ref.feat.sampleRate = config.feat.sampleRate;
c.ref.feat.featureDim = config.feat.featureDim;
// transducer
c.ref.model.transducer.encoder =
config.model.transducer.encoder.toNativeUtf8();
c.ref.model.transducer.decoder =
config.model.transducer.decoder.toNativeUtf8();
c.ref.model.transducer.joiner =
config.model.transducer.joiner.toNativeUtf8();
// paraformer
c.ref.model.paraformer.encoder =
config.model.paraformer.encoder.toNativeUtf8();
c.ref.model.paraformer.decoder =
config.model.paraformer.decoder.toNativeUtf8();
// zipformer2Ctc
c.ref.model.zipformer2Ctc.model =
config.model.zipformer2Ctc.model.toNativeUtf8();
c.ref.model.tokens = config.model.tokens.toNativeUtf8();
c.ref.model.numThreads = config.model.numThreads;
c.ref.model.provider = config.model.provider.toNativeUtf8();
c.ref.model.debug = config.model.debug ? 1 : 0;
c.ref.model.modelType = config.model.modelType.toNativeUtf8();
c.ref.decodingMethod = config.decodingMethod.toNativeUtf8();
c.ref.maxActivePaths = config.maxActivePaths;
c.ref.enableEndpoint = config.enableEndpoint ? 1 : 0;
c.ref.rule1MinTrailingSilence = config.rule1MinTrailingSilence;
c.ref.rule2MinTrailingSilence = config.rule2MinTrailingSilence;
c.ref.rule3MinUtteranceLength = config.rule3MinUtteranceLength;
c.ref.hotwordsFile = config.hotwordsFile.toNativeUtf8();
c.ref.hotwordsScore = config.hotwordsScore;
c.ref.ctcFstDecoderConfig.graph =
config.ctcFstDecoderConfig.graph.toNativeUtf8();
c.ref.ctcFstDecoderConfig.maxActive = config.ctcFstDecoderConfig.maxActive;
final ptr = SherpaOnnxBindings.createOnlineRecognizer?.call(c) ?? nullptr;
calloc.free(c.ref.ctcFstDecoderConfig.graph);
calloc.free(c.ref.hotwordsFile);
calloc.free(c.ref.decodingMethod);
calloc.free(c.ref.model.modelType);
calloc.free(c.ref.model.provider);
calloc.free(c.ref.model.tokens);
calloc.free(c.ref.model.zipformer2Ctc.model);
calloc.free(c.ref.model.paraformer.encoder);
calloc.free(c.ref.model.paraformer.decoder);
calloc.free(c.ref.model.transducer.encoder);
calloc.free(c.ref.model.transducer.decoder);
calloc.free(c.ref.model.transducer.joiner);
calloc.free(c);
return OnlineRecognizer._(ptr: ptr, config: config);
}
void free() {
SherpaOnnxBindings.destroyOnlineRecognizer?.call(ptr);
ptr = nullptr;
}
/// The user has to invoke stream.free() on the returned instance
/// to avoid memory leak
OnlineStream createStream({String hotwords = ''}) {
if (hotwords == '') {
final p = SherpaOnnxBindings.createOnlineStream?.call(ptr) ?? nullptr;
return OnlineStream(ptr: p);
}
final utf8 = hotwords.toNativeUtf8();
final p =
SherpaOnnxBindings.createOnlineStreamWithHotwords?.call(ptr, utf8) ??
nullptr;
calloc.free(utf8);
return OnlineStream(ptr: p);
}
bool isReady(OnlineStream stream) {
int ready =
SherpaOnnxBindings.isOnlineStreamReady?.call(ptr, stream.ptr) ?? 0;
return ready == 1;
}
OnlineRecognizerResult getResult(OnlineStream stream) {
final json =
SherpaOnnxBindings.getOnlineStreamResultAsJson?.call(ptr, stream.ptr) ??
nullptr;
if (json == nullptr) {
return OnlineRecognizerResult(text: '', tokens: [], timestamps: []);
}
final parsedJson = jsonDecode(json.toDartString());
SherpaOnnxBindings.destroyOnlineStreamResultJson?.call(json);
return OnlineRecognizerResult(
text: parsedJson['text'],
tokens: List<String>.from(parsedJson['tokens']),
timestamps: List<double>.from(parsedJson['timestamps']));
}
void reset(OnlineStream stream) {
SherpaOnnxBindings.reset?.call(ptr, stream.ptr);
}
void decode(OnlineStream stream) {
SherpaOnnxBindings.decodeOnlineStream?.call(ptr, stream.ptr);
}
bool isEndpoint(OnlineStream stream) {
int yes = SherpaOnnxBindings.isEndpoint?.call(ptr, stream.ptr) ?? 0;
return yes == 1;
}
Pointer<SherpaOnnxOnlineRecognizer> ptr;
OnlineRecognizerConfig config;
}