Verify local Mumble audio frequency round trip

This commit is contained in:
Brandon McGinty (chatgpt)
2026-08-09 20:24:55 -04:00
committed by Brandon McGinty
parent 794610929b
commit 074bd67ad3
+34 -9
View File
@@ -58,10 +58,11 @@ func TestLocalMumbleAudioRoundTrip(t *testing.T) {
}
time.Sleep(500 * time.Millisecond)
frame := make([]int16, sendConfig.AudioFrameSize())
for i := range frame {
frame[i] = int16(12000 * math.Sin(2*math.Pi*440*float64(i)/gumble.AudioSampleRate))
}
for i := 0; i < 8; i++ {
for sample := range frame {
index := i*len(frame) + sample
frame[sample] = int16(12000 * math.Sin(2*math.Pi*440*float64(index)/gumble.AudioSampleRate))
}
raw, err := sender.AudioEncoder.Encode(frame, len(frame), sendConfig.AudioDataBytes)
if err != nil {
t.Fatal(err)
@@ -70,21 +71,45 @@ func TestLocalMumbleAudioRoundTrip(t *testing.T) {
t.Fatal(err)
}
}
// Discard the first few decoder warm-up frames before measuring pitch.
var packet *gumble.AudioPacket
for i := 0; i < 3; i++ {
select {
case packet = <-listener.packets:
case <-time.After(8 * time.Second):
t.Fatal("timed out waiting for relayed audio")
}
}
select {
case packet := <-listener.packets:
case packet = <-listener.packets:
peak := 0
for _, sample := range packet.AudioBuffer {
v := int(sample)
crossings := 0
previous := 0
// The decoder returns interleaved stereo; inspect one channel. A rising
// zero-crossing estimate verifies the 440 Hz source survived Opus rather
// than merely proving that arbitrary non-silent audio was relayed.
for i := 0; i < len(packet.AudioBuffer); i += gumble.AudioChannels {
v := int(packet.AudioBuffer[i])
if v < 0 {
v = -v
}
if v > peak {
if -v > peak {
peak = -v
}
} else if v > peak {
peak = v
}
if previous <= 0 && v > 0 {
crossings++
}
previous = v
}
if peak < 500 {
t.Fatalf("received silent audio peak=%d", peak)
}
samples := len(packet.AudioBuffer) / gumble.AudioChannels
frequency := float64(crossings*gumble.AudioSampleRate) / float64(samples)
if math.Abs(frequency-440) > 120 {
t.Fatalf("received frequency %.1f Hz, want generated 440 Hz", frequency)
}
case <-time.After(8 * time.Second):
t.Fatal("timed out waiting for relayed audio")
}