audio: Context.Update now returns error (#197)

This commit is contained in:
Hajime Hoshi 2016-04-05 00:09:00 +09:00
parent b6a02ddc94
commit f222885d97
5 changed files with 23 additions and 7 deletions

View File

@ -180,7 +180,9 @@ func (p *Player) close() error {
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
audioContext.Update() if err := audioContext.Update(); err != nil {
return err
}
if musicPlayer == nil { if musicPlayer == nil {
select { select {
case musicPlayer = <-musicCh: case musicPlayer = <-musicCh:

View File

@ -145,7 +145,9 @@ func addNote() error {
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
audioContext.Update() if err := audioContext.Update(); err != nil {
return err
}
defer func() { defer func() {
frames++ frames++
}() }()

View File

@ -206,7 +206,9 @@ func init() {
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
audioContext.Update() if err := audioContext.Update(); err != nil {
return err
}
updateInput() updateInput()
for i, key := range keys { for i, key := range keys {
if keyStates[key] != 1 { if keyStates[key] != 1 {

View File

@ -87,7 +87,9 @@ func (s *stream) Close() error {
var player *audio.Player var player *audio.Player
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
audioContext.Update() if err := audioContext.Update(); err != nil {
return err
}
if player == nil { if player == nil {
var err error var err error
player, err = audioContext.NewPlayer(&stream{}) player, err = audioContext.NewPlayer(&stream{})

View File

@ -108,6 +108,7 @@ type Context struct {
stream *mixedPlayersStream stream *mixedPlayersStream
players map[*Player]struct{} players map[*Player]struct{}
frames int frames int
errorCh chan error
sync.Mutex sync.Mutex
} }
@ -116,6 +117,7 @@ func NewContext(sampleRate int) (*Context, error) {
c := &Context{ c := &Context{
sampleRate: sampleRate, sampleRate: sampleRate,
players: map[*Player]struct{}{}, players: map[*Player]struct{}{},
errorCh: make(chan error),
} }
c.stream = &mixedPlayersStream{ c.stream = &mixedPlayersStream{
context: c, context: c,
@ -133,8 +135,8 @@ func NewContext(sampleRate int) (*Context, error) {
break break
} }
if err != nil { if err != nil {
// TODO: Record the last error c.errorCh <- err
panic(err) return
} }
time.Sleep(1 * time.Millisecond) 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 // 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. // 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. // In unsync mode, the audio never stops even when the game stops.
func (c *Context) Update() { func (c *Context) Update() error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
select {
case err := <-c.errorCh:
return err
default:
}
c.frames++ c.frames++
return nil
} }
// SampleRate returns the sample rate. // SampleRate returns the sample rate.