-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathmain.dart
More file actions
167 lines (147 loc) · 4.51 KB
/
main.dart
File metadata and controls
167 lines (147 loc) · 4.51 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
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:onnxruntime/onnxruntime.dart';
import 'model_type_test.dart';
import 'vad_iterator.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late String _version;
VadIterator? _vadIterator;
static const frameSize = 64;
static const sampleRate = 16000;
String? costTime;
@override
void initState() {
super.initState();
_version = OrtEnv.version;
_vadIterator = VadIterator(frameSize, sampleRate);
_vadIterator?.initModel();
}
@override
Widget build(BuildContext context) {
const textStyle = TextStyle(fontSize: 16);
return MaterialApp(
theme: ThemeData(useMaterial3: true),
home: Scaffold(
appBar: AppBar(
title: const Text('OnnxRuntime'),
centerTitle: true,
),
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'OnnxRuntime Version = $_version',
style: textStyle,
textAlign: TextAlign.center,
),
const SizedBox(
height: 50,
),
TextButton(
onPressed: _typeTest,
child: const Text('Mode Type Test'),
),
const SizedBox(
height: 50,
),
TextButton(
onPressed: () => _vad(false),
child: const Text('VAD'),
),
const SizedBox(
height: 50,
),
TextButton(
onPressed: () => _vad(true),
child: const Text('VAD Concurrency'),
),
const SizedBox(
height: 50,
),
Text(
costTime ?? '',
style: textStyle,
textAlign: TextAlign.center,
),
],
),
),
),
),
);
}
_typeTest() async {
setState(() => costTime = null);
final startTime = DateTime.now().millisecondsSinceEpoch;
List<OrtValue?>? outputs;
outputs = await ModelTypeTest.testBool();
print('out=${outputs[0]?.value}');
outputs.forEach((element) {
element?.release();
});
outputs = await ModelTypeTest.testFloat();
print('out=${outputs[0]?.value}');
outputs.forEach((element) {
element?.release();
});
outputs = await ModelTypeTest.testInt64();
print('out=${outputs[0]?.value}');
outputs.forEach((element) {
element?.release();
});
outputs = await ModelTypeTest.testString();
print('out=${outputs[0]?.value}');
outputs.forEach((element) {
element?.release();
});
final endTime = DateTime.now().millisecondsSinceEpoch;
String inferCost = 'Infer cost time=${endTime - startTime}ms';
print(inferCost);
setState(() => costTime = inferCost);
}
_vad(bool concurrent) async {
setState(() => costTime = null);
const windowByteCount = frameSize * 2 * sampleRate ~/ 1000;
final rawAssetFile = await rootBundle.load('assets/audio/vad_example.pcm');
final bytes = rawAssetFile.buffer.asUint8List();
var start = 0;
var end = start + windowByteCount;
List<int> frameBuffer;
final startTime = DateTime.now().millisecondsSinceEpoch;
while (end <= bytes.length) {
frameBuffer = bytes.sublist(start, end).toList();
final floatBuffer =
_transformBuffer(frameBuffer).map((e) => e / 32768).toList();
await _vadIterator?.predict(
Float32List.fromList(floatBuffer), concurrent);
start += windowByteCount;
end = start + windowByteCount;
}
_vadIterator?.reset();
final endTime = DateTime.now().millisecondsSinceEpoch;
String costTimeTemp = 'Vad cost time=${endTime - startTime}ms';
print(costTimeTemp);
setState(() => costTime = costTimeTemp);
}
Int16List _transformBuffer(List<int> buffer) {
final bytes = Uint8List.fromList(buffer);
return Int16List.view(bytes.buffer);
}
@override
void dispose() {
_vadIterator?.release();
super.dispose();
}
}