Repository navigation
Expand file tree
/
Copy pathmain.dart
More file actions
417 lines (361 loc) · 15.8 KB
/
Copy pathmain.dart
File metadata and controls
417 lines (361 loc) · 15.8 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'package:google_speech/endless_streaming_service_v2.dart';
import 'package:google_speech/generated/google/cloud/speech/v2/cloud_speech.pb.dart';
import 'package:google_speech/google_speech.dart';
import 'package:logging/logging.dart';
import 'package:share_plus/share_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:simple_frame_app/rx/audio.dart';
import 'package:simple_frame_app/rx/tap.dart';
import 'package:simple_frame_app/tx/code.dart';
import 'package:simple_frame_app/tx/plain_text.dart';
import 'package:simple_frame_app/text_utils.dart';
import 'package:simple_frame_app/simple_frame_app.dart';
import 'foreground_service.dart';
void main() {
// Set up Android foreground service
initializeForegroundService();
runApp(const MainApp());
}
final _log = Logger("MainApp");
class MainApp extends StatefulWidget {
const MainApp({super.key});
@override
MainAppState createState() => MainAppState();
}
/// SimpleFrameAppState mixin helps to manage the lifecycle of the Frame connection outside of this file
class MainAppState extends State<MainApp> with SimpleFrameAppState {
MainAppState() {
// filter logging
hierarchicalLoggingEnabled = true;
Logger.root.level = Level.INFO;
Logger('Bluetooth').level = Level.OFF;
Logger('RxAudio').level = Level.FINE;
Logger('RxTap').level = Level.FINE;
Logger.root.onRecord.listen((record) {
debugPrint('${record.level.name}: [${record.loggerName}] ${record.time}: ${record.message}');
});
}
/// speech to text application members
final TextEditingController _serviceAccountJsonController = TextEditingController();
final TextEditingController _projectIdController = TextEditingController();
final TextEditingController _languageCodeController = TextEditingController();
StreamSubscription<StreamingRecognizeResponse>? _recognitionSubs;
// 16-bit linear PCM from Frame mic
Stream<Uint8List>? _audioSampleStream;
static const _sampleRate = 8000;
// tap subscription and audio streaming status
StreamSubscription<int>? _tapSubs;
bool _streaming = false;
// transcription UI display
String _partialResult = '';
final List<String> _transcript = List.empty(growable: true);
final ScrollController _transcriptController = ScrollController();
final ScrollController _partialResultController = ScrollController();
static const _textStyle = TextStyle(fontSize: 24);
String? _errorMsg;
@override
void initState() {
super.initState();
// load up the saved text field data
_loadPrefs()
// then kick off the connection to Frame and start the app if possible
.then((_) => tryScanAndConnectAndStart(andRun: true));
}
Future<void> _loadPrefs() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_serviceAccountJsonController.text = prefs.getString('service_account_json') ?? '';
_projectIdController.text = prefs.getString('project_id') ?? '';
_languageCodeController.text = prefs.getString('language_code') ?? '';
});
}
Future<void> _savePrefs() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('service_account_json', _serviceAccountJsonController.text);
await prefs.setString('project_id', _projectIdController.text);
await prefs.setString('language_code', _languageCodeController.text);
}
RecognitionConfigV2 _getRecognitionConfig() => RecognitionConfigV2(
model: RecognitionModelV2.telephony, // TODO try .long, .telephony (for self vs others' speech)
languageCodes: [_languageCodeController.text], // TODO try multi-codes e.g. ['de-DE', 'en-US'], how does it choose the transcription language, or do I get transcriptions in both as alternatives?
features: RecognitionFeatures(),
explicitDecodingConfig: ExplicitDecodingConfig(
encoding: ExplicitDecodingConfig_AudioEncoding.LINEAR16,
sampleRateHertz: _sampleRate,
audioChannelCount: 1,
)
);
/// This application uses google cloud speech-to-text to listen to audio from the Frame mic, convert to text,
/// and send the text to the Frame in real-time. It has a running main loop in this function
/// and also on the Frame (frame_app.lua)
@override
Future<void> run() async {
// validate Google Cloud Speech-to-Text API parameters exist at least
_errorMsg = null;
if (_serviceAccountJsonController.text.isEmpty || _projectIdController.text.isEmpty || _languageCodeController.text.isEmpty) {
setState(() {
_errorMsg = 'Error: Set values for service account, project id and language code';
});
return;
}
setState(() {
currentState = ApplicationState.running;
_partialResult = '';
_transcript.clear();
});
try {
// listen for double taps to start/stop transcribing
_tapSubs?.cancel();
_tapSubs = RxTap().attach(frame!.dataResponse)
.listen((taps) async {
if (taps >= 2) {
if (!_streaming) {
_streaming = true;
// clear the display, the transcribed text will start showing
await frame!.sendMessage(TxPlainText(msgCode: 0x0b, text: ' '));
await _startRecognition();
}
else {
await _stopRecognition();
_streaming = false;
// prompt the user to begin tapping
await frame!.sendMessage(TxPlainText(msgCode: 0x0b, text: 'Double-Tap to resume!'));
}
}
// ignore spurious 1-taps
});
// let Frame know to subscribe for taps and send them to us
await frame!.sendMessage(TxCode(msgCode: 0x10, value: 1));
// prompt the user to begin tapping
await frame!.sendMessage(TxPlainText(msgCode: 0x0b, text: 'Double-Tap to begin!'));
} catch (e) {
_errorMsg = 'Error executing application logic: $e';
_log.fine(_errorMsg);
setState(() {
currentState = ApplicationState.ready;
});
}
}
/// Once running(), audio streaming is controlled by taps. But the user can cancel
/// here as well, whether they are currently streaming audio or not.
@override
Future<void> cancel() async {
setState(() {
currentState = ApplicationState.canceling;
});
// cancel the subscription for taps
_tapSubs?.cancel();
// cancel the endless recognition subscription if it's running
_recognitionSubs?.cancel();
// tell the Frame to stop streaming audio (regardless of if we are currently)
await frame!.sendMessage(TxCode(msgCode: 0x30, value: 0));
// let Frame know to stop sending taps too
await frame!.sendMessage(TxCode(msgCode: 0x10, value: 0));
// clear the display
await frame!.sendMessage(TxPlainText(msgCode: 0x0b, text: ' '));
setState(() {
currentState = ApplicationState.ready;
});
}
/// When we receive a tap to start the transcribing, we need to start
/// audio streaming on Frame and start streaming recognition on the google speech API end
Future<void> _startRecognition() async {
final speechToText = EndlessStreamingServiceV2.viaServiceAccount(
ServiceAccount.fromString(_serviceAccountJsonController.text),
projectId: _projectIdController.text,
);
final recognitionConfig = _getRecognitionConfig();
// tell Frame to start streaming audio
await frame!.sendMessage(TxCode(msgCode: 0x30, value: 1));
try {
// TODO put this object where I can call on it later to detach()
var rxAudio = RxAudio(streaming: true);
// the audio stream from Frame, which needs to be closed() to stop the streaming recognition
_audioSampleStream = rxAudio.attach(frame!.dataResponse);
String prevText = '';
speechToText.endlessStreamingRecognize(
StreamingRecognitionConfigV2(
config: recognitionConfig,
streamingFeatures: StreamingRecognitionFeatures(interimResults: true)
),
_audioSampleStream!,
restartTime: const Duration(seconds: 120),
transitionBufferTime: const Duration(milliseconds: 500)
);
// note: we stop the previous recognition stream by stopping its audio stream first
// but also manage stream subscription here
_recognitionSubs?.cancel();
_recognitionSubs = speechToText.endlessStream.listen((data) async {
// log the streamed results
_log.fine(() => 'Result: $data');
// just use the first alternative for now (consider other language alternatives if provided?)
// and concatenate all the pieces (regardless of stability or whether this result(s) is final or not)
final currentText =
data.results.where((e) => e.alternatives.isNotEmpty)
.map((e) => e.alternatives.first.transcript)
.join();
if (_log.isLoggable(Level.FINE)) {
_log.fine(() => 'Recognized text: $currentText');
}
// if the current text is different from the previous text, send an update to Frame
// (there are many responses that split in different places, have different stability values,
// but concatenate to the same string in our case, so don't update Frame display)
if (currentText != prevText) { // TODO (&& Latin-only script?)
// Frame can display 6 lines of plain text, so work out the text wrapping
List<String> wrappedText = TextUtils.wrapText(currentText, 640, 4);
// then send the bottom 6 lines joined with newlines as a single string
// (they get split and drawn on the Lua side)
String displayText = wrappedText.sublist(wrappedText.length <= 6 ? 0 : wrappedText.length - 6)
.join('\n');
await frame!.sendMessage(TxPlainText(msgCode: 0x0b, text: displayText));
}
// When a complete utterance is detected, isFinal is set and we can append
// the utterance to the final transcript.
// In between, there are non-final fragments, and they can be shown as previews but not appended
// to the final transcript. A newline doesn't need to be added either.
if (data.results.first.isFinal) {
// data.results.first.alternatives.first.transcript should match currentText
// (the complete utterance) when isFinal is true
assert(currentText == data.results.first.alternatives.first.transcript);
// add currentText to the official transcript and clear out the interim
setState(() {
_transcript.add(currentText);
_partialResult = '';
});
_scrollToBottom();
} else {
// interim results, show the interim result but don't add to the official transcript yet
setState(() {
_partialResult = currentText;
});
_scrollToBottom();
}
}, onError: (error) {
_log.warning('Error occurred in endless stream: $error');
setState(() {
currentState = ApplicationState.ready;
});
}, onDone: () {
// audio stream was stopped so Recognizer stream stopped
_log.info('Endless Stream is done');
_recognitionSubs?.cancel();
speechToText.dispose();
setState(() {
currentState = ApplicationState.ready;
});
});
} catch (e) {
_log.warning(() => 'Error executing application logic: $e');
}
}
/// When we receive a tap to stop transcribing, cancel the audio streaming from Frame,
/// which will send "final chunk" message, which will close the audio stream
/// and the streaming recognizer stream will stop because its audio stream stopped
Future<void> _stopRecognition() async {
// tell Frame to stop streaming audio
await frame!.sendMessage(TxCode(msgCode: 0x30, value: 0));
// TODO we should also be able to send
// rxAudio.detach() to close the controller controlling our audio stream
}
void _scrollToBottom() {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_transcriptController.hasClients) {
_transcriptController.animateTo(
_transcriptController.position.maxScrollExtent,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
}
if (_partialResultController.hasClients) {
_partialResultController.animateTo(
_partialResultController.position.maxScrollExtent,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
}
});
}
@override
Widget build(BuildContext context) {
startForegroundService();
return WithForegroundTask(
child: MaterialApp(
title: 'Transcribe - Google Cloud Speech',
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('Transcribe - Google Cloud Speech'),
actions: [getBatteryWidget()]
),
body: Center(
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(controller: _serviceAccountJsonController, obscureText: false, decoration: const InputDecoration(hintText: 'Enter Service Account JSON'),),
TextField(controller: _projectIdController, obscureText: false, decoration: const InputDecoration(hintText: 'Enter Project Id'),),
TextField(controller: _languageCodeController, obscureText: false, decoration: const InputDecoration(hintText: 'Enter Language Code e.g. en-US'),),
if (_errorMsg != null) Text(_errorMsg!, style: const TextStyle(backgroundColor: Colors.red)),
ElevatedButton(onPressed: _savePrefs, child: const Text('Save')),
Expanded(child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: ListView.builder(
controller: _transcriptController, // Auto-scroll controller
itemCount: _transcript.length,
itemBuilder: (context, index) {
return Text(
_transcript[index],
style: _textStyle,
);
},
),
),
const Divider(),
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: _textStyle.fontSize! * 5
),
child: Align(alignment: Alignment.centerLeft,
child: SingleChildScrollView(
controller: _partialResultController,
child: Text(_partialResult, style: _textStyle)
)
),
),
],
)),
],
),
),
),
floatingActionButton: Stack(
children: [
if (_transcript.isNotEmpty) Positioned(
bottom: 90,
right: 20,
child: FloatingActionButton(
onPressed: () {
Share.share(_transcript.join('\n'));
},
child: const Icon(Icons.share)),
),
Positioned(
bottom: 20,
right: 20,
child: getFloatingActionButtonWidget(const Icon(Icons.mic), const Icon(Icons.mic_off)) ?? Container(),
),
]
),
persistentFooterButtons: getFooterButtonsWidget(),
),
)
);
}
}