SPB Git

Repository Public License

EditorView.swift 25.0 KB 424 lines swift

1//2//  ORIX Audio3//  File: ORIXAudio/Features/Editor/EditorView.swift4//  Description: Non-destructive audio editor: selection-based operations, fades, gain, normalize, enhancement chain, merge, undo / redo, save as new.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//9import ORIXAudioCore10import SwiftData11import SwiftUI1213@MainActor @Observable14final class EditorModel {15    let recording: Recording16    var document: EditDocument17    var timeline: TimelineState18    private(set) var summary: WaveformSummary?19    private(set) var isRendering = false20    private(set) var renderProgress = 0.021    private(set) var previewURL: URL?22    var error: String?23    private var renderTask: Task<Void, Never>?24    private var extraSources: [String: URL] = [:]2526    init(recording: Recording) {27        self.recording = recording28        let loaded = LibraryFiles.load(EditDocument.self, from: LibraryFiles.editURL(recording.id)) ?? EditDocument(sourceName: recording.fileName, duration: recording.duration)29        document = loaded30        timeline = TimelineState(duration: loaded.duration)31    }3233    var sourceURLs: [URL] {34        let folder = LibraryFiles.folder(for: recording.id)35        return document.sources.map { extraSources[$0] ?? folder.appendingPathComponent($0) }36    }3738    /// Applies an edit, persists the document (with its undo history) and re-renders the preview.39    func apply(_ body: (inout EditDocument) -> Void) {40        body(&document)41        try? LibraryFiles.save(document, to: LibraryFiles.editURL(recording.id))42        timeline.setDuration(document.duration)43        timeline.selection = nil44        renderPreview()45    }4647    /// Copies another recording's audio into this recording's folder so the edit document stays self-contained.48    func merge(_ other: Recording) {49        guard abs(other.sampleRate - recording.sampleRate) < 0.5 else {50            error = String(localized: "Both recordings must have the same sample rate to be merged (\(Formatters.sampleRate(recording.sampleRate)) and \(Formatters.sampleRate(other.sampleRate)))."); return51        }52        let name = "merge-\(other.id.uuidString.prefix(8)).\(other.audioURL.pathExtension)"53        let destination = LibraryFiles.folder(for: recording.id).appendingPathComponent(name)54        if !FileManager.default.fileExists(atPath: destination.path) {55            do { try FileManager.default.copyItem(at: other.audioURL, to: destination) } catch { self.error = error.localizedDescription; return }56        }57        apply { $0.append(sourceName: name, duration: other.duration) }58    }5960    /// Renders the edited programme to a temporary file for playback, building the waveform in the same pass.61    func renderPreview() {62        renderTask?.cancel()63        let document = document, urls = sourceURLs64        isRendering = true; renderProgress = 065        renderTask = Task {66            let out = FileManager.default.temporaryDirectory.appendingPathComponent("orix-preview-\(UUID().uuidString).caf")67            do {68                let result = try await Task.detached(priority: .userInitiated) { [weak self] () -> WaveformSummary in69                    let renderer = try EditRenderer(document: document, sourceURLs: urls)70                    let writer = try AudioFileWriter(url: out, format: .caf24, inputSampleRate: renderer.sampleRate, channels: renderer.channels)71                    let waveform = WaveformProcessor(sampleRate: renderer.sampleRate)72                    var buckets: [WaveformBucket] = []73                    var total = 074                    try renderer.render(progress: { p in Task { @MainActor in self?.renderProgress = p } }, isCancelled: { Task.isCancelled }) { chunk in75                        try writer.write(chunk)76                        let mono = AudioFileReader.mono(chunk)77                        mono.withUnsafeBufferPointer { waveform.push($0, into: &buckets) }78                        total += mono.count79                    }80                    try writer.finish()81                    waveform.finish(into: &buckets)82                    return WaveformSummary(sampleRate: renderer.sampleRate, samplesPerBucket: waveform.samplesPerBucket, totalSamples: total, base: buckets)83                }.value84                if Task.isCancelled { try? FileManager.default.removeItem(at: out); return }85                if let old = previewURL { try? FileManager.default.removeItem(at: old) }86                summary = result; previewURL = out; isRendering = false87            } catch {88                try? FileManager.default.removeItem(at: out)89                if !Task.isCancelled { if case AudioIOError.cancelled = error {} else { self.error = CoreText.describe(error) }; isRendering = false }90            }91        }92    }9394    func cleanup() { renderTask?.cancel(); if let previewURL { try? FileManager.default.removeItem(at: previewURL) } }95}9697struct EditorView: View {98    @Environment(AppModel.self) private var model99    @Environment(AppSettings.self) private var settings100    @Environment(\.dismiss) private var dismiss101    @State private var editor: EditorModel102    @State private var showEnhance = false103    @State private var showMerge = false104    @State private var showSave = false105    @State private var gainDB = 0.0106    @State private var showGain = false107    @State private var fadeSeconds = 1.0108    @State private var saving = false109    @State private var share: SharePayload?110111    init(recording: Recording) { _editor = State(initialValue: EditorModel(recording: recording)) }112113    private var playback: PlaybackEngine { model.playback }114    private var isLoaded: Bool { editor.previewURL != nil && playback.loadedURL == editor.previewURL }115    private var playhead: Double { isLoaded ? playback.currentTime : 0 }116    private var selection: ClosedRange<Double>? { editor.timeline.selection }117118    var body: some View {119        @Bindable var editor = editor120        NavigationStack {121            VStack(spacing: ORIX.Space.m) {122                PlotFrame {123                    FileWaveformView(summary: editor.summary, timeline: $editor.timeline, style: settings.waveformStyle, playhead: playhead, markers: [],124                                     isSelecting: true, isLoading: editor.isRendering) { time in loadPreview(); playback.seek(to: time) }125                }126                .frame(minHeight: 150, maxHeight: 240)127                .overlay(alignment: .top) { if editor.isRendering { ProgressView(value: editor.renderProgress).tint(ORIX.Palette.cyan).padding(.horizontal, 12).padding(.top, 26) } }128129                HStack {130                    Text(Timecode.precise(playhead)).font(.orixReadout)131                    Spacer()132                    if let s = selection { Text("\(Timecode.precise(s.lowerBound)) – \(Timecode.precise(s.upperBound)) (\(Timecode.precise(s.upperBound - s.lowerBound)))").font(.orixReadout).foregroundStyle(ORIX.Palette.cyan) }133                    else { Text("Drag on the waveform to select").font(.caption).foregroundStyle(ORIX.Palette.textSecondary) }134                    Spacer()135                    Text(Timecode.precise(editor.document.duration)).font(.orixReadout).foregroundStyle(ORIX.Palette.textSecondary)136                }137138                HStack(spacing: 18) {139                    Button { editor.apply { $0.undo() } } label: { Image(systemName: "arrow.uturn.backward.circle").font(.title2) }.disabled(!editor.document.canUndo).accessibilityLabel(Text("Undo"))140                    Button { editor.apply { $0.redo() } } label: { Image(systemName: "arrow.uturn.forward.circle").font(.title2) }.disabled(!editor.document.canRedo).accessibilityLabel(Text("Redo"))141                    Spacer()142                    Button { loadPreview(); playback.loopRange = selection; playback.togglePlay() } label: {143                        Image(systemName: playback.isPlaying && isLoaded ? "pause.fill" : "play.fill").font(.title2).foregroundStyle(.white).frame(width: 54, height: 54).background(ORIX.brandGradient, in: Circle())144                    }145                    .buttonStyle(.plain).disabled(editor.previewURL == nil).accessibilityLabel(playback.isPlaying && isLoaded ? Text("Pause") : Text("Play the edit"))146                    Spacer()147                    Button { editor.timeline.reset() } label: { Image(systemName: "arrow.left.and.right.circle").font(.title2) }.disabled(!editor.timeline.isZoomed).accessibilityLabel(Text("Show everything"))148                    Button { editor.timeline.selection = nil; playback.loopRange = nil } label: { Image(systemName: "xmark.circle").font(.title2) }.disabled(selection == nil).accessibilityLabel(Text("Clear selection"))149                }150151                ScrollView {152                    VStack(spacing: ORIX.Space.m) {153                        toolGrid154                        if !editor.document.state.effects.isEmpty || editor.document.state.masterGainDB != 0 || editor.document.state.fadeIn > 0 || editor.document.state.fadeOut > 0 || editor.document.state.channelMode != .original {155                            appliedCard156                        }157                        Text("Edits are non-destructive: the original file is never changed. Saving creates a new recording.").font(.caption).foregroundStyle(ORIX.Palette.textSecondary)158                    }159                }160            }161            .padding(.horizontal, ORIX.Space.l)162            .orixScreenBackground()163            .navigationTitle("Edit audio").navigationBarTitleDisplayMode(.inline)164            .toolbar {165                ToolbarItem(placement: .cancellationAction) { Button("Close") { playback.pause(); playback.unload(); editor.cleanup(); dismiss() } }166                ToolbarItem(placement: .confirmationAction) { Button("Save as new") { showSave = true }.disabled(!editor.document.isEdited || editor.isRendering).accessibilityIdentifier("saveEditButton") }167            }168            .task { editor.renderPreview() }169            .onChange(of: editor.previewURL) { _, _ in if playback.isPlaying { playback.pause() } }170            .sheet(isPresented: $showEnhance) { EnhanceSheet(effects: editor.document.state.effects) { effects in editor.apply { $0.setEffects(effects) } } }171            .sheet(isPresented: $showMerge) { MergePickerSheet(excluding: editor.recording.id) { editor.merge($0) } }172            .confirmationDialog("Save the edit as a new recording", isPresented: $showSave, titleVisibility: .visible) {173                ForEach(AudioExportFormat.allCases) { format in Button(format.displayName) { save(format) } }174            } message: { Text("Lossless formats keep the full quality of the edit. AAC is smaller but lossy.") }175            .alert("Gain", isPresented: $showGain) {176                TextField("dB", value: $gainDB, format: .number).keyboardType(.numbersAndPunctuation)177                Button("Apply") { let g = gainDB, r = selection; editor.apply { $0.adjustGain(by: g, in: r) } }178                Button("Cancel", role: .cancel) {}179            } message: { Text(selection == nil ? "Gain in decibels for the whole recording (negative to reduce)." : "Gain in decibels for the selection (negative to reduce).") }180            .alert("Editor", isPresented: Binding(get: { editor.error != nil }, set: { if !$0 { editor.error = nil } })) { Button("OK", role: .cancel) {} } message: { Text(editor.error ?? "") }181            .overlay { if saving { ProgressView("Saving…").padding(24).background(.regularMaterial, in: RoundedRectangle(cornerRadius: 16)) } }182            .shareSheet($share)183        }184    }185186    private func loadPreview() {187        guard let url = editor.previewURL, playback.loadedURL != url else { return }188        try? playback.load(url: url, title: editor.recording.title)189    }190191    private var toolGrid: some View {192        LazyVGrid(columns: [GridItem(.adaptive(minimum: 104), spacing: 8)], spacing: 8) {193            tool("Trim to selection", "crop", needsSelection: true) { if let s = selection { editor.apply { $0.crop(to: s) } } }194            tool("Delete selection", "delete.left", needsSelection: true) { if let s = selection { editor.apply { $0.delete(s) } } }195            tool("Silence selection", "speaker.slash", needsSelection: true) { if let s = selection { editor.apply { $0.silence(s) } } }196            tool("Split at playhead", "scissors") { let t = playhead; editor.apply { $0.split(at: t) } }197            tool("Insert 1 s silence", "plus.rectangle") { let t = playhead; editor.apply { $0.insertSilence(at: t, duration: 1) } }198            tool("Fade in", "chart.line.uptrend.xyaxis") { let d = selection.map { $0.upperBound - $0.lowerBound } ?? fadeSeconds; editor.apply { $0.setFadeIn(min(d, $0.duration / 2)) } }199            tool("Fade out", "chart.line.downtrend.xyaxis") { let d = selection.map { $0.upperBound - $0.lowerBound } ?? fadeSeconds; editor.apply { $0.setFadeOut(min(d, $0.duration / 2)) } }200            tool("Gain…", "dial.medium") { gainDB = 0; showGain = true }201            tool("Normalize to −1 dBFS", "arrow.up.to.line") { normalize() }202            tool("Remove silences", "waveform.badge.minus") { removeSilences() }203            tool("Enhance…", "wand.and.stars") { showEnhance = true }204            tool("Merge recording…", "arrow.triangle.merge") { showMerge = true }205            Menu {206                Picker("Channels", selection: Binding(get: { editor.document.state.channelMode }, set: { mode in editor.apply { $0.setChannelMode(mode) } })) {207                    Text("Original").tag(ChannelMode.original); Text("Mono mix").tag(ChannelMode.mono); Text("Left only").tag(ChannelMode.left); Text("Right only").tag(ChannelMode.right)208                }209            } label: { toolLabel("Channels", "speaker.wave.2") }210            .disabled(editor.recording.channels < 2)211            tool("Export selection", "square.and.arrow.up", needsSelection: true) { exportSelection() }212        }213    }214215    private func toolLabel(_ title: LocalizedStringKey, _ symbol: String) -> some View {216        VStack(spacing: 6) {217            Image(systemName: symbol).font(.title3).foregroundStyle(ORIX.Palette.cyan)218            Text(title).font(.caption).multilineTextAlignment(.center).lineLimit(2).minimumScaleFactor(0.8).foregroundStyle(ORIX.Palette.text)219        }220        .frame(maxWidth: .infinity, minHeight: 64).padding(8)221        .background(ORIX.Palette.surface, in: RoundedRectangle(cornerRadius: ORIX.Radius.control, style: .continuous))222        .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.control, style: .continuous).strokeBorder(ORIX.Palette.hairline, lineWidth: 1))223    }224225    private func tool(_ title: LocalizedStringKey, _ symbol: String, needsSelection: Bool = false, action: @escaping () -> Void) -> some View {226        Button(action: action) { toolLabel(title, symbol) }227            .buttonStyle(.plain)228            .disabled((needsSelection && selection == nil) || editor.isRendering)229            .opacity(needsSelection && selection == nil ? 0.4 : 1)230    }231232    private var appliedCard: some View {233        ORIXCard(padding: ORIX.Space.m) {234            VStack(alignment: .leading, spacing: 6) {235                SectionTitle("Applied to the whole recording", symbol: "slider.horizontal.3")236                let s = editor.document.state237                if s.masterGainDB != 0 { Text("Gain \(Formatters.dB(s.masterGainDB))").font(.footnote) }238                if s.fadeIn > 0 { Text("Fade in \(s.fadeIn.formatted(.number.precision(.fractionLength(1)))) s").font(.footnote) }239                if s.fadeOut > 0 { Text("Fade out \(s.fadeOut.formatted(.number.precision(.fractionLength(1)))) s").font(.footnote) }240                if s.channelMode != .original { Text("Channels: \(CoreText.name(s.channelMode))").font(.footnote) }241                ForEach(Array(s.effects.enumerated()), id: \.offset) { _, effect in Text(EffectText.describe(effect)).font(.footnote) }242            }243        }244    }245246    private func normalize() {247        let document = editor.document, urls = editor.sourceURLs248        Task {249            do {250                let peak = try await Task.detached { try EditRenderer(document: document, sourceURLs: urls).measurePeakDBFS() }.value251                editor.apply { $0.normalize(measuredPeakDBFS: peak, targetPeakDBFS: -1) }252            } catch { editor.error = error.localizedDescription }253        }254    }255256    /// Uses the silences measured on the current preview (≥ 1 s below −50 dBFS).257    private func removeSilences() {258        guard let url = editor.previewURL else { return }259        Task {260            do {261                let report = try await Task.detached { try AudioAnalyzer.analyze(url: url).report }.value262                let silences = report.time.silences.filter { $0.duration >= 1 }263                if silences.isEmpty { editor.error = String(localized: "No silence longer than one second was found.") }264                else { editor.apply { $0.removeSilences(silences) } }265            } catch { editor.error = error.localizedDescription }266        }267    }268269    private func save(_ format: AudioExportFormat) {270        let document = editor.document, urls = editor.sourceURLs, channels = editor.recording.channels271        saving = true272        playback.pause()273        Task {274            do {275                let created = try await model.addDerivedRecording(from: editor.recording, titleSuffix: String(localized: "(edited)"), format: format) { url in276                    try EditRenderer(document: document, sourceURLs: urls).export(to: url, format: format, aacBitRate: 128_000 * min(2, channels))277                }278                saving = false279                playback.unload(); editor.cleanup()280                dismiss()281                model.pendingOpen = created.id282            } catch { saving = false; editor.error = error.localizedDescription }283        }284    }285286    private func exportSelection() {287        guard let s = selection, let url = editor.previewURL else { return }288        saving = true289        let title = editor.recording.title, duration = editor.document.duration290        Task {291            do { share = SharePayload(urls: [try await ExportService.audio(sourceURL: url, title: "\(title) – selection", duration: duration, format: .wav24, range: s)]) }292            catch { editor.error = error.localizedDescription }293            saving = false294        }295    }296}297298enum EffectText {299    static func describe(_ effect: AudioEffect) -> String {300        switch effect {301        case .highPass(let f): String(localized: "High-pass \(Int(f)) Hz (24 dB/octave)")302        case .lowPass(let f): String(localized: "Low-pass \(Int(f)) Hz (24 dB/octave)")303        case .equalizer(let lo, let mid, let hi): String(localized: "EQ low \(lo.formatted()) dB · mid \(mid.formatted()) dB · high \(hi.formatted()) dB")304        case .compressor(let t, let r, _, _, let m): String(localized: "Compressor \(t.formatted()) dBFS · \(r.formatted()):1 · make-up \(m.formatted()) dB")305        case .noiseReduction(let s): String(localized: "Noise reduction \(Int(s * 100)) %")306        case .voiceEnhance: String(localized: "Voice enhance (80 Hz high-pass, presence lift, 3:1 compression)")307        }308    }309}310311struct EnhanceSheet: View {312    @Environment(\.dismiss) private var dismiss313    let onApply: ([AudioEffect]) -> Void314    @State private var noise: Double315    @State private var voice: Bool316    @State private var highPassOn: Bool317    @State private var highPass: Double318    @State private var lowPassOn: Bool319    @State private var lowPass: Double320    @State private var eqOn: Bool321    @State private var eqLow: Double322    @State private var eqMid: Double323    @State private var eqHigh: Double324    @State private var compOn: Bool325    @State private var threshold: Double326    @State private var ratio: Double327    @State private var makeup: Double328329    init(effects: [AudioEffect], onApply: @escaping ([AudioEffect]) -> Void) {330        self.onApply = onApply331        var noise = 0.0, voice = false, hpOn = false, hp = 80.0, lpOn = false, lp = 12_000.0, eqOn = false, lo = 0.0, mid = 0.0, hi = 0.0, cOn = false, t = -20.0, r = 3.0, m = 0.0332        for e in effects {333            switch e {334            case .noiseReduction(let s): noise = s335            case .voiceEnhance: voice = true336            case .highPass(let f): hpOn = true; hp = f337            case .lowPass(let f): lpOn = true; lp = f338            case .equalizer(let a, let b, let c): eqOn = true; lo = a; mid = b; hi = c339            case .compressor(let a, let b, _, _, let c): cOn = true; t = a; r = b; m = c340            }341        }342        _noise = State(initialValue: noise); _voice = State(initialValue: voice); _highPassOn = State(initialValue: hpOn); _highPass = State(initialValue: hp)343        _lowPassOn = State(initialValue: lpOn); _lowPass = State(initialValue: lp); _eqOn = State(initialValue: eqOn); _eqLow = State(initialValue: lo)344        _eqMid = State(initialValue: mid); _eqHigh = State(initialValue: hi); _compOn = State(initialValue: cOn); _threshold = State(initialValue: t)345        _ratio = State(initialValue: r); _makeup = State(initialValue: m)346    }347348    private var effects: [AudioEffect] {349        var out: [AudioEffect] = []350        if noise > 0 { out.append(.noiseReduction(strength: noise)) }351        if voice { out.append(.voiceEnhance) }352        if highPassOn { out.append(.highPass(frequencyHz: highPass)) }353        if lowPassOn { out.append(.lowPass(frequencyHz: lowPass)) }354        if eqOn { out.append(.equalizer(lowDB: eqLow, midDB: eqMid, highDB: eqHigh)) }355        if compOn { out.append(.compressor(thresholdDB: threshold, ratio: ratio, attackMs: 10, releaseMs: 120, makeupDB: makeup)) }356        return out357    }358359    var body: some View {360        NavigationStack {361            Form {362                Section {363                    LabeledContent("Strength", value: "\(Int(noise * 100)) %")364                    Slider(value: $noise, in: 0...1, step: 0.05)365                } header: { Text("Noise reduction") } footer: {366                    Text("Spectral subtraction using the quietest moments of the recording as the noise profile. Works on steady noise (hiss, hum, fans); it cannot separate voices or remove music.")367                }368                Section {369                    Toggle("Voice enhance", isOn: $voice)370                } footer: { Text("A fixed chain: 80 Hz high-pass, presence lift, gentle 3:1 compression.") }371                Section("Filters") {372                    Toggle("High-pass", isOn: $highPassOn)373                    if highPassOn { LabeledContent("Cutoff", value: "\(Int(highPass)) Hz"); Slider(value: $highPass, in: 20...500, step: 10) }374                    Toggle("Low-pass", isOn: $lowPassOn)375                    if lowPassOn { LabeledContent("Cutoff", value: Formatters.frequency(lowPass)); Slider(value: $lowPass, in: 2000...20_000, step: 500) }376                }377                Section("Equalizer") {378                    Toggle("3-band equalizer", isOn: $eqOn)379                    if eqOn {380                        LabeledContent("Low (150 Hz shelf)", value: "\(eqLow.formatted()) dB"); Slider(value: $eqLow, in: -12...12, step: 0.5)381                        LabeledContent("Mid (1.5 kHz)", value: "\(eqMid.formatted()) dB"); Slider(value: $eqMid, in: -12...12, step: 0.5)382                        LabeledContent("High (6 kHz shelf)", value: "\(eqHigh.formatted()) dB"); Slider(value: $eqHigh, in: -12...12, step: 0.5)383                    }384                }385                Section("Dynamics") {386                    Toggle("Compressor", isOn: $compOn)387                    if compOn {388                        LabeledContent("Threshold", value: "\(Int(threshold)) dBFS"); Slider(value: $threshold, in: -50...0, step: 1)389                        LabeledContent("Ratio", value: "\(ratio.formatted()):1"); Slider(value: $ratio, in: 1...10, step: 0.5)390                        LabeledContent("Make-up gain", value: "\(makeup.formatted()) dB"); Slider(value: $makeup, in: 0...18, step: 0.5)391                    }392                }393                Section { Text("De-essing and source separation are not offered: ORIX Audio has no validated implementation of either.").font(.footnote).foregroundStyle(ORIX.Palette.textSecondary) }394            }395            .navigationTitle("Enhance").navigationBarTitleDisplayMode(.inline)396            .toolbar {397                ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } }398                ToolbarItem(placement: .confirmationAction) { Button("Apply") { onApply(effects); dismiss() } }399            }400        }401    }402}403404struct MergePickerSheet: View {405    @Environment(\.dismiss) private var dismiss406    @Query(filter: #Predicate<Recording> { $0.deletedAt == nil }, sort: \Recording.createdAt, order: .reverse) private var recordings: [Recording]407    let excluding: UUID408    let onPick: (Recording) -> Void409    var body: some View {410        NavigationStack {411            List(recordings.filter { $0.id != excluding }) { r in412                Button { onPick(r); dismiss() } label: {413                    VStack(alignment: .leading) {414                        Text(r.title).foregroundStyle(ORIX.Palette.text)415                        Text("\(Formatters.duration(r.duration)) · \(r.formatLine)").font(.caption).foregroundStyle(ORIX.Palette.textSecondary)416                    }417                }418            }419            .navigationTitle("Append a recording").navigationBarTitleDisplayMode(.inline)420            .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } } }421        }422    }423}424
Files main
Menu

Theme