Handle "Infinity" values for ReplayGain. Fix #2862

This commit is contained in:
Deluan 2024-02-16 18:44:58 -05:00
parent 0b2cf30096
commit 9a051967f6
3 changed files with 36 additions and 8 deletions

View File

@ -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

View File

@ -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),
)
})
})

View File

@ -5,6 +5,7 @@ import (
"fmt"
"time"
"github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
@ -36,9 +37,10 @@ func initialSetup(ds model.DataStore) {
})
}
// If the Dev Admin user is not present, create it
func createInitialAdminUser(ds model.DataStore, initialPassword string) error {
users := ds.User(context.TODO())
c, err := users.CountAll()
c, err := users.CountAll(model.QueryOptions{Filters: squirrel.Eq{"user_name": consts.DevInitialUserName}})
if err != nil {
panic(fmt.Sprintf("Could not access User table: %s", err))
}