|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { afterAll, describe, expect, it } from "vitest"; |
| 6 | +import { getFfmpegBinary, getFfprobeBinary } from "../utils/ffmpegBinaries.js"; |
| 7 | +import { aacSegmentCount, encodeAac, splitAdtsFrames } from "./aacEncode.js"; |
| 8 | + |
| 9 | +const HAS_FFMPEG = spawnSync(getFfmpegBinary(), ["-version"], { encoding: "utf-8" }).status === 0; |
| 10 | + |
| 11 | +function ffmpeg(args: string[]): Buffer { |
| 12 | + const result = spawnSync(getFfmpegBinary(), ["-nostdin", "-loglevel", "error", ...args], { |
| 13 | + maxBuffer: 1 << 28, |
| 14 | + }); |
| 15 | + if (result.status !== 0) throw new Error(`ffmpeg failed: ${result.stderr?.toString()}`); |
| 16 | + return result.stdout; |
| 17 | +} |
| 18 | + |
| 19 | +function decode(path: string, filter = "anull"): Float32Array { |
| 20 | + const pcm = ffmpeg(["-i", path, "-af", filter, "-f", "f32le", "-ac", "2", "-"]); |
| 21 | + return new Float32Array(pcm.buffer, pcm.byteOffset, pcm.length / 4); |
| 22 | +} |
| 23 | + |
| 24 | +/** Where the audio stream says it ends, in 1/48000 s ticks. */ |
| 25 | +function streamEnd(path: string): string { |
| 26 | + const args = ["-v", "error", "-select_streams", "a", "-show_entries", "stream=duration_ts"]; |
| 27 | + const result = spawnSync(getFfprobeBinary(), [...args, "-of", "csv=p=0", "--", path], { |
| 28 | + encoding: "utf-8", |
| 29 | + }); |
| 30 | + return result.stdout.trim(); |
| 31 | +} |
| 32 | + |
| 33 | +/** Join k decodes from segment k-1's last packet and segment k's first: samples [(cut-1)*1024, cut*1024). */ |
| 34 | +function joinCentres(seconds: number, segments: number): number[] { |
| 35 | + const packets = Math.ceil(Math.round(seconds * 48_000) / 1024) + 1; |
| 36 | + return Array.from({ length: segments - 1 }, (_, k) => { |
| 37 | + const cut = Math.round(((k + 1) * packets) / segments); |
| 38 | + return (cut - 1) * 1024 + 512; |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +/** Error against the source: overall RMS, and the peak within 4096 frames of each cut versus elsewhere. */ |
| 43 | +function errorStats(source: Float32Array, coded: Float32Array, cutFrames: number[]) { |
| 44 | + const nearCut = (i: number) => |
| 45 | + cutFrames.some((cut) => Math.abs(Math.floor(i / 2) - cut * 1024) < 4096); |
| 46 | + let sum = 0; |
| 47 | + let peakNearCut = 0; |
| 48 | + let peakElsewhere = 0; |
| 49 | + for (let i = 0; i < source.length; i++) { |
| 50 | + const error = Math.abs(coded[i]! - source[i]!); |
| 51 | + sum += error * error; |
| 52 | + if (nearCut(i)) peakNearCut = Math.max(peakNearCut, error); |
| 53 | + else peakElsewhere = Math.max(peakElsewhere, error); |
| 54 | + } |
| 55 | + return { rms: Math.sqrt(sum / source.length), peakNearCut, peakElsewhere }; |
| 56 | +} |
| 57 | + |
| 58 | +describe("aacSegmentCount", () => { |
| 59 | + it("keeps audio under two minutes in one encoder", () => { |
| 60 | + expect(aacSegmentCount(119)).toBe(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it("gives each segment at least a minute, up to eight segments", () => { |
| 64 | + expect(aacSegmentCount(180)).toBe(3); |
| 65 | + expect(aacSegmentCount(1800)).toBe(8); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe.skipIf(!HAS_FFMPEG)("encodeAac", () => { |
| 70 | + const dir = mkdtempSync(join(tmpdir(), "hf-aac-encode-")); |
| 71 | + afterAll(() => rmSync(dir, { recursive: true, force: true })); |
| 72 | + |
| 73 | + it("encodes a long mix in parallel segments that decode like one encoder's output", async () => { |
| 74 | + const source = join(dir, "mix.wav"); |
| 75 | + ffmpeg([ |
| 76 | + ...["-f", "lavfi", "-i", "anoisesrc=d=150.5:c=pink:r=48000:a=0.2"], |
| 77 | + ...["-f", "lavfi", "-i", "sine=f=330:d=150.5:r=48000"], |
| 78 | + ...["-filter_complex", "[0][1]amix=2:normalize=0,aformat=channel_layouts=stereo"], |
| 79 | + ...["-c:a", "pcm_f32le", "-y", source], |
| 80 | + ]); |
| 81 | + const single = join(dir, "single.m4a"); |
| 82 | + const split = join(dir, "split.m4a"); |
| 83 | + ffmpeg(["-i", source, "-t", "150.5", "-acodec", "aac", "-b:a", "192k", "-y", single]); |
| 84 | + expect((await encodeAac(source, split, 150.5, { timeout: 120_000 })).success).toBe(true); |
| 85 | + // 150.5 s is not a whole number of AAC frames, so the last packet is short. |
| 86 | + expect(streamEnd(split)).toBe(streamEnd(single)); |
| 87 | + |
| 88 | + const packets = (path: string) => |
| 89 | + splitAdtsFrames(ffmpeg(["-i", path, "-c", "copy", "-f", "adts", "-"])); |
| 90 | + const singlePackets = packets(single); |
| 91 | + const splitPackets = packets(split); |
| 92 | + expect(splitPackets.length).toBe(singlePackets.length); |
| 93 | + expect(Buffer.concat(splitPackets).equals(Buffer.concat(singlePackets))).toBe(false); |
| 94 | + |
| 95 | + const reference = decode(source); |
| 96 | + const singleDecoded = decode(single); |
| 97 | + const splitDecoded = decode(split); |
| 98 | + expect(splitDecoded.length).toBe(singleDecoded.length); |
| 99 | + const cuts = [Math.round(singlePackets.length / 2)]; |
| 100 | + const singleError = errorStats(reference, singleDecoded, cuts); |
| 101 | + const splitError = errorStats(reference, splitDecoded, cuts); |
| 102 | + // Measured: under 1% more RMS error, and cut peaks at one encoder's own level. |
| 103 | + expect(splitError.rms).toBeLessThan(singleError.rms * 1.02); |
| 104 | + expect(splitError.peakNearCut).toBeLessThan(singleError.peakElsewhere); |
| 105 | + }, 120_000); |
| 106 | + |
| 107 | + it("encodes a frame-aligned duration whose sample count is not exact in floating point", async () => { |
| 108 | + // 147.52 s is exactly 6915 AAC frames, but 147.52 * 48000 lands one ulp above it. |
| 109 | + const source = join(dir, "aligned.wav"); |
| 110 | + ffmpeg(["-f", "lavfi", "-i", "sine=f=440:d=147.52:r=48000", "-ac", "2", "-y", source]); |
| 111 | + const single = join(dir, "aligned-single.m4a"); |
| 112 | + const split = join(dir, "aligned-split.m4a"); |
| 113 | + ffmpeg(["-i", source, "-t", "147.52", "-acodec", "aac", "-b:a", "192k", "-y", single]); |
| 114 | + expect((await encodeAac(source, split, 147.52, { timeout: 120_000 })).success).toBe(true); |
| 115 | + expect(streamEnd(split)).toBe(streamEnd(single)); |
| 116 | + }, 120_000); |
| 117 | + |
| 118 | + it("leaves no click at the joins of a pure tone", async () => { |
| 119 | + const source = join(dir, "tone.wav"); |
| 120 | + ffmpeg([ |
| 121 | + ...["-f", "lavfi", "-i", "sine=f=440:d=150.5:r=48000"], |
| 122 | + ...["-af", "volume=0.5,aformat=channel_layouts=stereo", "-c:a", "pcm_f32le", "-y", source], |
| 123 | + ]); |
| 124 | + const output = join(dir, "tone.m4a"); |
| 125 | + expect((await encodeAac(source, output, 150.5, { timeout: 120_000 })).success).toBe(true); |
| 126 | + |
| 127 | + const joins = joinCentres(150.5, aacSegmentCount(150.5)); |
| 128 | + expect(joins).toHaveLength(1); |
| 129 | + const window = 2 * 2400; // 50 ms of interleaved stereo |
| 130 | + const peakAt = (signal: Float32Array, frame: number) => { |
| 131 | + let peak = 0; |
| 132 | + for (let i = Math.max(0, frame * 2 - window / 2); i < frame * 2 + window / 2; i++) { |
| 133 | + peak = Math.max(peak, Math.abs(signal[i] ?? 0)); |
| 134 | + } |
| 135 | + return peak; |
| 136 | + }; |
| 137 | + const medianPeak = (signal: Float32Array) => { |
| 138 | + const peaks = Array.from({ length: Math.floor(signal.length / window) }, (_, w) => |
| 139 | + peakAt(signal, w * (window / 2) + window / 4), |
| 140 | + ).sort((a, b) => a - b); |
| 141 | + return peaks[Math.floor(peaks.length / 2)]!; |
| 142 | + }; |
| 143 | + // A 440 Hz tone has almost nothing above 2 kHz, so a click shows as a broadband spike there; |
| 144 | + // a dropped or repeated frame shows as a phase jump against the source. |
| 145 | + const highPassed = decode(output, Array(4).fill("highpass=f=2000").join(",")); |
| 146 | + const reference = decode(source); |
| 147 | + const residual = decode(output).map((sample, i) => sample - (reference[i] ?? 0)); |
| 148 | + const [hpMedian, residualMedian] = [medianPeak(highPassed), medianPeak(residual)]; |
| 149 | + // Measured: joins sit at the median; the source's own stream-copy joins read about 40 dB above it. |
| 150 | + for (const centre of joins) { |
| 151 | + expect(peakAt(highPassed, centre)).toBeLessThan(hpMedian * 2); |
| 152 | + expect(peakAt(residual, centre)).toBeLessThan(residualMedian * 2); |
| 153 | + } |
| 154 | + }, 120_000); |
| 155 | +}); |
0 commit comments