mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
Handle "Infinity" values for ReplayGain. Fix #2862
This commit is contained in:
parent
0b2cf30096
commit
9a051967f6
3 changed files with 36 additions and 8 deletions
|
@ -223,7 +223,8 @@ func (t Tags) BirthTime() time.Time {
|
|||
return time.Now()
|
||||
}
|
||||
|
||||
// Replaygain Properties
|
||||
// ReplayGain Properties
|
||||
|
||||
func (t Tags) RGAlbumGain() float64 { return t.getGainValue("replaygain_album_gain") }
|
||||
func (t Tags) RGAlbumPeak() float64 { return t.getPeakValue("replaygain_album_peak") }
|
||||
func (t Tags) RGTrackGain() float64 { return t.getGainValue("replaygain_track_gain") }
|
||||
|
@ -232,15 +233,12 @@ func (t Tags) RGTrackPeak() float64 { return t.getPeakValue("replaygain_track_pe
|
|||
func (t Tags) getGainValue(tagName string) float64 {
|
||||
// Gain is in the form [-]a.bb dB
|
||||
var tag = t.getFirstTagValue(tagName)
|
||||
|
||||
if tag == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
tag = strings.TrimSpace(strings.Replace(tag, "dB", "", 1))
|
||||
|
||||
var value, err = strconv.ParseFloat(tag, 64)
|
||||
if err != nil {
|
||||
if err != nil || value == math.Inf(-1) || value == math.Inf(1) {
|
||||
return 0
|
||||
}
|
||||
return value
|
||||
|
@ -249,8 +247,8 @@ func (t Tags) getGainValue(tagName string) float64 {
|
|||
func (t Tags) getPeakValue(tagName string) float64 {
|
||||
var tag = t.getFirstTagValue(tagName)
|
||||
var value, err = strconv.ParseFloat(tag, 64)
|
||||
if err != nil {
|
||||
// A default of 1 for peak value resulds in no changes
|
||||
if err != nil || value == math.Inf(-1) || value == math.Inf(1) {
|
||||
// A default of 1 for peak value results in no changes
|
||||
return 1
|
||||
}
|
||||
return value
|
||||
|
|
|
@ -101,4 +101,32 @@ var _ = Describe("Tags", func() {
|
|||
Expect(t.Bpm()).To(Equal(142))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("ReplayGain", func() {
|
||||
DescribeTable("getGainValue",
|
||||
func(tag string, expected float64) {
|
||||
md := &Tags{}
|
||||
md.Tags = map[string][]string{"replaygain_track_gain": {tag}}
|
||||
Expect(md.RGTrackGain()).To(Equal(expected))
|
||||
|
||||
},
|
||||
Entry("0", "0", 0.0),
|
||||
Entry("1.2dB", "1.2dB", 1.2),
|
||||
Entry("Infinity", "Infinity", 0.0),
|
||||
Entry("Invalid value", "INVALID VALUE", 0.0),
|
||||
)
|
||||
DescribeTable("getPeakValue",
|
||||
func(tag string, expected float64) {
|
||||
md := &Tags{}
|
||||
md.Tags = map[string][]string{"replaygain_track_peak": {tag}}
|
||||
Expect(md.RGTrackPeak()).To(Equal(expected))
|
||||
|
||||
},
|
||||
Entry("0", "0", 0.0),
|
||||
Entry("0.5", "0.5", 0.5),
|
||||
Entry("Invalid dB suffix", "0.7dB", 1.0),
|
||||
Entry("Infinity", "Infinity", 1.0),
|
||||
Entry("Invalid value", "INVALID VALUE", 1.0),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue