audio/mp3: Bug fix: decoding a mono MP3 panics on browsers (#421)

This commit is contained in:
Hajime Hoshi 2017-08-28 01:31:29 +09:00
parent 6c10251a45
commit 8725589619

View File

@ -18,6 +18,7 @@ package mp3
import ( import (
"errors" "errors"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -98,22 +99,31 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
return nil, err return nil, err
} }
s := &Stream{} s := &Stream{}
ch := make(chan struct{}) ch := make(chan error)
klass := js.Global.Get("OfflineAudioContext") klass := js.Global.Get("OfflineAudioContext")
if klass == js.Undefined { if klass == js.Undefined {
klass = js.Global.Get("webkitOfflineAudioContext") klass = js.Global.Get("webkitOfflineAudioContext")
} }
if klass == js.Undefined { if klass == js.Undefined {
return nil, errors.New("vorbis: OfflineAudioContext is not available") return nil, errors.New("audio/mp3: OfflineAudioContext is not available")
} }
// TODO: 1 is a correct second argument? // TODO: 1 is a correct second argument?
oc := klass.New(2, 1, context.SampleRate()) oc := klass.New(2, 1, context.SampleRate())
oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) { oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) {
s.leftData = buf.Call("getChannelData", 0).Interface().([]float32) s.leftData = buf.Call("getChannelData", 0).Interface().([]float32)
s.rightData = buf.Call("getChannelData", 1).Interface().([]float32) switch n := buf.Get("numberOfChannels").Int(); n {
case 1:
s.rightData = s.leftData
case 2:
s.rightData = buf.Call("getChannelData", 1).Interface().([]float32)
default:
ch <- fmt.Errorf("audio/mp3: Number of channels must be 1 or 2 but %d", n)
}
close(ch) close(ch)
}) })
<-ch if err := <-ch; err != nil {
return nil, err
}
return s, nil return s, nil
} }