-
Notifications
You must be signed in to change notification settings - Fork 4k
fix(audio): carry authoring gain above unity into preview and render #3304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
miguel-heygen
wants to merge
4
commits into
main
Choose a base branch
from
audio-gain-above-unity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d302bc0
fix(audio): carry authoring gain above unity into preview and render
miguel-heygen adfddff
fix(audio): let the volume probe run where there is no DOM
miguel-heygen 907362b
test(engine): give the mixer level cases the ffmpeg timeout its sibli…
miguel-heygen 6a5267a
fix(studio): hold the percent volume slider on a clip authored above …
miguel-heygen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| AUDIO_GAIN_FADER_MAX, | ||
| formatAudioGain, | ||
| AUDIO_GAIN_FADER_MIN, | ||
| MAX_AUDIO_GAIN, | ||
| audioGainToFaderPosition, | ||
| audioGainToText, | ||
| audioFaderPositionToGain, | ||
| } from "./audioGain"; | ||
|
|
||
| describe("audio gain fader", () => { | ||
| it("puts unity gain at the physical midpoint", () => { | ||
| expect(audioGainToFaderPosition(1)).toBe(0); | ||
| expect(audioFaderPositionToGain(0)).toBe(1); | ||
| }); | ||
|
|
||
| it("provides +12 dB of boost above unity", () => { | ||
| expect(audioFaderPositionToGain(AUDIO_GAIN_FADER_MAX)).toBeCloseTo(MAX_AUDIO_GAIN, 6); | ||
| expect(audioGainToText(MAX_AUDIO_GAIN)).toBe("+12.0 dB"); | ||
| }); | ||
|
|
||
| it("preserves a true silence endpoint below unity", () => { | ||
| expect(audioFaderPositionToGain(AUDIO_GAIN_FADER_MIN)).toBe(0); | ||
| expect(audioGainToText(0)).toBe("-∞ dB"); | ||
| }); | ||
|
|
||
| it("pins sub-floor gain to the fader's silence endpoint", () => { | ||
| expect(audioGainToFaderPosition(0.00001)).toBe(AUDIO_GAIN_FADER_MIN); | ||
| }); | ||
|
|
||
| it("round-trips representative attenuation and boost values", () => { | ||
| for (const gain of [0.1, 0.5, 1, 2, MAX_AUDIO_GAIN]) { | ||
| expect(audioFaderPositionToGain(audioGainToFaderPosition(gain))).toBeCloseTo(gain, 6); | ||
| } | ||
| }); | ||
|
|
||
| describe("formatAudioGain", () => { | ||
| it("never collapses an audible fader stop onto silence", () => { | ||
| for (let position = AUDIO_GAIN_FADER_MIN + 1; position <= AUDIO_GAIN_FADER_MAX; position++) { | ||
| const serialized = formatAudioGain(audioFaderPositionToGain(position)); | ||
| expect(Number(serialized)).toBeGreaterThan(0); | ||
| } | ||
| // Only the very bottom of the travel is a real mute. | ||
| expect(formatAudioGain(audioFaderPositionToGain(AUDIO_GAIN_FADER_MIN))).toBe("0"); | ||
| }); | ||
|
|
||
| it("puts the knob back where the user let go of it", () => { | ||
| for (let position = AUDIO_GAIN_FADER_MIN; position <= AUDIO_GAIN_FADER_MAX; position++) { | ||
| const written = Number(formatAudioGain(audioFaderPositionToGain(position))); | ||
| expect(Math.round(audioGainToFaderPosition(written))).toBe(position); | ||
| } | ||
| }); | ||
|
|
||
| it("keeps a serialized gain short and inside the ceiling", () => { | ||
| expect(formatAudioGain(1)).toBe("1"); | ||
| expect(formatAudioGain(0.5)).toBe("0.5"); | ||
| expect(formatAudioGain(99)).toBe(formatAudioGain(MAX_AUDIO_GAIN)); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /** | ||
| * Authoring gain for a media clip. | ||
| * | ||
| * HTMLMediaElement.volume is limited to 0..1, but HyperFrames' Web Audio | ||
| * preview and FFmpeg render paths both support gain above unity. Keep the | ||
| * shared ceiling here so Studio, preview, and render cannot drift. | ||
| */ | ||
| export const MAX_AUDIO_GAIN_DB = 12; | ||
| export const MAX_AUDIO_GAIN = 10 ** (MAX_AUDIO_GAIN_DB / 20); | ||
|
|
||
| /** Studio fader coordinates. Unity is deliberately the physical midpoint. */ | ||
| export const AUDIO_GAIN_FADER_MIN = -100; | ||
| export const AUDIO_GAIN_FADER_MAX = 100; | ||
|
|
||
| const MIN_AUDIO_GAIN_DB = -60; | ||
|
|
||
| export function clampAudioGain(value: number): number { | ||
| if (!Number.isFinite(value)) return 1; | ||
| return Math.max(0, Math.min(MAX_AUDIO_GAIN, value)); | ||
| } | ||
|
|
||
| export function clampNativeMediaVolume(value: number): number { | ||
| if (!Number.isFinite(value)) return 1; | ||
| return Math.max(0, Math.min(1, value)); | ||
| } | ||
|
|
||
| /** | ||
| * Serialize an authored gain for `data-volume`. | ||
| * | ||
| * The fader travels in dB, so its stops are irrational (position -70 is | ||
| * 10 ** (-42/20)). Rounding to two decimals — what the generic numeric | ||
| * attribute formatter does — collapses the whole bottom of the fader onto | ||
| * `"0"` (a hard mute) and makes the knob jump on release everywhere below | ||
| * unity. Six decimals round-trip every integer fader stop back to itself. | ||
| */ | ||
| export function formatAudioGain(gain: number): string { | ||
| return clampAudioGain(gain) | ||
| .toFixed(6) | ||
| .replace(/\.?0+$/, ""); | ||
| } | ||
|
|
||
| /** | ||
| * Run `probe` with `el.volume` shadowed by an accessor that keeps the authored | ||
| * value instead of the spec's [0,1] clamp. | ||
| * | ||
| * `HTMLMediaElement.volume` cannot hold gain above unity, so a clip authored | ||
| * at `data-volume="1.95"` reads back as 1 the moment the probe seeds it — and | ||
| * a GSAP tween started from that seed fades from 0 dB rather than from the | ||
| * authored boost. Both the FFmpeg mixer and the Web Audio transport carry gain | ||
| * up to MAX_AUDIO_GAIN, so the clamp is a probe artefact, not a real ceiling. | ||
| * The native setter still receives the clamped value, so nothing outside the | ||
| * probe observes an out-of-range volume, and the shadow is removed afterwards. | ||
| */ | ||
| export function withUnclampedVolume<T>(el: HTMLMediaElement, probe: () => T): T { | ||
| // Guarded for non-DOM runtimes: the probe that calls this is also reachable | ||
| // from tests and tools that run outside a browser, where the clamped path is | ||
| // the right (and only) answer. | ||
| const descriptor = | ||
| typeof HTMLMediaElement === "undefined" | ||
| ? undefined | ||
| : Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, "volume"); | ||
| const nativeGet = descriptor?.get; | ||
| const nativeSet = descriptor?.set; | ||
| if (!nativeGet || !nativeSet) return probe(); | ||
|
|
||
| let authored = Number(nativeGet.call(el)); | ||
| Object.defineProperty(el, "volume", { | ||
| configurable: true, | ||
| get: () => authored, | ||
| set: (value: number) => { | ||
| authored = Number(value); | ||
| nativeSet.call(el, clampNativeMediaVolume(authored)); | ||
| }, | ||
| }); | ||
| try { | ||
| return probe(); | ||
| } finally { | ||
| delete (el as unknown as Record<"volume", unknown>).volume; | ||
| nativeSet.call(el, clampNativeMediaVolume(authored)); | ||
| } | ||
| } | ||
|
|
||
| export function audioFaderPositionToGain(position: number): number { | ||
| const safe = Math.max(AUDIO_GAIN_FADER_MIN, Math.min(AUDIO_GAIN_FADER_MAX, position)); | ||
| if (safe === AUDIO_GAIN_FADER_MIN) return 0; | ||
| const db = | ||
| safe < 0 | ||
| ? (safe / Math.abs(AUDIO_GAIN_FADER_MIN)) * Math.abs(MIN_AUDIO_GAIN_DB) | ||
| : (safe / AUDIO_GAIN_FADER_MAX) * MAX_AUDIO_GAIN_DB; | ||
| return 10 ** (db / 20); | ||
| } | ||
|
|
||
| export function audioGainToFaderPosition(gain: number): number { | ||
| const safe = clampAudioGain(gain); | ||
| if (safe === 0) return AUDIO_GAIN_FADER_MIN; | ||
| const db = 20 * Math.log10(safe); | ||
| const position = | ||
| db < 0 | ||
| ? (db / Math.abs(MIN_AUDIO_GAIN_DB)) * Math.abs(AUDIO_GAIN_FADER_MIN) | ||
| : (db / MAX_AUDIO_GAIN_DB) * AUDIO_GAIN_FADER_MAX; | ||
| return Math.max(AUDIO_GAIN_FADER_MIN, Math.min(AUDIO_GAIN_FADER_MAX, position)); | ||
| } | ||
|
|
||
| export function audioGainToText(gain: number): string { | ||
| const safe = clampAudioGain(gain); | ||
| if (safe === 0) return "-∞ dB"; | ||
| const db = 20 * Math.log10(safe); | ||
| const rounded = Math.abs(db) < 0.05 ? 0 : db; | ||
| return (rounded > 0 ? "+" : "") + rounded.toFixed(1) + " dB"; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing calls this at the current head. The volume write path is still
formatNumericValue->roundToCenti(packages/studio/src/components/editor/propertyPanelHelpers.ts:232-237), i.e. the two-decimal formatter this doc comment names as the thing that collapses the bottom of the fader onto0.So the six-decimal serializer ships correct and unreferenced, while the write it exists to fix still rounds to two. Presumably
u2-studio-gain-surfacewires it — worth confirming, because the test suite here reads as thoughdata-volumealready round-trips through it.