Improved normalizer with running ratio
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
DataHoarder 2022-03-08 13:37:27 +01:00
parent ace1c78c0a
commit 13b9682824
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
2 changed files with 38 additions and 4 deletions

View file

@ -86,6 +86,36 @@ func TestVorbisDecode(t *testing.T) {
doTest(".vorbis", t)
}
func TestReplayGainNormalization24(t *testing.T) {
t.Parallel()
fp, err := os.Open(TestSingleSample24)
if err != nil {
t.Error(err)
return
}
defer fp.Close()
source, err := flac.NewFormat().Open(fp)
if err != nil {
t.Error(err)
return
}
source = replaygain.NewNormalizationFilter(5).Process(source)
gain, peak, err := replaygain.GetTrackReplayGain(source)
if err != nil {
t.Error(err)
return
}
expect := "-0.181563 dB, 0.688681"
result := fmt.Sprintf("%f dB, %f", gain, peak)
if result != expect {
t.Errorf("Wrong ReplayGain %s != %s", result, expect)
}
}
func TestReplayGain24(t *testing.T) {
t.Parallel()

View file

@ -70,13 +70,17 @@ func (f NormalizationFilter) Process(source audio.Source) audio.Source {
}
volume := math.Pow(10, (gain)/20)
//prevent clipping
volume = math.Min(volume, 1/peak)
adjustment = float32(volume)
nsamples := source.SampleRate * source.Channels * f.delay
ratio := float32(math.Min(1, float64(len(block))/float64(nsamples)))
adjustment = adjustment*(1-ratio) + float32(volume)*ratio
if adjustment > float32(1/peak) {
adjustment = float32(1 / peak)
}
if len(sampleBuffer) > nsamples {
size := len(sampleBuffer) - nsamples
out := make([]float32, size)