Skip to main content

waveform_points

Function waveform_points 

Source
pub fn waveform_points(
    samples: &[f32],
    bounds: Rect,
    max_points: usize,
) -> Vec<Point>
Expand description

Map audio samples to a polyline inside bounds.

  • Samples are amplitudes nominally in [-1, 1]; larger magnitudes are clamped, NaN and infinities count as 0.
  • x runs from bounds.x (first point) to bounds.x + bounds.width (last point); y = centre - sample * (height / 2), so +1 is the top edge, -1 the bottom edge and 0 the centre line.
  • More samples than max_points are reduced by buckets, keeping the largest-magnitude sample per bucket. The result never has more than max(max_points, 2) points.
  • No samples (silence) gives the centre line: two points, left-centre and right-centre. A lone sample gives a flat line at its level.
  • Every point is finite and lies inside bounds (edges inclusive), for any input. Negative width/height count as 0, non-finite components as 0, and components beyond +-1e15 are clamped to it so that no sum overflows; the “inside” promise is then to those sanitized bounds, which equal bounds whenever it is finite, not negative and within +-1e15.