processVad method

Map<String, dynamic> processVad(
  1. Float32List samples, {
  2. int sampleRate = 16000,
})

Process audio for voice activity detection.

Implementation

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

  // Use native VAD if available
  if (_vadUseNative && _vadHandle != null) {
    try {
      final samplesPtr = calloc<Float>(samples.length);
      final nativeList = samplesPtr.asTypedList(samples.length);
      nativeList.setAll(0, samples);

      final resultPtr = calloc<RacVadOnnxResultStruct>();

      try {
        final process = _lib.lookupFunction<RacVadOnnxProcessNative,
            RacVadOnnxProcessDart>('rac_vad_onnx_process');

        final status = process(
          _vadHandle!,
          samplesPtr,
          samples.length,
          resultPtr.cast(),
        );

        if (status == RAC_SUCCESS) {
          final result = resultPtr.ref;
          return {
            'isSpeech': result.isSpeech == RAC_TRUE,
            'probability': result.probability,
          };
        }
      } finally {
        calloc.free(samplesPtr);
        calloc.free(resultPtr);
      }
    } catch (_) {
      // Fall through to energy-based detection
    }
  }

  // Fallback: Basic energy-based VAD
  double energy = 0;
  for (final sample in samples) {
    energy += sample * sample;
  }
  energy = samples.isNotEmpty ? energy / samples.length : 0;

  const threshold = 0.01;
  final isSpeech = energy > threshold;

  return {
    'isSpeech': isSpeech,
    'probability': energy.clamp(0.0, 1.0),
  };
}