mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
audio: refactoring
This commit is contained in:
parent
061d8617ce
commit
b9f0db955c
@ -70,7 +70,7 @@ type Context struct {
|
||||
ready bool
|
||||
readyOnce sync.Once
|
||||
|
||||
players map[*playerImpl]struct{}
|
||||
playingPlayers map[*playerImpl]struct{}
|
||||
|
||||
m sync.Mutex
|
||||
semaphore chan struct{}
|
||||
@ -97,11 +97,11 @@ func NewContext(sampleRate int) *Context {
|
||||
}
|
||||
|
||||
c := &Context{
|
||||
sampleRate: sampleRate,
|
||||
playerFactory: newPlayerFactory(sampleRate),
|
||||
players: map[*playerImpl]struct{}{},
|
||||
inited: make(chan struct{}),
|
||||
semaphore: make(chan struct{}, 1),
|
||||
sampleRate: sampleRate,
|
||||
playerFactory: newPlayerFactory(sampleRate),
|
||||
playingPlayers: map[*playerImpl]struct{}{},
|
||||
inited: make(chan struct{}),
|
||||
semaphore: make(chan struct{}, 1),
|
||||
}
|
||||
theContext = c
|
||||
|
||||
@ -175,14 +175,14 @@ func (c *Context) setReady() {
|
||||
c.m.Unlock()
|
||||
}
|
||||
|
||||
func (c *Context) addPlayer(p *playerImpl) {
|
||||
func (c *Context) addPlayingPlayer(p *playerImpl) {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.players[p] = struct{}{}
|
||||
c.playingPlayers[p] = struct{}{}
|
||||
|
||||
// Check the source duplication
|
||||
srcs := map[io.Reader]struct{}{}
|
||||
for p := range c.players {
|
||||
for p := range c.playingPlayers {
|
||||
if _, ok := srcs[p.source()]; ok {
|
||||
c.err = errors.New("audio: a same source is used by multiple Player")
|
||||
return
|
||||
@ -191,9 +191,9 @@ func (c *Context) addPlayer(p *playerImpl) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Context) removePlayer(p *playerImpl) {
|
||||
func (c *Context) removePlayingPlayer(p *playerImpl) {
|
||||
c.m.Lock()
|
||||
delete(c.players, p)
|
||||
delete(c.playingPlayers, p)
|
||||
c.m.Unlock()
|
||||
}
|
||||
|
||||
@ -202,8 +202,8 @@ func (c *Context) gcPlayers() error {
|
||||
// Copy the playerImpls and iterate them without a lock.
|
||||
var players []*playerImpl
|
||||
c.m.Lock()
|
||||
players = make([]*playerImpl, 0, len(c.players))
|
||||
for p := range c.players {
|
||||
players = make([]*playerImpl, 0, len(c.playingPlayers))
|
||||
for p := range c.playingPlayers {
|
||||
players = append(players, p)
|
||||
}
|
||||
c.m.Unlock()
|
||||
@ -225,7 +225,7 @@ func (c *Context) gcPlayers() error {
|
||||
|
||||
c.m.Lock()
|
||||
for _, p := range playersToRemove {
|
||||
delete(c.players, p)
|
||||
delete(c.playingPlayers, p)
|
||||
}
|
||||
c.m.Unlock()
|
||||
|
||||
@ -242,7 +242,7 @@ func (c *Context) IsReady() bool {
|
||||
if c.ready {
|
||||
return true
|
||||
}
|
||||
if len(c.players) != 0 {
|
||||
if len(c.playingPlayers) != 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -142,7 +142,7 @@ func UpdateForTesting() error {
|
||||
func PlayersCountForTesting() int {
|
||||
c := CurrentContext()
|
||||
c.m.Lock()
|
||||
n := len(c.players)
|
||||
n := len(c.playingPlayers)
|
||||
c.m.Unlock()
|
||||
return n
|
||||
}
|
||||
|
@ -177,7 +177,7 @@ func (p *playerImpl) Play() {
|
||||
return
|
||||
}
|
||||
p.player.Play()
|
||||
p.context.addPlayer(p)
|
||||
p.context.addPlayingPlayer(p)
|
||||
}
|
||||
|
||||
func (p *playerImpl) Pause() {
|
||||
@ -192,7 +192,7 @@ func (p *playerImpl) Pause() {
|
||||
}
|
||||
|
||||
p.player.Pause()
|
||||
p.context.removePlayer(p)
|
||||
p.context.removePlayingPlayer(p)
|
||||
}
|
||||
|
||||
func (p *playerImpl) IsPlaying() bool {
|
||||
@ -250,7 +250,7 @@ func (p *playerImpl) Position() time.Duration {
|
||||
return 0
|
||||
}
|
||||
|
||||
samples := (p.stream.Current() - int64(p.player.BufferedSize())) / bytesPerSampleInt16
|
||||
samples := (p.stream.position() - int64(p.player.BufferedSize())) / bytesPerSampleInt16
|
||||
return time.Duration(samples) * time.Second / time.Duration(p.factory.sampleRate)
|
||||
}
|
||||
|
||||
@ -366,7 +366,7 @@ func (s *timeStream) timeDurationToPos(offset time.Duration) int64 {
|
||||
return o
|
||||
}
|
||||
|
||||
func (s *timeStream) Current() int64 {
|
||||
func (s *timeStream) position() int64 {
|
||||
s.m.Lock()
|
||||
defer s.m.Unlock()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user