Skip to content

Commit

Permalink
audio: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Feb 1, 2024
1 parent 061d861 commit b9f0db9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
30 changes: 15 additions & 15 deletions audio/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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()
}

Expand All @@ -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()
Expand All @@ -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()

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion audio/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions audio/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (p *playerImpl) Play() {
return
}
p.player.Play()
p.context.addPlayer(p)
p.context.addPlayingPlayer(p)
}

func (p *playerImpl) Pause() {
Expand All @@ -192,7 +192,7 @@ func (p *playerImpl) Pause() {
}

p.player.Pause()
p.context.removePlayer(p)
p.context.removePlayingPlayer(p)
}

func (p *playerImpl) IsPlaying() bool {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit b9f0db9

Please sign in to comment.