Repository Public License
1// ORIX Convert — Packages/EngineMedia/Tests/EngineMediaTests/AudioTests.swift2// Description: Audio engine tests: every declared path converted and validated, sample-exact lossless paths, bit3// depth and dither, loudness normalisation, tags and artwork, trim, MIDI rendering, bad and hostile input.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import ConvertCore8@testable import EngineMedia910extension MediaTests {1112@Suite(.serialized) struct AudioPaths {13 static let engine = AudioEngine()14 static let conversions = engine.paths.filter { $0.source != .midi }15 static let midiPaths = engine.paths.filter { $0.source == .midi }16 static let losslessFormats: [FormatID] = [.wav, .flac, .alac, .aiff, .caf]17 static let losslessPairs: [ConversionPath] = conversions.filter { losslessFormats.contains($0.source) && losslessFormats.contains($0.destination) }1819 @Test func declaredPathsAreHonest() {20 #expect(Self.conversions.count == 121 && Self.midiPaths.count == 11)21 #expect(Set(Self.engine.paths.map(\.id)).count == Self.engine.paths.count)22 for p in Self.conversions {23 let lossless = Self.losslessFormats.contains(p.source) && Self.losslessFormats.contains(p.destination)24 #expect(p.fidelity == (lossless ? .lossless : .lossy), "\(p.id) fidelity")25 #expect(!p.limitations.isEmpty)26 }27 #expect(Self.losslessPairs.count == 25)28 #expect(Self.midiPaths.allSatisfy { $0.fidelity == .structural })29 // Lossy → lossless and lossy → lossy are announced on the path itself.30 #expect(Self.engine.paths.first { $0.source == .mp3 && $0.destination == .flac }?.limitations.contains { $0.contains("not restored") } == true)31 #expect(Self.engine.paths.first { $0.source == .mp3 && $0.destination == .ogg }?.limitations.contains { $0.contains("generation loss") } == true)32 }3334 @Test(arguments: conversions) func conversion(_ path: ConversionPath) async throws {35 let source = try await Fixtures.shared.audio(path.source)36 let r = try await T.convert(source, path)37 let target = try #require(MediaTables.audioTarget(path.destination))38 let a = try #require(try await T.probe(r.url).audio.first)39 #expect(a.channels == 2)40 #expect(a.sampleRate == (target.codec == "opus" || path.source == .opus ? 48000 : 44100), "Opus works at 48 kHz; everything else keeps 44.1 kHz")41 #expect(r.facts["audio.encoder"]?.isEmpty == false)42 #expect(r.progress.contains { ($0 ?? 0) > 0 } && r.progress.last == .some(1))43 let sourceLossless = Self.losslessFormats.contains(path.source)44 #expect(r.hasWarning("lossy-to-lossless") == (!sourceLossless && target.lossless), "\(path.id): lossy→lossless warning")45 #expect(r.hasWarning("lossy-to-lossy") == (!sourceLossless && !target.lossless), "\(path.id): generation-loss warning")46 }4748 @Test(arguments: losslessPairs) func losslessPathsAreSampleExact(_ path: ConversionPath) async throws {49 for bits in [16, 24] {50 // Source in the path's own format at this depth, made from the master WAV.51 let master = try await Fixtures.shared.masterAudio(bits: bits)52 let reference = try await T.decodedMD5(master, stream: "a:0", extra: ["-c:a", bits == 16 ? "pcm_s16le" : "pcm_s24le"])53 let source = path.source == .wav ? master : try await T.convert(master, try T.audioPath(.wav, path.source), baseName: "src\(bits)").url54 let r = try await T.convert(source, path)55 let a = try #require(try await T.probe(r.url).audio.first)56 #expect(a.bitDepth == bits, "\(path.id): \(bits)-bit kept (got \(a.bitDepth ?? -1))")57 #expect(r.facts["audio.dither"] == nil, "no dither on an untouched lossless copy")58 let md5 = try await T.decodedMD5(r.url, stream: "a:0", extra: ["-c:a", bits == 16 ? "pcm_s16le" : "pcm_s24le"])59 #expect(md5 == reference, "\(path.id) at \(bits)-bit is not sample-exact")60 #expect(r.warnings.isEmpty, "\(path.id): unexpected warnings \(r.warnings.map(\.code))")61 }62 }6364 @Test(arguments: midiPaths) func midiRendering(_ path: ConversionPath) async throws {65 #expect(MidiRenderer.isAvailable)66 let midi = try await Fixtures.shared.midi()67 let r = try await T.convert(midi, path)68 let rendered = try #require(r.facts["midi.rendered.seconds"].flatMap(Double.init))69 #expect(r.facts["midi.sequence.seconds"].flatMap(Double.init).map { abs($0 - 2.5) < 0.01 } == true)70 #expect(rendered >= 2.5 + MidiRenderer.minTail - 0.01 && rendered <= 2.5 + MidiRenderer.maxTail + 0.1)71 let info = try await T.probe(r.url)72 #expect(abs((info.duration ?? 0) - rendered) < 0.25, "\(path.id): duration \(info.duration ?? 0) vs rendered \(rendered)")73 }7475 @Test func midiRenderIsNonSilentAndOnTime() async throws {76 let midi = try await Fixtures.shared.midi()77 let inspection = try await AudioEngine().inspect(midi, format: .midi)78 #expect(abs((inspection.durationSeconds ?? 0) - 2.5) < 0.01)79 #expect(inspection.fields.contains { $0.key == "Tracks" && $0.value == "1" })80 let r = try await T.convert(midi, try T.audioPath(.midi, .wav), options: ["midi.samplerate": .string("48000"), "audio.bitdepth": .string("24")])81 let a = try #require(try await T.probe(r.url).audio.first)82 #expect(a.sampleRate == 48000 && a.bitDepth == 24 && a.channels == 2)83 let wave = try await MediaQualityLab.waveform(of: r.url, buckets: 400)84 let perSecond = Double(wave.maximums.count) / wave.durationSeconds85 func peak(_ from: Double, _ to: Double) -> Float {86 let lo = Int(from * perSecond), hi = min(wave.maximums.count, Int(to * perSecond))87 return (lo..<hi).map { max(abs(wave.maximums[$0]), abs(wave.minimums[$0])) }.max() ?? 088 }89 #expect(peak(0, 0.9) < 0.001, "silence before the first note at 1.0 s (peak \(peak(0, 0.9)))")90 #expect(peak(1.0, 1.9) > 0.02, "the piano note sounds from 1.0 s (peak \(peak(1.0, 1.9)))")91 #expect(peak(2.0, 2.4) > 0.02, "the snare on channel 10 sounds at 2.0 s (peak \(peak(2.0, 2.4)))")92 // Two renders of the same file: same music; the release tail is cut where the synthesiser falls silent, which93 // can differ by a few render buffers from one run to the next.94 let again = try await T.convert(midi, try T.audioPath(.midi, .wav), options: ["midi.samplerate": .string("48000")])95 let first = try #require(r.facts["midi.rendered.seconds"].flatMap(Double.init)), second = try #require(again.facts["midi.rendered.seconds"].flatMap(Double.init))96 print("MIDI render lengths: \(first) s and \(second) s")97 #expect(abs(first - second) <= 0.5, "render lengths \(first) s and \(second) s")98 #expect(r.facts["backend"]?.contains("DLSSynth") == true)99 // No temporary rendering is left in the work directory.100 #expect(try FileManager.default.contentsOfDirectory(atPath: r.url.deletingLastPathComponent().path).count == 1)101 }102103 // Regression (2026-11-21): several MIDI files rendered at the same time crashed the whole process in104 // AVAudioEngine.stop → AudioUnitUninitialize (3 of 12 runs of 24 MIDI → WAV with 8 parallel jobs). The105 // synthesiser now runs in the helper process, or in-process one rendering at a time when the helper is absent.106 @Test(arguments: [MidiRenderer.Isolation.inProcess, .required])107 func twentyFourConcurrentMidiRendersRepeated(_ isolation: MidiRenderer.Isolation) async throws {108 let midi = try await Fixtures.shared.midi()109 if isolation == .required { #expect(MidiRenderer.helperURL() != nil, "the test target builds \(MidiRenderer.helperName) next to the test bundle") }110 for round in 1...5 {111 let dir = T.newDirectory("midi-burst")112 let results = try await withThrowingTaskGroup(of: (URL, Double, MidiRenderer.RenderResult).self) { group -> [(URL, Double, MidiRenderer.RenderResult)] in113 for i in 0..<24 {114 group.addTask {115 let out = dir.appendingPathComponent("render-\(i).wav"), rate: Double = i % 2 == 0 ? 44100 : 48000116 return (out, rate, try await MidiRenderer.render(midi, to: out, sampleRate: rate, isolation: isolation) { _ in })117 }118 }119 var all: [(URL, Double, MidiRenderer.RenderResult)] = []120 for try await r in group { all.append(r) }121 return all122 }123 #expect(results.count == 24, "round \(round)")124 for (url, rate, r) in results {125 #expect(r.isolated == (isolation == .required))126 #expect(abs(r.sequenceSeconds - 2.5) < 0.01 && r.seconds >= 2.5 + MidiRenderer.minTail - 0.01 && r.seconds <= 2.5 + MidiRenderer.maxTail + 0.1, "round \(round): \(r)")127 #expect(r.peak > 0.02 && r.peak < 4, "round \(round): the rendering is not silent (peak \(r.peak))")128 // Every output is a valid WAV of the right rate and length, read back by ffprobe.129 let info = try await T.probe(url)130 let audio = try #require(info.audio.first)131 #expect(audio.sampleRate == rate && audio.channels == 2, "round \(round): \(url.lastPathComponent)")132 #expect(abs((info.duration ?? 0) - r.seconds) < 0.05, "round \(round): duration \(info.duration ?? 0) vs \(r.seconds)")133 }134 }135 // In-process renderings never overlap, whatever the number of callers.136 if isolation == .inProcess { #expect(MidiRenderer.peakLiveRenders.current == 1) }137 }138139 @Test func twentyFourConcurrentMidiConversionsThroughTheEngine() async throws {140 let midi = try await Fixtures.shared.midi()141 let paths = [try T.audioPath(.midi, .wav), try T.audioPath(.midi, .flac), try T.audioPath(.midi, .mp3)]142 for round in 1...3 {143 let durations = try await withThrowingTaskGroup(of: (Double, Double, String?).self) { group -> [(Double, Double, String?)] in144 for i in 0..<24 {145 group.addTask {146 let r = try await T.convert(midi, paths[i % paths.count])147 return (try await T.probe(r.url).duration ?? 0, r.facts["midi.rendered.seconds"].flatMap(Double.init) ?? -1, r.facts["midi.synthesiser.process"])148 }149 }150 var all: [(Double, Double, String?)] = []151 for try await d in group { all.append(d) }152 return all153 }154 #expect(durations.count == 24, "round \(round)")155 for (probed, rendered, process) in durations {156 #expect(rendered >= 2.5 && abs(probed - rendered) < 0.25, "round \(round): \(probed) vs \(rendered)")157 #expect(process?.contains(MidiRenderer.helperName) == true, "round \(round): the helper built with the tests is used (\(process ?? "nil"))")158 }159 }160 }161162 @Test func midiHelperFailuresAreContained() async throws {163 let midi = try await Fixtures.shared.midi()164 let dir = T.newDirectory("midi-helper")165 let helper = try #require(MidiRenderer.helperURL())166 // Errors found by the helper come back as the same errors the in-process renderer throws.167 let garbage = dir.appendingPathComponent("header-only.mid")168 try Data([0x4D, 0x54, 0x68, 0x64, 0, 0, 0, 6, 0, 0, 0, 1, 0x01, 0xE0]).write(to: garbage)169 let direct = try await ProcessRunner.run(helper, arguments: ["render", garbage.path, dir.appendingPathComponent("g.wav").path, "44100"])170 #expect(direct.status == 3 && direct.stderrString.contains("ORIX-ERROR malformed"), "\(direct.status): \(direct.stderrString)")171 #expect(try await ProcessRunner.run(helper, arguments: ["render", midi.path]).status == 3)172 let unwritable = try await ProcessRunner.run(helper, arguments: ["render", midi.path, "/nonexistent-folder/out.wav", "44100"])173 #expect(unwritable.status == 3 && unwritable.stderrString.contains("ORIX-ERROR write"), "\(unwritable.stderrString)")174 #if DEBUG175 // A crash of the synthesiser process (simulated: the debug helper aborts on request) is an error of that one176 // job, with nothing left behind; this process goes on and the next rendering works.177 let out = dir.appendingPathComponent("crash.wav")178 do {179 _ = try await MidiRenderer.render(midi, to: out, sampleRate: 44100, isolation: .required, helperEnvironment: ["ORIX_MIDI_RENDER_TEST_ABORT": "1"]) { _ in }180 Issue.record("the aborted helper produced a result")181 } catch let ConversionError.toolFailed(tool, status, message) {182 #expect(tool == MidiRenderer.helperName && status != 0 && message.contains("separate process"))183 }184 #expect(!FileManager.default.fileExists(atPath: out.path))185 #endif186 let after = try await MidiRenderer.render(midi, to: dir.appendingPathComponent("after.wav"), sampleRate: 44100, isolation: .required) { _ in }187 #expect(after.isolated && after.peak > 0.02)188 // Cancelling a job that waits for its turn in the in-process queue ends it without rendering.189 let waiting = dir.appendingPathComponent("cancelled.wav")190 let task = Task { try await MidiRenderer.render(midi, to: waiting, sampleRate: 44100, isolation: .inProcess) { _ in } }191 task.cancel()192 _ = try? await task.value193 if task.isCancelled, (try? await task.value) == nil { #expect(!FileManager.default.fileExists(atPath: waiting.path)) }194 }195196 @Test func midiBadInput() async throws {197 let dir = T.newDirectory("badmidi")198 let cases: [(String, Data)] = [199 ("random.mid", Data((0..<2000).map { _ in UInt8.random(in: 0...255) })),200 ("empty.mid", Data()),201 ("header-only.mid", Data([0x4D, 0x54, 0x68, 0x64, 0, 0, 0, 6, 0, 0, 0, 1, 0x01, 0xE0])),202 ("lying-length.mid", Data([0x4D, 0x54, 0x68, 0x64, 0, 0, 0, 6, 0, 0, 0, 1, 0x01, 0xE0, 0x4D, 0x54, 0x72, 0x6B, 0x7F, 0xFF, 0xFF, 0xFF, 0x00, 0x90, 60, 100])),203 ]204 for (name, bytes) in cases {205 let url = dir.appendingPathComponent(name)206 try bytes.write(to: url)207 let work = T.newDirectory("badmidiout")208 await #expect(throws: (any Error).self, "\(name) must be refused") {209 _ = try await AudioEngine().convert(ConversionRequest(source: url, sourceFormat: .midi, destinationFormat: .wav), context: ConversionContext(workDirectory: work))210 }211 #expect(try FileManager.default.contentsOfDirectory(atPath: work.path).isEmpty, "\(name): nothing left behind")212 }213 // A MIDI file with no notes has nothing to play: refused rather than delivered as a silent track.214 let track: [UInt8] = [0x00, 0xFF, 0x51, 0x03, 0x07, 0xA1, 0x20, 0x83, 0x60, 0xFF, 0x2F, 0x00]215 var data: [UInt8] = [0x4D, 0x54, 0x68, 0x64, 0, 0, 0, 6, 0, 0, 0, 1, 0x01, 0xE0, 0x4D, 0x54, 0x72, 0x6B, 0, 0, 0, UInt8(track.count)]216 data += track217 let silent = dir.appendingPathComponent("no-notes.mid")218 try Data(data).write(to: silent)219 do {220 _ = try await AudioEngine().convert(ConversionRequest(source: silent, sourceFormat: .midi, destinationFormat: .wav), context: ConversionContext(workDirectory: T.newDirectory("silent")))221 Issue.record("a MIDI file without notes produced audio")222 } catch let error as ConversionError {223 switch error {224 case .outputInvalid, .malformedInput: break225 default: Issue.record("unexpected error \(error)")226 }227 }228 }229}230231@Suite(.serialized) struct AudioFeatures {232 @Test func sampleRateBitDepthChannelsAndDither() async throws {233 let master24 = try await Fixtures.shared.masterAudio(bits: 24)234 // 24 → 16-bit with TPDF dither: not the same as plain truncation, but within ±2 LSB of it, and noise-like.235 let dithered = try await T.convert(master24, try T.audioPath(.wav, .wav), options: ["audio.bitdepth": .string("16")])236 #expect(dithered.facts["audio.dither"] == "triangular (TPDF)" && dithered.hasWarning("bit-depth-reduced"))237 let plain = try await T.convert(master24, try T.audioPath(.wav, .wav), options: ["audio.bitdepth": .string("16"), "audio.dither": .bool(false)])238 #expect(plain.facts["audio.dither"] == nil)239 let d = try await T.pcm16(dithered.url), p = try await T.pcm16(plain.url)240 #expect(d.count == p.count && d != p)241 let diffs = zip(d, p).map { Int($0) - Int($1) }242 #expect(diffs.allSatisfy { abs($0) <= 3 }, "dither stays within a few LSB (max \(diffs.map(abs).max() ?? 0))")243 #expect(Double(diffs.filter { $0 != 0 }.count) / Double(diffs.count) > 0.2, "dither changes a large share of samples")244 // 16-bit source kept at 16-bit: no dither, sample-exact (covered by the lossless matrix); to 32-bit float: exact too.245 let master = try await Fixtures.shared.masterAudio()246 let float = try await T.convert(master, try T.audioPath(.wav, .caf), options: ["audio.bitdepth": .string("32f")])247 let fa = try #require(try await T.probe(float.url).audio.first)248 #expect(fa.codec == "pcm_f32le" && fa.isFloat)249 #expect(try await T.pcm16(float.url) == T.pcm16(master))250 // Float → FLAC is stored as 24-bit, and says so.251 let flac = try await T.convert(float.url, try T.audioPath(.caf, .flac))252 #expect(flac.hasWarning("bit-depth-reduced"))253 #expect(try await T.probe(flac.url).audio.first?.bitDepth == 24)254 await #expect(throws: ConversionError.self) { _ = try await T.convert(master, try T.audioPath(.wav, .flac), options: ["audio.bitdepth": .string("32f")]) }255 // Sample-rate conversion and mono.256 let down = try await T.convert(master, try T.audioPath(.wav, .wav), options: ["audio.samplerate": .string("22050"), "audio.channels": .string("mono")])257 let da = try #require(try await T.probe(down.url).audio.first)258 #expect(da.sampleRate == 22050 && da.channels == 1 && down.hasWarning("sample-rate-reduced") && down.hasWarning("stereo-to-mono"))259 #expect(abs((try await T.probe(down.url).duration ?? 0) - 3) < 0.01)260 let up = try await T.convert(master, try T.audioPath(.wav, .aiff), options: ["audio.samplerate": .string("96000"), "audio.bitdepth": .string("24")])261 let ua = try #require(try await T.probe(up.url).audio.first)262 #expect(ua.sampleRate == 96000 && ua.bitDepth == 24 && ua.codec == "pcm_s24be")263 // MP3 cannot store 96 kHz: resampled to 48 kHz with a warning.264 let mp3 = try await T.convert(up.url, try T.audioPath(.aiff, .mp3))265 #expect(mp3.hasWarning("sample-rate-changed"))266 #expect(try await T.probe(mp3.url).audio.first?.sampleRate == 48000)267 }268269 @Test func surroundDownmixIsAnnounced() async throws {270 let surround = T.newDirectory("surround").appendingPathComponent("surround.flac")271 try await T.ffmpeg(["-filter_complex", "sine=frequency=500:sample_rate=48000:duration=2,aformat=channel_layouts=5.1[o]", "-map", "[o]", "-c:a", "flac", "-f", "flac", "file:" + surround.path])272 let inspection = try await AudioEngine().inspect(surround, format: .flac)273 #expect(inspection.channels == 6)274 let pre = AudioEngine().preflight(ConversionRequest(source: surround, sourceFormat: .flac, destinationFormat: .mp3), inspection: inspection)275 #expect(pre.contains { $0.code == "surround-downmix" && $0.severity == .dataLoss })276 let mp3 = try await T.convert(surround, try T.audioPath(.flac, .mp3))277 #expect(mp3.hasWarning("surround-downmix"))278 #expect(try await T.probe(mp3.url).audio.first?.channels == 2)279 // Formats that hold surround keep it without warning.280 for dest in [FormatID.m4a, .opus, .ogg, .wav, .alac] {281 let r = try await T.convert(surround, try T.audioPath(.flac, dest))282 #expect(try await T.probe(r.url).audio.first?.channels == 6, "\(dest) keeps 5.1")283 #expect(!r.hasWarning("surround-downmix"), "\(dest) keeps 5.1 without warning")284 }285 let stereo = try await T.convert(surround, try T.audioPath(.flac, .wav), options: ["audio.channels": .string("stereo")])286 #expect(stereo.hasWarning("surround-downmix"))287 #expect(try await T.probe(stereo.url).audio.first?.channels == 2)288 }289290 @Test func bitrateAndQualitySettings() async throws {291 let master = try await Fixtures.shared.masterAudio()292 for (dest, low, high) in [(FormatID.mp3, 64, 320), (.m4a, 64, 256), (.opus, 32, 256), (.ogg, 64, 320), (.wma, 64, 192), (.aac, 64, 256)] {293 let a = try await T.convert(master, try T.audioPath(.wav, dest), options: ["audio.bitrate": .int(low)])294 let b = try await T.convert(master, try T.audioPath(.wav, dest), options: ["audio.bitrate": .int(high)])295 let sa = try #require(try a.url.resourceValues(forKeys: [.fileSizeKey]).fileSize), sb = try #require(try b.url.resourceValues(forKeys: [.fileSizeKey]).fileSize)296 #expect(Double(sb) > Double(sa) * 1.4, "\(dest): \(high) kb/s (\(sb) B) must be larger than \(low) kb/s (\(sa) B)")297 }298 for dest in [FormatID.mp3, .ogg, .m4a] {299 let lo = try await T.convert(master, try T.audioPath(.wav, dest), options: ["audio.mode": .string("quality"), "audio.quality": .int(10)])300 let hi = try await T.convert(master, try T.audioPath(.wav, dest), options: ["audio.mode": .string("quality"), "audio.quality": .int(95)])301 let sl = try #require(try lo.url.resourceValues(forKeys: [.fileSizeKey]).fileSize), sh = try #require(try hi.url.resourceValues(forKeys: [.fileSizeKey]).fileSize)302 #expect(sh > sl, "\(dest): VBR quality 95 (\(sh) B) must be larger than quality 10 (\(sl) B)")303 #expect(hi.facts["audio.encoder"]?.contains("VBR") == true)304 }305 // FLAC level changes the size, never the samples.306 let l0 = try await T.convert(master, try T.audioPath(.wav, .flac), options: ["audio.flac.level": .int(0)])307 let l12 = try await T.convert(master, try T.audioPath(.wav, .flac), options: ["audio.flac.level": .int(12)])308 #expect(try await T.pcm16(l0.url) == T.pcm16(l12.url))309 // AAC is encoded by AudioToolbox, and reported as such.310 let aac = try await T.convert(master, try T.audioPath(.wav, .m4a))311 #expect(aac.facts["audio.encoder"]?.contains("AudioToolbox") == true)312 }313314 @Test func loudnessNormalisationReachesTheTarget() async throws {315 let master = try await Fixtures.shared.masterAudio()316 // Independent measurement of the source with the ebur128 filter.317 func ebur128(_ url: URL) async throws -> Double {318 let tool = try BundledTools.require("ffmpeg")319 let r = try await ProcessRunner.run(tool, arguments: ["-hide_banner", "-nostdin", "-i", "file:" + url.path, "-af", "ebur128=framelog=quiet", "-f", "null", "-"], timeout: 120)320 let text = r.stderrString321 let tail = try #require(text.range(of: "Integrated loudness:", options: .backwards))322 let line = try #require(text[tail.upperBound...].split(whereSeparator: \.isNewline).first { $0.contains("I:") })323 let number = line.replacingOccurrences(of: "I:", with: "").replacingOccurrences(of: "LUFS", with: "").trimmingCharacters(in: .whitespaces)324 return try #require(Double(number))325 }326 let sourceLoudness = try await ebur128(master)327 for (dest, target) in [(FormatID.wav, -16.0), (.wav, -23.0), (.flac, -14.0), (.mp3, -16.0), (.m4a, -18.0), (.opus, -16.0)] {328 let r = try await T.convert(master, try T.audioPath(.wav, dest), options: ["loudness.normalize": .bool(true), "loudness.target": .double(target)])329 let measuredIn = try #require(r.facts["loudness.input.i"]?.split(separator: " ").first.flatMap { Double($0) })330 let reported = try #require(r.facts["loudness.output.i"]?.split(separator: " ").first.flatMap { Double($0) })331 #expect(abs(measuredIn - sourceLoudness) < 0.3, "reported input loudness \(measuredIn) vs ebur128 \(sourceLoudness)")332 let independent = try await ebur128(r.url)333 #expect(abs(independent - target) <= 0.6, "\(dest) → \(target) LUFS: ebur128 measures \(independent)")334 #expect(abs(reported - independent) < 0.3, "the reported output loudness is the measured one")335 #expect(r.facts["loudness.output.tp"] != nil && r.facts["loudness.input.tp"] != nil && r.facts["loudness.mode"] == "linear")336 #expect(r.checks.contains { $0.name == "Loudness target" && $0.passed })337 #expect(abs((try await T.probe(r.url).duration ?? 0) - 3) < 0.15)338 #expect((try await T.probe(r.url).audio.first?.sampleRate) == (dest == .opus ? 48000 : 44100), "the sample rate is kept")339 }340 // A plain gain would clip here (peaks near full scale, quiet average): dynamic normalisation, announced.341 let peaky = T.newDirectory("peaky").appendingPathComponent("peaky.wav")342 try await T.ffmpeg(["-filter_complex", "sine=frequency=220:sample_rate=44100:duration=20,volume='if(lt(mod(t,2),0.1),7.5,0.6)':eval=frame,aformat=channel_layouts=stereo[o]",343 "-map", "[o]", "-c:a", "pcm_s16le", "-f", "wav", "file:" + peaky.path])344 let dyn = try await T.convert(peaky, try T.audioPath(.wav, .wav), options: ["loudness.normalize": .bool(true), "loudness.target": .double(-14)])345 #expect(dyn.facts["loudness.mode"] == "dynamic" && dyn.hasWarning("loudness-dynamic"), "facts: \(dyn.facts)")346 let dynOut = try await ebur128(dyn.url)347 #expect(abs(dynOut - (-14)) <= 2.0, "dynamic normalisation lands near the target (\(dynOut) LUFS)")348 // Silence cannot be normalised: skipped with a warning, not amplified noise.349 let silence = T.newDirectory("silence").appendingPathComponent("silence.wav")350 try await T.ffmpeg(["-filter_complex", "anullsrc=r=44100:cl=stereo:d=2[o]", "-map", "[o]", "-c:a", "pcm_s16le", "-f", "wav", "file:" + silence.path])351 let s = try await T.convert(silence, try T.audioPath(.wav, .wav), options: ["loudness.normalize": .bool(true)])352 #expect(s.hasWarning("loudness-silent") && s.facts["loudness.output.i"] == nil)353 }354355 static let tagExpectations: [(FormatID, tags: Bool, artwork: Bool)] = [356 (.mp3, true, true), (.m4a, true, true), (.alac, true, true), (.flac, true, true), (.aiff, true, true),357 (.ogg, true, false), (.opus, true, false), (.wma, true, false), (.wav, true, false), (.caf, true, false), (.aac, false, false),358 ]359360 @Test func tagsAndArtworkRoundTrip() async throws {361 let tagged = try await Fixtures.shared.taggedFLAC()362 let inspection = try await AudioEngine().inspect(tagged, format: .flac)363 #expect(inspection.hasMetadata == true)364 #expect(inspection.fields.contains { $0.group == "General" && $0.key == "Artwork" && $0.value.contains("PNG 96×96") })365 #expect(inspection.fields.contains { $0.group == "Tags" && $0.key.lowercased() == "title" && $0.value == "Test Title" })366 #expect(inspection.audioCodec == "flac" && inspection.bitDepth == 16 && inspection.sampleRate == 44100 && inspection.channels == 2)367 for (dest, tags, artwork) in Self.tagExpectations {368 let target = try #require(MediaTables.audioTarget(dest))369 #expect(target.keepsTags == tags && target.keepsArtwork == artwork, "\(dest): table matches the expectation")370 let r = try await T.convert(tagged, try T.audioPath(.flac, dest))371 let info = try await T.probe(r.url)372 if tags {373 #expect(info.tag("title") == "Test Title" && info.tag("artist") == "Test Artist" && info.tag("album") == "Test Album", "\(dest): title/artist/album kept")374 #expect(info.tag("genre") == "Electronic", "\(dest): genre kept")375 #expect(info.tag("date")?.hasPrefix("2026") == true || info.tag("year")?.hasPrefix("2026") == true, "\(dest): year kept (\(info.tags))")376 if dest != .wma { #expect(info.tag("track")?.hasPrefix("7") == true, "\(dest): track number kept (\(info.tags))") }377 #expect(info.tag("comment") == "Made by tests", "\(dest): comment kept (\(info.tags))")378 #expect(!r.hasWarning("tags-dropped"))379 } else {380 #expect(info.tag("title") == nil && r.hasWarning("tags-dropped"), "\(dest): tag loss is announced")381 #expect(r.warnings.first { $0.code == "tags-dropped" }?.severity == .dataLoss)382 }383 if artwork {384 #expect(info.artwork.count == 1 && info.artwork.first?.codec == "png" && info.artwork.first?.width == 96, "\(dest): artwork kept")385 #expect(r.facts["artwork"] == "kept" && !r.hasWarning("artwork-dropped"))386 } else {387 #expect(info.artwork.isEmpty && r.hasWarning("artwork-dropped"), "\(dest): artwork loss is announced")388 }389 let pre = AudioEngine().preflight(ConversionRequest(source: tagged, sourceFormat: .flac, destinationFormat: dest), inspection: inspection)390 #expect(pre.contains { $0.code == "artwork-dropped" } == !artwork && pre.contains { $0.code == "tags-dropped" } == !tags, "\(dest): preflight says the same")391 }392 // Editing: new values replace the source's, untouched fields stay; strip removes the rest.393 var r = try await T.convert(tagged, try T.audioPath(.flac, .mp3), options: ["metadata.title": .string("Édité — \"new\" title=1"), "metadata.year": .string("1999"), "metadata.track": .string("3/12")])394 var info = try await T.probe(r.url)395 #expect(info.tag("title") == "Édité — \"new\" title=1" && info.tag("date") == "1999" && info.tag("track") == "3/12" && info.tag("artist") == "Test Artist")396 r = try await T.convert(tagged, try T.audioPath(.flac, .m4a), options: ["metadata.strip": .bool(true), "metadata.artist": .string("Only Me"), "artwork.keep": .bool(false)])397 info = try await T.probe(r.url)398 #expect(info.tag("artist") == "Only Me" && info.tag("title") == nil && info.tag("album") == nil && info.artwork.isEmpty)399 // Round trip back from a tagged MP3 and from Ogg (stream-level tags) to FLAC.400 let mp3 = try await T.convert(tagged, try T.audioPath(.flac, .mp3))401 let back = try await T.probe(try await T.convert(mp3.url, try T.audioPath(.mp3, .flac)).url)402 #expect(back.tag("title") == "Test Title" && back.tag("album") == "Test Album" && back.artwork.count == 1)403 let ogg = try await T.convert(tagged, try T.audioPath(.flac, .ogg))404 let fromOgg = try await T.probe(try await T.convert(ogg.url, try T.audioPath(.ogg, .m4a)).url)405 #expect(fromOgg.tag("title") == "Test Title" && fromOgg.tag("artist") == "Test Artist")406 await #expect(throws: ConversionError.self) { _ = try await T.convert(tagged, try T.audioPath(.flac, .aac), options: ["metadata.title": .string("x")]) }407 }408409 @Test func trimIsSampleAccurate() async throws {410 let master = try await Fixtures.shared.masterAudio()411 let r = try await T.convert(master, try T.audioPath(.wav, .wav), options: ["trim.start": .double(0.5), "trim.end": .double(2.0)])412 let all = try await T.pcm16(master), cut = try await T.pcm16(r.url)413 #expect(cut.count == 66150 * 2, "1.5 s at 44.1 kHz stereo is 66150 frames, got \(cut.count / 2)")414 #expect(Array(all[(22050 * 2)..<(88200 * 2)]) == cut, "the trimmed samples are exactly samples 22050…88199 of the source")415 for dest in [FormatID.mp3, .m4a, .flac, .opus] {416 let t = try await T.convert(master, try T.audioPath(.wav, dest), options: ["trim.start": .double(1.0)])417 #expect(abs((try await T.probe(t.url).duration ?? 0) - 2.0) < 0.12, "\(dest): trimmed to 2 s")418 }419 await #expect(throws: ConversionError.self) { _ = try await T.convert(master, try T.audioPath(.wav, .mp3), options: ["trim.start": .double(5)]) }420 }421422 @Test func inspectionReportsRealAudioProperties() async throws {423 let master24 = try await Fixtures.shared.masterAudio(bits: 24)424 var i = try await AudioEngine().inspect(master24, format: .wav)425 #expect(i.audioCodec == "pcm_s24le" && i.bitDepth == 24 && i.sampleRate == 44100 && i.channels == 2 && abs((i.durationSeconds ?? 0) - 3) < 0.001)426 #expect(i.fields.contains { $0.group == "Audio" && $0.key == "Compression" && $0.value == "Lossless" })427 #expect(i.videoCodec == nil && i.pixelWidth == nil)428 let mp3 = try await Fixtures.shared.audio(.mp3)429 i = try await AudioEngine().inspect(mp3, format: .mp3)430 #expect(i.audioCodec == "mp3" && i.bitDepth == nil, "a lossy codec has no bit depth: it stays nil")431 #expect(i.fields.contains { $0.key == "Compression" && $0.value == "Lossy" } && (i.bitRate ?? 0) > 100_000)432 let alac = try await Fixtures.shared.audio(.alac)433 i = try await AudioEngine().inspect(alac, format: .m4a)434 #expect(i.audioCodec == "alac" && i.bitDepth == 16, "an .m4a holding ALAC is reported as ALAC")435 }436437 @Test func malformedAndHostileAudioInput() async throws {438 let dir = T.newDirectory("badaudio -x %d")439 for (name, bytes) in [("random.mp3", Data((0..<30_000).map { _ in UInt8.random(in: 0...255) })), ("empty.wav", Data()), ("text.flac", Data("fLaC? no.\n".utf8)),440 ("riff-only.wav", Data("RIFF\u{24}\u{00}\u{00}\u{7F}WAVEfmt ".utf8))] {441 let url = dir.appendingPathComponent(name)442 try bytes.write(to: url)443 let work = T.newDirectory("badaudioout")444 await #expect(throws: ConversionError.self, "\(name) must be refused") {445 _ = try await AudioEngine().convert(ConversionRequest(source: url, sourceFormat: FormatID(url.pathExtension), destinationFormat: .mp3), context: ConversionContext(workDirectory: work))446 }447 #expect(try FileManager.default.contentsOfDirectory(atPath: work.path).isEmpty)448 }449 let master = try await Fixtures.shared.masterAudio()450 for name in ["-i", "-af volume=0", "100%d %s", "a:b|c;[d]", "file:x", "été 🎧 «son»"] {451 let source = dir.appendingPathComponent(name).appendingPathExtension("wav")452 try FileManager.default.copyItem(at: master, to: source)453 let r = try await T.convert(source, try T.audioPath(.wav, .flac), options: ["metadata.title": .string(name), "metadata.comment": .string("-metadata artist=x\n:;=%")], baseName: name)454 #expect(r.url.deletingPathExtension().lastPathComponent == name)455 let info = try await T.probe(r.url)456 #expect(info.tag("title") == name && info.tag("artist") == nil)457 #expect(try await T.pcm16(r.url) == T.pcm16(master))458 let n = try await T.convert(source, try T.audioPath(.wav, .m4a), options: ["loudness.normalize": .bool(true)], baseName: name)459 #expect(n.facts["loudness.output.i"] != nil)460 _ = try await MediaQualityLab.waveform(of: source, buckets: 50)461 }462 }463464 @Test func cancellationOfAnAudioEncode() async throws {465 let long = T.newDirectory("longaudio").appendingPathComponent("long.wav")466 try await T.ffmpeg(["-filter_complex", "anoisesrc=sample_rate=48000:duration=1800:amplitude=0.3,aformat=channel_layouts=stereo[o]", "-map", "[o]", "-c:a", "pcm_s16le", "-f", "wav", "file:" + long.path])467 let work = T.newDirectory("cancelaudio")468 let started = Collector<Double>()469 let request = ConversionRequest(source: long, sourceFormat: .wav, destinationFormat: .flac, options: ConversionOptions(["audio.flac.level": .int(12), "loudness.normalize": .bool(true)]))470 let context = ConversionContext(workDirectory: work, progress: { if let f = $0.fraction, f > 0 { started.add(f) } })471 let task = Task { try await AudioEngine().convert(request, context: context) }472 let begin = Date()473 while started.all.isEmpty, Date().timeIntervalSince(begin) < 60 { try await Task.sleep(nanoseconds: 20_000_000) }474 task.cancel()475 do { _ = try await task.value; Issue.record("finished instead of being cancelled") } catch is CancellationError {} catch { Issue.record("expected CancellationError, got \(error)") }476 #expect(try FileManager.default.contentsOfDirectory(atPath: work.path).isEmpty, "no partial output left")477 try? FileManager.default.removeItem(at: long)478 }479}480481}482