mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 19:28:57 +01:00
audio/internal/convert: Reuse a buffer cache
This commit is contained in:
parent
611cbad5d7
commit
630df60894
@ -22,6 +22,7 @@ type Stereo16 struct {
|
|||||||
source io.ReadSeeker
|
source io.ReadSeeker
|
||||||
mono bool
|
mono bool
|
||||||
eight bool
|
eight bool
|
||||||
|
buf []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStereo16(source io.ReadSeeker, mono, eight bool) *Stereo16 {
|
func NewStereo16(source io.ReadSeeker, mono, eight bool) *Stereo16 {
|
||||||
@ -40,15 +41,19 @@ func (s *Stereo16) Read(b []byte) (int, error) {
|
|||||||
if s.eight {
|
if s.eight {
|
||||||
l /= 2
|
l /= 2
|
||||||
}
|
}
|
||||||
buf := make([]byte, l)
|
|
||||||
n, err := s.source.Read(buf)
|
if cap(s.buf) < l {
|
||||||
|
s.buf = make([]byte, l)
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := s.source.Read(s.buf[:l])
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
switch {
|
switch {
|
||||||
case s.mono && s.eight:
|
case s.mono && s.eight:
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
v := int16(int(buf[i])*0x101 - (1 << 15))
|
v := int16(int(s.buf[i])*0x101 - (1 << 15))
|
||||||
b[4*i] = byte(v)
|
b[4*i] = byte(v)
|
||||||
b[4*i+1] = byte(v >> 8)
|
b[4*i+1] = byte(v >> 8)
|
||||||
b[4*i+2] = byte(v)
|
b[4*i+2] = byte(v)
|
||||||
@ -56,15 +61,15 @@ func (s *Stereo16) Read(b []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
case s.mono && !s.eight:
|
case s.mono && !s.eight:
|
||||||
for i := 0; i < n/2; i++ {
|
for i := 0; i < n/2; i++ {
|
||||||
b[4*i] = buf[2*i]
|
b[4*i] = s.buf[2*i]
|
||||||
b[4*i+1] = buf[2*i+1]
|
b[4*i+1] = s.buf[2*i+1]
|
||||||
b[4*i+2] = buf[2*i]
|
b[4*i+2] = s.buf[2*i]
|
||||||
b[4*i+3] = buf[2*i+1]
|
b[4*i+3] = s.buf[2*i+1]
|
||||||
}
|
}
|
||||||
case !s.mono && s.eight:
|
case !s.mono && s.eight:
|
||||||
for i := 0; i < n/2; i++ {
|
for i := 0; i < n/2; i++ {
|
||||||
v0 := int16(int(buf[2*i])*0x101 - (1 << 15))
|
v0 := int16(int(s.buf[2*i])*0x101 - (1 << 15))
|
||||||
v1 := int16(int(buf[2*i+1])*0x101 - (1 << 15))
|
v1 := int16(int(s.buf[2*i+1])*0x101 - (1 << 15))
|
||||||
b[4*i] = byte(v0)
|
b[4*i] = byte(v0)
|
||||||
b[4*i+1] = byte(v0 >> 8)
|
b[4*i+1] = byte(v0 >> 8)
|
||||||
b[4*i+2] = byte(v1)
|
b[4*i+2] = byte(v1)
|
||||||
|
Loading…
Reference in New Issue
Block a user