-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix halved sample value for 16/24bit wav audio #161
Conversation
Thanks! I'll review this after I'm back from holliday. |
The pipeline fails on Do you happen to have time to figure out the encoding as well? It's a bit more complexly written. Testcase: func TestEncodeDecodeRoundTrip(t *testing.T) {
numChannelsS := []int{1, 2}
precisions := []int{1, 2, 3}
for _, numChannels := range numChannelsS {
for _, precision := range precisions {
name := fmt.Sprintf("%d_channel(s)_%d_precision", numChannels, precision)
t.Run(name, func(t *testing.T) {
var s beep.Streamer
s, data := testtools.RandomDataStreamer(100)
if numChannels == 1 {
s = effects.Mono(s)
for i := range data {
mix := (data[i][0] + data[i][1]) / 2
data[i][0] = mix
data[i][1] = mix
}
}
var w writerseeker.WriterSeeker
format := beep.Format{SampleRate: 44100, NumChannels: numChannels, Precision: precision}
err := Encode(&w, s, format)
assert.NoError(t, err)
s, decodedFormat, err := Decode(w.Reader())
assert.NoError(t, err)
assert.Equal(t, format, decodedFormat)
assert.Equal(t, data, testtools.Collect(s))
})
}
}
} |
I think it may not be an appropriate test case. |
Ok, does this seem fairer? func TestEncodeDecodeRoundTrip(t *testing.T) {
numChannelsS := []int{1, 2}
precisions := []int{1, 2, 3}
for _, numChannels := range numChannelsS {
for _, precision := range precisions {
name := fmt.Sprintf("%d_channel(s)_%d_precision", numChannels, precision)
t.Run(name, func(t *testing.T) {
var s beep.Streamer
s, data := testtools.RandomDataStreamer(1000)
if numChannels == 1 {
s = effects.Mono(s)
for i := range data {
mix := (data[i][0] + data[i][1]) / 2
data[i][0] = mix
data[i][1] = mix
}
}
var w writerseeker.WriterSeeker
format := beep.Format{SampleRate: 44100, NumChannels: numChannels, Precision: precision}
err := Encode(&w, s, format)
assert.NoError(t, err)
s, decodedFormat, err := Decode(w.Reader())
assert.NoError(t, err)
assert.Equal(t, format, decodedFormat)
actual := testtools.Collect(s)
assert.Len(t, actual, 1000)
// Delta is determined as follows:
// The float values range from -1 to 1, which difference is 2.0.
// For each byte of precision, there are 8 bits -> 2^(precision*8) different possible values.
// So, fitting 2^(precision*8) values into a range of 2.0, each "step" must not
// be bigger than 2.0 / math.Pow(2, float64(precision*8)).
delta := 2.0 / math.Pow(2, float64(precision*8))
for i := range actual {
if actual[i][0] < data[i][0]-delta || actual[i][0] > data[i][0]+delta {
t.Fatalf("encoded & decoded sample doesn't match orginal. expected: %v, actual: %v", data[i][0], actual[i][0])
}
if actual[i][1] < data[i][1]-delta || actual[i][1] > data[i][1]+delta {
t.Fatalf("encoded & decoded sample doesn't match orginal. expected: %v, actual: %v", data[i][1], actual[i][1])
}
}
})
}
}
} |
I updated the code. Now it should pass the test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay, life got in the way & I want to make sure we have the encoding/decoding right this time.
I updated the code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're a star!
The original code is mapping sample value of 16/24bit wav audio to [-0.5xxx,0.5].
This commit changes the range to [-1,1).