inferIsolate method

Future<String> inferIsolate(
  1. String inputPath, {
  2. String? logPath,
  3. int numProcessors = 1,
  4. String language = "auto",
  5. bool translate = false,
  6. String initialPrompt = "",
  7. int strategy = whisper_sampling_strategy.WHISPER_SAMPLING_GREEDY,
  8. int startTime = 0,
  9. int endTime = -1,
  10. bool useOriginalTime = true,
  11. void newSegmentCallback(
    1. Pointer<whisper_context>,
    2. Pointer<whisper_state>,
    3. int,
    4. Pointer<Void>,
    )?,
  12. Pointer<Void>? newSegmentCallbackUserData,
})

Implementation

Future<String> inferIsolate(String inputPath,
    {String? logPath,
    int numProcessors = 1,
    String language = "auto",
    bool translate = false,
    String initialPrompt = "",
    int strategy = whisper_sampling_strategy.WHISPER_SAMPLING_GREEDY,
    int startTime = 0,
    int endTime = -1,
    bool useOriginalTime = true,
    void Function(Pointer<whisper_context>, Pointer<whisper_state>, int,
            Pointer<Void>)?
        newSegmentCallback,
    Pointer<Void>? newSegmentCallbackUserData}) async {
  logPath ??= path.join((await getTemporaryDirectory()).path, "log.txt");
  var wparams = createFullDefaultParams(strategy);
  wparams.language = language.toNativeUtf8().cast<Char>();
  wparams.translate = translate;
  if (newSegmentCallback != null) {
    wparams.new_segment_callback = NativeCallable<
            Void Function(Pointer<whisper_context>, Pointer<whisper_state>,
                Int, Pointer<Void>)>.listener(newSegmentCallback)
        .nativeFunction;
  }
  if (newSegmentCallbackUserData != null) {
    wparams.new_segment_callback_user_data = newSegmentCallbackUserData;
  }
  if (initialPrompt != "") {
    wparams.initial_prompt = initialPrompt.toNativeUtf8().cast<Char>();
  }
  this.startTime = startTime;
  this.endTime = endTime;
  timeOffset = useOriginalTime ? startTime : 0;

  // 确保 isolate 已经启动
  if (_isolateSendPort == null) {
    final ReceivePort isolateReceivePort = ReceivePort();
    bool isolateStarted = false;
    isolateReceivePort.listen((message) {
      if (message is SendPort) {
        _isolateSendPort = message;
        isolateStarted = true;
      }
    });

    await Isolate.spawn(_startInferIsolate, isolateReceivePort.sendPort);
    while (!isolateStarted) {
      await Future.delayed(Duration(milliseconds: 10));
    }
  }

  var rootToken = RootIsolateToken.instance!;
  final ReceivePort resultReceivePort = ReceivePort();
  Map<String, dynamic> whisper2Json = toJson();
  _isolateSendPort?.send([
    whisper2Json,
    inputPath,
    logPath,
    numProcessors,
    wparams,
    resultReceivePort.sendPort,
    rootToken
  ]);

  return resultReceivePort.first.then((message) {
    if (message is String) {
      return message;
    } else {
      throw TypeError();
    }
  });
}