fromBuiltInKeywords static method

Future<Porcupine> fromBuiltInKeywords(
  1. String accessKey,
  2. List<BuiltInKeyword> keywords, {
  3. String? modelPath,
  4. List<double>? sensitivities,
})

Static creator for initializing Porcupine from a selection of built-in keywords

accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/).

keywords is a List of (phrases) for detection. The list of available keywords can be retrieved using BuiltInKeyword enum.

modelPath is a path to the file containing model parameters. If not set it will be set to the default location.

sensitivities sensitivities for each keywords model. A higher sensitivity reduces miss rate at the cost of potentially higher false alarm rate. Sensitivity should be a floating-point number within 0 and 1.

Throws a PorcupineException if not initialized correctly

returns an instance of the wake word engine

Implementation

static Future<Porcupine> fromBuiltInKeywords(
    String accessKey, List<BuiltInKeyword> keywords,
    {String? modelPath, List<double>? sensitivities}) async {
  if (modelPath != null) {
    modelPath = await _tryExtractFlutterAsset(modelPath);
  }

  List<String> keywordValues = List.empty(growable: true);
  for (var keyword in keywords) {
    keywordValues.add(keyword
        .toString()
        .split('.')
        .last
        .replaceAll('_', ' ')
        .toLowerCase());
  }

  try {
    Map<String, dynamic> result = Map<String, dynamic>.from(
        await _channel.invokeMethod(_NativeFunctions.FROM_BUILTIN_KEYWORDS.name, {
      'accessKey': accessKey,
      'modelPath': modelPath,
      'keywords': keywordValues,
      'sensitivities': sensitivities
    }));

    return Porcupine._(result['handle'], result['frameLength'],
        result['sampleRate'], result['version']);
  } on PlatformException catch (error) {
    throw porcupineStatusToException(error.code, error.message);
  } on Exception catch (error) {
    throw porcupineStatusToException("PorcupineException", error.toString());
  }
}