|
| 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 { |
| 9 | + addAudioEq, |
| 10 | + audioEqIds, |
| 11 | + audioEqSummary, |
| 12 | + HF_AUDIO_EQ_3, |
| 13 | + HF_AUDIO_EQ_5, |
| 14 | + HF_AUDIO_EQ_RANGE_DB, |
| 15 | + readAudioEqBands, |
| 16 | + removeAudioEq, |
| 17 | + setAudioEqBandGain, |
| 18 | +} from "./audioFxEq.js"; |
| 19 | + |
| 20 | +const empty = (): HfAudioFxChain => ({ version: HF_AUDIO_FX_CHAIN_VERSION, nodes: [] }); |
| 21 | + |
| 22 | +describe("adding an EQ", () => { |
| 23 | + it("writes one ordinary filter per band, in band order", () => { |
| 24 | + const { chain } = addAudioEq(empty()); |
| 25 | + expect(chain.nodes.map((n) => n.type)).toEqual(["lowshelf", "peaking", "highshelf"]); |
| 26 | + // Ordinary nodes: an author who opens the details finds filters they could |
| 27 | + // have added by hand, not an opaque "eq" the graph has to special-case. |
| 28 | + expect(chain.nodes.every((n) => n.enabled)).toBe(true); |
| 29 | + expect(chain.nodes.map((n) => n.label)).toEqual(["Bass", "Middle", "Treble"]); |
| 30 | + }); |
| 31 | + |
| 32 | + it("gives every band an id, because a lane addresses effects by id", () => { |
| 33 | + const { chain } = addAudioEq(empty(), HF_AUDIO_EQ_5); |
| 34 | + const ids = chain.nodes.map((n) => n.id); |
| 35 | + expect(ids.every(Boolean)).toBe(true); |
| 36 | + expect(new Set(ids).size).toBe(ids.length); |
| 37 | + }); |
| 38 | + |
| 39 | + it("starts flat, so adding one changes nothing until a fader moves", () => { |
| 40 | + const { chain, eqId } = addAudioEq(empty()); |
| 41 | + for (const band of readAudioEqBands(chain, eqId)) expect(band.gain).toBe(0); |
| 42 | + expect(audioEqSummary(readAudioEqBands(chain, eqId))).toMatch(/^Flat/); |
| 43 | + }); |
| 44 | + |
| 45 | + it("keeps two EQs apart", () => { |
| 46 | + const first = addAudioEq(empty()); |
| 47 | + const second = addAudioEq(first.chain, HF_AUDIO_EQ_5); |
| 48 | + expect(second.eqId).not.toBe(first.eqId); |
| 49 | + expect(audioEqIds(second.chain)).toEqual([first.eqId, second.eqId]); |
| 50 | + expect(readAudioEqBands(second.chain, first.eqId)).toHaveLength(3); |
| 51 | + expect(readAudioEqBands(second.chain, second.eqId)).toHaveLength(5); |
| 52 | + expect(new Set(second.chain.nodes.map((n) => n.id)).size).toBe(second.chain.nodes.length); |
| 53 | + }); |
| 54 | + |
| 55 | + it("leaves effects that were already there alone", () => { |
| 56 | + const before: HfAudioFxChain = { |
| 57 | + version: HF_AUDIO_FX_CHAIN_VERSION, |
| 58 | + nodes: [{ type: "reverb", id: "mine", enabled: true }], |
| 59 | + }; |
| 60 | + const { chain } = addAudioEq(before); |
| 61 | + expect(chain.nodes[0]?.id).toBe("mine"); |
| 62 | + expect(chain.nodes).toHaveLength(4); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe("moving a fader", () => { |
| 67 | + it("changes only that band", () => { |
| 68 | + const { chain, eqId } = addAudioEq(empty()); |
| 69 | + const next = setAudioEqBandGain(chain, eqId, "Bass", 4.5); |
| 70 | + const bands = readAudioEqBands(next, eqId); |
| 71 | + expect(bands.find((b) => b.name === "Bass")?.gain).toBe(4.5); |
| 72 | + expect(bands.find((b) => b.name === "Middle")?.gain).toBe(0); |
| 73 | + expect(bands.find((b) => b.name === "Treble")?.gain).toBe(0); |
| 74 | + }); |
| 75 | + |
| 76 | + it("leaves the band's frequency and width alone", () => { |
| 77 | + // The fader is one control. Moving it must not quietly re-seed the rest of |
| 78 | + // the band, or an author who set a frequency by hand loses it on the next drag. |
| 79 | + const { chain, eqId } = addAudioEq(empty(), HF_AUDIO_EQ_5); |
| 80 | + const before = readAudioEqBands(chain, eqId).find((b) => b.name === "Clarity")!; |
| 81 | + const next = setAudioEqBandGain(chain, eqId, "Clarity", -3); |
| 82 | + const after = readAudioEqBands(next, eqId).find((b) => b.name === "Clarity")!; |
| 83 | + expect(after.frequency).toBe(before.frequency); |
| 84 | + expect(after.q).toBe(before.q); |
| 85 | + expect(after.gain).toBe(-3); |
| 86 | + }); |
| 87 | + |
| 88 | + it("holds the fader to a tone control's range, not a repair tool's", () => { |
| 89 | + // The filters themselves allow ±40 dB. A tone control that can bury a |
| 90 | + // track under 40 dB of bass is not a tone control. |
| 91 | + const { chain, eqId } = addAudioEq(empty()); |
| 92 | + const hot = setAudioEqBandGain(chain, eqId, "Bass", 40); |
| 93 | + const cold = setAudioEqBandGain(chain, eqId, "Bass", -40); |
| 94 | + expect(readAudioEqBands(hot, eqId)[0]?.gain).toBe(HF_AUDIO_EQ_RANGE_DB); |
| 95 | + expect(readAudioEqBands(cold, eqId)[0]?.gain).toBe(-HF_AUDIO_EQ_RANGE_DB); |
| 96 | + }); |
| 97 | + |
| 98 | + it("ignores a band name that is not in this EQ", () => { |
| 99 | + const { chain, eqId } = addAudioEq(empty()); |
| 100 | + const next = setAudioEqBandGain(chain, eqId, "Nonsense", 6); |
| 101 | + expect(readAudioEqBands(next, eqId).every((b) => b.gain === 0)).toBe(true); |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe("the chain is the truth", () => { |
| 106 | + it("reads a frequency the author moved by hand", () => { |
| 107 | + // The nodes are authoritative, not a cached band list: opening the details |
| 108 | + // and moving a frequency has to show up on the fader's own band. |
| 109 | + const { chain, eqId } = addAudioEq(empty()); |
| 110 | + const edited: HfAudioFxChain = { |
| 111 | + ...chain, |
| 112 | + nodes: chain.nodes.map((n) => |
| 113 | + n.label === "Middle" ? { ...n, params: { ...n.params, frequency: 700 } } : n, |
| 114 | + ), |
| 115 | + }; |
| 116 | + expect(readAudioEqBands(edited, eqId).find((b) => b.name === "Middle")?.frequency).toBe(700); |
| 117 | + }); |
| 118 | + |
| 119 | + it("survives being written to an attribute and read back", () => { |
| 120 | + const { chain, eqId } = addAudioEq(empty(), HF_AUDIO_EQ_5); |
| 121 | + const moved = setAudioEqBandGain(chain, eqId, "Air", 2.5); |
| 122 | + const back = parseAudioFxChain(serializeAudioFxChain(moved)); |
| 123 | + // Without fromEq surviving, the module cannot find its own bands after a |
| 124 | + // reload and the EQ silently becomes five loose filters. |
| 125 | + expect(audioEqIds(back)).toEqual([eqId]); |
| 126 | + expect(readAudioEqBands(back, eqId).map((b) => b.name)).toEqual([ |
| 127 | + "Bass", |
| 128 | + "Warmth", |
| 129 | + "Middle", |
| 130 | + "Clarity", |
| 131 | + "Air", |
| 132 | + ]); |
| 133 | + expect(readAudioEqBands(back, eqId).find((b) => b.name === "Air")?.gain).toBe(2.5); |
| 134 | + }); |
| 135 | + |
| 136 | + it("removes a whole EQ without touching anything else", () => { |
| 137 | + const before: HfAudioFxChain = { |
| 138 | + version: HF_AUDIO_FX_CHAIN_VERSION, |
| 139 | + nodes: [{ type: "reverb", id: "mine", enabled: true }], |
| 140 | + }; |
| 141 | + const { chain, eqId } = addAudioEq(before); |
| 142 | + const gone = removeAudioEq(chain, eqId); |
| 143 | + expect(gone.nodes.map((n) => n.id)).toEqual(["mine"]); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +describe("what it says when closed", () => { |
| 148 | + it("names only the bands that were moved", () => { |
| 149 | + const { chain, eqId } = addAudioEq(empty()); |
| 150 | + const next = setAudioEqBandGain(setAudioEqBandGain(chain, eqId, "Bass", 3), eqId, "Treble", -2); |
| 151 | + expect(audioEqSummary(readAudioEqBands(next, eqId))).toBe("Bass +3, Treble −2"); |
| 152 | + }); |
| 153 | + |
| 154 | + it("says so when nothing has been touched", () => { |
| 155 | + expect(audioEqSummary(HF_AUDIO_EQ_3)).toMatch(/^Flat/); |
| 156 | + }); |
| 157 | +}); |
0 commit comments