summaryrefslogtreecommitdiff
path: root/soundbox/pipewire.go
diff options
context:
space:
mode:
authorxegineering <me@xegineering.eu>2024-12-15 12:01:53 +0100
committerxegineering <me@xegineering.eu>2024-12-15 12:36:59 +0100
commit2184e703ba3c3fd79e792a78a70e76daa2aa1062 (patch)
tree78bcc6ccc9eb380f8d159a1951bc3011c8723327 /soundbox/pipewire.go
parentca5ebc8a795114a72f4410f05b2270f24ead1d60 (diff)
downloadsoundbox-go-2184e703ba3c3fd79e792a78a70e76daa2aa1062.tar
soundbox-go-2184e703ba3c3fd79e792a78a70e76daa2aa1062.tar.zst
soundbox-go-2184e703ba3c3fd79e792a78a70e76daa2aa1062.zip
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.
Diffstat (limited to 'soundbox/pipewire.go')
-rw-r--r--soundbox/pipewire.go14
1 files 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