spoken_language_identification.dart
5.6 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
// Copyright (c) 2024 Xiaomi Corporation
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import './offline_stream.dart';
import './sherpa_onnx_bindings.dart';
import './utils.dart';
class SpokenLanguageIdentificationWhisperConfig {
const SpokenLanguageIdentificationWhisperConfig({
this.encoder = '',
this.decoder = '',
this.tailPaddings = 0,
});
factory SpokenLanguageIdentificationWhisperConfig.fromJson(
Map<String, dynamic> json) {
return SpokenLanguageIdentificationWhisperConfig(
encoder: json['encoder'] as String? ?? '',
decoder: json['decoder'] as String? ?? '',
tailPaddings: json['tailPaddings'] as int? ?? 0,
);
}
@override
String toString() {
return 'SpokenLanguageIdentificationWhisperConfig(encoder: $encoder, decoder: $decoder, tailPaddings: $tailPaddings)';
}
Map<String, dynamic> toJson() => {
'encoder': encoder,
'decoder': decoder,
'tailPaddings': tailPaddings,
};
final String encoder;
final String decoder;
final int tailPaddings;
}
class SpokenLanguageIdentificationConfig {
const SpokenLanguageIdentificationConfig({
this.whisper = const SpokenLanguageIdentificationWhisperConfig(),
this.numThreads = 1,
this.debug = false,
this.provider = 'cpu',
});
factory SpokenLanguageIdentificationConfig.fromJson(
Map<String, dynamic> json) {
return SpokenLanguageIdentificationConfig(
whisper: json['whisper'] != null
? SpokenLanguageIdentificationWhisperConfig.fromJson(
json['whisper'] as Map<String, dynamic>)
: const SpokenLanguageIdentificationWhisperConfig(),
numThreads: json['numThreads'] as int? ?? 1,
debug: json['debug'] as bool? ?? false,
provider: json['provider'] as String? ?? 'cpu',
);
}
@override
String toString() {
return 'SpokenLanguageIdentificationConfig(whisper: $whisper, numThreads: $numThreads, debug: $debug, provider: $provider)';
}
Map<String, dynamic> toJson() => {
'whisper': whisper.toJson(),
'numThreads': numThreads,
'debug': debug,
'provider': provider,
};
final SpokenLanguageIdentificationWhisperConfig whisper;
final int numThreads;
final bool debug;
final String provider;
}
class SpokenLanguageIdentificationResult {
const SpokenLanguageIdentificationResult({
required this.lang,
});
factory SpokenLanguageIdentificationResult.fromJson(
Map<String, dynamic> json) {
return SpokenLanguageIdentificationResult(
lang: json['lang'] as String? ?? '',
);
}
@override
String toString() {
return 'SpokenLanguageIdentificationResult(lang: $lang)';
}
Map<String, dynamic> toJson() => {
'lang': lang,
};
final String lang;
}
class SpokenLanguageIdentification {
SpokenLanguageIdentification.fromPtr(
{required this.ptr, required this.config});
SpokenLanguageIdentification._({required this.ptr, required this.config});
void free() {
SherpaOnnxBindings.sherpaOnnxDestroySpokenLanguageIdentification?.call(ptr);
ptr = nullptr;
}
/// The user is responsible to call the SpokenLanguageIdentification.free()
/// method of the returned instance to avoid memory leak.
factory SpokenLanguageIdentification(
SpokenLanguageIdentificationConfig config) {
final c = convertConfig(config);
if (SherpaOnnxBindings.sherpaOnnxCreateSpokenLanguageIdentification ==
null) {
freeConfig(c);
throw Exception("Please initialize sherpa-onnx first");
}
final ptr = SherpaOnnxBindings.sherpaOnnxCreateSpokenLanguageIdentification
?.call(c) ??
nullptr;
if (ptr == nullptr) {
freeConfig(c);
throw Exception(
"Failed to create spoken language identification. Please check your config");
}
freeConfig(c);
return SpokenLanguageIdentification._(ptr: ptr, config: config);
}
static Pointer<SherpaOnnxSpokenLanguageIdentificationConfig> convertConfig(
SpokenLanguageIdentificationConfig config) {
final c = calloc<SherpaOnnxSpokenLanguageIdentificationConfig>();
c.ref.whisper.encoder = config.whisper.encoder.toNativeUtf8();
c.ref.whisper.decoder = config.whisper.decoder.toNativeUtf8();
c.ref.whisper.tailPaddings = config.whisper.tailPaddings;
c.ref.numThreads = config.numThreads;
c.ref.debug = config.debug ? 1 : 0;
c.ref.provider = config.provider.toNativeUtf8();
return c;
}
static void freeConfig(
Pointer<SherpaOnnxSpokenLanguageIdentificationConfig> c) {
malloc.free(c.ref.whisper.encoder);
malloc.free(c.ref.whisper.decoder);
malloc.free(c.ref.provider);
malloc.free(c);
}
/// The user has to invoke stream.free() on the returned instance
/// to avoid memory leak
OfflineStream createStream() {
final p = SherpaOnnxBindings
.sherpaOnnxSpokenLanguageIdentificationCreateOfflineStream
?.call(ptr) ??
nullptr;
return OfflineStream(ptr: p);
}
SpokenLanguageIdentificationResult compute(OfflineStream stream) {
final result = SherpaOnnxBindings
.sherpaOnnxSpokenLanguageIdentificationCompute
?.call(ptr, stream.ptr) ??
nullptr;
if (result == nullptr) {
return const SpokenLanguageIdentificationResult(lang: '');
}
final lang = toDartString(result.ref.lang);
SherpaOnnxBindings.sherpaOnnxDestroySpokenLanguageIdentificationResult
?.call(result);
return SpokenLanguageIdentificationResult(lang: lang);
}
Pointer<SherpaOnnxSpokenLanguageIdentification> ptr;
SpokenLanguageIdentificationConfig config;
}