stop method

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

Stops the device and returns peaks for all the audio that it captured.

These peaks come from the history of the audio thread, and not from the frames that the visualizer collected. Therefore, they are complete even if the application went to the background and missed drains. The caller owns the result and must 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,
  );
}