/
main.dart
212 lines (189 loc) · 5.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
//
// Copyright 2020-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:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_voice_processor/flutter_voice_processor.dart';
import 'package:flutter_voice_processor_example/vu_meter_painter.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final int frameLength = 512;
final int sampleRate = 16000;
final int volumeHistoryCapacity = 5;
final double dbOffset = 50.0;
final List<double> _volumeHistory = [];
double _smoothedVolumeValue = 0.0;
bool _isButtonDisabled = false;
bool _isProcessing = false;
String? _errorMessage;
VoiceProcessor? _voiceProcessor;
@override
void initState() {
super.initState();
_initVoiceProcessor();
}
void _initVoiceProcessor() async {
_voiceProcessor = VoiceProcessor.instance;
}
Future<void> _startProcessing() async {
setState(() {
_isButtonDisabled = true;
});
_voiceProcessor?.addFrameListener(_onFrame);
_voiceProcessor?.addErrorListener(_onError);
try {
if (await _voiceProcessor?.hasRecordAudioPermission() ?? false) {
await _voiceProcessor?.start(frameLength, sampleRate);
bool? isRecording = await _voiceProcessor?.isRecording();
setState(() {
_isProcessing = isRecording!;
});
} else {
setState(() {
_errorMessage = "Recording permission not granted";
});
}
} on PlatformException catch (ex) {
setState(() {
_errorMessage = "Failed to start recorder: " + ex.toString();
});
} finally {
setState(() {
_isButtonDisabled = false;
});
}
}
Future<void> _stopProcessing() async {
setState(() {
_isButtonDisabled = true;
});
try {
await _voiceProcessor?.stop();
} on PlatformException catch (ex) {
setState(() {
_errorMessage = "Failed to stop recorder: " + ex.toString();
});
} finally {
bool? isRecording = await _voiceProcessor?.isRecording();
setState(() {
_isButtonDisabled = false;
_isProcessing = isRecording!;
});
}
}
void _toggleProcessing() async {
if (_isProcessing) {
await _stopProcessing();
} else {
await _startProcessing();
}
}
double _calculateVolumeLevel(List<int> frame) {
double rms = 0.0;
for (int sample in frame) {
rms += pow(sample, 2);
}
rms = sqrt(rms / frame.length);
double dbfs = 20 * log(rms / 32767.0) / log(10);
double normalizedValue = (dbfs + dbOffset) / dbOffset;
return normalizedValue.clamp(0.0, 1.0);
}
void _onFrame(List<int> frame) {
double volumeLevel = _calculateVolumeLevel(frame);
if (_volumeHistory.length == volumeHistoryCapacity) {
_volumeHistory.removeAt(0);
}
_volumeHistory.add(volumeLevel);
setState(() {
_smoothedVolumeValue =
_volumeHistory.reduce((a, b) => a + b) / _volumeHistory.length;
});
}
void _onError(VoiceProcessorException error) {
setState(() {
_errorMessage = error.message;
});
}
Color picoBlue = Color.fromRGBO(55, 125, 255, 1);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Voice Processor'),
),
body: Column(children: [
buildVuMeter(context),
buildStartButton(context),
buildErrorMessage(context)
]),
),
);
}
buildVuMeter(BuildContext context) {
return Expanded(
flex: 2,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
alignment: Alignment.bottomCenter,
child: CustomPaint(
painter: VuMeterPainter(_smoothedVolumeValue, picoBlue),
size: Size(constraints.maxWidth * 0.95, 50)));
},
));
}
buildStartButton(BuildContext context) {
final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
primary: picoBlue,
shape: CircleBorder(),
textStyle: TextStyle(color: Colors.white));
return Expanded(
flex: 2,
child: Container(
child: SizedBox(
width: 150,
height: 150,
child: ElevatedButton(
style: buttonStyle,
onPressed: _isButtonDisabled || _errorMessage != null
? null
: _toggleProcessing,
child: Text(_isProcessing ? "Stop" : "Start",
style: TextStyle(fontSize: 30)),
))),
);
}
buildErrorMessage(BuildContext context) {
return Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
margin: EdgeInsets.all(30),
decoration: _errorMessage == null
? null
: BoxDecoration(
color: Colors.red, borderRadius: BorderRadius.circular(5)),
child: _errorMessage == null
? null
: Text(
_errorMessage!,
style: TextStyle(color: Colors.white, fontSize: 20),
)));
}
}