Skip to content

Commit d15f5ad

Browse files
committed
validate Replay Gain at api
1 parent 8219ecc commit d15f5ad

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/player/audio.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export class AudioController {
2323
private statsListener : any = null
2424
private replayGainMode = ReplayGainMode.None
2525
private replayGain: ReplayGain | null = null
26-
private preAmp = 0.0
2726

2827
ontimeupdate: (value: number) => void = () => { /* do nothing */ }
2928
ondurationchange: (value: number) => void = () => { /* do nothing */ }
@@ -181,14 +180,9 @@ export class AudioController {
181180
}
182181

183182
private replayGainFactor(): number {
184-
if (this.replayGainMode === ReplayGainMode.None) {
183+
if (this.replayGainMode === ReplayGainMode.None || !this.replayGain) {
185184
return 1.0
186185
}
187-
if (!this.replayGain) {
188-
console.warn('AudioController: no ReplayGain information')
189-
return 1.0
190-
}
191-
192186
const gain = this.replayGainMode === ReplayGainMode.Track
193187
? this.replayGain.trackGain
194188
: this.replayGain.albumGain
@@ -204,7 +198,8 @@ export class AudioController {
204198

205199
// Implementing min(10^((RG + Gpre-amp)/20), 1/peakamplitude)
206200
// https://wiki.hydrogenaud.io/index.php?title=ReplayGain_2.0_specification
207-
const gainFactor = Math.pow(10, (gain + this.preAmp) / 20)
201+
const preAmp = 0.0
202+
const gainFactor = Math.pow(10, (gain + preAmp) / 20)
208203
const peakFactor = 1 / peak
209204
const factor = Math.min(gainFactor, peakFactor)
210205

src/shared/api.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ export interface Track {
2424
isPodcast?: boolean
2525
isUnavailable?: boolean
2626
playCount?: number
27-
replayGain?: {trackGain: number, trackPeak: number, albumGain: number, albumPeak: number}
27+
replayGain?: {
28+
trackGain: number
29+
trackPeak: number
30+
albumGain: number
31+
albumPeak: number
32+
}
2833
}
2934

3035
export interface Genre {
@@ -529,6 +534,14 @@ export class API {
529534
}
530535

531536
private normalizeTrack(item: any): Track {
537+
const replayGain =
538+
Number.isFinite(item.replayGain?.trackGain) &&
539+
Number.isFinite(item.replayGain?.albumGain) &&
540+
item.replayGain?.trackPeak > 0 &&
541+
item.replayGain?.albumPeak > 0
542+
? item.replayGain
543+
: null
544+
532545
return {
533546
id: item.id,
534547
title: item.title,
@@ -542,7 +555,7 @@ export class API {
542555
: [{ id: item.artistId, name: item.artist }],
543556
url: this.getStreamUrl(item.id),
544557
image: this.getCoverArtUrl(item),
545-
replayGain: item.replayGain,
558+
replayGain,
546559
}
547560
}
548561

0 commit comments

Comments
 (0)