https://github.com/alphacep/vosk-flutter
https://github.com/alphacep/vosk-flutter
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/alphacep/vosk-flutter
- Owner: alphacep
- License: apache-2.0
- Created: 2021-12-08T16:10:19.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-26T04:30:56.000Z (over 1 year ago)
- Last Synced: 2025-06-11T12:24:35.585Z (about 1 year ago)
- Language: Dart
- Size: 76.6 MB
- Stars: 65
- Watchers: 9
- Forks: 58
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Vosk Flutter Plugin
[](https://pub.dev/packages/vosk_flutter)
[](https://pub.dev/packages/very_good_analysis)
[](https://github.com/alphacep/vosk-flutter/actions/workflows/vosk_flutter.yml?query=branch%3Amaster)
Flutter plugin for Vosk speech recognition.
## Platform Support
| Android | iOS | MacOS | Web | Linux | Windows |
| :-----: | :-: | :---: | :-: | :---: | :----: |
| ✔ | ➖ | ➖ | ➖ | ✔ | ✔ |
## Usage
### Configurations
Follow the instruction at the [Installing page of the package](https://pub.dev/packages/vosk_flutter/install).
#### Android
Add this pro guard rules in `android/app/proguard-rules.pro`(if the file does not exist - create it):
```properties
-keep class com.sun.jna.* { *; }
-keepclassmembers class * extends com.sun.jna.* { public *; }
```
If you want to use a microphone input, add the microphone permission to your `AndroidManifest.xml`:
```xml
```
### Load model
```yaml
flutter:
assets:
- assets/models/
```
```dart
final vosk = VoskFlutterPlugin.instance();
final enSmallModelPath = await ModelLoader()
.loadFromAssets('assets/models/vosk-model-small-en-us-0.15.zip');
```
### Create recognizer
```dart
final recognizer = await vosk.createRecognizer(
model: model,
sampleRate: sampleRate,
);
final recognizerWithGrammar = await vosk.createRecognizer(
model: model,
sampleRate: sampleRate,
grammar: ['one', 'two', 'three'],
);
```
### Recognize audio data
```dart
Uint8List audioBytes = ...; // audio data in PCM 16-bit mono format
List results = [];
int chunkSize = 8192;
int pos = 0;
while (pos + chunkSize < audioBytes.length) {
final resultReady = await recognizer.acceptWaveformBytes(
Uint8List.fromList(audioBytes.getRange(pos, pos + chunkSize).toList()));
pos += chunkSize;
if (resultReady) {
print(await recognizer.getResult());
} else {
print(await recognizer.getPartialResult());
}
}
await recognizer.acceptWaveformBytes(
Uint8List.fromList(audioBytes.getRange(pos, audioBytes.length).toList()));
print(await recognizer.getFinalResult());
```
### Recognize microphone data
#### Android
```dart
final speechService = await vosk.initSpeechService(recognizer);
speechService.onPartial().forEach((partial) => print(partial));
speechService.onResult().forEach((result) => print(result));
await speechService.start();
```
#### Linux & Windows
Use any suitable plugin to get the microphone input and [pass it to a recognizer](#recognize-audio-data)