GlassWaveform
Bounded, aggregated audio samples drawn with Canvas and described in text.
- Content
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-waveform
Content on an opaque, readable surface. Glass is kept for controls.
CaptureInstallation
spglass add glass-waveformCopies the sources and their dependencies into your project. Set up the CLI.
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")
import SPGlassKit| 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
@State private var waveform = GlassWaveformData(samples: samples)
GlassWaveform(waveform)Variants
| Variant | Use |
|---|---|
GlassWaveform(data) | Every bar in the text colour. |
progress: | Bars before the fraction take the theme's primary colour, the rest are dimmed, and a playhead marks the position. |
showsPlayhead: false | Progress colours without the playhead line. |
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:showsPlayhead:)
| 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. |
showsPlayhead | Bool | true | Thin vertical marker at progress; ignored without a progress. |
Bars 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.
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
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.
Example
import SwiftUI
import SPGlassKit
public struct GlassWaveformExample: GlassExample {
public static let componentID = "glass-waveform"
/// Aggregated once, when the example is created, not on every frame.
@State private var voice = GlassWaveformData(samples: Self.syntheticSamples(count: 48_000), maxBuckets: 96, aggregation: .rms)
@State private var peaks = GlassWaveformData(samples: Self.syntheticSamples(count: 48_000), maxBuckets: 160)
@State private var progress = 0.35
public init() {}
public var body: some View {
VStack(alignment: .leading, spacing: 16) {
Text("RMS, 96 buckets, with progress")
.font(.headline)
GlassWaveform(voice, progress: progress)
Slider(value: $progress, in: 0...1) { Text("Progress") }
Text("Peak, 160 buckets")
.font(.headline)
GlassWaveform(peaks, minimumHeight: 72)
Text("Progress without the playhead")
.font(.headline)
GlassWaveform(voice, progress: progress, showsPlayhead: false)
Text("No samples")
.font(.headline)
GlassWaveform(.empty, minimumHeight: 24)
Text("Sample data · a synthetic signal built from sine waves.")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding()
}
/// A deterministic signal: a carrier shaped by two slow envelopes.
static func syntheticSamples(count: Int) -> [Float] {
(0..<count).map { index in
let t = Double(index) / Double(count)
let envelope = 0.55 + 0.45 * sin(t * .pi * 6)
let swell = 0.6 + 0.4 * cos(t * .pi * 17)
return Float(sin(Double(index) * 0.21) * envelope * swell)
}
}
}
#Preview("GlassWaveform") { GlassWaveformExample().glassTheme(.aurora) }
Registry entry
| Identifier | glass-waveform |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-foundation |
| Frameworks | SwiftUI |
| Public symbols | GlassWaveformAggregation, GlassWaveformDownsampler, GlassWaveformData, GlassWaveform, GlassWaveformLayout |
| Licence | MIT |
| Manifest | manifest.json |
Files
Media/Media/GlassWaveform.swift11035 bytes · sha256 682352cacdb8…