|
| 1 | +import { execFileSync } 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 { describe, expect, it } from "vitest"; |
| 6 | +import { findFfBinary } from "@hyperframes/parsers/ff-binaries"; |
| 7 | +import { |
| 8 | + accumulatePeaks, |
| 9 | + buildPeakMapCacheKey, |
| 10 | + createPeakAccumulator, |
| 11 | + decodePeakMap, |
| 12 | + finishPeaks, |
| 13 | +} from "./peakMap.js"; |
| 14 | + |
| 15 | +function stereo(frames: Array<[number, number]>): Buffer { |
| 16 | + const buf = Buffer.alloc(frames.length * 8); |
| 17 | + frames.forEach(([l, r], i) => { |
| 18 | + buf.writeFloatLE(l, i * 8); |
| 19 | + buf.writeFloatLE(r, i * 8 + 4); |
| 20 | + }); |
| 21 | + return buf; |
| 22 | +} |
| 23 | + |
| 24 | +describe("peak accumulation", () => { |
| 25 | + it("keeps the absolute max of either channel per bin, un-normalized", () => { |
| 26 | + const acc = createPeakAccumulator(4, 0.5, 2); |
| 27 | + accumulatePeaks( |
| 28 | + acc, |
| 29 | + stereo([ |
| 30 | + [0.1, -0.3], |
| 31 | + [0.2, 0], |
| 32 | + [0, 0.05], |
| 33 | + [-0.04, 0], |
| 34 | + ]), |
| 35 | + ); |
| 36 | + expect(finishPeaks(acc)).toEqual([0.3, 0.05]); |
| 37 | + }); |
| 38 | + |
| 39 | + it("carries a frame split across chunks", () => { |
| 40 | + const acc = createPeakAccumulator(2, 1, 2); |
| 41 | + const whole = stereo([ |
| 42 | + [0.5, 0], |
| 43 | + [0, -0.9], |
| 44 | + ]); |
| 45 | + accumulatePeaks(acc, whole.subarray(0, 5)); |
| 46 | + accumulatePeaks(acc, whole.subarray(5)); |
| 47 | + expect(finishPeaks(acc)).toEqual([0.9]); |
| 48 | + expect(acc.frame).toBe(2); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("peak accumulation, mono", () => { |
| 53 | + it("reads one channel per frame", () => { |
| 54 | + const acc = createPeakAccumulator(2, 1, 1); |
| 55 | + const buf = Buffer.alloc(8); |
| 56 | + buf.writeFloatLE(-1, 0); |
| 57 | + buf.writeFloatLE(0.5, 4); |
| 58 | + accumulatePeaks(acc, buf); |
| 59 | + expect(finishPeaks(acc)).toEqual([1]); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("buildPeakMapCacheKey", () => { |
| 64 | + it("changes when the file is replaced and never collides with a waveform entry", () => { |
| 65 | + const a = buildPeakMapCacheKey("a/talk.mp4", { size: 10, mtimeMs: 1 }); |
| 66 | + expect(a).toBe(buildPeakMapCacheKey("a/talk.mp4", { size: 10, mtimeMs: 1 })); |
| 67 | + expect(a).not.toBe(buildPeakMapCacheKey("a/talk.mp4", { size: 11, mtimeMs: 1 })); |
| 68 | + expect(a).not.toBe(buildPeakMapCacheKey("a/talk.mp4", { size: 10, mtimeMs: 2 })); |
| 69 | + expect(a).toMatch(/^peaks-v2_a_talk\.mp4_10-1\.json$/); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe("decodePeakMap", () => { |
| 74 | + const ffmpeg = findFfBinary("ffmpeg"); |
| 75 | + it.skipIf(!ffmpeg)( |
| 76 | + "reads a 0 dBFS video track near 1.0 and a -12 dBFS one near 0.25", |
| 77 | + async () => { |
| 78 | + const dir = mkdtempSync(join(tmpdir(), "hf-peaks-")); |
| 79 | + try { |
| 80 | + const make = (name: string, volume: string) => { |
| 81 | + const file = join(dir, name); |
| 82 | + execFileSync(ffmpeg ?? "ffmpeg", [ |
| 83 | + "-v", |
| 84 | + "error", |
| 85 | + "-f", |
| 86 | + "lavfi", |
| 87 | + "-i", |
| 88 | + "color=c=black:s=32x32:d=1", |
| 89 | + "-f", |
| 90 | + "lavfi", |
| 91 | + "-i", |
| 92 | + `sine=frequency=440:duration=1,volume=${volume}`, |
| 93 | + "-shortest", |
| 94 | + "-c:v", |
| 95 | + "libx264", |
| 96 | + "-c:a", |
| 97 | + "pcm_s16le", |
| 98 | + file, |
| 99 | + ]); |
| 100 | + return file; |
| 101 | + }; |
| 102 | + const loud = await decodePeakMap(make("loud.mov", "8")); |
| 103 | + const quiet = await decodePeakMap(make("quiet.mov", "2")); |
| 104 | + expect(Math.max(...loud)).toBeGreaterThan(0.95); |
| 105 | + expect(Math.max(...quiet)).toBeLessThan(0.3); |
| 106 | + expect(loud.length).toBeGreaterThanOrEqual(19); |
| 107 | + } finally { |
| 108 | + rmSync(dir, { recursive: true, force: true }); |
| 109 | + } |
| 110 | + }, |
| 111 | + ); |
| 112 | + |
| 113 | + it.skipIf(!ffmpeg)( |
| 114 | + "decodes the probed first audio stream when a louder stereo stream follows it", |
| 115 | + async () => { |
| 116 | + const dir = mkdtempSync(join(tmpdir(), "hf-peaks-")); |
| 117 | + try { |
| 118 | + const file = join(dir, "multi.mkv"); |
| 119 | + execFileSync(ffmpeg ?? "ffmpeg", [ |
| 120 | + "-v", |
| 121 | + "error", |
| 122 | + "-f", |
| 123 | + "lavfi", |
| 124 | + "-i", |
| 125 | + "aevalsrc=0.1*sin(2*PI*440*t):s=48000:d=1", |
| 126 | + "-f", |
| 127 | + "lavfi", |
| 128 | + "-i", |
| 129 | + "aevalsrc=0.9*sin(2*PI*440*t)|0.9*sin(2*PI*440*t):s=48000:d=1", |
| 130 | + "-map", |
| 131 | + "0:a", |
| 132 | + "-map", |
| 133 | + "1:a", |
| 134 | + "-c:a", |
| 135 | + "pcm_f32le", |
| 136 | + "-disposition:a", |
| 137 | + "0", |
| 138 | + file, |
| 139 | + ]); |
| 140 | + const peaks = await decodePeakMap(file); |
| 141 | + expect(peaks.length).toBeGreaterThanOrEqual(19); |
| 142 | + expect(peaks.length).toBeLessThanOrEqual(21); |
| 143 | + expect(Math.max(...peaks)).toBeCloseTo(0.1, 2); |
| 144 | + } finally { |
| 145 | + rmSync(dir, { recursive: true, force: true }); |
| 146 | + } |
| 147 | + }, |
| 148 | + ); |
| 149 | +}); |
0 commit comments