This commit is contained in:
Junker der Provinz 2026-10-08 02:03:28 +08:00 • committed by GitHub
commit 53986cd7f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 0 deletions

View file

@ -11,6 +11,16 @@ const calculateReplayGain = (preAmp, gain, peak) => {
export const calculateGain = (gainInfo, song) => {
switch (gainInfo.gainMode) {
case 'album': {
// Fall back to track gain when the album gain is missing (singles, or
// tracks without album ReplayGain tags), matching common ReplayGain
// players instead of applying no adjustment at all.
if (song.rgAlbumGain === undefined || song.rgAlbumPeak === undefined) {
return calculateReplayGain(
gainInfo.preAmp,
song.rgTrackGain,
song.rgTrackPeak,
)
}
return calculateReplayGain(
gainInfo.preAmp,
song.rgAlbumGain,

View file

@ -0,0 +1,46 @@
import { describe, it, expect } from 'vitest'
import { calculateGain } from './calculateReplayGain'
describe('calculateGain', () => {
const preAmp = 0
const albumSong = {
rgAlbumGain: -6,
rgAlbumPeak: 1,
rgTrackGain: -3,
rgTrackPeak: 1,
}
const trackOnlySong = {
rgTrackGain: -3,
rgTrackPeak: 1,
}
const noGainSong = {}
it('uses album gain in album mode when it is present', () => {
const result = calculateGain({ gainMode: 'album', preAmp }, albumSong)
expect(result).toBeCloseTo(10 ** (-6 / 20))
})
it('falls back to track gain in album mode when the album gain is missing', () => {
const result = calculateGain({ gainMode: 'album', preAmp }, trackOnlySong)
// Without the fallback this returned 1 (no adjustment). It should now use
// the track gain instead.
expect(result).toBeCloseTo(10 ** (-3 / 20))
expect(result).not.toBe(1)
})
it('returns 1 in album mode when neither album nor track gain is present', () => {
const result = calculateGain({ gainMode: 'album', preAmp }, noGainSong)
expect(result).toBe(1)
})
it('uses track gain in track mode', () => {
const result = calculateGain({ gainMode: 'track', preAmp }, albumSong)
expect(result).toBeCloseTo(10 ** (-3 / 20))
})
it('returns 1 when gain is disabled', () => {
const result = calculateGain({ gainMode: 'none', preAmp }, albumSong)
expect(result).toBe(1)
})
})