asr_lib 0.1.0
asr_lib: ^0.1.0 copied to clipboard
On-device speech recognition for Flutter powered by sherpa-onnx.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:sherpa_asr/asr_lib.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'sherpa-asr Demo',
theme: ThemeData(
colorSchemeSeed: Colors.blue,
useMaterial3: true,
),
home: const AsrDemoPage(),
);
}
}
class AsrDemoPage extends StatefulWidget {
const AsrDemoPage({super.key});
@override
State<AsrDemoPage> createState() => _AsrDemoPageState();
}
class _AsrDemoPageState extends State<AsrDemoPage> {
final AsrLib _asr = AsrLib();
AsrState _state = AsrState.idle;
String _result = '';
bool _initialized = false;
@override
void initState() {
super.initState();
_asr.stateStream.listen((state) {
setState(() => _state = state);
});
_asr.resultStream.listen((result) {
setState(() => _result = '${result.text}\n$_result');
});
}
Future<void> _initialize() async {
final ok = await _asr.initialize();
setState(() => _initialized = ok);
}
@override
void dispose() {
_asr.release();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('sherpa-asr')),
body: Column(
children: [
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
reverse: true,
child: Text(_result.isEmpty ? 'No results yet' : _result),
),
),
Text('State: ${_state.name}'),
const SizedBox(height: 8),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (!_initialized)
FilledButton(onPressed: _initialize, child: const Text('Initialize'))
else ...[
FilledButton(
onPressed: _state == AsrState.ready ? () => _asr.startRecording() : null,
child: const Text('Start'),
),
const SizedBox(width: 8),
FilledButton(
onPressed: _state == AsrState.recording ? () => _asr.stopRecording() : null,
child: const Text('Stop'),
),
],
],
),
const SizedBox(height: 16),
],
),
);
}
}