GlassWaveform
| Identifier | glass-waveform |
| Module | SPGlassMedia |
| Kind | content-component — bars drawn with Canvas, no surface |
| Platforms | iOS 26, iPadOS 26, macOS 26 |
| Example | Examples/Gallery/Media/GlassWaveformExample.swift |
Minimal usage
swift
@State private var waveform = GlassWaveformData(samples: samples)
GlassWaveform(waveform)Variants
| Variant | Use |
|---|---|
GlassWaveform(data) |
Every bar in the primary text colour. |
progress: |
Bars before the fraction take the theme's primary colour, the rest the secondary text colour. |
aggregation: .peak |
Largest magnitude of each bucket; keeps transients visible (default). |
aggregation: .rms |
Root mean square of each bucket; closer to perceived loudness. |
GlassWaveform(samples:) |
Convenience for small arrays; aggregates each time the parent is rebuilt. |
Parameters
GlassWaveformData(samples:maxBuckets:aggregation:)
| Parameter | Type | Default | Meaning |
|---|---|---|---|
samples |
[Float] |
— | Amplitudes. Magnitudes are used; values are clamped to 0…1, NaN becomes 0, infinities become 1. |
maxBuckets |
Int |
120 |
Largest number of bars; clamped to 1…4096. |
aggregation |
GlassWaveformAggregation |
.peak |
How a bucket is reduced. |
GlassWaveform(_:progress:minimumHeight:)
| Parameter | Type | Default | Meaning |
|---|---|---|---|
data |
GlassWaveformData |
— | Samples aggregated once by the host. |
progress |
Double? |
nil |
Played fraction, 0…1 (clamped). |
minimumHeight |
CGFloat |
48 |
Smallest height; the drawing grows with the space it is given. |
States
- Data: one rounded bar per bucket, centred vertically; silence keeps a 2-point bar.
- Progress: whole buckets before the fraction are coloured.
- Empty: nothing is drawn and the accessibility label is "No data".
Advanced example
swift
struct RecordingRow: View {
let recording: Recording
let playback: any GlassAudioPlayback
@State private var waveform = GlassWaveformData.empty
var body: some View {
GlassWaveform(waveform, progress: GlassPlaybackMath.fraction(currentTime: playback.currentTime, duration: playback.duration))
.frame(height: 64)
.task(id: recording.id) {
// Aggregate off the main actor, once per recording.
let samples = recording.samples
waveform = await Task.detached { GlassWaveformData(samples: samples, maxBuckets: 160, aggregation: .rms) }.value
}
}
}Accessibility
- 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. - 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.
Limits
- 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. - The
Canvasclosure only draws the stored buckets: nothing is aggregated per frame. UseGlassWaveformData(not thesamples:convenience) for long recordings so that the pass happens once. - The view is not interactive; put a
Slideror a gesture next to it for scrubbing. - No audio decoding is included: the host supplies the samples.