Repository Public MIT
1# GlassWaveform23| | |4|---|---|5| Identifier | `glass-waveform` |6| Module | `SPGlassMedia` |7| Kind | `content-component` — bars drawn with `Canvas`, no surface |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Media/GlassWaveformExample.swift` |1011## Minimal usage1213```swift14@State private var waveform = GlassWaveformData(samples: samples)1516GlassWaveform(waveform)17```1819## Variants2021| Variant | Use |22|---|---|23| `GlassWaveform(data)` | Every bar in the primary text colour. |24| `progress:` | Bars before the fraction take the theme's `primary` colour, the rest the secondary text colour. |25| `aggregation: .peak` | Largest magnitude of each bucket; keeps transients visible (default). |26| `aggregation: .rms` | Root mean square of each bucket; closer to perceived loudness. |27| `GlassWaveform(samples:)` | Convenience for small arrays; aggregates each time the parent is rebuilt. |2829## Parameters3031`GlassWaveformData(samples:maxBuckets:aggregation:)`3233| Parameter | Type | Default | Meaning |34|---|---|---|---|35| `samples` | `[Float]` | — | Amplitudes. Magnitudes are used; values are clamped to 0…1, NaN becomes 0, infinities become 1. |36| `maxBuckets` | `Int` | `120` | Largest number of bars; clamped to 1…4096. |37| `aggregation` | `GlassWaveformAggregation` | `.peak` | How a bucket is reduced. |3839`GlassWaveform(_:progress:minimumHeight:)`4041| Parameter | Type | Default | Meaning |42|---|---|---|---|43| `data` | `GlassWaveformData` | — | Samples aggregated once by the host. |44| `progress` | `Double?` | `nil` | Played fraction, 0…1 (clamped). |45| `minimumHeight` | `CGFloat` | `48` | Smallest height; the drawing grows with the space it is given. |4647## States4849- **Data**: one rounded bar per bucket, centred vertically; silence keeps a 2-point bar.50- **Progress**: whole buckets before the fraction are coloured.51- **Empty**: nothing is drawn and the accessibility label is "No data".5253## Advanced example5455```swift56struct RecordingRow: View {57 let recording: Recording58 let playback: any GlassAudioPlayback59 @State private var waveform = GlassWaveformData.empty6061 var body: some View {62 GlassWaveform(waveform, progress: GlassPlaybackMath.fraction(currentTime: playback.currentTime, duration: playback.duration))63 .frame(height: 64)64 .task(id: recording.id) {65 // Aggregate off the main actor, once per recording.66 let samples = recording.samples67 waveform = await Task.detached { GlassWaveformData(samples: samples, maxBuckets: 160, aggregation: .rms) }.value68 }69 }70}71```7273## Accessibility7475- The drawing is one element with the image trait. Its label is a textual summary, "Waveform, 48,000 samples, peak level 87 percent" (`GlassL10n` `.waveformSummary`), and its value is the progress as a percentage when one is given.76- Played and unplayed bars differ in colour only because the same information is in the accessibility value and, in a player, in the time labels next to it.7778## Limits7980- The input is bounded: whatever the number of samples, at most `min(maxBuckets, 4096)` values are kept and drawn. Aggregation is one pass over the input; a one-million-sample array is covered by the tests.81- The `Canvas` closure only draws the stored buckets: nothing is aggregated per frame. Use `GlassWaveformData` (not the `samples:` convenience) for long recordings so that the pass happens once.82- The view is not interactive; put a `Slider` or a gesture next to it for scrubbing.83- No audio decoding is included: the host supplies the samples.84