computePeaks static method
Max absolute amplitude per bucket of 16-bit mono pcm, normalized to
the loudest bucket.
Implementation
static Float32List computePeaks(Uint8List pcm, int sampleRate) {
final data = ByteData.sublistView(pcm);
final samples = pcm.length ~/ 2;
final bucket = math.max(1, sampleRate ~/ peaksPerSecond);
final count = (samples / bucket).ceil();
final peaks = Float32List(count);
var loudest = 1;
for (var b = 0; b < count; b++) {
var peak = 0;
final end = math.min(samples, (b + 1) * bucket);
for (var i = b * bucket; i < end; i++) {
final v = data.getInt16(i * 2, Endian.little).abs();
if (v > peak) peak = v;
}
peaks[b] = peak.toDouble();
if (peak > loudest) loudest = peak;
}
for (var b = 0; b < count; b++) {
peaks[b] = peaks[b] / loudest;
}
return peaks;
}