Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Audio9// File : ORIXAudio/Services/MediaImporter.swift10// Description : Imports audio files and extracts the audio track of video11// files into the library. Originals are never modified.12// Module : ORIXAudio13// Repository : https://git.spboucher.ai/orix-audio14// Created : 2026-11-2015// Last Modified : 2026-11-2016//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author : Simon-Pierre Boucher22// Position : Professor of Finance23// Institution : Université du Québec en Outaouais (UQO)24// Department : Département des sciences administratives25// Organization : SP Boucher26//27// Website : https://spboucher.ai28// Git : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General : info@spboucher.ai35// Professional : contact@spboucher.ai36// Direct : simon@spboucher.ai37// Research : research@spboucher.ai38// Development : dev@spboucher.ai39// Support : support@spboucher.ai40// Security/Admin : admin@spboucher.ai41// Notifications : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification : Public68// Distribution : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License : Proprietary, all rights reserved (see license file)82// License File : LICENSE83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import AVFoundation91import Foundation92import ORIXAudioCore93import UniformTypeIdentifiers9495enum ImportError: Error, LocalizedError {96 case noAudioTrack, unsupported(String), copyFailed(String)97 var errorDescription: String? {98 switch self {99 case .noAudioTrack: String(localized: "This file doesn't contain an audio track.")100 case .unsupported(let m): String(localized: "This file can't be decoded on this device (\(m)).")101 case .copyFailed(let m): String(localized: "The file couldn't be copied into the library (\(m)).")102 }103 }104}105106struct ImportedMedia: Sendable {107 var id: UUID108 var fileName: String109 var info: AudioFileInfo110 var originalName: String111 var isVideo: Bool112}113114enum MediaImporter {115 static let audioTypes: [UTType] = [.audio, .mpeg4Audio, .mp3, .wav, .aiff]116 static let videoTypes: [UTType] = [.movie, .video, .mpeg4Movie, .quickTimeMovie]117 static var allTypes: [UTType] { audioTypes + videoTypes }118119 static func isVideo(_ url: URL) -> Bool {120 guard let type = UTType(filenameExtension: url.pathExtension.lowercased()) else { return false }121 return type.conforms(to: .movie) || type.conforms(to: .video)122 }123124 /// Copies or extracts `source` into a new recording folder. Works on a security-scoped URL from the Files picker.125 static func importFile(at source: URL, progress: (@Sendable (Double) -> Void)? = nil) async throws -> ImportedMedia {126 let scoped = source.startAccessingSecurityScopedResource()127 defer { if scoped { source.stopAccessingSecurityScopedResource() } }128 let id = UUID()129 let folder = LibraryFiles.ensureFolder(for: id)130 do {131 if isVideo(source) {132 let (name, info) = try await extractAudio(from: source, into: folder, progress: progress)133 return ImportedMedia(id: id, fileName: name, info: info, originalName: source.lastPathComponent, isVideo: true)134 }135 let ext = source.pathExtension.isEmpty ? "m4a" : source.pathExtension.lowercased()136 let destination = folder.appendingPathComponent("audio.\(ext)")137 do { try FileManager.default.copyItem(at: source, to: destination) } catch { throw ImportError.copyFailed(error.localizedDescription) }138 try? FileManager.default.setAttributes([.protectionKey: LibraryFiles.protection], ofItemAtPath: destination.path)139 if var info = try? AudioFileReader.info(url: destination), info.frameCount > 0 {140 info.fileName = destination.lastPathComponent141 progress?(1)142 return ImportedMedia(id: id, fileName: destination.lastPathComponent, info: info, originalName: source.lastPathComponent, isVideo: false)143 }144 // AVAudioFile couldn't open it (some containers); try decoding through AVAssetReader instead.145 let (name, info) = try await extractAudio(from: destination, into: folder, progress: progress, outputName: "audio-decoded")146 try? FileManager.default.removeItem(at: destination)147 return ImportedMedia(id: id, fileName: name, info: info, originalName: source.lastPathComponent, isVideo: false)148 } catch {149 try? FileManager.default.removeItem(at: folder)150 throw error151 }152 }153154 /// Extracts the first audio track. AAC and Apple Lossless tracks are copied bit-for-bit into an .m4a (no re-encoding);155 /// anything else is decoded and stored as Apple Lossless so no further quality is lost.156 static func extractAudio(from source: URL, into folder: URL, progress: (@Sendable (Double) -> Void)?, outputName: String = "audio") async throws -> (String, AudioFileInfo) {157 let asset = AVURLAsset(url: source)158 let tracks: [AVAssetTrack]159 do { tracks = try await asset.loadTracks(withMediaType: .audio) } catch { throw ImportError.unsupported(error.localizedDescription) }160 guard let track = tracks.first else { throw ImportError.noAudioTrack }161 let duration = (try? await asset.load(.duration).seconds) ?? 0162 let descriptions = (try? await track.load(.formatDescriptions)) ?? []163 let subtype = descriptions.first.map { CMFormatDescriptionGetMediaSubType($0) }164 let destination = folder.appendingPathComponent("\(outputName).m4a")165 try? FileManager.default.removeItem(at: destination)166167 if subtype == kAudioFormatMPEG4AAC || subtype == kAudioFormatAppleLossless,168 let export = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetPassthrough) {169 let composition = AVMutableComposition()170 if let target = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid),171 let range = try? await track.load(.timeRange), (try? target.insertTimeRange(range, of: track, at: .zero)) != nil,172 let passthrough = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetPassthrough) {173 _ = export174 do {175 try await passthrough.export(to: destination, as: .m4a)176 if var info = try? AudioFileReader.info(url: destination), info.frameCount > 0 {177 info.fileName = destination.lastPathComponent178 progress?(1)179 return (destination.lastPathComponent, info)180 }181 } catch {}182 try? FileManager.default.removeItem(at: destination)183 }184 }185 return try await decode(asset: asset, track: track, duration: duration, to: destination, progress: progress)186 }187188 private static func decode(asset: AVURLAsset, track: AVAssetTrack, duration: Double, to destination: URL, progress: (@Sendable (Double) -> Void)?) async throws -> (String, AudioFileInfo) {189 let reader: AVAssetReader190 do { reader = try AVAssetReader(asset: asset) } catch { throw ImportError.unsupported(error.localizedDescription) }191 let descriptions = (try? await track.load(.formatDescriptions)) ?? []192 let asbd = descriptions.first.flatMap { CMAudioFormatDescriptionGetStreamBasicDescription($0)?.pointee }193 let rate = asbd.map(\.mSampleRate).flatMap { $0 > 0 ? $0 : nil } ?? 48_000194 let channels = min(2, max(1, Int(asbd?.mChannelsPerFrame ?? 2)))195 let output = AVAssetReaderTrackOutput(track: track, outputSettings: [196 AVFormatIDKey: kAudioFormatLinearPCM, AVLinearPCMBitDepthKey: 32, AVLinearPCMIsFloatKey: true, AVLinearPCMIsBigEndianKey: false,197 AVLinearPCMIsNonInterleaved: false, AVSampleRateKey: rate, AVNumberOfChannelsKey: channels,198 ])199 output.alwaysCopiesSampleData = false200 guard reader.canAdd(output) else { throw ImportError.unsupported("reader output") }201 reader.add(output)202 guard reader.startReading() else { throw ImportError.unsupported(reader.error?.localizedDescription ?? "unknown codec") }203 let writer = try AudioFileWriter(url: destination, format: .alac, inputSampleRate: rate, channels: channels)204 var written = 0205 while let sample = output.copyNextSampleBuffer() {206 try Task.checkCancellation()207 guard let block = CMSampleBufferGetDataBuffer(sample) else { continue }208 let frames = CMSampleBufferGetNumSamples(sample)209 guard frames > 0 else { continue }210 var length = 0211 var pointer: UnsafeMutablePointer<Int8>?212 guard CMBlockBufferGetDataPointer(block, atOffset: 0, lengthAtOffsetOut: nil, totalLengthOut: &length, dataPointerOut: &pointer) == kCMBlockBufferNoErr,213 let pointer, length >= frames * channels * 4 else { continue }214 let interleaved = UnsafeRawPointer(pointer).bindMemory(to: Float.self, capacity: frames * channels)215 var planar = [[Float]](repeating: [Float](repeating: 0, count: frames), count: channels)216 for c in 0..<channels { for i in 0..<frames { planar[c][i] = interleaved[i * channels + c] } }217 try writer.write(planar)218 written += frames219 if duration > 0 { progress?(min(1, Double(written) / rate / duration)) }220 }221 if reader.status == .failed { throw ImportError.unsupported(reader.error?.localizedDescription ?? "decode failed") }222 try writer.finish()223 guard written > 0, var info = try? AudioFileReader.info(url: destination) else { throw ImportError.noAudioTrack }224 info.fileName = destination.lastPathComponent225 return (destination.lastPathComponent, info)226 }227}228