updateAudioLevel method

void updateAudioLevel(
  1. Float32List buffer
)

Update audio level for visualization

This would be called from platform-specific audio buffer callbacks. Calculates RMS (root mean square) and normalizes to 0-1 range.

buffer Float audio samples

Implementation

void updateAudioLevel(Float32List buffer) {
  if (buffer.isEmpty) return;

  // Calculate RMS (root mean square) for audio level
  double sum = 0.0;
  for (final sample in buffer) {
    sum += sample * sample;
  }

  final rms = _sqrt(sum / buffer.length);
  final dbLevel =
      20 * _log10(rms + 0.0001); // Add small value to avoid log(0)

  // Normalize to 0-1 range (-60dB to 0dB)
  final normalizedLevel = (dbLevel + 60) / 60;
  _audioLevel = normalizedLevel.clamp(0.0, 1.0);

  _audioLevelController.add(_audioLevel);
}