mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
audio: Ignore seeking players
This commit is contained in:
parent
38af8ce615
commit
bf4e453cdb
@ -38,7 +38,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type players struct {
|
type players struct {
|
||||||
players map[*Player]struct{}
|
players map[*Player]struct{}
|
||||||
|
seekings map[*Player]struct{}
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,18 +70,24 @@ func (p *players) Read(b []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
closed := []*Player{}
|
closed := []*Player{}
|
||||||
l := len(b)
|
l := len(b)
|
||||||
for p := range p.players {
|
for player := range p.players {
|
||||||
if err := p.readToBuffer(l); err == io.EOF {
|
if _, ok := p.seekings[player]; ok {
|
||||||
closed = append(closed, p)
|
continue
|
||||||
|
}
|
||||||
|
if err := player.readToBuffer(l); err == io.EOF {
|
||||||
|
closed = append(closed, player)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
l = min(p.bufferLength(), l)
|
l = min(player.bufferLength(), l)
|
||||||
}
|
}
|
||||||
l &= mask
|
l &= mask
|
||||||
b16s := [][]int16{}
|
b16s := [][]int16{}
|
||||||
for p := range p.players {
|
for player := range p.players {
|
||||||
b16s = append(b16s, p.bufferToInt16(l))
|
if _, ok := p.seekings[player]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
b16s = append(b16s, player.bufferToInt16(l))
|
||||||
}
|
}
|
||||||
for i := 0; i < l/2; i++ {
|
for i := 0; i < l/2; i++ {
|
||||||
x := 0
|
x := 0
|
||||||
@ -96,8 +103,11 @@ func (p *players) Read(b []byte) (int, error) {
|
|||||||
b[2*i] = byte(x)
|
b[2*i] = byte(x)
|
||||||
b[2*i+1] = byte(x >> 8)
|
b[2*i+1] = byte(x >> 8)
|
||||||
}
|
}
|
||||||
for p := range p.players {
|
for player := range p.players {
|
||||||
p.proceed(l)
|
if _, ok := p.seekings[player]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
player.proceed(l)
|
||||||
}
|
}
|
||||||
for _, pl := range closed {
|
for _, pl := range closed {
|
||||||
delete(p.players, pl)
|
delete(p.players, pl)
|
||||||
@ -117,6 +127,18 @@ func (p *players) removePlayer(player *Player) {
|
|||||||
delete(p.players, player)
|
delete(p.players, player)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *players) addSeeking(player *Player) {
|
||||||
|
p.Lock()
|
||||||
|
defer p.Unlock()
|
||||||
|
p.seekings[player] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *players) removeSeeking(player *Player) {
|
||||||
|
p.Lock()
|
||||||
|
defer p.Unlock()
|
||||||
|
delete(p.seekings, player)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *players) hasPlayer(player *Player) bool {
|
func (p *players) hasPlayer(player *Player) bool {
|
||||||
p.RLock()
|
p.RLock()
|
||||||
defer p.RUnlock()
|
defer p.RUnlock()
|
||||||
@ -190,7 +212,8 @@ func NewContext(sampleRate int) (*Context, error) {
|
|||||||
}
|
}
|
||||||
theContext = c
|
theContext = c
|
||||||
c.players = &players{
|
c.players = &players{
|
||||||
players: map[*Player]struct{}{},
|
players: map[*Player]struct{}{},
|
||||||
|
seekings: map[*Player]struct{}{},
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
|
|
||||||
@ -286,7 +309,7 @@ func NewPlayer(context *Context, src ReadSeekCloser) (*Player, error) {
|
|||||||
volume: 1,
|
volume: 1,
|
||||||
}
|
}
|
||||||
// Get the current position of the source.
|
// Get the current position of the source.
|
||||||
pos, err := p.src.Seek(0, 1)
|
pos, err := p.src.Seek(0, io.SeekCurrent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -380,6 +403,8 @@ func (p *Player) Rewind() error {
|
|||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func (p *Player) Seek(offset time.Duration) error {
|
func (p *Player) Seek(offset time.Duration) error {
|
||||||
|
p.players.addSeeking(p)
|
||||||
|
defer p.players.removeSeeking(p)
|
||||||
o := int64(offset) * bytesPerSample * channelNum * int64(p.sampleRate) / int64(time.Second)
|
o := int64(offset) * bytesPerSample * channelNum * int64(p.sampleRate) / int64(time.Second)
|
||||||
o &= mask
|
o &= mask
|
||||||
p.buf = []byte{}
|
p.buf = []byte{}
|
||||||
|
Loading…
Reference in New Issue
Block a user