SPB Git

Repository Public MIT

doc-glass-waveform.md 3.9 KB 88 lines markdown

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 text colour. |24| `progress:` | Bars before the fraction take the theme's `primary` colour, the rest are dimmed, and a playhead marks the position. |25| `showsPlayhead: false` | Progress colours without the playhead line. |26| `aggregation: .peak` | Largest magnitude of each bucket; keeps transients visible (default). |27| `aggregation: .rms` | Root mean square of each bucket; closer to perceived loudness. |28| `GlassWaveform(samples:)` | Convenience for small arrays; aggregates each time the parent is rebuilt. |2930## Parameters3132`GlassWaveformData(samples:maxBuckets:aggregation:)`3334| Parameter | Type | Default | Meaning |35|---|---|---|---|36| `samples` | `[Float]` | — | Amplitudes. Magnitudes are used; values are clamped to 0…1, NaN becomes 0, infinities become 1. |37| `maxBuckets` | `Int` | `120` | Largest number of bars; clamped to 1…4096. |38| `aggregation` | `GlassWaveformAggregation` | `.peak` | How a bucket is reduced. |3940`GlassWaveform(_:progress:minimumHeight:showsPlayhead:)`4142| Parameter | Type | Default | Meaning |43|---|---|---|---|44| `data` | `GlassWaveformData` | — | Samples aggregated once by the host. |45| `progress` | `Double?` | `nil` | Played fraction, 0…1 (clamped). |46| `minimumHeight` | `CGFloat` | `48` | Smallest height; the drawing grows with the space it is given. |47| `showsPlayhead` | `Bool` | `true` | Thin vertical marker at `progress`; ignored without a progress. |4849Bars are at most 4 points wide (`GlassWaveformLayout.barWidth`), so a short recording does not turn into blocks. In a right-to-left layout the drawing is mirrored.5051## States5253- **Data**: one rounded bar per bucket, centred vertically; silence keeps a 2-point bar.54- **Progress**: whole buckets before the fraction are coloured.55- **Empty**: nothing is drawn and the accessibility label is "No data".5657## Advanced example5859```swift60struct RecordingRow: View {61    let recording: Recording62    let playback: any GlassAudioPlayback63    @State private var waveform = GlassWaveformData.empty6465    var body: some View {66        GlassWaveform(waveform, progress: GlassPlaybackMath.fraction(currentTime: playback.currentTime, duration: playback.duration))67            .frame(height: 64)68            .task(id: recording.id) {69                // Aggregate off the main actor, once per recording.70                let samples = recording.samples71                waveform = await Task.detached { GlassWaveformData(samples: samples, maxBuckets: 160, aggregation: .rms) }.value72            }73    }74}75```7677## Accessibility7879- 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.80- 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.8182## Limits8384- 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.85- 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.86- The view is not interactive; put a `Slider` or a gesture next to it for scrubbing.87- No audio decoding is included: the host supplies the samples.88
Files main
Menu

Theme