Skip to content

Commit

Permalink
feat: option to disable normalization + change default pregain to 0
Browse files Browse the repository at this point in the history
Resolves #17
  • Loading branch information
phts committed Jan 14, 2024
1 parent 9226eef commit d408168
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
5 changes: 4 additions & 1 deletion audio/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ func ExtractMetadataPage(r io.ReaderAt, limit int64) (librespot.SizedReadAtSeeke
return io.NewSectionReader(r, int64(syncState.Returned), limit-int64(syncState.Returned)), &metadata, nil
}

func (m MetadataPage) GetTrackFactor(normalisationPregain float32) float32 {
func (m MetadataPage) GetTrackFactor(normalisationEnabled bool, normalisationPregain float32) float32 {
if !normalisationEnabled {
return 0
}
normalisationFactor := float32(math.Pow(10, float64((m.trackGainDb+normalisationPregain)/20)))
if normalisationFactor*m.trackPeak > 1 {
log.Warn("reducing track normalisation factor to prevent clipping, please add negative pregain to avoid")
Expand Down
5 changes: 3 additions & 2 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (app *App) newAppPlayer(creds any) (_ *AppPlayer, err error) {
appPlayer.initState()

if appPlayer.player, err = player.NewPlayer(
appPlayer.sess.Spclient(), appPlayer.sess.AudioKey(),
appPlayer.sess.Spclient(), appPlayer.sess.AudioKey(), app.cfg.NormalisationEnabled,
*app.cfg.NormalisationPregain, appPlayer.countryCode, *app.cfg.AudioDevice,
*app.cfg.VolumeSteps, app.cfg.ExternalVolume,
); err != nil {
Expand Down Expand Up @@ -236,6 +236,7 @@ type Config struct {
AudioDevice *string `yaml:"audio_device"`
Bitrate *int `yaml:"bitrate"`
VolumeSteps *uint32 `yaml:"volume_steps"`
NormalisationEnabled bool `yaml:"normalisation_enabled"`
NormalisationPregain *float32 `yaml:"normalisation_pregain"`
ExternalVolume bool `yaml:"external_volume"`
Credentials struct {
Expand Down Expand Up @@ -291,7 +292,7 @@ func loadConfig(cfg *Config) error {
}
if cfg.NormalisationPregain == nil {
cfg.NormalisationPregain = new(float32)
*cfg.NormalisationPregain = 1
*cfg.NormalisationPregain = 0
}

return nil
Expand Down
7 changes: 6 additions & 1 deletion config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,15 @@
"min": 1,
"default": 100
},
"normalisation_enabled": {
"type": "boolean",
"description": "Whether track/album normalisation is enabled",
"default": false
},
"normalisation_pregain": {
"type": "number",
"description": "The normalisation pregain to apply to track/album normalisation factors",
"default": 1
"default": 0
},
"external_volume": {
"type": "boolean",
Expand Down
6 changes: 4 additions & 2 deletions player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Channels = 2
const MaxStateVolume = 65535

type Player struct {
normalisationEnabled bool
normalisationPregain float32
countryCode *string

Expand Down Expand Up @@ -61,10 +62,11 @@ type playerCmdDataSet struct {
paused bool
}

func NewPlayer(sp *spclient.Spclient, audioKey *audio.KeyProvider, normalisationPregain float32, countryCode *string, device string, volumeSteps uint32, externalVolume bool) (*Player, error) {
func NewPlayer(sp *spclient.Spclient, audioKey *audio.KeyProvider, normalisationEnabled bool, normalisationPregain float32, countryCode *string, device string, volumeSteps uint32, externalVolume bool) (*Player, error) {
p := &Player{
sp: sp,
audioKey: audioKey,
normalisationEnabled: normalisationEnabled,
normalisationPregain: normalisationPregain,
countryCode: countryCode,
newOutput: func(reader librespot.Float32Reader, paused bool, volume float32) (*output.Output, error) {
Expand Down Expand Up @@ -324,7 +326,7 @@ func (p *Player) NewStream(spotId librespot.SpotifyId, bitrate int, mediaPositio
return nil, fmt.Errorf("failed reading metadata page: %w", err)
}

stream, err := vorbis.New(audioStream, meta, meta.GetTrackFactor(p.normalisationPregain))
stream, err := vorbis.New(audioStream, meta, meta.GetTrackFactor(p.normalisationEnabled, p.normalisationPregain))
if err != nil {
return nil, fmt.Errorf("failed initializing ogg vorbis stream: %w", err)
}
Expand Down

0 comments on commit d408168

Please sign in to comment.