Repository Public License
1//2// ORIX Audio3// File: Core/Sources/ORIXAudioCore/Analysis/AudioAnalyzer.swift4// Description: One incremental pass over an audio file producing the analysis report and the waveform summary.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//9import Foundation1011public enum AudioAnalyzer {12 public struct Output: Sendable {13 public var report: AnalysisReport14 public var waveform: WaveformSummary15 }1617 /// Analyses a file in fixed-size chunks; memory use is independent of the file's length (except the waveform summary:18 /// 40 bytes per 512 samples).19 public static func analyze(url: URL, progress: (@Sendable (Double) -> Void)? = nil, isCancelled: @Sendable () -> Bool = { false }) throws -> Output {20 let reader = try AudioFileReader(url: url)21 let fs = reader.sampleRate22 let meterChannels = min(2, reader.channels)23 var time = TimeDomainAccumulator(sampleRate: fs)24 let spectral = SpectralAccumulator(sampleRate: fs)25 var loudness = LoudnessMeter(sampleRate: fs, channels: meterChannels)26 var vad = VoiceActivityDetector(sampleRate: fs)27 let waveform = WaveformProcessor(sampleRate: fs)28 var buckets: [WaveformBucket] = []29 buckets.reserveCapacity(reader.frameCount / WaveformSummary.defaultSamplesPerBucket + 1)30 var done = 031 let total = max(1, reader.frameCount)32 // Peak, clipping and DC are measured per channel and combined, so a clipped channel is never hidden by the mono mix.33 var perChannel = (0..<meterChannels).map { _ in TimeDomainAccumulator(sampleRate: fs) }34 while true {35 if isCancelled() { throw AudioIOError.cancelled }36 let data = try reader.read(maxFrames: 65_536)37 guard let n = data.first?.count, n > 0 else { break }38 let mono = AudioFileReader.mono(data)39 mono.withUnsafeBufferPointer { m in40 time.push(m); spectral.push(m); vad.push(m); waveform.push(m, into: &buckets)41 }42 for c in 0..<meterChannels { perChannel[c].push(data[c]) }43 data[0].withUnsafeBufferPointer { l in44 if meterChannels == 2 { data[1].withUnsafeBufferPointer { r in loudness.push([l.baseAddress!, r.baseAddress!], frames: n) } }45 else { loudness.push([l.baseAddress!], frames: n) }46 }47 done += n48 progress?(Double(done) / Double(total))49 }50 waveform.finish(into: &buckets)51 var t = time.finish()52 let channelMetrics = perChannel.indices.map { perChannel[$0].finish() }53 if let worst = channelMetrics.max(by: { $0.peak < $1.peak }) {54 t.peak = worst.peak; t.peakDBFS = worst.peakDBFS55 t.minimum = channelMetrics.map(\.minimum).min() ?? t.minimum56 t.maximum = channelMetrics.map(\.maximum).max() ?? t.maximum57 t.clippedSamples = channelMetrics.map(\.clippedSamples).max() ?? 058 t.clippingEvents = channelMetrics.map(\.clippingEvents).max() ?? 059 t.crestFactorDB = t.rms > 0 ? t.peakDBFS - t.rmsDBFS : 060 }61 let s = spectral.finish(), l = loudness.finish(), v = vad.finish()62 let report = AnalysisReport(sampleRate: fs, channels: reader.channels, time: t, spectral: s, loudness: l, speech: v,63 findings: AudioDiagnostics.findings(time: t, spectral: s, loudness: l, speech: v, sampleRate: fs))64 return Output(report: report, waveform: WaveformSummary(sampleRate: fs, samplesPerBucket: waveform.samplesPerBucket, totalSamples: done, base: buckets))65 }66}67