Skip to content

jtdLab/vosk_flutter

Repository files navigation

Vosk Flutter Plugin

pub package style: very good analysis vosk_flutter

IOS works with pod 1.14.3

Flutter plugin for Vosk speech recognition.

Platform Support

Android iOS MacOS Web Linux Windows

Usage

Installation

Follow the instruction at the Installing page to install the plugin.

Load model

From the assets:

flutter:
  assets:
    - assets/models/

From the network:

final vosk = VoskFlutterPlugin.instance();
final enSmallModelPath = await ModelLoader()
    .loadFromAssets('assets/models/vosk-model-small-en-us-0.15.zip');

Create recognizer

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

From the file:

Uint8List audioBytes = ...; // audio data in PCM 16-bit mono format
List<String> 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());

From the microphone(you can use any suitable flutter plugin to get the micStream, for example mic_stream):

Uint8List micStream = ...;
final speechService = await vosk.initSpeechService(recognizer);
speechService.onPartial().forEach((partial) => print(partial));
speechService.onResult().forEach((result) => print(result));
await speechService.start(micStream);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published