From 80f8bc6c62d8909d74a9955bbdaecec170a75a53 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Sat, 28 May 2022 22:45:21 +0530 Subject: [PATCH] Re-introduce quantization of audio level to dampen small changes (#732) --- pkg/rtc/room.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index 2aa416f9d..568dbecc0 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -2,6 +2,7 @@ package rtc import ( "context" + "math" "sort" "sync" "time" @@ -25,6 +26,7 @@ const ( DefaultEmptyTimeout = 5 * 60 // 5m DefaultRoomDepartureGrace = 20 AudioLevelQuantization = 8 // ideally power of 2 to minimize float decimal + invAudioLevelQuantization = 1.0 / AudioLevelQuantization subscriberUpdateInterval = 3 * time.Second ) @@ -157,6 +159,12 @@ func (r *Room) GetActiveSpeakers() []*livekit.SpeakerInfo { sort.Slice(speakers, func(i, j int) bool { return speakers[i].Level > speakers[j].Level }) + + // quantize to smooth out small changes + for _, speaker := range speakers { + speaker.Level = float32(math.Ceil(float64(speaker.Level*AudioLevelQuantization)) * invAudioLevelQuantization) + } + return speakers }