Declare audio inactive if stale. (#2229)
* Declare audio inactive if stale. Stale samples were used to declare audio active. Maintain last update time and declare inactive if samples are stale. * correct comment * spelling * check level in test
This commit is contained in:
@@ -16,6 +16,7 @@ package audio
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -30,46 +31,79 @@ const (
|
||||
|
||||
func TestAudioLevel(t *testing.T) {
|
||||
t.Run("initially to return not noisy, within a few samples", func(t *testing.T) {
|
||||
clock := time.Now()
|
||||
a := createAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration)
|
||||
_, noisy := a.GetLevel()
|
||||
|
||||
_, noisy := a.GetLevel(clock)
|
||||
require.False(t, noisy)
|
||||
|
||||
observeSamples(a, 28, 5)
|
||||
_, noisy = a.GetLevel()
|
||||
observeSamples(a, 28, 5, clock)
|
||||
clock = clock.Add(5 * 20 * time.Millisecond)
|
||||
|
||||
_, noisy = a.GetLevel(clock)
|
||||
require.False(t, noisy)
|
||||
})
|
||||
|
||||
t.Run("not noisy when all samples are below threshold", func(t *testing.T) {
|
||||
clock := time.Now()
|
||||
a := createAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration)
|
||||
|
||||
observeSamples(a, 35, 100)
|
||||
_, noisy := a.GetLevel()
|
||||
observeSamples(a, 35, 100, clock)
|
||||
clock = clock.Add(100 * 20 * time.Millisecond)
|
||||
|
||||
_, noisy := a.GetLevel(clock)
|
||||
require.False(t, noisy)
|
||||
})
|
||||
|
||||
t.Run("not noisy when less than percentile samples are above threshold", func(t *testing.T) {
|
||||
clock := time.Now()
|
||||
a := createAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration)
|
||||
|
||||
observeSamples(a, 35, samplesPerBatch-2)
|
||||
observeSamples(a, 25, 1)
|
||||
observeSamples(a, 35, 1)
|
||||
observeSamples(a, 35, samplesPerBatch-2, clock)
|
||||
clock = clock.Add((samplesPerBatch - 2) * 20 * time.Millisecond)
|
||||
observeSamples(a, 25, 1, clock)
|
||||
clock = clock.Add(20 * time.Millisecond)
|
||||
observeSamples(a, 35, 1, clock)
|
||||
clock = clock.Add(20 * time.Millisecond)
|
||||
|
||||
_, noisy := a.GetLevel()
|
||||
_, noisy := a.GetLevel(clock)
|
||||
require.False(t, noisy)
|
||||
})
|
||||
|
||||
t.Run("noisy when higher than percentile samples are above threshold", func(t *testing.T) {
|
||||
clock := time.Now()
|
||||
a := createAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration)
|
||||
|
||||
observeSamples(a, 35, samplesPerBatch-16)
|
||||
observeSamples(a, 25, 8)
|
||||
observeSamples(a, 29, 8)
|
||||
observeSamples(a, 35, samplesPerBatch-16, clock)
|
||||
clock = clock.Add((samplesPerBatch - 16) * 20 * time.Millisecond)
|
||||
observeSamples(a, 25, 8, clock)
|
||||
clock = clock.Add(8 * 20 * time.Millisecond)
|
||||
observeSamples(a, 29, 8, clock)
|
||||
clock = clock.Add(8 * 20 * time.Millisecond)
|
||||
|
||||
level, noisy := a.GetLevel()
|
||||
level, noisy := a.GetLevel(clock)
|
||||
require.True(t, noisy)
|
||||
require.Greater(t, level, ConvertAudioLevel(float64(defaultActiveLevel)))
|
||||
require.Less(t, level, ConvertAudioLevel(float64(25)))
|
||||
})
|
||||
|
||||
t.Run("not noisy when samples are stale", func(t *testing.T) {
|
||||
clock := time.Now()
|
||||
a := createAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration)
|
||||
|
||||
observeSamples(a, 25, 100, clock)
|
||||
clock = clock.Add(100 * 20 * time.Millisecond)
|
||||
level, noisy := a.GetLevel(clock)
|
||||
require.True(t, noisy)
|
||||
require.Greater(t, level, ConvertAudioLevel(float64(defaultActiveLevel)))
|
||||
require.Less(t, level, ConvertAudioLevel(float64(20)))
|
||||
|
||||
// let enough time pass to make the samples stale
|
||||
clock = clock.Add(1500 * time.Millisecond)
|
||||
level, noisy = a.GetLevel(clock)
|
||||
require.Equal(t, float64(0.0), level)
|
||||
require.False(t, noisy)
|
||||
})
|
||||
}
|
||||
|
||||
func createAudioLevel(activeLevel uint8, minPercentile uint8, observeDuration uint32) *AudioLevel {
|
||||
@@ -80,8 +114,8 @@ func createAudioLevel(activeLevel uint8, minPercentile uint8, observeDuration ui
|
||||
})
|
||||
}
|
||||
|
||||
func observeSamples(a *AudioLevel, level uint8, count int) {
|
||||
func observeSamples(a *AudioLevel, level uint8, count int, baseTime time.Time) {
|
||||
for i := 0; i < count; i++ {
|
||||
a.Observe(level, 20)
|
||||
a.Observe(level, 20, baseTime.Add(+time.Duration(i*20)*time.Millisecond))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user