SP GlassKit1.1.0

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.

GlassWaveform running in SP Glass Studio.Capture
Capture of the component running in SP Glass Studio on the iOS 26.5 simulator. Liquid Glass is rendered by the system; this is an image, not a live preview.

Installation

spglass add glass-waveform

Copies the sources and their dependencies into your project. Set up the CLI.

Swift
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")

import SPGlassKit
Identifierglass-waveform
ModuleSPGlassMedia
Kindcontent-component — bars drawn with Canvas, no surface
PlatformsiOS 26, iPadOS 26, macOS 26
ExampleExamples/Gallery/Media/GlassWaveformExample.swift

Minimal usage

Swift
@State private var waveform = GlassWaveformData(samples: samples)

GlassWaveform(waveform)

Variants

VariantUse
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: falseProgress colours without the playhead line.
aggregation: .peakLargest magnitude of each bucket; keeps transients visible (default).
aggregation: .rmsRoot 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:)

ParameterTypeDefaultMeaning
samples[Float]—Amplitudes. Magnitudes are used; values are clamped to 0…1, NaN becomes 0, infinities become 1.
maxBucketsInt120Largest number of bars; clamped to 1…4096.
aggregationGlassWaveformAggregation.peakHow a bucket is reduced.

GlassWaveform(_:progress:minimumHeight:showsPlayhead:)

ParameterTypeDefaultMeaning
dataGlassWaveformData—Samples aggregated once by the host.
progressDouble?nilPlayed fraction, 0…1 (clamped).
minimumHeightCGFloat48Smallest height; the drawing grows with the space it is given.
showsPlayheadBooltrueThin 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

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 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.
  • The view is not interactive; put a Slider or a gesture next to it for scrubbing.
  • No audio decoding is included: the host supplies the samples.

Example

Complete example (compiled with the package)
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

Identifierglass-waveform
Version1.1.0
Depends onglass-foundation
FrameworksSwiftUI
Public symbolsGlassWaveformAggregation, GlassWaveformDownsampler, GlassWaveformData, GlassWaveform, GlassWaveformLayout
LicenceMIT
Manifestmanifest.json

Files