Skip to content

Commit

Permalink
validate Replay Gain at api
Browse files Browse the repository at this point in the history
  • Loading branch information
tamland committed Jan 1, 2025
1 parent 8219ecc commit d15f5ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
11 changes: 3 additions & 8 deletions src/player/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */ }
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
17 changes: 15 additions & 2 deletions src/shared/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
}

Expand Down

0 comments on commit d15f5ad

Please sign in to comment.