(t *testing.T)
| 31 | ) |
| 32 | |
| 33 | func TestAudioLevel(t *testing.T) { |
| 34 | t.Run("initially to return not noisy, within a few samples", func(t *testing.T) { |
| 35 | clock := time.Now() |
| 36 | a := createAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration) |
| 37 | |
| 38 | _, noisy := a.GetLevel(clock.UnixNano()) |
| 39 | require.False(t, noisy) |
| 40 | |
| 41 | observeSamples(a, 28, 5, clock) |
| 42 | clock = clock.Add(5 * 20 * time.Millisecond) |
| 43 | |
| 44 | _, noisy = a.GetLevel(clock.UnixNano()) |
| 45 | require.False(t, noisy) |
| 46 | }) |
| 47 | |
| 48 | t.Run("not noisy when all samples are below threshold", func(t *testing.T) { |
| 49 | clock := time.Now() |
| 50 | a := createAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration) |
| 51 | |
| 52 | observeSamples(a, 35, 100, clock) |
| 53 | clock = clock.Add(100 * 20 * time.Millisecond) |
| 54 | |
| 55 | _, noisy := a.GetLevel(clock.UnixNano()) |
| 56 | require.False(t, noisy) |
| 57 | }) |
| 58 | |
| 59 | t.Run("not noisy when less than percentile samples are above threshold", func(t *testing.T) { |
| 60 | clock := time.Now() |
| 61 | a := createAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration) |
| 62 | |
| 63 | observeSamples(a, 35, samplesPerBatch-2, clock) |
| 64 | clock = clock.Add((samplesPerBatch - 2) * 20 * time.Millisecond) |
| 65 | observeSamples(a, 25, 1, clock) |
| 66 | clock = clock.Add(20 * time.Millisecond) |
| 67 | observeSamples(a, 35, 1, clock) |
| 68 | clock = clock.Add(20 * time.Millisecond) |
| 69 | |
| 70 | _, noisy := a.GetLevel(clock.UnixNano()) |
| 71 | require.False(t, noisy) |
| 72 | }) |
| 73 | |
| 74 | t.Run("noisy when higher than percentile samples are above threshold", func(t *testing.T) { |
| 75 | clock := time.Now() |
| 76 | a := createAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration) |
| 77 | |
| 78 | observeSamples(a, 35, samplesPerBatch-16, clock) |
| 79 | clock = clock.Add((samplesPerBatch - 16) * 20 * time.Millisecond) |
| 80 | observeSamples(a, 25, 8, clock) |
| 81 | clock = clock.Add(8 * 20 * time.Millisecond) |
| 82 | observeSamples(a, 29, 8, clock) |
| 83 | clock = clock.Add(8 * 20 * time.Millisecond) |
| 84 | |
| 85 | level, noisy := a.GetLevel(clock.UnixNano()) |
| 86 | require.True(t, noisy) |
| 87 | require.Greater(t, level, ConvertAudioLevel(float64(defaultActiveLevel))) |
| 88 | require.Less(t, level, ConvertAudioLevel(float64(25))) |
| 89 | }) |
| 90 |
nothing calls this directly
no test coverage detected