SPB Git

Repository Public License

FileTimelineViews.swift 14.8 KB 238 lines swift

1//2//  ORIX Audio3//  File: ORIXAudio/Features/Detail/FileTimelineViews.swift4//  Description: Zoomable, scrubbable file waveform and spectrogram sharing one timeline, with markers, selection and playhead.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//9import ORIXAudioCore10import SwiftUI1112/// Pinch to zoom, drag to pan (or select when selection mode is on), tap to move the playhead.13struct TimelineGestures: ViewModifier {14    @Binding var timeline: TimelineState15    let width: CGFloat16    let isSelecting: Bool17    let onTap: (CGPoint) -> Void18    @State private var zoomBase: TimelineState?19    @State private var dragBase: TimelineState?20    @State private var dragAnchorTime: Double?2122    func body(content: Content) -> some View {23        content24            .contentShape(Rectangle())25            .gesture(SpatialTapGesture().onEnded { onTap($0.location) })26            .simultaneousGesture(27                MagnifyGesture()28                    .onChanged { value in29                        if zoomBase == nil { zoomBase = timeline }30                        guard var base = zoomBase else { return }31                        base.zoom(by: Double(value.magnification), anchorFraction: Double(value.startAnchor.x))32                        base.selection = timeline.selection33                        timeline = base34                    }35                    .onEnded { _ in zoomBase = nil }36            )37            .simultaneousGesture(38                DragGesture(minimumDistance: 6)39                    .onChanged { value in40                        guard zoomBase == nil else { return }41                        if dragBase == nil { dragBase = timeline; dragAnchorTime = timeline.time(forX: value.startLocation.x, width: width) }42                        guard let base = dragBase, let anchor = dragAnchorTime else { return }43                        if isSelecting {44                            let t = base.time(forX: value.location.x, width: width)45                            timeline.selection = min(anchor, t)...max(anchor, t)46                        } else {47                            var moved = base48                            moved.pan(bySeconds: -Double(value.translation.width / max(1, width)) * base.visibleDuration)49                            moved.selection = timeline.selection50                            timeline = moved51                        }52                    }53                    .onEnded { _ in54                        dragBase = nil; dragAnchorTime = nil55                        if let s = timeline.selection, s.upperBound - s.lowerBound < 0.01 { timeline.selection = nil }56                    }57            )58    }59}6061struct FileWaveformView: View {62    let summary: WaveformSummary?63    @Binding var timeline: TimelineState64    let style: WaveformStyle65    let playhead: Double66    let markers: [Marker]67    var isSelecting = false68    var isLoading = false69    let onSeek: (Double) -> Void7071    /// Quiet recordings are magnified vertically so their shape is readable. The factor is always shown on the plot;72    /// it changes the drawing only, never the audio or any measurement.73    private var displayGain: CGFloat {74        guard let peak = summary?.levels.last?.map(\.peak).max(), peak > 0.001 else { return 1 }75        return peak >= 0.7 ? 1 : min(16, CGFloat(0.9 / peak))76    }7778    var body: some View {79        GeometryReader { geo in80            let width = geo.size.width81            ZStack(alignment: .topLeading) {82                Canvas { context, size in83                    let rect = CGRect(origin: .zero, size: size).insetBy(dx: 0, dy: 22)84                    var c = context85                    c.stroke(Path { $0.move(to: CGPoint(x: 0, y: rect.midY)); $0.addLine(to: CGPoint(x: size.width, y: rect.midY)) }, with: .color(ORIX.Palette.plotGrid), lineWidth: 1)86                    if let selection = timeline.selection {87                        let a = timeline.x(for: selection.lowerBound, width: size.width), b = timeline.x(for: selection.upperBound, width: size.width)88                        c.fill(Path(CGRect(x: a, y: 0, width: max(1, b - a), height: size.height)), with: .color(Color.white.opacity(0.12)))89                        for x in [a, b] { c.stroke(Path { $0.move(to: CGPoint(x: x, y: 0)); $0.addLine(to: CGPoint(x: x, y: size.height)) }, with: .color(.white.opacity(0.8)), lineWidth: 1.5) }90                    }91                    if let summary {92                        let barWidth: CGFloat = 393                        let count = max(1, Int(size.width / barWidth))94                        let view = summary.buckets(for: timeline.visible, points: count)95                        if !view.buckets.isEmpty {96                            // Map buckets to x through their own time span so partially visible ends stay aligned.97                            let first = Double(view.buckets.startIndex) * view.secondsPerBucket98                            let span = Double(view.buckets.count) * view.secondsPerBucket99                            let x0 = timeline.x(for: first, width: size.width), x1 = timeline.x(for: first + span, width: size.width)100                            let bars = WaveformRendering.rebin(view.buckets, to: max(1, Int((x1 - x0) / barWidth)))101                            WaveformRendering.draw(bars, in: CGRect(x: x0, y: rect.minY, width: x1 - x0, height: rect.height), context: &c, style: style,102                                                   barWidth: (x1 - x0) / CGFloat(max(1, bars.count)), gain: displayGain)103                        }104                    }105                    for marker in markers where timeline.visible.contains(marker.time) {106                        let x = timeline.x(for: marker.time, width: size.width)107                        c.stroke(Path { $0.move(to: CGPoint(x: x, y: 20)); $0.addLine(to: CGPoint(x: x, y: size.height)) }, with: .color(ORIX.Palette.warning.opacity(0.9)), style: StrokeStyle(lineWidth: 1, dash: [4, 3]))108                        c.draw(Text(Image(systemName: "flag.fill")).font(.system(size: 9)).foregroundStyle(ORIX.Palette.warning), at: CGPoint(x: x + 2, y: 26), anchor: .leading)109                    }110                    if timeline.visible.contains(playhead) {111                        let x = timeline.x(for: playhead, width: size.width)112                        c.stroke(Path { $0.move(to: CGPoint(x: x, y: 0)); $0.addLine(to: CGPoint(x: x, y: size.height)) }, with: .color(.white), lineWidth: 1.5)113                    }114                }115                TimeRuler(timeline: timeline)116                if displayGain > 1.05 {117                    Text("display ×\(String(format: "%.1f", Double(displayGain)))").font(.system(size: 9, weight: .medium, design: .monospaced)).foregroundStyle(ORIX.Palette.plotText)118                        .padding(4).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomTrailing)119                        .accessibilityLabel(Text("Waveform height magnified \(String(format: "%.1f", Double(displayGain))) times for display"))120                }121                if summary == nil {122                    HStack(spacing: 8) { if isLoading { ProgressView().tint(.white) }; Text(isLoading ? "Building waveform…" : "Waveform not computed yet").font(.footnote).foregroundStyle(ORIX.Palette.plotText) }123                        .frame(maxWidth: .infinity, maxHeight: .infinity)124                }125            }126            .modifier(TimelineGestures(timeline: $timeline, width: width, isSelecting: isSelecting) { onSeek(timeline.time(forX: $0.x, width: width)) })127        }128        .accessibilityElement(children: .ignore)129        .accessibilityLabel(Text("Waveform"))130        .accessibilityValue(Text("Position \(Timecode.clock(playhead)) of \(Timecode.clock(timeline.duration))"))131        .accessibilityAdjustableAction { direction in onSeek(playhead + (direction == .increment ? 5 : -5)) }132    }133}134135struct FileSpectrogramView: View {136    let store: SpectrogramTileStore?137    @Binding var timeline: TimelineState138    let display: SpectrogramDisplay139    let playhead: Double140    let onSeek: (Double) -> Void141    @State private var images: [String: CGImage] = [:]142    @State private var inspect: (time: Double, frequency: Double, value: Float?)?143    private static let rows = 256144145    private func imageKey(_ key: SpectrogramTileStore.Key) -> String {146        "\(key.level)-\(key.index)-\(key.fftSize)-\(display.palette.rawValue)-\(display.axis.rawValue)-\(Int(display.floorDB))-\(Int(display.ceilingDB))-\(Int(display.minFrequency))-\(Int(display.maxFrequency))"147    }148149    var body: some View {150        GeometryReader { geo in151            let width = geo.size.width, height = geo.size.height152            ZStack(alignment: .topLeading) {153                ORIX.Palette.plot154                if let store {155                    let level = store.level(forSecondsPerPoint: timeline.visibleDuration / Double(max(1, width)), fftSize: display.fftSize)156                    let tileSeconds = store.tileDuration(level: level, fftSize: display.fftSize)157                    let first = max(0, Int(timeline.visibleStart / tileSeconds)), last = max(first, Int(timeline.visibleEnd / tileSeconds))158                    let keys = (first...last).map { SpectrogramTileStore.Key(level: level, index: $0, fftSize: display.fftSize) }159                    Canvas { context, size in160                        for key in keys {161                            guard let image = images[imageKey(key)] else { continue }162                            let start = Double(key.index) * tileSeconds163                            let span = tileSeconds * Double(image.width) / Double(SpectrogramTileStore.tileColumns)164                            let x0 = timeline.x(for: start, width: size.width), x1 = timeline.x(for: start + span, width: size.width)165                            context.draw(Image(decorative: image, scale: 1), in: CGRect(x: x0, y: 0, width: x1 - x0 + 0.5, height: size.height))166                        }167                        if timeline.visible.contains(playhead) {168                            let x = timeline.x(for: playhead, width: size.width)169                            context.stroke(Path { $0.move(to: CGPoint(x: x, y: 0)); $0.addLine(to: CGPoint(x: x, y: size.height)) }, with: .color(.white), lineWidth: 1.5)170                        }171                        if let inspect, timeline.visible.contains(inspect.time) {172                            let x = timeline.x(for: inspect.time, width: size.width)173                            let y = size.height * (1 - CGFloat(display.axis.position(of: inspect.frequency, min: lowFrequency, max: topFrequency(store))))174                            context.stroke(Path(ellipseIn: CGRect(x: x - 7, y: y - 7, width: 14, height: 14)), with: .color(.white), lineWidth: 1.5)175                        }176                    }177                    .task(id: keys.map(imageKey).joined(separator: "|")) { await load(keys, store: store) }178                    FrequencyAxisLabels(display: display, sampleRate: store.sampleRate)179                    if let inspect {180                        Text("\(Timecode.precise(inspect.time))  \(Formatters.frequency(inspect.frequency))  \(inspect.value.map { Formatters.dB(Double($0), unit: "dBFS") } ?? "—")")181                            .font(.orixReadout).foregroundStyle(.white).padding(.horizontal, 8).padding(.vertical, 4).background(.black.opacity(0.65), in: Capsule())182                            .frame(maxWidth: .infinity, alignment: .trailing).padding(8)183                    }184                    Color.clear185                        .modifier(TimelineGestures(timeline: $timeline, width: width, isSelecting: false) { point in186                            let time = timeline.time(forX: point.x, width: width)187                            let frequency = display.axis.frequency(at: Double(1 - point.y / max(1, height)), min: lowFrequency, max: topFrequency(store))188                            onSeek(time)189                            inspect = (time, frequency, nil)190                            Task { let v = await store.value(time: time, frequency: frequency, level: level, fftSize: display.fftSize); if inspect?.time == time { inspect?.value = v } }191                        })192                } else {193                    Text("This file can't be analysed.").font(.footnote).foregroundStyle(ORIX.Palette.plotText).frame(maxWidth: .infinity, maxHeight: .infinity)194                }195            }196        }197        .accessibilityElement(children: .ignore)198        .accessibilityLabel(Text("Spectrogram"))199        .accessibilityValue(Text("\(display.palette.displayName), \(Int(display.floorDB)) to \(Int(display.ceilingDB)) decibels full scale"))200    }201202    private var lowFrequency: Double { display.axis == .logarithmic ? max(display.minFrequency, 10) : display.minFrequency }203    private func topFrequency(_ store: SpectrogramTileStore) -> Double { min(display.maxFrequency, store.sampleRate / 2) }204205    private func load(_ keys: [SpectrogramTileStore.Key], store: SpectrogramTileStore) async {206        // Keep memory bounded: drop images that are no longer on screen (the tile store keeps the numeric data it needs).207        let wanted = Set(keys.map(imageKey))208        if images.count > 24 { images = images.filter { wanted.contains($0.key) } }209        let display = display210        for key in keys where images[imageKey(key)] == nil {211            if Task.isCancelled { return }212            guard let tile = await store.tile(key) else { continue }213            let rows = Self.rows214            let image = await Task.detached(priority: .userInitiated) { SpectrogramImage.render(tile: tile, rows: rows, display: display) }.value215            if let image, !Task.isCancelled { images[imageKey(key)] = image }216        }217    }218}219220struct SpectrogramSettingsMenu: View {221    @Binding var display: SpectrogramDisplay222    let sampleRate: Double223    var body: some View {224        Menu {225            Picker("Palette", selection: $display.palette) { ForEach(Colormap.allCases) { Text(CoreText.name($0)).tag($0) } }226            Picker("Frequency axis", selection: $display.axis) { Text("Logarithmic").tag(FrequencyAxis.logarithmic); Text("Linear").tag(FrequencyAxis.linear) }227            Picker("Frequency resolution", selection: $display.fftSize) {228                ForEach([512, 1024, 2048, 4096, 8192], id: \.self) { n in Text("\(String(format: "%.1f", sampleRate / Double(n))) Hz (FFT \(n))").tag(n) }229            }230            Picker("Top frequency", selection: $display.maxFrequency) {231                ForEach([2000.0, 4000, 8000, 12_000, 20_000], id: \.self) { f in if f <= max(2000, sampleRate / 2) { Text(Formatters.frequency(f)).tag(f) } }232            }233            Picker("Quietest level shown", selection: $display.floorDB) { ForEach([Float(-120), -100, -80, -60], id: \.self) { Text("\(Int($0)) dBFS").tag($0) } }234            Picker("Loudest level shown", selection: $display.ceilingDB) { ForEach([Float(0), -10, -20, -30], id: \.self) { Text("\(Int($0)) dBFS").tag($0) } }235        } label: { Label("Spectrogram settings", systemImage: "slider.horizontal.3").labelStyle(.iconOnly) }236    }237}238
Files main
Menu

Theme