-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.dart
355 lines (321 loc) · 10.8 KB
/
main.dart
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
//
// Copyright 2022-2023 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.
//
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:leopard_demo/mic_recorder.dart';
import 'package:leopard_flutter/leopard.dart';
import 'package:leopard_flutter/leopard_error.dart';
import 'package:leopard_flutter/leopard_transcript.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final String accessKey =
'{YOUR_ACCESS_KEY_HERE}'; // AccessKey obtained from Picovoice Console (https://console.picovoice.ai/)
final int maxRecordingLengthSecs = 120;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
bool isError = false;
String errorMessage = "";
bool isButtonDisabled = false;
bool isRecording = false;
bool isProcessing = false;
double recordedLength = 0.0;
String statusAreaText = "";
String transcriptText = "";
List<LeopardWord> words = [];
MicRecorder? _micRecorder;
Leopard? _leopard;
@override
void initState() {
super.initState();
setState(() {
isButtonDisabled = true;
recordedLength = 0.0;
statusAreaText = "Initializing Leopard...";
transcriptText = "";
words = [];
});
initLeopard();
}
Future<void> initLeopard() async {
String language = "";
try {
final paramsString =
await DefaultAssetBundle.of(context).loadString('assets/params.json');
final params = json.decode(paramsString);
language = params["language"];
} catch (_) {
errorCallback(LeopardException(
"Could not find `params.json`. Ensure 'prepare_demo.dart' script was run before launching the demo."));
return;
}
final String suffix = language != "en" ? "_$language" : "";
final String modelPath = "assets/models/leopard_params$suffix.pv";
try {
_leopard = await Leopard.create(accessKey,
modelPath,
enableAutomaticPunctuation: true,
enableDiarization: true);
_micRecorder = await MicRecorder.create(
_leopard!.sampleRate, recordedCallback, errorCallback);
setState(() {
statusAreaText =
"Press START to start recording some audio to transcribe";
isButtonDisabled = false;
});
} on LeopardActivationException {
errorCallback(LeopardActivationException("AccessKey activation error."));
} on LeopardActivationLimitException {
errorCallback(LeopardActivationLimitException(
"AccessKey reached its device limit."));
} on LeopardActivationRefusedException {
errorCallback(LeopardActivationRefusedException("AccessKey refused."));
} on LeopardActivationThrottledException {
errorCallback(
LeopardActivationThrottledException("AccessKey has been throttled."));
} on LeopardException catch (ex) {
errorCallback(ex);
}
}
Future<void> recordedCallback(double length) async {
if (length < maxRecordingLengthSecs) {
setState(() {
recordedLength = length;
statusAreaText =
"Recording : ${length.toStringAsFixed(1)} / $maxRecordingLengthSecs seconds";
});
} else {
setState(() {
isButtonDisabled = true;
recordedLength = length;
statusAreaText = "Transcribing, please wait...";
});
await _stopRecording();
}
}
void errorCallback(LeopardException error) {
setState(() {
isError = true;
errorMessage = error.message!;
});
}
Future<void> _startRecording() async {
if (isRecording || _micRecorder == null) {
return;
}
try {
await _micRecorder!.startRecord();
setState(() {
isRecording = true;
});
} on LeopardException catch (ex) {
errorCallback(ex);
}
}
Future<void> _stopRecording() async {
if (!isRecording || _micRecorder == null) {
return;
}
try {
File recordedFile = await _micRecorder!.stopRecord();
setState(() {
statusAreaText = "Transcribing, please wait...";
isRecording = false;
isButtonDisabled = true;
});
_processAudio(recordedFile);
} on LeopardException catch (ex) {
errorCallback(ex);
}
}
Future<void> _processAudio(File recordedFile) async {
if (_leopard == null) {
return;
}
Stopwatch stopwatch = Stopwatch()..start();
LeopardTranscript? result = await _leopard?.processFile(recordedFile.path);
Duration elapsed = stopwatch.elapsed;
String audioLength = recordedLength.toStringAsFixed(1);
String transcriptionTime =
(elapsed.inMilliseconds / 1000).toStringAsFixed(1);
setState(() {
statusAreaText =
"Transcribed $audioLength(s) of audio in $transcriptionTime(s)";
transcriptText = result?.transcript ?? "";
words = result?.words ?? [];
isButtonDisabled = false;
});
}
Color picoBlue = Color.fromRGBO(55, 125, 255, 1);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: const Text('Leopard Demo'),
backgroundColor: picoBlue,
),
body: Container(
color: Colors.white,
child: Column(
children: [
buildLeopardTextArea(context),
buildLeopardWordArea(context),
buildErrorMessage(context),
buildLeopardStatusArea(context),
buildStartButton(context),
footer
],
)),
),
);
}
buildStartButton(BuildContext context) {
final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
backgroundColor: picoBlue,
shape: BeveledRectangleBorder(),
textStyle: TextStyle(color: Colors.white));
return Expanded(
flex: 1,
child: Container(
child: SizedBox(
width: 130,
height: 65,
child: ElevatedButton(
style: buttonStyle,
onPressed: (isButtonDisabled || isError)
? null
: isRecording
? _stopRecording
: _startRecording,
child: Text(isRecording ? "Stop" : "Start",
style: TextStyle(fontSize: 30)),
))),
);
}
buildLeopardTextArea(BuildContext context) {
return Expanded(
flex: 6,
child: Container(
alignment: Alignment.topCenter,
color: Color(0xff25187e),
margin: EdgeInsets.all(10),
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
padding: EdgeInsets.all(10),
physics: RangeMaintainingScrollPhysics(),
child: Align(
alignment: Alignment.topLeft,
child: Text(
transcriptText,
textAlign: TextAlign.left,
style: TextStyle(color: Colors.white, fontSize: 20),
)))));
}
buildLeopardWordArea(BuildContext context) {
List<TableRow> tableRows = words.map<TableRow>((leopardWord) {
return TableRow(children: [
Column(children: [
Text(leopardWord.word, style: TextStyle(color: Colors.white))
]),
Column(children: [
Text('${leopardWord.startSec.toStringAsFixed(2)}s',
style: TextStyle(color: Colors.white))
]),
Column(children: [
Text('${leopardWord.endSec.toStringAsFixed(2)}s',
style: TextStyle(color: Colors.white))
]),
Column(children: [
Text('${(leopardWord.confidence * 100).toStringAsFixed(0)}%',
style: TextStyle(color: Colors.white))
]),
Column(children: [
Text('${leopardWord.speakerTag}',
style: TextStyle(color: Colors.white))
]),
]);
}).toList();
return Expanded(
flex: 4,
child: Container(
color: Color(0xff25187e),
alignment: Alignment.topCenter,
margin: EdgeInsets.all(10),
child: Column(children: [
Container(
color: Colors.white,
padding: EdgeInsets.only(bottom: 5, top: 5),
child: Table(children: [
TableRow(children: [
Column(children: [Text("Word")]),
Column(children: [Text("Start")]),
Column(children: [Text("End")]),
Column(children: [Text("Confidence")]),
Column(children: [Text("Tag")]),
])
])),
Flexible(
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
padding: EdgeInsets.all(10),
physics: RangeMaintainingScrollPhysics(),
child: Table(children: tableRows)))
]),
));
}
buildLeopardStatusArea(BuildContext context) {
return Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.only(bottom: 20),
child: Text(
statusAreaText,
style: TextStyle(color: Colors.black),
)));
}
buildErrorMessage(BuildContext context) {
return Expanded(
flex: isError ? 4 : 0,
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.only(left: 20, right: 20),
padding: EdgeInsets.all(5),
decoration: !isError
? null
: BoxDecoration(
color: Colors.red, borderRadius: BorderRadius.circular(5)),
child: !isError
? null
: Text(
errorMessage,
style: TextStyle(color: Colors.white, fontSize: 18),
)));
}
Widget footer = Expanded(
flex: 1,
child: Container(
alignment: Alignment.bottomCenter,
padding: EdgeInsets.only(bottom: 20),
margin: EdgeInsets.only(top: 10),
child: const Text(
"Made in Vancouver, Canada by Picovoice",
style: TextStyle(color: Color(0xff666666)),
)));
}