Added normalization filter
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
4f8f274a09
commit
1189316ff5
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue