mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 18:52:44 +01:00
audio: Move seeking from the loop to Seek function
Seek should work in both cases the loop runs and not. This change is a preparation.
This commit is contained in:
parent
c4c6acacf9
commit
fe3e8e376a
@ -250,8 +250,8 @@ type playerImpl struct {
|
|||||||
|
|
||||||
closeCh chan struct{}
|
closeCh chan struct{}
|
||||||
closedCh chan struct{}
|
closedCh chan struct{}
|
||||||
seekCh chan seekArgs
|
seekCh chan struct{}
|
||||||
seekedCh chan error
|
seekedCh chan struct{}
|
||||||
proceedCh chan []int16
|
proceedCh chan []int16
|
||||||
proceededCh chan proceededValues
|
proceededCh chan proceededValues
|
||||||
syncCh chan func()
|
syncCh chan func()
|
||||||
@ -259,11 +259,6 @@ type playerImpl struct {
|
|||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type seekArgs struct {
|
|
||||||
offset int64
|
|
||||||
whence int
|
|
||||||
}
|
|
||||||
|
|
||||||
type proceededValues struct {
|
type proceededValues struct {
|
||||||
buf []int16
|
buf []int16
|
||||||
err error
|
err error
|
||||||
@ -294,8 +289,8 @@ func NewPlayer(context *Context, src io.ReadCloser) (*Player, error) {
|
|||||||
volume: 1,
|
volume: 1,
|
||||||
closeCh: make(chan struct{}),
|
closeCh: make(chan struct{}),
|
||||||
closedCh: make(chan struct{}),
|
closedCh: make(chan struct{}),
|
||||||
seekCh: make(chan seekArgs),
|
seekCh: make(chan struct{}),
|
||||||
seekedCh: make(chan error),
|
seekedCh: make(chan struct{}),
|
||||||
proceedCh: make(chan []int16),
|
proceedCh: make(chan []int16),
|
||||||
proceededCh: make(chan proceededValues),
|
proceededCh: make(chan proceededValues),
|
||||||
},
|
},
|
||||||
@ -432,20 +427,8 @@ func (p *playerImpl) readLoop() {
|
|||||||
p.closedCh <- struct{}{}
|
p.closedCh <- struct{}{}
|
||||||
return
|
return
|
||||||
|
|
||||||
case s := <-p.seekCh:
|
case <-p.seekCh:
|
||||||
seeker, ok := p.src.(io.Seeker)
|
p.seekedCh <- struct{}{}
|
||||||
if !ok {
|
|
||||||
panic("audio: the source must be io.Seeker when seeking")
|
|
||||||
}
|
|
||||||
pos, err := seeker.Seek(s.offset, s.whence)
|
|
||||||
|
|
||||||
p.m.Lock()
|
|
||||||
p.buf = nil
|
|
||||||
p.pos = pos
|
|
||||||
p.srcEOF = false
|
|
||||||
p.m.Unlock()
|
|
||||||
|
|
||||||
p.seekedCh <- err
|
|
||||||
if timer != nil {
|
if timer != nil {
|
||||||
timer.Stop()
|
timer.Stop()
|
||||||
}
|
}
|
||||||
@ -626,10 +609,27 @@ func (p *playerImpl) Seek(offset time.Duration) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.m.Lock()
|
||||||
|
defer p.m.Unlock()
|
||||||
|
|
||||||
o := int64(offset) * bytesPerSample * int64(p.sampleRate) / int64(time.Second)
|
o := int64(offset) * bytesPerSample * int64(p.sampleRate) / int64(time.Second)
|
||||||
o &= mask
|
o &= mask
|
||||||
p.seekCh <- seekArgs{o, io.SeekStart}
|
|
||||||
return <-p.seekedCh
|
seeker, ok := p.src.(io.Seeker)
|
||||||
|
if !ok {
|
||||||
|
panic("audio: the source must be io.Seeker when seeking")
|
||||||
|
}
|
||||||
|
pos, err := seeker.Seek(o, io.SeekStart)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
p.buf = nil
|
||||||
|
p.pos = pos
|
||||||
|
p.srcEOF = false
|
||||||
|
p.seekCh <- struct{}{}
|
||||||
|
<-p.seekedCh
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pause pauses the playing.
|
// Pause pauses the playing.
|
||||||
|
Loading…
Reference in New Issue
Block a user