|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + HF_AUDIO_FX_CHAIN_VERSION, |
| 4 | + parseAudioFxChain, |
| 5 | + serializeAudioFxChain, |
| 6 | + type HfAudioFxChain, |
| 7 | +} from "./audioFx.js"; |
| 8 | +import { sampleAutomationLane } from "./audioAutomation.js"; |
| 9 | +import { applyAudioFxPreset, getAudioFxPreset } from "./audioFxPresets.js"; |
| 10 | +import { |
| 11 | + analyseLevelling, |
| 12 | + levellerProfile, |
| 13 | + levellingResult, |
| 14 | + levellingSummary, |
| 15 | + removeLevelling, |
| 16 | +} from "./audioLeveller.js"; |
| 17 | + |
| 18 | +const SR = 48000; |
| 19 | +const empty = (): HfAudioFxChain => ({ version: HF_AUDIO_FX_CHAIN_VERSION, nodes: [] }); |
| 20 | + |
| 21 | +/** A tone whose amplitude changes per section, so the levelling has real work. */ |
| 22 | +function uneven(sections: { seconds: number; amp: number }[]): Float32Array { |
| 23 | + const total = sections.reduce((n, s) => n + Math.floor(SR * s.seconds), 0); |
| 24 | + const out = new Float32Array(total); |
| 25 | + let at = 0; |
| 26 | + for (const section of sections) { |
| 27 | + const n = Math.floor(SR * section.seconds); |
| 28 | + for (let i = 0; i < n; i += 1) { |
| 29 | + out[at + i] = section.amp * Math.sin((2 * Math.PI * 300 * (at + i)) / SR); |
| 30 | + } |
| 31 | + at += n; |
| 32 | + } |
| 33 | + return out; |
| 34 | +} |
| 35 | + |
| 36 | +const at = (points: { t: number; v: number }[], t: number) => |
| 37 | + sampleAutomationLane({ target: "fx.n1.gain", points }, t); |
| 38 | + |
| 39 | +describe("measuring", () => { |
| 40 | + it("lifts a quiet passage and leaves the loud one alone", () => { |
| 41 | + // Loud, then 18 dB down, then loud again. |
| 42 | + const points = analyseLevelling( |
| 43 | + uneven([ |
| 44 | + { seconds: 3, amp: 0.5 }, |
| 45 | + { seconds: 3, amp: 0.06 }, |
| 46 | + { seconds: 3, amp: 0.5 }, |
| 47 | + ]), |
| 48 | + SR, |
| 49 | + ); |
| 50 | + expect(points.length).toBeGreaterThan(0); |
| 51 | + // Mid-quiet-section, well past the attack. |
| 52 | + expect(at(points, 5.5)).toBeGreaterThan(3); |
| 53 | + // Mid-loud-section, well past the release. |
| 54 | + expect(Math.abs(at(points, 2.5))).toBeLessThan(2); |
| 55 | + }); |
| 56 | + |
| 57 | + it("finds nothing to do on a track that is already even", () => { |
| 58 | + // A script that always writes something teaches an author it is doing |
| 59 | + // nothing; saying "already even" is the useful answer. |
| 60 | + expect(analyseLevelling(uneven([{ seconds: 6, amp: 0.4 }]), SR)).toEqual([]); |
| 61 | + }); |
| 62 | + |
| 63 | + it("leaves room tone alone rather than lifting it into the mix", () => { |
| 64 | + // Deliberately NOT digital silence: a real pause is quiet but finite, and |
| 65 | + // that is the case the floor exists for. Absolute zero would be skipped by |
| 66 | + // the isFinite check alone and prove nothing. |
| 67 | + const points = analyseLevelling( |
| 68 | + uneven([ |
| 69 | + { seconds: 3, amp: 0.5 }, |
| 70 | + { seconds: 3, amp: 0.0008 }, |
| 71 | + { seconds: 3, amp: 0.5 }, |
| 72 | + ]), |
| 73 | + SR, |
| 74 | + 1, |
| 75 | + ); |
| 76 | + // ~56 dB down: a pause with a noise floor, not a quiet passage. Gain |
| 77 | + // applied here is gain applied to the room. |
| 78 | + expect(Math.abs(at(points, 5.5))).toBeLessThan(1.5); |
| 79 | + }); |
| 80 | + |
| 81 | + it("targets a level the track reaches, not its loudest instant", () => { |
| 82 | + // Mostly quiet with one loud burst. Against the PEAK the whole body of the |
| 83 | + // track reads as "quiet" and gets hauled up; against a level the track |
| 84 | + // actually sustains, the body is already the target and barely moves. |
| 85 | + const points = analyseLevelling( |
| 86 | + uneven([ |
| 87 | + { seconds: 6, amp: 0.08 }, |
| 88 | + { seconds: 1, amp: 0.8 }, |
| 89 | + { seconds: 5, amp: 0.08 }, |
| 90 | + ]), |
| 91 | + SR, |
| 92 | + 1, |
| 93 | + ); |
| 94 | + expect(Math.abs(at(points, 3))).toBeLessThan(3); |
| 95 | + }); |
| 96 | + |
| 97 | + it("never asks for more correction than it can justify", () => { |
| 98 | + // ~30 dB below the loud section: still well above the silence floor, so it |
| 99 | + // IS a passage to lift — and at full strength the raw ask is over 25 dB, |
| 100 | + // which is more gain than any quiet passage should be given. |
| 101 | + const points = analyseLevelling( |
| 102 | + uneven([ |
| 103 | + { seconds: 3, amp: 0.6 }, |
| 104 | + { seconds: 4, amp: 0.019 }, |
| 105 | + ]), |
| 106 | + SR, |
| 107 | + 1, |
| 108 | + ); |
| 109 | + expect(points.some((p) => p.v > 6)).toBe(true); |
| 110 | + for (const p of points) expect(Math.abs(p.v)).toBeLessThanOrEqual(12); |
| 111 | + }); |
| 112 | + |
| 113 | + it("starts at the clip's start, so the lane does not slide in from nowhere", () => { |
| 114 | + const points = analyseLevelling( |
| 115 | + uneven([ |
| 116 | + { seconds: 3, amp: 0.5 }, |
| 117 | + { seconds: 3, amp: 0.06 }, |
| 118 | + ]), |
| 119 | + SR, |
| 120 | + ); |
| 121 | + expect(points[0]?.t).toBe(0); |
| 122 | + }); |
| 123 | + |
| 124 | + it("handles an empty track without inventing a lane", () => { |
| 125 | + expect(analyseLevelling(new Float32Array(0), SR)).toEqual([]); |
| 126 | + expect(analyseLevelling(uneven([{ seconds: 1, amp: 0.4 }]), 0)).toEqual([]); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +describe("strength", () => { |
| 131 | + it("corrects more the further it is turned up", () => { |
| 132 | + const track = uneven([ |
| 133 | + { seconds: 3, amp: 0.5 }, |
| 134 | + { seconds: 3, amp: 0.06 }, |
| 135 | + ]); |
| 136 | + const gentle = at(analyseLevelling(track, SR, 0.1), 5.5); |
| 137 | + const strong = at(analyseLevelling(track, SR, 1), 5.5); |
| 138 | + expect(strong).toBeGreaterThan(gentle); |
| 139 | + }); |
| 140 | + |
| 141 | + it("still leaves some of the performance in at full strength", () => { |
| 142 | + // Driving a track to a flat line removes the performance along with the |
| 143 | + // inconsistency, so even 1.0 corrects most rather than all of it. |
| 144 | + expect(levellerProfile(1).correction).toBeLessThan(1); |
| 145 | + expect(levellerProfile(0).correction).toBeGreaterThan(0); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe("what it writes", () => { |
| 150 | + it("rides a gain node, because a volume lane can only attenuate", () => { |
| 151 | + // VOLUME_RANGE is 0..1 and normaliseEnvelope clamps into it, so a volume |
| 152 | + // lane cannot lift a quiet passage at all. This is the whole reason the |
| 153 | + // script writes a node instead of only a lane. |
| 154 | + const result = levellingResult( |
| 155 | + empty(), |
| 156 | + uneven([ |
| 157 | + { seconds: 3, amp: 0.5 }, |
| 158 | + { seconds: 3, amp: 0.06 }, |
| 159 | + ]), |
| 160 | + SR, |
| 161 | + ); |
| 162 | + expect(result).not.toBeNull(); |
| 163 | + const node = result!.chain.nodes[0]!; |
| 164 | + expect(node.type).toBe("gain"); |
| 165 | + expect(node.label).toBe("Even Out Levels"); |
| 166 | + expect(node.params?.gain).toBe(0); |
| 167 | + expect(result!.automation.lanes[0]!.target).toBe(`fx.${node.id}.gain`); |
| 168 | + }); |
| 169 | + |
| 170 | + it("returns nothing when there is nothing to correct", () => { |
| 171 | + expect(levellingResult(empty(), uneven([{ seconds: 6, amp: 0.4 }]), SR)).toBeNull(); |
| 172 | + }); |
| 173 | + |
| 174 | + it("replaces its own stage instead of stacking a second one", () => { |
| 175 | + const track = uneven([ |
| 176 | + { seconds: 3, amp: 0.5 }, |
| 177 | + { seconds: 3, amp: 0.06 }, |
| 178 | + ]); |
| 179 | + const once = levellingResult(empty(), track, SR)!; |
| 180 | + const twice = levellingResult(once.chain, track, SR)!; |
| 181 | + expect(twice.chain.nodes.filter((n) => n.fromLeveller)).toHaveLength(1); |
| 182 | + // Same node id, so the lane it already wrote still addresses the right stage. |
| 183 | + expect(twice.chain.nodes[0]!.id).toBe(once.chain.nodes[0]!.id); |
| 184 | + }); |
| 185 | + |
| 186 | + it("goes in FRONT of a trailing limiter, never after it", () => { |
| 187 | + // The likely sequence: apply Clean Voice, then even out the levels. Clean |
| 188 | + // Voice ends in a Peak Ceiling, and up to 12 dB of lift landing after that |
| 189 | + // ceiling means loud material at -1 dBFS goes over full scale and the |
| 190 | + // render shears it flat. A ceiling with something after it is not a ceiling. |
| 191 | + const voiced = applyAudioFxPreset(empty(), getAudioFxPreset("voice-clean")!); |
| 192 | + expect(voiced.nodes[voiced.nodes.length - 1]!.type).toBe("limiter"); |
| 193 | + |
| 194 | + const result = levellingResult( |
| 195 | + voiced, |
| 196 | + uneven([ |
| 197 | + { seconds: 3, amp: 0.5 }, |
| 198 | + { seconds: 3, amp: 0.06 }, |
| 199 | + ]), |
| 200 | + SR, |
| 201 | + )!; |
| 202 | + const types = result.chain.nodes.map((n) => n.type); |
| 203 | + expect(types[types.length - 1]).toBe("limiter"); |
| 204 | + expect(types[types.length - 2]).toBe("gain"); |
| 205 | + expect(result.chain.nodes.find((n) => n.fromLeveller)).toBeTruthy(); |
| 206 | + }); |
| 207 | + |
| 208 | + it("leaves hand-added effects alone", () => { |
| 209 | + const chain: HfAudioFxChain = { |
| 210 | + version: HF_AUDIO_FX_CHAIN_VERSION, |
| 211 | + nodes: [{ type: "reverb", id: "mine", enabled: true }], |
| 212 | + }; |
| 213 | + const result = levellingResult( |
| 214 | + chain, |
| 215 | + uneven([ |
| 216 | + { seconds: 3, amp: 0.5 }, |
| 217 | + { seconds: 3, amp: 0.06 }, |
| 218 | + ]), |
| 219 | + SR, |
| 220 | + )!; |
| 221 | + expect(result.chain.nodes.map((n) => n.id)).toContain("mine"); |
| 222 | + expect(result.chain.nodes).toHaveLength(2); |
| 223 | + }); |
| 224 | + |
| 225 | + it("survives the attribute round trip", () => { |
| 226 | + const result = levellingResult( |
| 227 | + empty(), |
| 228 | + uneven([ |
| 229 | + { seconds: 3, amp: 0.5 }, |
| 230 | + { seconds: 3, amp: 0.06 }, |
| 231 | + ]), |
| 232 | + SR, |
| 233 | + )!; |
| 234 | + const back = parseAudioFxChain(serializeAudioFxChain(result.chain)); |
| 235 | + // Without fromLeveller surviving, re-running stacks a second gain stage. |
| 236 | + expect(back.nodes.filter((n) => n.fromLeveller)).toHaveLength(1); |
| 237 | + expect(back.nodes[0]!.label).toBe("Even Out Levels"); |
| 238 | + }); |
| 239 | + |
| 240 | + it("names the lane it takes away with it", () => { |
| 241 | + const result = levellingResult( |
| 242 | + empty(), |
| 243 | + uneven([ |
| 244 | + { seconds: 3, amp: 0.5 }, |
| 245 | + { seconds: 3, amp: 0.06 }, |
| 246 | + ]), |
| 247 | + SR, |
| 248 | + )!; |
| 249 | + const removed = removeLevelling(result.chain); |
| 250 | + expect(removed.chain.nodes).toHaveLength(0); |
| 251 | + // An orphaned lane keeps driving a parameter that is no longer there. |
| 252 | + expect(removed.removedTarget).toBe(result.automation.lanes[0]!.target); |
| 253 | + }); |
| 254 | +}); |
| 255 | + |
| 256 | +describe("what it says", () => { |
| 257 | + it("describes the moves rather than listing numbers", () => { |
| 258 | + expect( |
| 259 | + levellingSummary([ |
| 260 | + { t: 0, v: 0 }, |
| 261 | + { t: 1, v: 4.2 }, |
| 262 | + ]), |
| 263 | + ).toMatch(/lifting quiet parts/); |
| 264 | + expect(levellingSummary([])).toMatch(/Already even/); |
| 265 | + }); |
| 266 | +}); |
0 commit comments