OfflineRecognizer constructor

OfflineRecognizer(
  1. OfflineRecognizerConfig config
)

The user is responsible to call the OfflineRecognizer.free() method of the returned instance to avoid memory leak.

Implementation

factory OfflineRecognizer(OfflineRecognizerConfig config) {
  final c = calloc<SherpaOnnxOfflineRecognizerConfig>();

  c.ref.feat.sampleRate = config.feat.sampleRate;
  c.ref.feat.featureDim = config.feat.featureDim;

  // transducer
  c.ref.model.transducer.encoder =
      config.model.transducer.encoder.toNativeUtf8();
  c.ref.model.transducer.decoder =
      config.model.transducer.decoder.toNativeUtf8();
  c.ref.model.transducer.joiner =
      config.model.transducer.joiner.toNativeUtf8();

  // paraformer
  c.ref.model.paraformer.model = config.model.paraformer.model.toNativeUtf8();

  // nemoCtc
  c.ref.model.nemoCtc.model = config.model.nemoCtc.model.toNativeUtf8();

  // whisper
  c.ref.model.whisper.encoder = config.model.whisper.encoder.toNativeUtf8();

  c.ref.model.whisper.decoder = config.model.whisper.decoder.toNativeUtf8();

  c.ref.model.whisper.language = config.model.whisper.language.toNativeUtf8();

  c.ref.model.whisper.task = config.model.whisper.task.toNativeUtf8();

  c.ref.model.whisper.tailPaddings = config.model.whisper.tailPaddings;

  c.ref.model.tdnn.model = config.model.tdnn.model.toNativeUtf8();

  c.ref.model.tokens = config.model.tokens.toNativeUtf8();

  c.ref.model.numThreads = config.model.numThreads;
  c.ref.model.debug = config.model.debug ? 1 : 0;
  c.ref.model.provider = config.model.provider.toNativeUtf8();
  c.ref.model.modelType = config.model.modelType.toNativeUtf8();

  c.ref.lm.model = config.lm.model.toNativeUtf8();
  c.ref.lm.scale = config.lm.scale;

  c.ref.decodingMethod = config.decodingMethod.toNativeUtf8();
  c.ref.maxActivePaths = config.maxActivePaths;

  c.ref.hotwordsFile = config.hotwordsFile.toNativeUtf8();
  c.ref.hotwordsScore = config.hotwordsScore;

  final ptr = SherpaOnnxBindings.createOfflineRecognizer?.call(c) ?? nullptr;

  calloc.free(c.ref.hotwordsFile);
  calloc.free(c.ref.decodingMethod);
  calloc.free(c.ref.lm.model);
  calloc.free(c.ref.model.modelType);
  calloc.free(c.ref.model.provider);
  calloc.free(c.ref.model.tokens);
  calloc.free(c.ref.model.tdnn.model);
  calloc.free(c.ref.model.whisper.task);
  calloc.free(c.ref.model.whisper.language);
  calloc.free(c.ref.model.whisper.decoder);
  calloc.free(c.ref.model.whisper.encoder);
  calloc.free(c.ref.model.nemoCtc.model);
  calloc.free(c.ref.model.paraformer.model);
  calloc.free(c.ref.model.transducer.encoder);
  calloc.free(c.ref.model.transducer.decoder);
  calloc.free(c.ref.model.transducer.joiner);
  calloc.free(c);

  return OfflineRecognizer._(ptr: ptr, config: config);
}