From 2184e703ba3c3fd79e792a78a70e76daa2aa1062 Mon Sep 17 00:00:00 2001 From: xegineering Date: Sun, 15 Dec 2024 12:01:53 +0100 Subject: pipewire: Fix silence dropping It used to drop silence per channel, not silence per sample. In the unlikely case of perfect silence on one channel and sound on the other this would switch effectively the channels. --- soundbox/pipewire.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/soundbox/pipewire.go b/soundbox/pipewire.go index 476d2e4..bb672f8 100644 --- a/soundbox/pipewire.go +++ b/soundbox/pipewire.go @@ -54,12 +54,20 @@ func (pwc pwCapture) Read(p []byte) (int, error) { func s16leDropSilence(input []byte) []byte { output := make([]byte, 0) - for i := 0; i < (len(input) - 1); i += 2 { - if input[i] == byte(0) && input[i+1] == byte(0) { + cut := len(input) % 4 + length := len(input) - cut // s16 raw audio is 4 bytes per sample + + for i := 0; i < length; i += 4 { + if input[i+0] == byte(0) && + input[i+1] == byte(0) && + input[i+2] == byte(0) && + input[i+3] == byte(0) { continue } - output = append(output, input[i]) + output = append(output, input[i+0]) output = append(output, input[i+1]) + output = append(output, input[i+2]) + output = append(output, input[i+3]) } return output -- cgit v1.2.3-70-g09d2