From 816c6469a20fccde22266f6bfabeea2fb8fb8f8a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 13 Feb 2016 23:08:01 +0900 Subject: [PATCH] doc: Update --- _docs/public/examples/piano.html | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/_docs/public/examples/piano.html b/_docs/public/examples/piano.html index 50dd5a562..7517b4992 100644 --- a/_docs/public/examples/piano.html +++ b/_docs/public/examples/piano.html @@ -102,12 +102,18 @@ func (s *stream) Close() error { return nil } -func addNote(freq float64, vol float64) { +func addNote(freq float64, vol float64) error { f := int(freq) if n, ok := noteCache[f]; ok { - p := audio.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate) + p, err := audio.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate) + if err == audio.ErrTooManyPlayers { + return nil + } + if err != nil { + return err + } p.Play() - return + return nil } length := len(pcm) * baseFreq / f l := make([]int16, length) @@ -123,8 +129,15 @@ func addNote(freq float64, vol float64) { } n := toBytes(l, r) noteCache[f] = n - p := audio.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate) + p, err := audio.NewPlayer(&stream{bytes.NewReader(n)}, sampleRate) + if err == audio.ErrTooManyPlayers { + return nil + } + if err != nil { + return err + } p.Play() + return nil } var keys = []ebiten.Key{ @@ -217,7 +230,9 @@ func update(screen *ebiten.Image) error { if keyStates[key] != 1 { continue } - addNote(220*math.Exp2(float64(i-1)/12.0), 1.0) + if err := addNote(220*math.Exp2(float64(i-1)/12.0), 1.0); err != nil { + return err + } } screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})