From 5d99993183886f66d4c2685e773d071ce0fc9365 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 12 Jul 2017 00:51:36 +0900 Subject: [PATCH] audio: Bug fix: stream size must be multiple of 4 (#384) --- audio/internal/convert/resampling.go | 3 ++- audio/vorbis/vorbis.go | 5 +++-- audio/wav/decode.go | 5 +++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/audio/internal/convert/resampling.go b/audio/internal/convert/resampling.go index 4d9b510d7..0c4da261d 100644 --- a/audio/internal/convert/resampling.go +++ b/audio/internal/convert/resampling.go @@ -52,7 +52,8 @@ func NewResampling(source audio.ReadSeekCloser, size int64, from, to int) *Resam } func (r *Resampling) Size() int64 { - return int64(float64(r.size) * float64(r.to) / float64(r.from)) + s := int64(float64(r.size) * float64(r.to) / float64(r.from)) + return s / 4 * 4 } func (r *Resampling) src(i int) (float64, float64, error) { diff --git a/audio/vorbis/vorbis.go b/audio/vorbis/vorbis.go index 1c0c7195e..87be74427 100644 --- a/audio/vorbis/vorbis.go +++ b/audio/vorbis/vorbis.go @@ -188,8 +188,9 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) { size *= 2 } if sampleRate != context.SampleRate() { - s = convert.NewResampling(s, size, sampleRate, context.SampleRate()) - size = size * int64(context.SampleRate()) / int64(sampleRate) + r := convert.NewResampling(s, size, sampleRate, context.SampleRate()) + s = r + size = r.Size() } return &Stream{decoded: s, size: size}, nil } diff --git a/audio/wav/decode.go b/audio/wav/decode.go index f32e1fc01..e88ecca21 100644 --- a/audio/wav/decode.go +++ b/audio/wav/decode.go @@ -219,8 +219,9 @@ chunks: } } if sampleRateFrom != sampleRateTo { - s = convert.NewResampling(s, dataSize, sampleRateFrom, sampleRateTo) - dataSize = dataSize * int64(sampleRateTo) / int64(sampleRateFrom) + r := convert.NewResampling(s, dataSize, sampleRateFrom, sampleRateTo) + s = r + dataSize = r.Size() } return &Stream{inner: s, size: dataSize}, nil }