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 {
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)

View File

@ -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 {

View File

@ -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

View File

@ -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