Skip to content

Commit

Permalink
feat: support disabling normalisation
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Jan 15, 2024
1 parent 1ba7773 commit 1128433
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
28 changes: 15 additions & 13 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func (app *App) newAppPlayer(creds any) (_ *AppPlayer, err error) {

if appPlayer.player, err = player.NewPlayer(
appPlayer.sess.Spclient(), appPlayer.sess.AudioKey(),
*app.cfg.NormalisationPregain, appPlayer.countryCode, *app.cfg.AudioDevice,
!app.cfg.NormalisationDisabled, *app.cfg.NormalisationPregain,
appPlayer.countryCode, *app.cfg.AudioDevice,
*app.cfg.VolumeSteps, app.cfg.ExternalVolume,
); err != nil {
return nil, fmt.Errorf("failed initializing player: %w", err)
Expand Down Expand Up @@ -227,18 +228,19 @@ type Config struct {
ConfigPath string `yaml:"-"`
CredentialsPath string `yaml:"-"`

LogLevel *string `yaml:"log_level"`
DeviceId *string `yaml:"device_id"`
DeviceName *string `yaml:"device_name"`
DeviceType *string `yaml:"device_type"`
ClientToken *string `yaml:"client_token"`
ServerPort *int `yaml:"server_port"`
AudioDevice *string `yaml:"audio_device"`
Bitrate *int `yaml:"bitrate"`
VolumeSteps *uint32 `yaml:"volume_steps"`
NormalisationPregain *float32 `yaml:"normalisation_pregain"`
ExternalVolume bool `yaml:"external_volume"`
Credentials struct {
LogLevel *string `yaml:"log_level"`
DeviceId *string `yaml:"device_id"`
DeviceName *string `yaml:"device_name"`
DeviceType *string `yaml:"device_type"`
ClientToken *string `yaml:"client_token"`
ServerPort *int `yaml:"server_port"`
AudioDevice *string `yaml:"audio_device"`
Bitrate *int `yaml:"bitrate"`
VolumeSteps *uint32 `yaml:"volume_steps"`
NormalisationDisabled bool `yaml:"normalisation_disabled"`
NormalisationPregain *float32 `yaml:"normalisation_pregain"`
ExternalVolume bool `yaml:"external_volume"`
Credentials struct {
Type string `yaml:"type"`
UserPass struct {
Username string `yaml:"username"`
Expand Down
5 changes: 5 additions & 0 deletions config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
"min": 1,
"default": 100
},
"normalisation_disabled": {
"type": "boolean",
"description": "Whether track/album normalisation should be disabled",
"default": false
},
"normalisation_pregain": {
"type": "number",
"description": "The normalisation pregain to apply to track/album normalisation factors",
Expand Down
13 changes: 11 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,14 @@ 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))
var normalisationFactor float32
if p.normalisationEnabled {
normalisationFactor = meta.GetTrackFactor(p.normalisationPregain)
} else {
normalisationFactor = 1
}

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

0 comments on commit 1128433

Please sign in to comment.