mirror of
https://github.com/faiface/beep.git
synced 2026-01-21 13:40:03 -05:00
62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
package beep_test
|
|
|
|
import (
|
|
"math"
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/faiface/beep"
|
|
)
|
|
|
|
func TestFormatEncodeDecode(t *testing.T) {
|
|
formats := make(chan beep.Format)
|
|
go func() {
|
|
defer close(formats)
|
|
for _, sampleRate := range []int{100, 2347, 44100, 48000} {
|
|
for _, numChannels := range []int{1, 2, 3, 4} {
|
|
for _, precision := range []int{1, 2, 3, 4, 5, 6} {
|
|
formats <- beep.Format{
|
|
SampleRate: sampleRate,
|
|
NumChannels: numChannels,
|
|
Precision: precision,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
for format := range formats {
|
|
for i := 0; i < 20; i++ {
|
|
deviation := 2.0 / (math.Pow(2, float64(format.Precision)*8) - 2)
|
|
sample := [2]float64{rand.Float64()*2 - 1, rand.Float64()*2 - 1}
|
|
|
|
tmp := make([]byte, format.Width())
|
|
format.EncodeSigned(tmp, sample)
|
|
decoded, _ := format.DecodeSigned(tmp)
|
|
|
|
if format.NumChannels == 1 {
|
|
if math.Abs((sample[0]+sample[1])/2-decoded[0]) > deviation || decoded[0] != decoded[1] {
|
|
t.Fatalf("signed decoded sample is too different: %v -> %v (deviation: %v)", sample, decoded, deviation)
|
|
}
|
|
} else {
|
|
if math.Abs(sample[0]-decoded[0]) > deviation || math.Abs(sample[1]-decoded[1]) > deviation {
|
|
t.Fatalf("signed decoded sample is too different: %v -> %v (deviation: %v)", sample, decoded, deviation)
|
|
}
|
|
}
|
|
|
|
format.EncodeUnsigned(tmp, sample)
|
|
decoded, _ = format.DecodeUnsigned(tmp)
|
|
|
|
if format.NumChannels == 1 {
|
|
if math.Abs((sample[0]+sample[1])/2-decoded[0]) > deviation || decoded[0] != decoded[1] {
|
|
t.Fatalf("unsigned decoded sample is too different: %v -> %v (deviation: %v)", sample, decoded, deviation)
|
|
}
|
|
} else {
|
|
if math.Abs(sample[0]-decoded[0]) > deviation || math.Abs(sample[1]-decoded[1]) > deviation {
|
|
t.Fatalf("unsigned decoded sample is too different: %v -> %v (deviation: %v)", sample, decoded, deviation)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|