Moving smoothing into the audio level module. (#636)

This commit is contained in:
Raja Subramanian
2022-04-20 23:59:51 +05:30
committed by GitHub
parent 6a53891f9f
commit 43d0573693
11 changed files with 93 additions and 124 deletions
+45 -28
View File
@@ -7,38 +7,46 @@ import (
)
const (
// duration of audio frames for observe window
SilentAudioLevel = 127
silentAudioLevel = 127
negInv20 = -1.0 / 20
)
type AudioLevelParams struct {
ActiveLevel uint8
MinPercentile uint8
ObserveDuration uint32
SmoothIntervals uint32
}
// keeps track of audio level for a participant
type AudioLevel struct {
params AudioLevelParams
currentLevel *atomic.Uint32
// min duration to be considered active
// min duration within an observe duration window to be considered active
minActiveDuration uint32
smoothFactor float64
activeThreshold float64
// for Observe goroutine use
// keeps track of current activity
observeLevel uint8
activeDuration uint32 // ms
observedDuration uint32 // ms
smoothedLevel atomic.Float64
loudestObservedLevel uint8
activeDuration uint32 // ms
observedDuration uint32 // ms
}
func NewAudioLevel(params AudioLevelParams) *AudioLevel {
l := &AudioLevel{
params: params,
minActiveDuration: uint32(params.MinPercentile) * params.ObserveDuration / 100,
currentLevel: atomic.NewUint32(SilentAudioLevel),
observeLevel: SilentAudioLevel,
params: params,
minActiveDuration: uint32(params.MinPercentile) * params.ObserveDuration / 100,
smoothFactor: 1,
activeThreshold: ConvertAudioLevel(float64(params.ActiveLevel)),
loudestObservedLevel: silentAudioLevel,
}
if l.params.SmoothIntervals > 0 {
// exponential moving average (EMA), same center of mass with simple moving average (SMA)
l.smoothFactor = float64(2) / (float64(l.params.SmoothIntervals + 1))
}
return l
}
@@ -48,34 +56,43 @@ func (l *AudioLevel) Observe(level uint8, durationMs uint32) {
if level <= l.params.ActiveLevel {
l.activeDuration += durationMs
if l.observeLevel > level {
l.observeLevel = level
if l.loudestObservedLevel > level {
l.loudestObservedLevel = level
}
}
if l.observedDuration >= l.params.ObserveDuration {
// compute and reset
if l.activeDuration >= l.minActiveDuration {
level := uint32(l.observeLevel) - uint32(20*math.Log10(float64(l.activeDuration)/float64(l.params.ObserveDuration)))
l.currentLevel.Store(level)
// adjust loudest observed level by how much of the window was active.
// Weight will be 0 if active the entire duration
// > 0 if active for longer than observe duration
// < 0 if active for less than observe duration
activityWeight := 20 * math.Log10(float64(l.activeDuration)/float64(l.params.ObserveDuration))
adjustedLevel := float64(l.loudestObservedLevel) - activityWeight
linearLevel := ConvertAudioLevel(adjustedLevel)
// exponential smoothing to dampen transients
smoothedLevel := l.smoothedLevel.Load()
smoothedLevel += (linearLevel - smoothedLevel) * l.smoothFactor
l.smoothedLevel.Store(smoothedLevel)
} else {
l.currentLevel.Store(SilentAudioLevel)
l.smoothedLevel.Store(0)
}
l.observeLevel = SilentAudioLevel
l.loudestObservedLevel = silentAudioLevel
l.activeDuration = 0
l.observedDuration = 0
}
}
// returns current audio level, 0 (loudest) to 127 (silent)
func (l *AudioLevel) GetLevel() (uint8, bool) {
level := uint8(l.currentLevel.Load())
active := level != SilentAudioLevel
return level, active
// returns current soothed audio level
func (l *AudioLevel) GetLevel() (float64, bool) {
smoothedLevel := l.smoothedLevel.Load()
active := smoothedLevel >= l.activeThreshold
return smoothedLevel, active
}
// convert decibel back to linear
func ConvertAudioLevel(level uint8) float32 {
const negInv20 = -1.0 / 20
return float32(math.Pow(10, float64(level)*negInv20))
func ConvertAudioLevel(level float64) float64 {
return math.Pow(10, level*negInv20)
}
+2 -2
View File
@@ -53,8 +53,8 @@ func TestAudioLevel(t *testing.T) {
level, noisy := a.GetLevel()
require.True(t, noisy)
require.Less(t, level, uint8(defaultActiveLevel))
require.Greater(t, level, uint8(25))
require.Greater(t, level, ConvertAudioLevel(float64(defaultActiveLevel)))
require.Less(t, level, ConvertAudioLevel(float64(25)))
})
}
+2 -2
View File
@@ -628,12 +628,12 @@ func (b *Buffer) GetDeltaStats() *StreamStatsWithLayers {
}
}
func (b *Buffer) GetAudioLevel() (uint8, bool) {
func (b *Buffer) GetAudioLevel() (float64, bool) {
b.RLock()
defer b.RUnlock()
if b.audioLevel == nil {
return audio.SilentAudioLevel, false
return 0, false
}
return b.audioLevel.GetLevel()
+5 -4
View File
@@ -40,7 +40,7 @@ type TrackReceiver interface {
ReadRTP(buf []byte, layer uint8, sn uint16) (int, error)
GetBitrateTemporalCumulative() Bitrates
GetAudioLevel() (uint8, bool)
GetAudioLevel() (float64, bool)
SendPLI(layer int32)
@@ -275,6 +275,7 @@ func (w *WebRTCReceiver) AddUpTrack(track *webrtc.TrackRemote, buff *buffer.Buff
ActiveLevel: w.audioConfig.ActiveLevel,
MinPercentile: w.audioConfig.MinPercentile,
ObserveDuration: w.audioConfig.UpdateInterval,
SmoothIntervals: w.audioConfig.SmoothIntervals,
})
buff.OnFeedback(w.sendRTCP)
@@ -450,9 +451,9 @@ func (w *WebRTCReceiver) GetTrackStats() *livekit.RTPStats {
return buffer.AggregateRTPStats(stats)
}
func (w *WebRTCReceiver) GetAudioLevel() (uint8, bool) {
func (w *WebRTCReceiver) GetAudioLevel() (float64, bool) {
if w.Kind() == webrtc.RTPCodecTypeVideo {
return audio.SilentAudioLevel, false
return 0, false
}
w.bufferMu.RLock()
@@ -466,7 +467,7 @@ func (w *WebRTCReceiver) GetAudioLevel() (uint8, bool) {
return buff.GetAudioLevel()
}
return audio.SilentAudioLevel, false
return 0, false
}
func (w *WebRTCReceiver) getQualityParams() *buffer.ConnectionQualityParams {