audio: Add NewPlayerFromBytes (#231)

This commit is contained in:
Hajime Hoshi 2016-06-27 02:22:44 +09:00
parent a2402c32c0
commit b02d89cdc3
3 changed files with 25 additions and 24 deletions

View File

@ -26,6 +26,7 @@
package audio
import (
"bytes"
"errors"
"io"
"runtime"
@ -295,6 +296,27 @@ func NewPlayer(context *Context, src ReadSeekCloser) (*Player, error) {
return p, nil
}
type bytesReadSeekCloser struct {
*bytes.Reader
}
func (b *bytesReadSeekCloser) Close() error {
return nil
}
// NewPlayerFromBytes creates a new player with the given bytes.
//
// As opposed to NewPlayer, you don't have to care if src is already used by another player or not.
// src can be shared by multiple players.
//
// The format of src should be same as noted at NewPlayer.
//
// This function is concurrent-safe.
func NewPlayerFromBytes(context *Context, src []byte) (*Player, error) {
b := &bytesReadSeekCloser{bytes.NewReader(src)}
return NewPlayer(context, b)
}
// Close closes the stream. Ths source stream passed by NewPlayer will also be closed.
//
// After closing, the stream owned by the player will be usable to a new player again.

View File

@ -15,7 +15,6 @@
package main
import (
"bytes"
"fmt"
"log"
"math"
@ -97,15 +96,6 @@ func toBytes(l, r []int16) []byte {
return b
}
type srcStream struct {
*bytes.Reader
}
func (s *srcStream) Close() error {
s.Reader = nil
return nil
}
func addNote() error {
size := sampleRate / ebiten.FPS
notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2}
@ -135,8 +125,7 @@ func addNote() error {
square(l, vol, freq, 0.25)
square(r, vol, freq, 0.25)
b := toBytes(l, r)
s := &srcStream{bytes.NewReader(b)}
p, err := audio.NewPlayer(audioContext, s)
p, err := audio.NewPlayerFromBytes(audioContext, b)
if err != nil {
return err
}

View File

@ -15,7 +15,6 @@
package main
import (
"bytes"
"fmt"
"image/color"
"log"
@ -80,22 +79,13 @@ func toBytes(l, r []int16) []byte {
return b
}
type stream struct {
*bytes.Reader
}
func (s *stream) Close() error {
s.Reader = nil
return nil
}
func addNote(freq float64, vol float64) error {
// 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, err := audio.NewPlayer(audioContext, &stream{bytes.NewReader(n)})
p, err := audio.NewPlayerFromBytes(audioContext, n)
if err != nil {
return err
}
@ -116,7 +106,7 @@ func addNote(freq float64, vol float64) error {
}
n := toBytes(l, r)
noteCache[f] = n
p, err := audio.NewPlayer(audioContext, &stream{bytes.NewReader(n)})
p, err := audio.NewPlayerFromBytes(audioContext, n)
if err != nil {
return err
}