Added mixer back to client

live
noah metz 2024-04-16 15:36:32 -06:00
parent af8ea403cd
commit caeed001c3
1 changed files with 24 additions and 8 deletions

@ -16,7 +16,8 @@ var decoders = map[pnyx.PeerID]chan[]byte{}
var encoder *opus.Encoder var encoder *opus.Encoder
var sample_rate int = 0 var sample_rate int = 0
var mic = make(chan []byte, 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 { func set_sample_rate(new_sample_rate int) error {
fmt.Printf("Setting sample rate to %d\n", new_sample_rate) 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 { if err != nil {
panic(err) panic(err)
} }
select { audio_data <- pcm
case speaker <- pcm:
default:
}
case data := <-decode_chan: case data := <-decode_chan:
missed = 0 missed = 0
@ -76,21 +74,39 @@ func handle_peer_decode(peer_id pnyx.PeerID, decode_chan chan[]byte, sample_rate
if err != nil { if err != nil {
panic(err) panic(err)
} }
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 { select {
case speaker <- pcm[:written]: case new_samples := <- data_chan:
default: for i, sample := range(new_samples) {
samples[i] += sample
} }
case speaker_chan <- samples:
samples = nil
} }
} }
} }
} }
func main() { func main() {
ctx, err := malgo.InitContext(nil, malgo.ContextConfig{}, nil) ctx, err := malgo.InitContext(nil, malgo.ContextConfig{}, nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }
go mixer(audio_data, speaker)
defer ctx.Free() defer ctx.Free()
defer ctx.Uninit() defer ctx.Uninit()