Skip to content

Commit db0414a

Browse files
committed
feat: support normalization for FLAC tracks
1 parent a8daa32 commit db0414a

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

player/player.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"math"
78
"net/http"
89
"net/url"
910
"time"
@@ -512,6 +513,14 @@ func (p *Player) retrieveAudioKey(ctx context.Context, spotId librespot.SpotifyI
512513
return p.audioKey.Request(ctx, spotId.Id(), fileId)
513514
}
514515

516+
func calculateNormalisationFactor(params *audiofilespb.NormalizationParams, pregain float32) float32 {
517+
normalisationFactor := float32(math.Pow(10, float64((params.LoudnessDb+pregain)/20)))
518+
if normalisationFactor*params.TruePeakDb > 1 {
519+
normalisationFactor = 1 / params.TruePeakDb
520+
}
521+
return normalisationFactor
522+
}
523+
515524
func (p *Player) NewStream(ctx context.Context, client *http.Client, spotId librespot.SpotifyId, bitrate int, mediaPosition int64) (*Stream, error) {
516525
log := p.log.WithField("uri", spotId.Uri())
517526

@@ -520,6 +529,7 @@ func (p *Player) NewStream(ctx context.Context, client *http.Client, spotId libr
520529

521530
p.events.PreStreamLoadNew(playbackId, spotId, mediaPosition)
522531

532+
var normalisationFactor float32
523533
var media *librespot.Media
524534
var file *metadatapb.AudioFile
525535
if spotId.Type() == librespot.SpotifyIdTypeTrack {
@@ -549,6 +559,22 @@ func (p *Player) NewStream(ctx context.Context, client *http.Client, spotId libr
549559
if file == nil {
550560
return nil, librespot.ErrNoSupportedFormats
551561
}
562+
563+
if p.normalisationEnabled {
564+
if p.normalisationUseAlbumGain {
565+
normalisationFactor = calculateNormalisationFactor(
566+
audioFilesResp.DefaultAlbumNormalizationParams,
567+
p.normalisationPregain,
568+
)
569+
} else {
570+
normalisationFactor = calculateNormalisationFactor(
571+
audioFilesResp.DefaultFileNormalizationParams,
572+
p.normalisationPregain,
573+
)
574+
}
575+
} else {
576+
normalisationFactor = 1
577+
}
552578
} else if spotId.Type() == librespot.SpotifyIdTypeEpisode {
553579
var episodeMeta metadatapb.Episode
554580
err := p.sp.ExtendedMetadataSimple(ctx, spotId, extmetadatapb.ExtensionKind_EPISODE_V4, &episodeMeta)
@@ -565,6 +591,8 @@ func (p *Player) NewStream(ctx context.Context, client *http.Client, spotId libr
565591
if file == nil {
566592
return nil, librespot.ErrNoSupportedFormats
567593
}
594+
595+
normalisationFactor = 1
568596
} else {
569597
return nil, fmt.Errorf("unsupported spotify type: %s", spotId.Type())
570598
}
@@ -608,17 +636,6 @@ func (p *Player) NewStream(ctx context.Context, client *http.Client, spotId libr
608636
return nil, fmt.Errorf("failed reading metadata page: %w", err)
609637
}
610638

611-
var normalisationFactor float32
612-
if p.normalisationEnabled {
613-
if p.normalisationUseAlbumGain {
614-
normalisationFactor = meta.GetAlbumFactor(p.normalisationPregain)
615-
} else {
616-
normalisationFactor = meta.GetTrackFactor(p.normalisationPregain)
617-
}
618-
} else {
619-
normalisationFactor = 1
620-
}
621-
622639
vorbisStream, err := vorbis.New(log, audioStream, meta, normalisationFactor)
623640
if err != nil {
624641
return nil, fmt.Errorf("failed initializing ogg vorbis stream: %w", err)
@@ -632,11 +649,8 @@ func (p *Player) NewStream(ctx context.Context, client *http.Client, spotId libr
632649

633650
stream = vorbisStream
634651
} else if audioFormat == AudioFormatFLAC {
635-
// FIXME: implement normalisation for FLAC by looking at AudioFilesExtensionResponse
636-
const flacNormalisationFactor = 1
637-
638652
audioStream := io.NewSectionReader(decryptedStream, 0, rawStream.Size())
639-
flacStream, err := flac.New(log, audioStream, flacNormalisationFactor)
653+
flacStream, err := flac.New(log, audioStream, normalisationFactor)
640654
if err != nil {
641655
return nil, fmt.Errorf("failed initializing flac stream: %w", err)
642656
}

0 commit comments

Comments
 (0)