stop method

  1. @override
Future<WaveformPeaks> stop()
override

Stops the device and returns peaks for everything captured.

These come from the audio thread's own history, not from what the visualizer happened to collect, so they are complete even if the app was backgrounded and missed drains. Caller owns the result and should dispose it.

Implementation

@override
Future<WaveformPeaks> stop() async {
  stopCount++;
  _recording = false;

  final canned = stopResult;
  if (canned != null) return canned;

  if (emitted.isEmpty) {
    throw const CaptureUnavailable('Nothing was captured.');
  }

  final base = Int16List(emitted.length * 2);
  for (var i = 0; i < emitted.length; i++) {
    base[i * 2] = emitted[i].min;
    base[i * 2 + 1] = emitted[i].max;
  }

  return WaveformPeaks.fromInterleaved(
    base,
    sampleRate: config.sampleRate,
    baseSamplesPerPixel: config.hop,
    channels: config.channels,
  );
}