createRecognizer method

Future<Recognizer> createRecognizer({
  1. required Model model,
  2. required int sampleRate,
  3. List<String>? grammar,
})

Create a recognizer that will use the specified model to process speech. sampleRate determines the sample rate of the audio fed to the recognizer(a mismatch in the sample rate causes accuracy problems).

You can optionally provide grammar for the recognizer, see Recognizer.setGrammar for more details about the grammar usage.

Implementation

Future<Recognizer> createRecognizer({
  required final Model model,
  required final int sampleRate,
  final List<String>? grammar,
}) async {
  if (_supportsFFI()) {
    return using((final arena) {
      final recognizerPointer = grammar == null
          ? _voskLibrary.vosk_recognizer_new(
              model.modelPointer!,
              sampleRate.toDouble(),
            )
          : _voskLibrary.vosk_recognizer_new_grm(
              model.modelPointer!,
              sampleRate.toDouble(),
              jsonEncode(grammar).toCharPtr(arena),
            );
      return Recognizer(
        id: -1,
        model: model,
        sampleRate: sampleRate,
        channel: _channel,
        recognizerPointer: recognizerPointer,
        voskLibrary: _voskLibrary,
      );
    });
  }

  final args = <String, dynamic>{
    'modelPath': model.path,
    'sampleRate': sampleRate,
  };
  if (grammar != null) {
    args['grammar'] = jsonEncode(grammar);
  }
  final id = await _channel.invokeMethod('recognizer.create', args);
  return Recognizer(
    id: id as int,
    model: model,
    sampleRate: sampleRate,
    channel: _channel,
  );
}