examples: Remove unneeded error handlings

This commit is contained in:
Hajime Hoshi 2017-06-03 01:34:17 +09:00
parent 98c48463d2
commit fb85bcf104
4 changed files with 16 additions and 38 deletions

View File

@ -84,10 +84,7 @@ func (p *Player) updateSE() error {
if keyState[ebiten.KeyP] != 1 { if keyState[ebiten.KeyP] != 1 {
return nil return nil
} }
sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes) sePlayer, _ := audio.NewPlayerFromBytes(audioContext, seBytes)
if err != nil {
return err
}
sePlayer.Play() sePlayer.Play()
return nil return nil
} }
@ -126,7 +123,8 @@ func (p *Player) updatePlayPause() error {
if p.audioPlayer.IsPlaying() { if p.audioPlayer.IsPlaying() {
return p.audioPlayer.Pause() return p.audioPlayer.Pause()
} }
return p.audioPlayer.Play() p.audioPlayer.Play()
return nil
} }
func (p *Player) updateBar() { func (p *Player) updateBar() {
@ -287,10 +285,7 @@ func main() {
} }
close(musicCh) close(musicCh)
// TODO: Is this goroutine-safe? // TODO: Is this goroutine-safe?
if err := p.Play(); err != nil { p.Play()
log.Fatal(err)
return
}
}() }()
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -97,7 +97,7 @@ func toBytes(l, r []int16) []byte {
return b return b
} }
func addNote() (rune, error) { func addNote() rune {
size := sampleRate / ebiten.FPS size := sampleRate / ebiten.FPS
notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2} notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2}
@ -126,25 +126,16 @@ func addNote() (rune, error) {
square(l, vol, freq, 0.25) square(l, vol, freq, 0.25)
square(r, vol, freq, 0.25) square(r, vol, freq, 0.25)
b := toBytes(l, r) b := toBytes(l, r)
p, err := audio.NewPlayerFromBytes(audioContext, b) p, _ := audio.NewPlayerFromBytes(audioContext, b)
if err != nil { p.Play()
return 0, err return rune(note)
}
if err := p.Play(); err != nil {
return 0, err
}
return rune(note), nil
} }
var currentNote rune var currentNote rune
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
if frames%30 == 0 { if frames%30 == 0 {
n, err := addNote() currentNote = addNote()
if err != nil {
return err
}
currentNote = n
} }
frames++ frames++
if err := audioContext.Update(); err != nil { if err := audioContext.Update(); err != nil {

View File

@ -81,17 +81,15 @@ func toBytes(l, r []int16) []byte {
return b return b
} }
func addNote(freq float64, vol float64) error { func addNote(freq float64, vol float64) {
// TODO: Call Close method of *audio.Player. // TODO: Call Close method of *audio.Player.
// However, this works without Close because Close is automatically called when GC // However, this works without Close because Close is automatically called when GC
// collects a *audio.Player object. // collects a *audio.Player object.
f := int(freq) f := int(freq)
if n, ok := noteCache[f]; ok { if n, ok := noteCache[f]; ok {
p, _ := audio.NewPlayerFromBytes(audioContext, n) p, _ := audio.NewPlayerFromBytes(audioContext, n)
if err := p.Play(); err != nil { p.Play()
return err return
}
return nil
} }
length := len(pcm) * baseFreq / f length := len(pcm) * baseFreq / f
l := make([]int16, length) l := make([]int16, length)
@ -108,10 +106,8 @@ func addNote(freq float64, vol float64) error {
n := toBytes(l, r) n := toBytes(l, r)
noteCache[f] = n noteCache[f] = n
p, _ := audio.NewPlayerFromBytes(audioContext, n) p, _ := audio.NewPlayerFromBytes(audioContext, n)
if err := p.Play(); err != nil { p.Play()
return err return
}
return nil
} }
var keys = []ebiten.Key{ var keys = []ebiten.Key{
@ -197,9 +193,7 @@ func update(screen *ebiten.Image) error {
if keyStates[key] != 1 { if keyStates[key] != 1 {
continue continue
} }
if err := addNote(220*math.Exp2(float64(i-1)/12.0), 1.0); err != nil { addNote(220*math.Exp2(float64(i-1)/12.0), 1.0)
return err
}
} }
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil

View File

@ -95,9 +95,7 @@ func update(screen *ebiten.Image) error {
if err != nil { if err != nil {
return err return err
} }
if err := player.Play(); err != nil { player.Play()
return err
}
} }
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil