Added normalization filter
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2022-03-08 12:29:24 +01:00
parent 4f8f274a09
commit 1189316ff5
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
2 changed files with 57 additions and 4 deletions

View file

@ -89,12 +89,12 @@ func (f RealTimeFilter) Process(source Source) Source {
}
type VolumeFilter struct {
adjustement float32
adjustment float32
}
func NewVolumeFilter(adjustement float32) VolumeFilter {
func NewVolumeFilter(adjustment float32) VolumeFilter {
return VolumeFilter{
adjustement: adjustement,
adjustment: adjustment,
}
}
@ -106,7 +106,7 @@ func (f VolumeFilter) Process(source Source) Source {
for block := range source.Blocks {
out := make([]float32, len(block))
for i := range block {
out[i] = block[i] * f.adjustement
out[i] = block[i] * f.adjustment
}
outBlocks <- out
}

View file

@ -2,6 +2,7 @@ package replaygain
import (
"git.gammaspectra.live/S.O.N.G/Kirika/audio"
libebur128 "git.gammaspectra.live/S.O.N.G/go-ebur128"
"math"
)
@ -13,3 +14,55 @@ func NewReplayGainFilter(gain, peak, preAmp float64) audio.VolumeFilter {
return audio.NewVolumeFilter(float32(volume))
}
// NormalizationFilter Normalizes running audio source
type NormalizationFilter struct {
}
func (f NormalizationFilter) Process(source audio.Source) audio.Source {
outBlocks := make(chan []float32)
go func() {
defer close(outBlocks)
state := libebur128.NewState(source.Channels, source.SampleRate, libebur128.LoudnessShortTerm|libebur128.SamplePeak)
if state == nil {
return
}
defer state.Close()
for block := range source.Blocks {
if state.AddFloat(block) != nil {
return
}
loudness, _ := state.GetLoudnessShortTerm()
peakSlice, _ := state.GetPreviousSamplePeak()
var peak float64
for _, p := range peakSlice {
if p > peak {
peak = p
}
}
volume := math.Pow(10, (referenceLevel-loudness)/20)
//prevent clipping
volume = math.Min(volume, 1/peak)
adjustment := float32(volume)
out := make([]float32, len(block))
for i := range block {
out[i] = block[i] * adjustment
}
outBlocks <- out
}
}()
return audio.Source{
Channels: source.Channels,
SampleRate: source.SampleRate,
Blocks: outBlocks,
}
}