mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
audio/internal/convert: Optimization for seeking
This commit is contained in:
parent
3be2b2516f
commit
e3176f6693
@ -29,25 +29,29 @@ func sinc(x float64) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Resampling struct {
|
type Resampling struct {
|
||||||
source audio.ReadSeekCloser
|
source audio.ReadSeekCloser
|
||||||
size int64
|
size int64
|
||||||
from int
|
from int
|
||||||
to int
|
to int
|
||||||
pos int64
|
pos int64
|
||||||
srcPos int64
|
srcBlock int64
|
||||||
srcCacheL []float64
|
srcBufL map[int64][]float64
|
||||||
srcCacheR []float64
|
srcBufR map[int64][]float64
|
||||||
|
lruBlock []int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const resamplingBufferSize = 4096
|
||||||
|
|
||||||
func NewResampling(source audio.ReadSeekCloser, size int64, from, to int) *Resampling {
|
func NewResampling(source audio.ReadSeekCloser, size int64, from, to int) *Resampling {
|
||||||
r := &Resampling{
|
r := &Resampling{
|
||||||
source: source,
|
source: source,
|
||||||
size: size,
|
size: size,
|
||||||
from: from,
|
from: from,
|
||||||
to: to,
|
to: to,
|
||||||
|
srcBlock: -1,
|
||||||
|
srcBufL: map[int64][]float64{},
|
||||||
|
srcBufR: map[int64][]float64{},
|
||||||
}
|
}
|
||||||
r.srcCacheL = make([]float64, r.size/4)
|
|
||||||
r.srcCacheR = make([]float64, r.size/4)
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,27 +65,63 @@ func (r *Resampling) src(i int) (float64, float64, error) {
|
|||||||
if i < 0 {
|
if i < 0 {
|
||||||
return 0, 0, nil
|
return 0, 0, nil
|
||||||
}
|
}
|
||||||
if len(r.srcCacheL) <= i {
|
if r.Size()/4 <= int64(i) {
|
||||||
return 0, 0, nil
|
return 0, 0, nil
|
||||||
}
|
}
|
||||||
pos := int(r.srcPos) / 4
|
nextPos := int64(i) / resamplingBufferSize
|
||||||
if pos <= i {
|
if _, ok := r.srcBufL[nextPos]; !ok {
|
||||||
buf := make([]uint8, 4096)
|
if r.srcBlock+1 != nextPos {
|
||||||
n, err := r.source.Read(buf)
|
if _, err := r.source.Seek(nextPos*resamplingBufferSize*4, io.SeekStart); err != nil {
|
||||||
if err != nil && err != io.EOF {
|
return 0, 0, err
|
||||||
return 0, 0, err
|
}
|
||||||
}
|
}
|
||||||
n = n / 4 * 4
|
buf := make([]uint8, resamplingBufferSize*4)
|
||||||
buf = buf[:n]
|
c := 0
|
||||||
|
for c < len(buf) {
|
||||||
|
n, err := r.source.Read(buf[c:])
|
||||||
|
c += n
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf = buf[:c]
|
||||||
|
sl := make([]float64, resamplingBufferSize)
|
||||||
|
sr := make([]float64, resamplingBufferSize)
|
||||||
for i := 0; i < len(buf)/4; i++ {
|
for i := 0; i < len(buf)/4; i++ {
|
||||||
srcL := float64(int16(buf[4*i])|(int16(buf[4*i+1])<<8)) / (1<<15 - 1)
|
sl[i] = float64(int16(buf[4*i])|(int16(buf[4*i+1])<<8)) / (1<<15 - 1)
|
||||||
srcR := float64(int16(buf[4*i+2])|(int16(buf[4*i+3])<<8)) / (1<<15 - 1)
|
sr[i] = float64(int16(buf[4*i+2])|(int16(buf[4*i+3])<<8)) / (1<<15 - 1)
|
||||||
r.srcCacheL[pos+i] = srcL
|
|
||||||
r.srcCacheR[pos+i] = srcR
|
|
||||||
}
|
}
|
||||||
r.srcPos += int64(n)
|
r.srcBlock = nextPos
|
||||||
|
r.srcBufL[r.srcBlock] = sl
|
||||||
|
r.srcBufR[r.srcBlock] = sr
|
||||||
|
// To keep srcBufL/R not too big, let's remove the least used buffers.
|
||||||
|
if len(r.lruBlock) >= 4 {
|
||||||
|
p := r.lruBlock[0]
|
||||||
|
delete(r.srcBufL, p)
|
||||||
|
delete(r.srcBufR, p)
|
||||||
|
r.lruBlock = r.lruBlock[1:]
|
||||||
|
}
|
||||||
|
r.lruBlock = append(r.lruBlock, r.srcBlock)
|
||||||
|
} else {
|
||||||
|
r.srcBlock = nextPos
|
||||||
|
idx := -1
|
||||||
|
for i, p := range r.lruBlock {
|
||||||
|
if p == r.srcBlock {
|
||||||
|
idx = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if idx == -1 {
|
||||||
|
panic("not reach")
|
||||||
|
}
|
||||||
|
r.lruBlock = append(r.lruBlock[:idx], r.lruBlock[idx+1:]...)
|
||||||
|
r.lruBlock = append(r.lruBlock, r.srcBlock)
|
||||||
}
|
}
|
||||||
return r.srcCacheL[i], r.srcCacheR[i], nil
|
ii := i % resamplingBufferSize
|
||||||
|
return r.srcBufL[r.srcBlock][ii], r.srcBufR[r.srcBlock][ii], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resampling) at(t int64) (float64, float64, error) {
|
func (r *Resampling) at(t int64) (float64, float64, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user