audio/internal/readerdriver: Refactoring

This commit is contained in:
Hajime Hoshi 2021-03-28 23:27:47 +09:00
parent bf4c1e3d74
commit f0d7e16eb2
2 changed files with 17 additions and 17 deletions

View File

@ -35,3 +35,11 @@ type Player interface {
Err() error Err() error
io.Closer io.Closer
} }
type playerState int
const (
playerPaused playerState = iota
playerPlay
playerClosed
)

View File

@ -87,19 +87,11 @@ func NewContext(sampleRate int, channelNum int, bitDepthInBytes int) (Context, e
return d, nil return d, nil
} }
type readerPlayerState int
const (
readerPlayerPaused readerPlayerState = iota
readerPlayerPlay
readerPlayerClosed
)
type playerImpl struct { type playerImpl struct {
context *contextImpl context *contextImpl
src io.Reader src io.Reader
eof bool eof bool
state readerPlayerState state playerState
gain js.Value gain js.Value
err error err error
@ -140,7 +132,7 @@ func (c *contextImpl) MaxBufferSize() int {
} }
func (p *playerImpl) Pause() { func (p *playerImpl) Pause() {
if p.state != readerPlayerPlay { if p.state != playerPlay {
return return
} }
@ -150,7 +142,7 @@ func (p *playerImpl) Pause() {
n.Call("stop") n.Call("stop")
n.Call("disconnect") n.Call("disconnect")
} }
p.state = readerPlayerPaused p.state = playerPaused
p.bufferSourceNodes = p.bufferSourceNodes[:0] p.bufferSourceNodes = p.bufferSourceNodes[:0]
p.nextPos = 0 p.nextPos = 0
} }
@ -165,7 +157,7 @@ func (p *playerImpl) appendBuffer(this js.Value, args []js.Value) interface{} {
} }
} }
if p.state != readerPlayerPlay { if p.state != playerPlay {
return nil return nil
} }
@ -221,20 +213,20 @@ func (p *playerImpl) appendBuffer(this js.Value, args []js.Value) interface{} {
} }
func (p *playerImpl) Play() { func (p *playerImpl) Play() {
if p.state != readerPlayerPaused { if p.state != playerPaused {
return return
} }
p.state = readerPlayerPlay p.state = playerPlay
p.appendBuffer(js.Undefined(), nil) p.appendBuffer(js.Undefined(), nil)
p.appendBuffer(js.Undefined(), nil) p.appendBuffer(js.Undefined(), nil)
} }
func (p *playerImpl) IsPlaying() bool { func (p *playerImpl) IsPlaying() bool {
return p.state == readerPlayerPlay return p.state == playerPlay
} }
func (p *playerImpl) Reset() { func (p *playerImpl) Reset() {
if p.state == readerPlayerClosed { if p.state == playerClosed {
return return
} }
@ -266,7 +258,7 @@ func (p *playerImpl) Err() error {
func (p *playerImpl) Close() error { func (p *playerImpl) Close() error {
runtime.SetFinalizer(p, nil) runtime.SetFinalizer(p, nil)
p.Reset() p.Reset()
p.state = readerPlayerClosed p.state = playerClosed
p.appendBufferFunc.Release() p.appendBufferFunc.Release()
return nil return nil
} }