From f0d7e16eb215f7864cdd0f0111d65a59d681195f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 28 Mar 2021 23:27:47 +0900 Subject: [PATCH] audio/internal/readerdriver: Refactoring --- audio/internal/readerdriver/driver.go | 8 ++++++++ audio/internal/readerdriver/driver_js.go | 26 ++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/audio/internal/readerdriver/driver.go b/audio/internal/readerdriver/driver.go index 87cc82b66..e0d2198c4 100644 --- a/audio/internal/readerdriver/driver.go +++ b/audio/internal/readerdriver/driver.go @@ -35,3 +35,11 @@ type Player interface { Err() error io.Closer } + +type playerState int + +const ( + playerPaused playerState = iota + playerPlay + playerClosed +) diff --git a/audio/internal/readerdriver/driver_js.go b/audio/internal/readerdriver/driver_js.go index 1b56e4cce..b7b81ee7b 100644 --- a/audio/internal/readerdriver/driver_js.go +++ b/audio/internal/readerdriver/driver_js.go @@ -87,19 +87,11 @@ func NewContext(sampleRate int, channelNum int, bitDepthInBytes int) (Context, e return d, nil } -type readerPlayerState int - -const ( - readerPlayerPaused readerPlayerState = iota - readerPlayerPlay - readerPlayerClosed -) - type playerImpl struct { context *contextImpl src io.Reader eof bool - state readerPlayerState + state playerState gain js.Value err error @@ -140,7 +132,7 @@ func (c *contextImpl) MaxBufferSize() int { } func (p *playerImpl) Pause() { - if p.state != readerPlayerPlay { + if p.state != playerPlay { return } @@ -150,7 +142,7 @@ func (p *playerImpl) Pause() { n.Call("stop") n.Call("disconnect") } - p.state = readerPlayerPaused + p.state = playerPaused p.bufferSourceNodes = p.bufferSourceNodes[: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 } @@ -221,20 +213,20 @@ func (p *playerImpl) appendBuffer(this js.Value, args []js.Value) interface{} { } func (p *playerImpl) Play() { - if p.state != readerPlayerPaused { + if p.state != playerPaused { return } - p.state = readerPlayerPlay + p.state = playerPlay p.appendBuffer(js.Undefined(), nil) p.appendBuffer(js.Undefined(), nil) } func (p *playerImpl) IsPlaying() bool { - return p.state == readerPlayerPlay + return p.state == playerPlay } func (p *playerImpl) Reset() { - if p.state == readerPlayerClosed { + if p.state == playerClosed { return } @@ -266,7 +258,7 @@ func (p *playerImpl) Err() error { func (p *playerImpl) Close() error { runtime.SetFinalizer(p, nil) p.Reset() - p.state = readerPlayerClosed + p.state = playerClosed p.appendBufferFunc.Release() return nil }