SPB Git

Repository Public License

DetailStore.swift 4.0 KB 96 lines swift

1//2//  ORIX Audio3//  File: ORIXAudio/Features/Detail/DetailStore.swift4//  Description: State of the recording workstation: sidecar data, shared timeline, spectrogram tiles and the file spectrum source.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//9import Foundation10import Observation11import ORIXAudioCore12import SwiftUI1314/// Computes spectrum frames from the file at the playhead. Keeps one analyzer so smoothing and peak hold behave as in the live view.15actor FileSpectrumSource {16    private let reader: AudioFileReader17    private var analyzer: SpectrumAnalyzer18    private var lastTime = -1.01920    init?(url: URL) {21        guard let r = try? AudioFileReader(url: url) else { return nil }22        reader = r23        analyzer = SpectrumAnalyzer(sampleRate: r.sampleRate)24    }2526    func frame(at time: Double, configuration: SpectrumConfiguration, continuous: Bool) -> SpectrumFrame? {27        var config = configuration28        if !continuous { config.smoothing = 0 }       // a paused playhead shows exactly the frame under it29        if config != analyzer.configuration { analyzer.update(config) }30        if !continuous || abs(time - lastTime) > 0.5 { analyzer.resetPeakHold() }31        let n = config.fftSize32        let centre = Int(time * reader.sampleRate)33        let start = max(0, min(reader.frameCount - n, centre - n / 2))34        guard start >= 0, var samples = try? reader.readMono(frames: start..<(start + n)) else { return nil }35        if samples.count < n { samples += [Float](repeating: 0, count: n - samples.count) }36        let elapsed = lastTime >= 0 && continuous ? max(0.001, min(0.25, time - lastTime)) : 0.0537        lastTime = time38        return samples.withUnsafeBufferPointer { analyzer.process($0.baseAddress!, elapsed: elapsed) }39    }40}4142enum DetailPanel: String, CaseIterable, Identifiable {43    case overview, spectrum, spectrogram, analysis, transcript, notes44    var id: String { rawValue }45    var title: LocalizedStringKey {46        switch self {47        case .overview: "Info"; case .spectrum: "Spectrum"; case .spectrogram: "Spectrogram"48        case .analysis: "Analysis"; case .transcript: "Transcript"; case .notes: "Notes"49        }50    }51    var symbol: String {52        switch self {53        case .overview: "info.circle"; case .spectrum: "chart.bar.xaxis"; case .spectrogram: "square.grid.3x3.fill"54        case .analysis: "chart.xyaxis.line"; case .transcript: "text.quote"; case .notes: "sparkles"55        }56    }57}5859@MainActor @Observable60final class DetailStore {61    let recording: Recording62    var timeline: TimelineState63    var summary: WaveformSummary?64    var report: AnalysisReport?65    var transcript: Transcript?66    var notes: MeetingNotes?67    var isSelecting = false68    var spectrogramDisplay = SpectrogramDisplay()69    private(set) var tileStore: SpectrogramTileStore?70    private(set) var spectrumSource: FileSpectrumSource?71    private(set) var loadedRevision = -17273    init(recording: Recording, palette: Colormap) {74        self.recording = recording75        timeline = TimelineState(duration: recording.duration)76        spectrogramDisplay.palette = palette77        spectrogramDisplay.maxFrequency = min(20_000, recording.sampleRate / 2)78        tileStore = SpectrogramTileStore(url: recording.audioURL)79        spectrumSource = FileSpectrumSource(url: recording.audioURL)80    }8182    /// Reloads sidecar files (off the main thread) when processing produced something new.83    func reload(revision: Int) async {84        guard revision != loadedRevision else { return }85        loadedRevision = revision86        let id = recording.id87        let loaded = await Task.detached(priority: .userInitiated) {88            (LibraryFiles.waveform(id), LibraryFiles.analysis(id), LibraryFiles.transcript(id), LibraryFiles.notes(id))89        }.value90        summary = loaded.0; report = loaded.1; transcript = loaded.2; notes = loaded.391    }9293    func marker(before time: Double) -> Marker? { recording.markers.last { $0.time < time - 0.3 } }94    func marker(after time: Double) -> Marker? { recording.markers.first { $0.time > time + 0.05 } }95}96
Files main
Menu

Theme