From d15f5ad3dc7c11f379a2b784ff23a8bf188e0dd2 Mon Sep 17 00:00:00 2001 From: Thomas Amland Date: Wed, 1 Jan 2025 12:06:45 +0100 Subject: [PATCH] validate Replay Gain at api --- src/player/audio.ts | 11 +++-------- src/shared/api.ts | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/player/audio.ts b/src/player/audio.ts index 6d15ae8..ce570c1 100644 --- a/src/player/audio.ts +++ b/src/player/audio.ts @@ -23,7 +23,6 @@ export class AudioController { private statsListener : any = null private replayGainMode = ReplayGainMode.None private replayGain: ReplayGain | null = null - private preAmp = 0.0 ontimeupdate: (value: number) => void = () => { /* do nothing */ } ondurationchange: (value: number) => void = () => { /* do nothing */ } @@ -181,14 +180,9 @@ export class AudioController { } private replayGainFactor(): number { - if (this.replayGainMode === ReplayGainMode.None) { + if (this.replayGainMode === ReplayGainMode.None || !this.replayGain) { return 1.0 } - if (!this.replayGain) { - console.warn('AudioController: no ReplayGain information') - return 1.0 - } - const gain = this.replayGainMode === ReplayGainMode.Track ? this.replayGain.trackGain : this.replayGain.albumGain @@ -204,7 +198,8 @@ export class AudioController { // Implementing min(10^((RG + Gpre-amp)/20), 1/peakamplitude) // https://wiki.hydrogenaud.io/index.php?title=ReplayGain_2.0_specification - const gainFactor = Math.pow(10, (gain + this.preAmp) / 20) + const preAmp = 0.0 + const gainFactor = Math.pow(10, (gain + preAmp) / 20) const peakFactor = 1 / peak const factor = Math.min(gainFactor, peakFactor) diff --git a/src/shared/api.ts b/src/shared/api.ts index 35c3b86..85852f2 100644 --- a/src/shared/api.ts +++ b/src/shared/api.ts @@ -24,7 +24,12 @@ export interface Track { isPodcast?: boolean isUnavailable?: boolean playCount?: number - replayGain?: {trackGain: number, trackPeak: number, albumGain: number, albumPeak: number} + replayGain?: { + trackGain: number + trackPeak: number + albumGain: number + albumPeak: number + } } export interface Genre { @@ -529,6 +534,14 @@ export class API { } private normalizeTrack(item: any): Track { + const replayGain = + Number.isFinite(item.replayGain?.trackGain) && + Number.isFinite(item.replayGain?.albumGain) && + item.replayGain?.trackPeak > 0 && + item.replayGain?.albumPeak > 0 + ? item.replayGain + : null + return { id: item.id, title: item.title, @@ -542,7 +555,7 @@ export class API { : [{ id: item.artistId, name: item.artist }], url: this.getStreamUrl(item.id), image: this.getCoverArtUrl(item), - replayGain: item.replayGain, + replayGain, } }