audio: Deprecate Update

This commit is contained in:
Hajime Hoshi 2017-12-01 00:26:28 +09:00
parent dba7124e38
commit 0379e3c591
3 changed files with 42 additions and 17 deletions

View File

@ -43,6 +43,7 @@ import (
"github.com/hajimehoshi/oto" "github.com/hajimehoshi/oto"
"github.com/hajimehoshi/ebiten/internal/audiobinding"
"github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/clock"
) )
@ -180,7 +181,6 @@ func (p *players) hasSource(src ReadSeekCloser) bool {
// } // }
type Context struct { type Context struct {
players *players players *players
errCh chan error
initCh chan struct{} initCh chan struct{}
initedCh chan struct{} initedCh chan struct{}
pingCount int pingCount int
@ -216,7 +216,6 @@ func NewContext(sampleRate int) (*Context, error) {
} }
c := &Context{ c := &Context{
sampleRate: sampleRate, sampleRate: sampleRate,
errCh: make(chan error, 1),
} }
theContext = c theContext = c
c.players = &players{ c.players = &players{
@ -266,7 +265,7 @@ func (c *Context) loop() {
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, c.bufferSize()) p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, c.bufferSize())
if err != nil { if err != nil {
c.errCh <- err audiobinding.SetError(err)
return return
} }
defer p.Close() defer p.Close()
@ -290,26 +289,21 @@ func (c *Context) loop() {
c.writtenBytes += l c.writtenBytes += l
buf := make([]uint8, l) buf := make([]uint8, l)
if _, err := io.ReadFull(c.players, buf); err != nil { if _, err := io.ReadFull(c.players, buf); err != nil {
c.errCh <- err audiobinding.SetError(err)
return
} }
if _, err = p.Write(buf); err != nil { if _, err = p.Write(buf); err != nil {
c.errCh <- err audiobinding.SetError(err)
return
} }
} }
} }
// Update returns an error if some errors happen, or nil if there is no error. // Update is deprecated as of 1.6.0-alpha.
// //
// As of 1.6.0-alpha, Update just returns the error if an error happens internally, // As of 1.6.0-alpha, Update always returns nil and does nothing related to updating the state.
// and do nothing related to updating the state. // The internal audio error is returned at ebiten.Run instead.
// Then, the audio is available without Update,
// but it is recommended to call Update every frame to check errors.
func (c *Context) Update() error { func (c *Context) Update() error {
select {
case err := <-c.errCh:
return err
default:
}
return nil return nil
} }

View File

@ -0,0 +1,25 @@
// Copyright 2017 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package audiobinding
var ch = make(chan error, 1)
func Error() <-chan error {
return ch
}
func SetError(err error) {
ch <- err
}

10
run.go
View File

@ -18,6 +18,7 @@ import (
"image" "image"
"sync/atomic" "sync/atomic"
"github.com/hajimehoshi/ebiten/internal/audiobinding"
"github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/ui" "github.com/hajimehoshi/ebiten/internal/ui"
) )
@ -79,6 +80,11 @@ func (u *updater) SetSize(width, height int, scale float64) {
} }
func (u *updater) Update(afterFrameUpdate func()) error { func (u *updater) Update(afterFrameUpdate func()) error {
select {
case err := <-audiobinding.Error():
return err
default:
}
n := clock.Update() n := clock.Update()
if err := u.g.Update(n, afterFrameUpdate); err != nil { if err := u.g.Update(n, afterFrameUpdate); err != nil {
return err return err
@ -108,8 +114,8 @@ func (u *updater) Invalidate() {
// //
// The given scale is ignored on fullscreen mode. // The given scale is ignored on fullscreen mode.
// //
// Run returns error when 1) OpenGL error happens, or 2) f returns error. // Run returns error when 1) OpenGL error happens, 2) audio error happens or 3) f returns error.
// In the case of 2), Run returns the same error. // In the case of 3), Run returns the same error.
// //
// The size unit is device-independent pixel. // The size unit is device-independent pixel.
// //