From 4b567ddb438d454d5503ce440dbb900ba136364b Mon Sep 17 00:00:00 2001 From: minie4 Date: Sun, 25 Feb 2024 11:57:19 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Respect=20mute,=20volume=20and=20ba?= =?UTF-8?q?lance=20of=20routes=20and=20outputs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routing.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/routing.go b/routing.go index 183e6d2..5b86b16 100644 --- a/routing.go +++ b/routing.go @@ -113,7 +113,7 @@ func startRouting() { } type JackOutputBuffers struct { - UUID string + Output *Port Buffers [][]jack.AudioSample } @@ -123,6 +123,12 @@ func addToBuffer(src *[]jack.AudioSample, dst *[]jack.AudioSample, volume float3 } } +func multiplyBuffer(dst *[]jack.AudioSample, factor float32) { + for i, _ := range *dst { + (*dst)[i] *= jack.AudioSample(factor) + } +} + func calcBalanceFactor(ch uint8, balance float32) float32 { if ch == 0 { return max(0, min(1, 1-balance)) @@ -139,7 +145,7 @@ func processJackCb(nframes uint32) int { // Get memory addresses to write to for all outputs var outputBuffers []JackOutputBuffers = []JackOutputBuffers{} for _, out := range portConfig.Output { - outputBuffer := JackOutputBuffers{out.UUID, [][]jack.AudioSample{}} + outputBuffer := JackOutputBuffers{&out, [][]jack.AudioSample{}} for _, port := range out.object { buffer := port.GetBuffer(nframes) for i := range buffer { @@ -153,6 +159,7 @@ func processJackCb(nframes uint32) int { // Write audio data from every input to all outputs it routes to for _, in := range portConfig.Input { if in.State.Mute { + // Input is muted continue } @@ -166,8 +173,13 @@ func processJackCb(nframes uint32) int { // For every route that exists for the input for _, route := range in.Route { + if route.Mute { + // Route is muted + continue + } + for _, out := range outputBuffers { - if out.UUID != route.ToUUID { + if out.Output.UUID != route.ToUUID { continue } @@ -176,6 +188,11 @@ func processJackCb(nframes uint32) int { continue } + if out.Output.State.Mute { + // Output is muted + continue + } + routeVolume := in.State.Volume * route.Volume * calcBalanceFactor(ch, in.State.Balance) if in.Properties.Channels < uint8(len(out.Buffers)) { @@ -193,6 +210,14 @@ func processJackCb(nframes uint32) int { } } } + + // Apply output volume and balance + // (mute state is already checked above) + for _, out := range outputBuffers { + for ch, buf := range out.Buffers { + multiplyBuffer(&buf, out.Output.State.Volume*calcBalanceFactor(uint8(ch), out.Output.State.Balance)) + } + } } return 0 }