transcribe method
Map<String, dynamic>
transcribe(
- Float32List samples, {
- int sampleRate = 16000,
- 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 C-allocated strings inside the result (strdup'd by rac_stt_onnx_transcribe).
// rac_stt_result_free handles text, detected_language, and words array.
try {
final resultFreeFn = _lib!.lookupFunction<
Void Function(Pointer<Void>),
void Function(Pointer<Void>)>('rac_stt_result_free');
resultFreeFn(resultPtr.cast<Void>());
} catch (_) {
// Fallback: manually free text if rac_stt_result_free not available
if (resultPtr.ref.text != nullptr) {
RacCore.free(resultPtr.ref.text.cast());
}
}
calloc.free(resultPtr);
}
}