SPB Git

Repository Public License

Models.swift 7.9 KB 191 lines swift

1//2//  ORIX Audio3//  File: ORIXAudio/Models/Models.swift4//  Description: SwiftData models (recordings, folders, workflows) and the value types stored with them.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//9import Foundation10import ORIXAudioCore11import SwiftData1213enum RecordingKind: String, Codable, CaseIterable, Identifiable {14    case memo, meeting, lecture, interview, imported15    var id: String { rawValue }16    var notesStyle: NotesStyle {17        switch self { case .meeting: .meeting; case .lecture: .lecture; case .interview: .interview; default: .memo }18    }19    var symbol: String {20        switch self {21        case .memo: "waveform"22        case .meeting: "person.3.fill"23        case .lecture: "graduationcap.fill"24        case .interview: "bubble.left.and.bubble.right.fill"25        case .imported: "square.and.arrow.down.fill"26        }27    }28}2930enum RecordingSource: String, Codable { case recorded, importedAudio, importedVideo, edited }31enum ProcessingStatus: String, Codable { case none, running, done, failed }3233struct Marker: Codable, Hashable, Identifiable, Sendable {34    var id = UUID()35    var time: Double36    var label: String37}3839@Model final class Recording {40    @Attribute(.unique) var id: UUID41    var title: String42    var createdAt: Date43    var duration: Double44    var kindRaw: String45    var sourceRaw: String46    /// Audio file name inside this recording's folder.47    var fileName: String48    var codec: String49    var sampleRate: Double50    var channels: Int51    var bitDepth: Int?52    var fileSize: Int6453    var isLossless: Bool54    var isFavorite: Bool55    var deletedAt: Date?56    var notes: String57    var participants: String58    var markersData: Data59    var originalFileName: String?60    var localeIdentifier: String?61    var transcriptStatusRaw: String62    var analysisStatusRaw: String63    var hasNotes: Bool64    var peakDBFS: Double?65    var integratedLUFS: Double?66    var folder: Folder?6768    init(id: UUID = UUID(), title: String, createdAt: Date = .now, kind: RecordingKind, source: RecordingSource, info: AudioFileInfo, fileName: String) {69        self.id = id; self.title = title; self.createdAt = createdAt70        duration = info.duration; kindRaw = kind.rawValue; sourceRaw = source.rawValue; self.fileName = fileName71        codec = info.codec; sampleRate = info.sampleRate; channels = info.channels; bitDepth = info.bitDepth; fileSize = info.fileSize; isLossless = info.isLossless72        isFavorite = false; notes = ""; participants = ""; markersData = Data()73        transcriptStatusRaw = ProcessingStatus.none.rawValue; analysisStatusRaw = ProcessingStatus.none.rawValue; hasNotes = false74    }7576    var kind: RecordingKind { get { RecordingKind(rawValue: kindRaw) ?? .memo } set { kindRaw = newValue.rawValue } }77    var source: RecordingSource { RecordingSource(rawValue: sourceRaw) ?? .recorded }78    var transcriptStatus: ProcessingStatus { get { ProcessingStatus(rawValue: transcriptStatusRaw) ?? .none } set { transcriptStatusRaw = newValue.rawValue } }79    var analysisStatus: ProcessingStatus { get { ProcessingStatus(rawValue: analysisStatusRaw) ?? .none } set { analysisStatusRaw = newValue.rawValue } }80    var markers: [Marker] {81        get { (try? JSONDecoder().decode([Marker].self, from: markersData)) ?? [] }82        set { markersData = (try? JSONEncoder().encode(newValue.sorted { $0.time < $1.time })) ?? Data() }83    }84    var audioURL: URL { LibraryFiles.folder(for: id).appendingPathComponent(fileName) }85    var isDeleted2: Bool { deletedAt != nil }8687    func apply(_ info: AudioFileInfo) {88        duration = info.duration; codec = info.codec; sampleRate = info.sampleRate; channels = info.channels89        bitDepth = info.bitDepth; fileSize = info.fileSize; isLossless = info.isLossless90    }9192    /// e.g. "AAC-LC · 48 kHz · Mono" — always what the file actually is.93    var formatLine: String {94        var parts = [codec]95        if let bitDepth, isLossless { parts.append("\(bitDepth)-bit") }96        parts.append(Formatters.sampleRate(sampleRate))97        parts.append(channels == 1 ? String(localized: "Mono") : channels == 2 ? String(localized: "Stereo") : String(localized: "\(channels) ch"))98        return parts.joined(separator: " · ")99    }100}101102@Model final class Folder {103    @Attribute(.unique) var id: UUID104    var name: String105    var createdAt: Date106    @Relationship(deleteRule: .nullify, inverse: \Recording.folder) var recordings: [Recording] = []107    init(name: String) { id = UUID(); self.name = name; createdAt = .now }108}109110/// One step of an ORIX Action. Every case maps to a real operation in `ActionRunner`.111enum ActionStep: Codable, Hashable, Identifiable {112    case analyze113    case transcribe(localeIdentifier: String)114    case summarize115    case removeSilence(minimumSeconds: Double)116    case normalize(targetPeakDBFS: Double)117    case enhance(noiseReduction: Double, voice: Bool, highPassHz: Double)118    case convert(format: AudioExportFormat)119    case exportTranscript(format: TranscriptFileFormat)120    case exportReport121122    var id: String { "\(self)" }123    /// Steps that produce a new audio file from the recording.124    var producesAudio: Bool {125        switch self { case .removeSilence, .normalize, .enhance, .convert: true; default: false }126    }127}128129enum TranscriptFileFormat: String, Codable, CaseIterable, Identifiable {130    case txt, markdown, srt, vtt, csv, json, pdf131    var id: String { rawValue }132    var fileExtension: String { self == .markdown ? "md" : rawValue }133    var displayName: String {134        switch self { case .txt: "Text"; case .markdown: "Markdown"; case .srt: "SRT"; case .vtt: "WebVTT"; case .csv: "CSV"; case .json: "JSON"; case .pdf: "PDF" }135    }136}137138enum WorkflowTrigger: String, Codable, CaseIterable, Identifiable {139    case manual, afterRecording, afterImport140    var id: String { rawValue }141}142143@Model final class Workflow {144    @Attribute(.unique) var id: UUID145    var name: String146    var stepsData: Data147    var triggerRaw: String148    var createdAt: Date149    var symbol: String150151    init(name: String, steps: [ActionStep], trigger: WorkflowTrigger = .manual, symbol: String = "bolt.fill") {152        id = UUID(); self.name = name; stepsData = (try? JSONEncoder().encode(steps)) ?? Data(); triggerRaw = trigger.rawValue; createdAt = .now; self.symbol = symbol153    }154    var steps: [ActionStep] {155        get { (try? JSONDecoder().decode([ActionStep].self, from: stepsData)) ?? [] }156        set { stepsData = (try? JSONEncoder().encode(newValue)) ?? Data() }157    }158    var trigger: WorkflowTrigger { get { WorkflowTrigger(rawValue: triggerRaw) ?? .manual } set { triggerRaw = newValue.rawValue } }159}160161/// How a recording session is captured and what file it becomes.162enum RecordingPreset: String, Codable, CaseIterable, Identifiable {163    case voice, meeting, lecture, interview, highQuality, lossless164    var id: String { rawValue }165166    var kind: RecordingKind {167        switch self { case .meeting: .meeting; case .lecture: .lecture; case .interview: .interview; default: .memo }168    }169    var format: AudioExportFormat {170        switch self { case .highQuality: .wav24; case .lossless: .alac; default: .aac }171    }172    /// AAC bit rate per channel, bits per second.173    var aacBitRatePerChannel: Int {174        switch self { case .voice: 64_000; case .meeting: 96_000; case .lecture: 80_000; case .interview: 96_000; default: 128_000 }175    }176    /// Stereo is used only when the active input really delivers two channels.177    var prefersStereo: Bool { self == .interview || self == .highQuality || self == .lossless }178    /// Capture master sample format (the master is always uncompressed PCM).179    var masterFormat: PCMSampleFormat { format == .aac ? .int16 : .int24 }180    var symbol: String {181        switch self {182        case .voice: "mic.fill"183        case .meeting: "person.3.fill"184        case .lecture: "graduationcap.fill"185        case .interview: "bubble.left.and.bubble.right.fill"186        case .highQuality: "hifispeaker.fill"187        case .lossless: "waveform.badge.checkmark"188        }189    }190}191
Files main
Menu

Theme