From fb85bcf104bf33f4873863a38a1f376d7be0f61d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 3 Jun 2017 01:34:17 +0900 Subject: [PATCH] examples: Remove unneeded error handlings --- examples/audio/main.go | 13 ++++--------- examples/pcm/main.go | 19 +++++-------------- examples/piano/main.go | 18 ++++++------------ examples/sinewave/main.go | 4 +--- 4 files changed, 16 insertions(+), 38 deletions(-) diff --git a/examples/audio/main.go b/examples/audio/main.go index 7c476c492..c4dfa8506 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -84,10 +84,7 @@ func (p *Player) updateSE() error { if keyState[ebiten.KeyP] != 1 { return nil } - sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes) - if err != nil { - return err - } + sePlayer, _ := audio.NewPlayerFromBytes(audioContext, seBytes) sePlayer.Play() return nil } @@ -126,7 +123,8 @@ func (p *Player) updatePlayPause() error { if p.audioPlayer.IsPlaying() { return p.audioPlayer.Pause() } - return p.audioPlayer.Play() + p.audioPlayer.Play() + return nil } func (p *Player) updateBar() { @@ -287,10 +285,7 @@ func main() { } close(musicCh) // TODO: Is this goroutine-safe? - if err := p.Play(); err != nil { - log.Fatal(err) - return - } + p.Play() }() if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { log.Fatal(err) diff --git a/examples/pcm/main.go b/examples/pcm/main.go index efab15b11..bad21edf6 100644 --- a/examples/pcm/main.go +++ b/examples/pcm/main.go @@ -97,7 +97,7 @@ func toBytes(l, r []int16) []byte { return b } -func addNote() (rune, error) { +func addNote() rune { size := sampleRate / ebiten.FPS 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(r, vol, freq, 0.25) b := toBytes(l, r) - p, err := audio.NewPlayerFromBytes(audioContext, b) - if err != nil { - return 0, err - } - if err := p.Play(); err != nil { - return 0, err - } - return rune(note), nil + p, _ := audio.NewPlayerFromBytes(audioContext, b) + p.Play() + return rune(note) } var currentNote rune func update(screen *ebiten.Image) error { if frames%30 == 0 { - n, err := addNote() - if err != nil { - return err - } - currentNote = n + currentNote = addNote() } frames++ if err := audioContext.Update(); err != nil { diff --git a/examples/piano/main.go b/examples/piano/main.go index 3f7e0fc23..a0b55a857 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -81,17 +81,15 @@ func toBytes(l, r []int16) []byte { return b } -func addNote(freq float64, vol float64) error { +func addNote(freq float64, vol float64) { // TODO: Call Close method of *audio.Player. // However, this works without Close because Close is automatically called when GC // collects a *audio.Player object. f := int(freq) if n, ok := noteCache[f]; ok { p, _ := audio.NewPlayerFromBytes(audioContext, n) - if err := p.Play(); err != nil { - return err - } - return nil + p.Play() + return } length := len(pcm) * baseFreq / f l := make([]int16, length) @@ -108,10 +106,8 @@ func addNote(freq float64, vol float64) error { n := toBytes(l, r) noteCache[f] = n p, _ := audio.NewPlayerFromBytes(audioContext, n) - if err := p.Play(); err != nil { - return err - } - return nil + p.Play() + return } var keys = []ebiten.Key{ @@ -197,9 +193,7 @@ func update(screen *ebiten.Image) error { if keyStates[key] != 1 { continue } - if err := addNote(220*math.Exp2(float64(i-1)/12.0), 1.0); err != nil { - return err - } + addNote(220*math.Exp2(float64(i-1)/12.0), 1.0) } if ebiten.IsRunningSlowly() { return nil diff --git a/examples/sinewave/main.go b/examples/sinewave/main.go index 31ee3db87..7828af25f 100644 --- a/examples/sinewave/main.go +++ b/examples/sinewave/main.go @@ -95,9 +95,7 @@ func update(screen *ebiten.Image) error { if err != nil { return err } - if err := player.Play(); err != nil { - return err - } + player.Play() } if ebiten.IsRunningSlowly() { return nil