audio/mp3: Bug fix: Accept non-io.Closer at Resampling

This commit is contained in:
Hajime Hoshi 2019-04-01 00:01:41 +09:00
parent 1ece2ee8f2
commit 2e6cb35274
2 changed files with 7 additions and 8 deletions

View File

@ -17,8 +17,6 @@ package convert
import ( import (
"io" "io"
"math" "math"
"github.com/hajimehoshi/ebiten/audio"
) )
var cosTable = [65536]float64{} var cosTable = [65536]float64{}
@ -67,7 +65,7 @@ func sinc01(x float64) float64 {
} }
type Resampling struct { type Resampling struct {
source audio.ReadSeekCloser source io.ReadSeeker
size int64 size int64
from int from int
to int to int
@ -78,7 +76,7 @@ type Resampling struct {
lruSrcBlocks []int64 lruSrcBlocks []int64
} }
func NewResampling(source audio.ReadSeekCloser, size int64, from, to int) *Resampling { func NewResampling(source io.ReadSeeker, size int64, from, to int) *Resampling {
r := &Resampling{ r := &Resampling{
source: source, source: source,
size: size, size: size,
@ -246,5 +244,8 @@ func (r *Resampling) Seek(offset int64, whence int) (int64, error) {
} }
func (r *Resampling) Close() error { func (r *Resampling) Close() error {
return r.source.Close() if closer, ok := r.source.(io.Closer); ok {
return closer.Close()
}
panic("audio/internal/convert: r.source must be io.Closer")
} }

View File

@ -82,17 +82,15 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
var toClose io.Closer = src
var r *convert.Resampling var r *convert.Resampling
if d.SampleRate() != context.SampleRate() { if d.SampleRate() != context.SampleRate() {
r = convert.NewResampling(d, d.Length(), d.SampleRate(), context.SampleRate()) r = convert.NewResampling(d, d.Length(), d.SampleRate(), context.SampleRate())
toClose = r
} }
s := &Stream{ s := &Stream{
orig: d, orig: d,
resampling: r, resampling: r,
toClose: toClose, toClose: src,
} }
runtime.SetFinalizer(s, (*Stream).Close) runtime.SetFinalizer(s, (*Stream).Close)
return s, nil return s, nil