From caeed001c3bc726c6b0e6345db793282b914103c Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 16 Apr 2024 15:36:32 -0600 Subject: [PATCH] Added mixer back to client --- cmd/client/main.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 6c51772..56fbfe4 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -16,7 +16,8 @@ var decoders = map[pnyx.PeerID]chan[]byte{} var encoder *opus.Encoder var sample_rate int = 0 var mic = make(chan []byte, 0) -var speaker = make(chan []int16, 1) +var speaker = make(chan []int16, 0) +var audio_data = make(chan []int16, 0) func set_sample_rate(new_sample_rate int) error { fmt.Printf("Setting sample rate to %d\n", new_sample_rate) @@ -61,10 +62,7 @@ func handle_peer_decode(peer_id pnyx.PeerID, decode_chan chan[]byte, sample_rate if err != nil { panic(err) } - select { - case speaker <- pcm: - default: - } + audio_data <- pcm case data := <-decode_chan: missed = 0 @@ -76,21 +74,39 @@ func handle_peer_decode(peer_id pnyx.PeerID, decode_chan chan[]byte, sample_rate if err != nil { panic(err) } - select { - case speaker <- pcm[:written]: - default: + audio_data <- pcm[:written] + } + } + } +} + +func mixer(data_chan chan []int16, speaker_chan chan []int16) { + var samples []int16 = nil + for true { + if samples == nil { + samples = <- data_chan + } else { + select { + case new_samples := <- data_chan: + for i, sample := range(new_samples) { + samples[i] += sample } + case speaker_chan <- samples: + samples = nil } } } } + func main() { ctx, err := malgo.InitContext(nil, malgo.ContextConfig{}, nil) if err != nil { panic(err) } + go mixer(audio_data, speaker) + defer ctx.Free() defer ctx.Uninit()