Files
ngzz-mc/internal/audio/audio_test.go

51 lines
1.2 KiB
Go
Raw Normal View History

package audio
import "testing"
// TestAttenuation 距离衰减公式(音频.md §3)。
func TestAttenuation(t *testing.T) {
if a := attenuation(0); a != 1 {
t.Fatalf("零距离衰减期望 1,实际 %v", a)
}
if a := attenuation(10); a != 0.5 {
t.Fatalf("10 格衰减期望 0.5,实际 %v", a)
}
if a := attenuation(20); a != 0 {
t.Fatalf("最大距离衰减期望 0,实际 %v", a)
}
if a := attenuation(30); a != 0 {
t.Fatalf("超距衰减期望 0,实际 %v", a)
}
}
// TestBurstStreamer 合成流:有限长度与数据填充。
func TestBurstStreamer(t *testing.T) {
b := sineBurst(440, 0.05)
samples := make([][2]float64, 1024)
total := 0
for {
n, ok := b.Stream(samples)
total += n
if ok {
break
}
}
if total != b.Len() {
t.Fatalf("流长度期望 %d,实际 %d", b.Len(), total)
}
if total < 1000 {
t.Fatalf("采样数异常: %d", total)
}
}
// TestManagerSilent 无音频设备环境(CI/服务器)下降级为静默、不崩溃。
func TestManagerSilent(t *testing.T) {
m := New() // 可能无设备:必须不 panic
defer m.Close()
m.SetVolumes(1, 1, 1, 1)
m.SetListener(0, 0, 0)
for i := 0; i < 100; i++ {
m.Play("block.stone.break", float64(i), 0, 0) // 池满淘汰也不崩
}
}