audio: Use variables instead of channels to avoid GopherJS bugs

This commit is contained in:
Hajime Hoshi 2017-07-11 02:38:18 +09:00
parent 40ae782a67
commit 80b10b2d49

View File

@ -173,7 +173,7 @@ func (p *players) hasSource(src ReadSeekCloser) bool {
type Context struct { type Context struct {
players *players players *players
errCh chan error errCh chan error
pingCh chan struct{} pingCount int
sampleRate int sampleRate int
frames int64 frames int64
framesReadOnly int64 framesReadOnly int64
@ -200,7 +200,6 @@ func NewContext(sampleRate int) (*Context, error) {
c := &Context{ c := &Context{
sampleRate: sampleRate, sampleRate: sampleRate,
errCh: make(chan error, 1), errCh: make(chan error, 1),
pingCh: make(chan struct{}),
} }
theContext = c theContext = c
c.players = &players{ c.players = &players{
@ -229,10 +228,9 @@ func (c *Context) Frame() int64 {
// Internal Only? // Internal Only?
func (c *Context) Ping() { func (c *Context) Ping() {
select { c.m.Lock()
case c.pingCh <- struct{}{}: c.pingCount = 5
default: c.m.Unlock()
}
} }
func (c *Context) loop() { func (c *Context) loop() {
@ -254,10 +252,13 @@ func (c *Context) loop() {
for { for {
c.m.Lock() c.m.Lock()
c.framesReadOnly = c.frames c.framesReadOnly = c.frames
if c.pingCount == 0 {
c.m.Unlock() c.m.Unlock()
if c.frames%10 == 0 { time.Sleep(10 * time.Millisecond)
<-c.pingCh continue
} }
c.pingCount--
c.m.Unlock()
c.frames++ c.frames++
bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / FPS bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / FPS
l := (c.frames * int64(bytesPerFrame)) - c.writtenBytes l := (c.frames * int64(bytesPerFrame)) - c.writtenBytes