audio: Treat Oto context's error

This commit is contained in:
Hajime Hoshi 2021-10-22 15:29:00 +09:00
parent 5cebe867d4
commit d9967c53e5
5 changed files with 28 additions and 13 deletions

View File

@ -126,9 +126,7 @@ func NewContext(sampleRate int) *Context {
var err error
theContextLock.Lock()
if theContext != nil {
theContext.m.Lock()
err = theContext.err
theContext.m.Unlock()
err = theContext.error()
}
theContextLock.Unlock()
if err != nil {
@ -152,13 +150,6 @@ func CurrentContext() *Context {
return c
}
func (c *Context) hasError() bool {
c.m.Lock()
r := c.err != nil
c.m.Unlock()
return r
}
func (c *Context) setError(err error) {
// TODO: What if c.err already exists?
c.m.Lock()
@ -166,6 +157,15 @@ func (c *Context) setError(err error) {
c.m.Unlock()
}
func (c *Context) error() error {
c.m.Lock()
defer c.m.Unlock()
if c.err != nil {
return c.err
}
return c.playerFactory.error()
}
func (c *Context) setReady() {
c.m.Lock()
c.ready = true

View File

@ -67,6 +67,10 @@ func (c *Context) Resume() error {
return nil
}
func (c *Context) Err() error {
return nil
}
func (c *Context) oneBufferSize() int {
// TODO: This must be audio.oneBufferSize(p.context.sampleRate). Avoid the duplication.
return c.sampleRate * c.channelNum * c.bitDepthInBytes / 4

View File

@ -27,6 +27,7 @@ type context interface {
NewPlayer(io.Reader) oto.Player
Suspend() error
Resume() error
Err() error
}
type playerFactory struct {
@ -91,6 +92,16 @@ func (f *playerFactory) resume() error {
return f.context.Resume()
}
func (f *playerFactory) error() error {
f.m.Lock()
defer f.m.Unlock()
if f.context == nil {
return nil
}
return f.context.Err()
}
func (f *playerFactory) initContextIfNeeded() (<-chan struct{}, error) {
f.m.Lock()
defer f.m.Unlock()

2
go.mod
View File

@ -7,7 +7,7 @@ require (
github.com/hajimehoshi/bitmapfont/v2 v2.1.3
github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41
github.com/hajimehoshi/go-mp3 v0.3.2
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2.0.20211022061803-81e245b09d0b
github.com/jakecoffman/cp v1.1.0
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240
github.com/jfreymuth/oggvorbis v1.0.3

4
go.sum
View File

@ -9,8 +9,8 @@ github.com/hajimehoshi/go-mp3 v0.3.2 h1:xSYNE2F3lxtOu9BRjCWHHceg7S91IHfXfXp5+LYQ
github.com/hajimehoshi/go-mp3 v0.3.2/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1 h1:7cJz/zRQV4aJvMSSRqzN2TImoVVMpE0BCY4nrNJaDOM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2 h1:DV2DcbY3YLuLB9gI9R1GT9TPOo92lUeWveV8ci1sBLk=
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2/go.mod h1:rUKQmwMkqmRxe+IAof9+tuYA2ofm8cAWXFmSfzDN8vQ=
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2.0.20211022061803-81e245b09d0b h1:DyHaCwXYwsZMLawB9G7uDP18WHvwO8KXm9JROxTY5ew=
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2.0.20211022061803-81e245b09d0b/go.mod h1:rUKQmwMkqmRxe+IAof9+tuYA2ofm8cAWXFmSfzDN8vQ=
github.com/jakecoffman/cp v1.1.0 h1:bhKvCNbAddYegYHSV5abG3G23vZdsISgqXa4X/lK8Oo=
github.com/jakecoffman/cp v1.1.0/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 h1:dy+DS31tGEGCsZzB45HmJJNHjur8GDgtRNX9U7HnSX4=