audio: Refactoring

This commit is contained in:
Hajime Hoshi 2017-12-23 04:01:18 +09:00
parent a5f8c1b1f8
commit a21571fb57

View File

@ -65,11 +65,7 @@ func (p *players) Read(b []byte) (int, error) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()
players := []*Player{} if len(p.players) == 0 {
for player := range p.players {
players = append(players, player)
}
if len(players) == 0 {
l := len(b) l := len(b)
l &= mask l &= mask
copy(b, make([]byte, l)) copy(b, make([]byte, l))
@ -77,7 +73,7 @@ func (p *players) Read(b []byte) (int, error) {
} }
l := len(b) l := len(b)
for _, player := range players { for player := range p.players {
select { select {
case err := <-player.readCh: case err := <-player.readCh:
if err != nil { if err != nil {
@ -89,7 +85,7 @@ func (p *players) Read(b []byte) (int, error) {
l &= mask l &= mask
b16s := [][]int16{} b16s := [][]int16{}
for _, player := range players { for player := range p.players {
b16s = append(b16s, player.bufferToInt16(l)) b16s = append(b16s, player.bufferToInt16(l))
} }
for i := 0; i < l/2; i++ { for i := 0; i < l/2; i++ {
@ -107,11 +103,15 @@ func (p *players) Read(b []byte) (int, error) {
b[2*i+1] = byte(x >> 8) b[2*i+1] = byte(x >> 8)
} }
for _, player := range players { closed := []*Player{}
for player := range p.players {
if player.eof() { if player.eof() {
delete(p.players, player) closed = append(closed, player)
} }
} }
for _, player := range closed {
delete(p.players, player)
}
return l, nil return l, nil
} }