mirror of
https://github.com/livekit/livekit.git
synced 2026-03-31 17:35:39 +00:00
* refactor: active speakers 1. Observe the loudest adjusted with active ratio instead of linear average of decibel values 2. Follow RFC6465 to convert audio level from decibel to linear value. 3. Quantize audio level for stable slice comparison 4. Switch moving average algorithm from MMA to EMA to have the same center of mass with SMA 5. Minor: remove seenSids map allocation 6. Minor: minimize division arithmetic * Update pkg/rtc/audiolevel.go Co-authored-by: David Zhao <david@davidzhao.com>
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package rtc_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/livekit/livekit-server/pkg/rtc"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
samplesPerBatch = 25
|
|
defaultActiveLevel = 30
|
|
// requires two noisy samples to count
|
|
defaultPercentile = 10
|
|
)
|
|
|
|
func TestAudioLevel(t *testing.T) {
|
|
t.Run("initially to return not noisy, within a few samples", func(t *testing.T) {
|
|
a := rtc.NewAudioLevel(defaultActiveLevel, defaultPercentile)
|
|
_, noisy := a.GetLevel()
|
|
require.False(t, noisy)
|
|
|
|
observeSamples(a, 28, 5)
|
|
_, noisy = a.GetLevel()
|
|
require.False(t, noisy)
|
|
})
|
|
|
|
t.Run("not noisy when all samples are below threshold", func(t *testing.T) {
|
|
a := rtc.NewAudioLevel(defaultActiveLevel, defaultPercentile)
|
|
|
|
observeSamples(a, 35, 100)
|
|
_, noisy := a.GetLevel()
|
|
require.False(t, noisy)
|
|
})
|
|
|
|
t.Run("not noisy when less than percentile samples are above threshold", func(t *testing.T) {
|
|
a := rtc.NewAudioLevel(defaultActiveLevel, defaultPercentile)
|
|
|
|
observeSamples(a, 35, samplesPerBatch-2)
|
|
observeSamples(a, 25, 1)
|
|
observeSamples(a, 35, 1)
|
|
|
|
_, noisy := a.GetLevel()
|
|
require.False(t, noisy)
|
|
})
|
|
|
|
t.Run("noisy when higher than percentile samples are above threshold", func(t *testing.T) {
|
|
a := rtc.NewAudioLevel(defaultActiveLevel, defaultPercentile)
|
|
|
|
observeSamples(a, 35, samplesPerBatch-16)
|
|
observeSamples(a, 25, 8)
|
|
observeSamples(a, 29, 8)
|
|
|
|
level, noisy := a.GetLevel()
|
|
require.True(t, noisy)
|
|
require.Less(t, level, uint8(defaultActiveLevel))
|
|
require.Greater(t, level, uint8(25))
|
|
})
|
|
}
|
|
|
|
func observeSamples(a *rtc.AudioLevel, level uint8, count int) {
|
|
for i := 0; i < count; i++ {
|
|
a.Observe(level)
|
|
}
|
|
}
|