keepRanges static method
The complement of silences within total, widened by padding and
without ranges shorter than minKeep.
Implementation
static List<TimeRange> keepRanges(
List<TimeRange> silences,
Duration total, {
Duration padding = const Duration(milliseconds: 120),
Duration minKeep = const Duration(milliseconds: 300),
}) {
final sorted = [...silences]..sort((a, b) => a.start.compareTo(b.start));
final keep = <TimeRange>[];
var cursor = Duration.zero;
void add(Duration from, Duration to) {
final s = from < Duration.zero ? Duration.zero : from;
final e = to > total ? total : to;
if (e - s < minKeep) return;
// Padding can make neighbours overlap; merge them.
if (keep.isNotEmpty && s <= keep.last.end) {
keep[keep.length - 1] = TimeRange(keep.last.start, e);
} else {
keep.add(TimeRange(s, e));
}
}
for (final silence in sorted) {
if (silence.start > cursor) add(cursor - padding, silence.start + padding);
if (silence.end > cursor) cursor = silence.end;
}
if (cursor < total) add(cursor - padding, total);
return keep;
}