processAudioBuffer method

void processAudioBuffer(
  1. List<int> buffer
)

Process an audio buffer for voice activity detection

Implementation

void processAudioBuffer(List<int> buffer) {
  if (!_isActive) return;
  if (_isTTSActive) return;
  if (_isPaused) return;
  if (buffer.isEmpty) return;

  final audioData = _convertPCMToFloat(buffer);
  final energy = _calculateAverageEnergy(audioData);
  _lastEnergyLevel = energy;

  _updateDebugStatistics(energy);

  if (_isCalibrating) {
    _handleCalibrationFrame(energy);
    return;
  }

  final hasVoice = energy > energyThreshold;

  if (_debugFrameCount % 10 == 0) {
    final avgRecent = _recentEnergyValues.isEmpty
        ? 0.0
        : _recentEnergyValues.reduce((a, b) => a + b) /
            _recentEnergyValues.length;
    final maxRecent = _recentEnergyValues.isEmpty
        ? 0.0
        : _recentEnergyValues.reduce(math.max);

    _logger.info(
      '📊 VAD Stats - Current: ${energy.toStringAsFixed(6)} | '
      'Threshold: ${energyThreshold.toStringAsFixed(6)} | '
      'Voice: ${hasVoice ? "✅" : "❌"} | '
      'Avg: ${avgRecent.toStringAsFixed(6)} | '
      'Max: ${maxRecent.toStringAsFixed(6)}',
    );
  }
  _debugFrameCount++;

  _updateVoiceActivityState(hasVoice);
  onAudioBuffer?.call(buffer);
}