transcribe method

Map<String, dynamic> transcribe(
  1. Float32List samples, {
  2. int sampleRate = 16000,
  3. String? language,
})

Transcribe audio samples (batch mode).

samples - Float32 audio samples (-1.0 to 1.0) sampleRate - Sample rate in Hz (typically 16000) language - Language code (e.g., "en", "es") or null for auto-detect

Returns a map with transcription result.

Implementation

Map<String, dynamic> transcribe(
  Float32List samples, {
  int sampleRate = 16000,
  String? language,
}) {
  _ensureBackendType('onnx');
  _ensureHandle();

  // Allocate native array
  final samplesPtr = calloc<Float>(samples.length);
  final nativeList = samplesPtr.asTypedList(samples.length);
  nativeList.setAll(0, samples);

  final resultPtr = calloc<RacSttOnnxResultStruct>();

  try {
    final transcribe = _lib.lookupFunction<RacSttOnnxTranscribeNative,
        RacSttOnnxTranscribeDart>('rac_stt_onnx_transcribe');

    final status = transcribe(
      _handle!,
      samplesPtr,
      samples.length,
      nullptr, // options
      resultPtr.cast(),
    );

    if (status != RAC_SUCCESS) {
      throw NativeBackendException(
        'Transcription failed: ${RacCore.getErrorMessage(status)}',
        code: status,
      );
    }

    // Extract result from struct
    final result = resultPtr.ref;
    final text = result.text != nullptr ? result.text.toDartString() : '';
    final confidence = result.confidence;
    final languageOut =
        result.language != nullptr ? result.language.toDartString() : null;

    return {
      'text': text,
      'confidence': confidence,
      'language': languageOut,
      'duration_ms': result.durationMs,
    };
  } finally {
    calloc.free(samplesPtr);
    // Free result text if allocated by C++
    if (resultPtr.ref.text != nullptr) {
      RacCore.free(resultPtr.ref.text.cast());
    }
    calloc.free(resultPtr);
  }
}