mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
audio: Refactoring
This commit is contained in:
parent
5ba5a1fb90
commit
d632a2d7a4
@ -51,24 +51,24 @@ func (s *mixedPlayersStream) Read(b []byte) (int, error) {
|
|||||||
return ll, nil
|
return ll, nil
|
||||||
}
|
}
|
||||||
closed := []*Player{}
|
closed := []*Player{}
|
||||||
bb := make([]byte, l)
|
|
||||||
ll := l
|
ll := l
|
||||||
for p := range s.context.players {
|
for p := range s.context.players {
|
||||||
n, err := p.src.Read(bb)
|
_, err := p.readToBuffer(l)
|
||||||
if 0 < n {
|
|
||||||
p.buf = append(p.buf, bb[:n]...)
|
|
||||||
}
|
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
closed = append(closed, p)
|
closed = append(closed, p)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
ll = min(len(p.buf)/4*4, ll)
|
ll = min(p.bufferLength()/4*4, ll)
|
||||||
|
}
|
||||||
|
b16s := [][]int16{}
|
||||||
|
for p := range s.context.players {
|
||||||
|
b16s = append(b16s, p.bufferToInt16(ll))
|
||||||
}
|
}
|
||||||
for i := 0; i < ll/2; i++ {
|
for i := 0; i < ll/2; i++ {
|
||||||
x := 0
|
x := 0
|
||||||
for p := range s.context.players {
|
for _, b16 := range b16s {
|
||||||
x += int(int16(p.buf[2*i]) | (int16(p.buf[2*i+1]) << 8))
|
x += int(b16[i])
|
||||||
}
|
}
|
||||||
if x > (1<<15)-1 {
|
if x > (1<<15)-1 {
|
||||||
x = (1 << 15) - 1
|
x = (1 << 15) - 1
|
||||||
@ -80,8 +80,7 @@ func (s *mixedPlayersStream) Read(b []byte) (int, error) {
|
|||||||
b[2*i+1] = byte(x >> 8)
|
b[2*i+1] = byte(x >> 8)
|
||||||
}
|
}
|
||||||
for p := range s.context.players {
|
for p := range s.context.players {
|
||||||
p.buf = p.buf[ll:]
|
p.proceed(ll)
|
||||||
p.pos += int64(ll)
|
|
||||||
}
|
}
|
||||||
for _, p := range closed {
|
for _, p := range closed {
|
||||||
delete(s.context.players, p)
|
delete(s.context.players, p)
|
||||||
@ -141,6 +140,32 @@ func (c *Context) NewPlayer(src io.ReadSeeker) (*Player, error) {
|
|||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Player) readToBuffer(length int) (int, error) {
|
||||||
|
bb := make([]byte, length)
|
||||||
|
n, err := p.src.Read(bb)
|
||||||
|
if 0 < n {
|
||||||
|
p.buf = append(p.buf, bb[:n]...)
|
||||||
|
}
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Player) bufferToInt16(lengthInBytes int) []int16 {
|
||||||
|
r := make([]int16, lengthInBytes/2)
|
||||||
|
for i := 0; i < lengthInBytes/2; i++ {
|
||||||
|
r[i] = int16(p.buf[2*i]) | (int16(p.buf[2*i+1]) << 8)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Player) proceed(length int) {
|
||||||
|
p.buf = p.buf[length:]
|
||||||
|
p.pos += int64(length)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Player) bufferLength() int {
|
||||||
|
return len(p.buf)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Player) Play() error {
|
func (p *Player) Play() error {
|
||||||
p.context.Lock()
|
p.context.Lock()
|
||||||
defer p.context.Unlock()
|
defer p.context.Unlock()
|
||||||
|
Loading…
Reference in New Issue
Block a user