From f222885d975003a230f64123246ab1507cdfb645 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 5 Apr 2016 00:09:00 +0900 Subject: [PATCH] audio: Context.Update now returns error (#197) --- examples/audio/main.go | 4 +++- examples/pcm/main.go | 4 +++- examples/piano/main.go | 4 +++- examples/sinewave/main.go | 4 +++- exp/audio/audio.go | 14 +++++++++++--- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/examples/audio/main.go b/examples/audio/main.go index d6cd4129d..be0fc3091 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -180,7 +180,9 @@ func (p *Player) close() error { } func update(screen *ebiten.Image) error { - audioContext.Update() + if err := audioContext.Update(); err != nil { + return err + } if musicPlayer == nil { select { case musicPlayer = <-musicCh: diff --git a/examples/pcm/main.go b/examples/pcm/main.go index 044beef0e..c56443d03 100644 --- a/examples/pcm/main.go +++ b/examples/pcm/main.go @@ -145,7 +145,9 @@ func addNote() error { } func update(screen *ebiten.Image) error { - audioContext.Update() + if err := audioContext.Update(); err != nil { + return err + } defer func() { frames++ }() diff --git a/examples/piano/main.go b/examples/piano/main.go index 7b3664622..dccfdea41 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -206,7 +206,9 @@ func init() { } func update(screen *ebiten.Image) error { - audioContext.Update() + if err := audioContext.Update(); err != nil { + return err + } updateInput() for i, key := range keys { if keyStates[key] != 1 { diff --git a/examples/sinewave/main.go b/examples/sinewave/main.go index 53766beb5..c7816987b 100644 --- a/examples/sinewave/main.go +++ b/examples/sinewave/main.go @@ -87,7 +87,9 @@ func (s *stream) Close() error { var player *audio.Player func update(screen *ebiten.Image) error { - audioContext.Update() + if err := audioContext.Update(); err != nil { + return err + } if player == nil { var err error player, err = audioContext.NewPlayer(&stream{}) diff --git a/exp/audio/audio.go b/exp/audio/audio.go index b25796f40..5e3639d7e 100644 --- a/exp/audio/audio.go +++ b/exp/audio/audio.go @@ -108,6 +108,7 @@ type Context struct { stream *mixedPlayersStream players map[*Player]struct{} frames int + errorCh chan error sync.Mutex } @@ -116,6 +117,7 @@ func NewContext(sampleRate int) (*Context, error) { c := &Context{ sampleRate: sampleRate, players: map[*Player]struct{}{}, + errorCh: make(chan error), } c.stream = &mixedPlayersStream{ context: c, @@ -133,8 +135,8 @@ func NewContext(sampleRate int) (*Context, error) { break } if err != nil { - // TODO: Record the last error - panic(err) + c.errorCh <- err + return } time.Sleep(1 * time.Millisecond) } @@ -148,10 +150,16 @@ func NewContext(sampleRate int) (*Context, error) { // In sync mode, the game logical time syncs the audio logical time and // you will find audio stops when the game stops e.g. when the window is deactivated. // In unsync mode, the audio never stops even when the game stops. -func (c *Context) Update() { +func (c *Context) Update() error { c.Lock() defer c.Unlock() + select { + case err := <-c.errorCh: + return err + default: + } c.frames++ + return nil } // SampleRate returns the sample rate.