From d9967c53e50d66dfde7ba5e49badf9de319760ad Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 22 Oct 2021 15:29:00 +0900 Subject: [PATCH] audio: Treat Oto context's error --- audio/audio.go | 20 ++++++++++---------- audio/internal/go2cpp/player_js.go | 4 ++++ audio/player.go | 11 +++++++++++ go.mod | 2 +- go.sum | 4 ++-- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/audio/audio.go b/audio/audio.go index 5aed43dce..6be932676 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -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 diff --git a/audio/internal/go2cpp/player_js.go b/audio/internal/go2cpp/player_js.go index 47f0494b2..ca962f255 100644 --- a/audio/internal/go2cpp/player_js.go +++ b/audio/internal/go2cpp/player_js.go @@ -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 diff --git a/audio/player.go b/audio/player.go index 2dbdff736..1a7d4a390 100644 --- a/audio/player.go +++ b/audio/player.go @@ -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() diff --git a/go.mod b/go.mod index 78f4f121d..7071209a9 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index f70fa3390..fc01f0433 100644 --- a/go.sum +++ b/go.sum @@ -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=