Repository Public License
1//2// ORIX Audio3// File: Core/Sources/ORIXAudioCore/Editing/EditRenderer.swift4// Description: Streams an edit document (clips, gains, fades, channel mode, effects) to chunks, a peak measurement or a file.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//9import AVFoundation10import Foundation1112public final class EditRenderer {13 public static let chunkFrames = 32_76814 public let state: EditState15 public let sampleRate: Double16 public let channels: Int17 private let sourceURLs: [URL]1819 /// - Parameter sourceURLs: resolved URLs for `EditDocument.sources`, same order.20 /// The output sample rate and channel count are those of the first source. Other sources must match its sample rate.21 public init(document: EditDocument, sourceURLs: [URL]) throws {22 state = document.state23 self.sourceURLs = sourceURLs24 guard let first = sourceURLs.first else { throw AudioIOError.unreadable("no source") }25 let reader = try AudioFileReader(url: first)26 sampleRate = reader.sampleRate27 let sourceChannels = reader.channels28 channels = state.channelMode == .original ? min(2, sourceChannels) : 129 }3031 public var totalFrames: Int { Int((state.duration * sampleRate).rounded()) }3233 /// Produces the dry timeline (gains, fades, channel mode; no effects) chunk by chunk.34 private func renderDry(_ body: ([[Float]]) throws -> Void, isCancelled: () -> Bool) throws {35 var readers: [Int: AudioFileReader] = [:]36 var timelineFrame = 037 let master = pow(10, state.masterGainDB / 20)38 for clip in state.clips {39 var remaining = Int((clip.duration * sampleRate).rounded())40 let clipGain = Float(pow(10, clip.gainDB / 20) * master)41 var reader: AudioFileReader?42 if case .file(let index, let start) = clip.source {43 if readers[index] == nil {44 guard index < sourceURLs.count else { throw AudioIOError.unreadable("missing source \(index)") }45 let r = try AudioFileReader(url: sourceURLs[index])46 guard abs(r.sampleRate - sampleRate) < 0.5 else { throw AudioIOError.unsupported("merging files with different sample rates (\(Int(r.sampleRate)) Hz vs \(Int(sampleRate)) Hz)") }47 readers[index] = r48 }49 reader = readers[index]50 reader?.seek(toFrame: Int((start * sampleRate).rounded()))51 }52 while remaining > 0 {53 if isCancelled() { throw AudioIOError.cancelled }54 let n = min(Self.chunkFrames, remaining)55 var out = [[Float]](repeating: [Float](repeating: 0, count: n), count: channels)56 if let reader {57 let data = try reader.read(maxFrames: n)58 let got = data.first?.count ?? 059 if got > 0 {60 switch state.channelMode {61 case .original:62 for c in 0..<channels { let src = data[min(c, data.count - 1)]; out[c].replaceSubrange(0..<got, with: src) }63 case .left: out[0].replaceSubrange(0..<got, with: data[0])64 case .right: out[0].replaceSubrange(0..<got, with: data[min(1, data.count - 1)])65 case .mono: out[0].replaceSubrange(0..<got, with: AudioFileReader.mono(data))66 }67 }68 }69 let needsFade = (state.fadeIn > 0 && Double(timelineFrame) / sampleRate < state.fadeIn)70 || (state.fadeOut > 0 && Double(timelineFrame + n) / sampleRate > state.duration - state.fadeOut)71 for c in 0..<channels {72 if needsFade {73 for i in 0..<n { out[c][i] *= clipGain * Float(EditDocument.fadeGain(at: Double(timelineFrame + i) / sampleRate, state: state)) }74 } else if clipGain != 1 {75 for i in 0..<n { out[c][i] *= clipGain }76 }77 }78 try body(out)79 timelineFrame += n80 remaining -= n81 }82 }83 }8485 /// Renders the finished programme (effects included), delivering exactly `totalFrames` frames.86 public func render(progress: ((Double) -> Void)? = nil, isCancelled: @escaping () -> Bool = { false }, _ body: ([[Float]]) throws -> Void) throws {87 let total = max(1, totalFrames)88 guard !state.effects.isEmpty else {89 var done = 090 try renderDry({ chunk in try body(chunk); done += chunk[0].count; progress?(Double(done) / Double(total)) }, isCancelled: isCancelled)91 return92 }93 // Noise reduction needs a noise profile first: one extra pass over the dry programme per channel.94 var profiles = [[Float]?](repeating: nil, count: channels)95 let needsProfile = state.effects.contains(where: \.isNoiseReduction)96 if needsProfile {97 let estimators = (0..<channels).map { _ in NoiseProfileEstimator() }98 var done = 099 try renderDry({ chunk in100 for c in 0..<channels { chunk[c].withUnsafeBufferPointer { estimators[c].push($0) } }101 done += chunk[0].count; progress?(0.35 * Double(done) / Double(total))102 }, isCancelled: isCancelled)103 profiles = estimators.map { $0.profile() }104 }105 let chains = (0..<channels).map { EffectChain(effects: state.effects, sampleRate: sampleRate, noiseProfile: profiles[$0]) }106 var toSkip = chains[0].latency, emitted = 0107 let base = needsProfile ? 0.35 : 0.0108 func emit(_ processed: [[Float]]) throws {109 var out = processed110 let n = out[0].count111 if toSkip > 0 { let drop = min(toSkip, n); for c in 0..<channels { out[c].removeFirst(drop) }; toSkip -= drop }112 let keep = min(out[0].count, totalFrames - emitted)113 guard keep > 0 else { return }114 if keep < out[0].count { for c in 0..<channels { out[c].removeLast(out[c].count - keep) } }115 try body(out); emitted += keep116 progress?(base + (1 - base) * Double(emitted) / Double(total))117 }118 try renderDry({ chunk in try emit((0..<channels).map { chains[$0].process(chunk[$0]) }) }, isCancelled: isCancelled)119 try emit((0..<channels).map { chains[$0].flush() })120 if emitted < totalFrames { try body([[Float]](repeating: [Float](repeating: 0, count: totalFrames - emitted), count: channels)) }121 }122123 /// Sample peak of the rendered programme in dBFS (used by Normalize).124 public func measurePeakDBFS(isCancelled: @escaping () -> Bool = { false }) throws -> Double {125 var peak: Float = 0126 try render(isCancelled: isCancelled) { chunk in for c in chunk { for v in c { peak = max(peak, abs(v)) } } }127 return peak > 0 ? 20 * log10(Double(peak)) : -160128 }129130 /// Renders to a new audio file. The file is written under a temporary name and moved into place only when complete.131 public func export(to url: URL, format: AudioExportFormat, outputSampleRate: Double? = nil, aacBitRate: Int = 128_000,132 progress: ((Double) -> Void)? = nil, isCancelled: @escaping () -> Bool = { false }) throws {133 let temp = url.deletingLastPathComponent().appendingPathComponent(".\(UUID().uuidString).\(format.fileExtension)")134 do {135 let writer = try AudioFileWriter(url: temp, format: format, inputSampleRate: sampleRate, channels: channels, outputSampleRate: outputSampleRate, aacBitRate: aacBitRate)136 try render(progress: progress, isCancelled: isCancelled) { try writer.write($0) }137 try writer.finish()138 try? FileManager.default.removeItem(at: url)139 try FileManager.default.moveItem(at: temp, to: url)140 } catch {141 try? FileManager.default.removeItem(at: temp)142 throw error143 }144 }145}146