2017-01-14 17:42:25 +01:00
|
|
|
// Copyright 2017 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-15 14:37:59 +01:00
|
|
|
package convert
|
2017-01-14 17:42:25 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
|
|
|
)
|
|
|
|
|
2018-01-05 11:40:12 +01:00
|
|
|
var cosTable = [65536]float64{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for i := range cosTable {
|
|
|
|
cosTable[i] = math.Cos(float64(i) * math.Pi / 2 / float64(len(cosTable)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fastCos(x float64) float64 {
|
|
|
|
if x < 0 {
|
|
|
|
x = -x
|
|
|
|
}
|
|
|
|
x /= 2 * math.Pi
|
|
|
|
if 1 < x {
|
|
|
|
_, x = math.Modf(x)
|
|
|
|
}
|
|
|
|
sign := 1
|
|
|
|
switch {
|
|
|
|
case x < 0.25:
|
|
|
|
case x < 0.5:
|
|
|
|
x = 0.5 - x
|
|
|
|
sign = -1
|
|
|
|
case x < 0.75:
|
|
|
|
x -= 0.5
|
|
|
|
sign = -1
|
|
|
|
default:
|
|
|
|
x = 1 - x
|
|
|
|
}
|
|
|
|
idx := int(4 * x * float64(len(cosTable)))
|
|
|
|
if idx == len(cosTable) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return float64(sign) * cosTable[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
func fastSin(x float64) float64 {
|
|
|
|
return fastCos(x - math.Pi/2)
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:42:25 +01:00
|
|
|
func sinc(x float64) float64 {
|
2018-01-05 09:39:47 +01:00
|
|
|
if math.Abs(x) < 1e-8 {
|
2017-01-14 17:42:25 +01:00
|
|
|
return 1
|
|
|
|
}
|
2018-01-05 11:40:12 +01:00
|
|
|
return fastSin(x) / x
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 14:37:59 +01:00
|
|
|
type Resampling struct {
|
2017-12-27 10:05:55 +01:00
|
|
|
source audio.ReadSeekCloser
|
|
|
|
size int64
|
|
|
|
from int
|
|
|
|
to int
|
|
|
|
pos int64
|
|
|
|
srcBlock int64
|
|
|
|
srcBufL map[int64][]float64
|
|
|
|
srcBufR map[int64][]float64
|
|
|
|
lruSrcBlocks []int64
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 14:37:59 +01:00
|
|
|
func NewResampling(source audio.ReadSeekCloser, size int64, from, to int) *Resampling {
|
|
|
|
r := &Resampling{
|
2017-06-25 19:18:11 +02:00
|
|
|
source: source,
|
|
|
|
size: size,
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
srcBlock: -1,
|
|
|
|
srcBufL: map[int64][]float64{},
|
|
|
|
srcBufR: map[int64][]float64{},
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
return r
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 14:37:59 +01:00
|
|
|
func (r *Resampling) Size() int64 {
|
2017-07-11 17:51:36 +02:00
|
|
|
s := int64(float64(r.size) * float64(r.to) / float64(r.from))
|
|
|
|
return s / 4 * 4
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 14:37:59 +01:00
|
|
|
func (r *Resampling) src(i int) (float64, float64, error) {
|
2018-01-05 11:40:12 +01:00
|
|
|
const resamplingBufferSize = 65536
|
|
|
|
|
2017-01-14 17:42:25 +01:00
|
|
|
// Use int here since int64 is very slow on browsers.
|
|
|
|
// TODO: Resampling is too heavy on browsers. How about using OfflineAudioContext?
|
|
|
|
if i < 0 {
|
|
|
|
return 0, 0, nil
|
|
|
|
}
|
2017-07-09 08:29:56 +02:00
|
|
|
if r.size/4 <= int64(i) {
|
2017-01-14 17:42:25 +01:00
|
|
|
return 0, 0, nil
|
|
|
|
}
|
2017-06-25 19:18:11 +02:00
|
|
|
nextPos := int64(i) / resamplingBufferSize
|
|
|
|
if _, ok := r.srcBufL[nextPos]; !ok {
|
|
|
|
if r.srcBlock+1 != nextPos {
|
|
|
|
if _, err := r.source.Seek(nextPos*resamplingBufferSize*4, io.SeekStart); err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf := make([]uint8, resamplingBufferSize*4)
|
|
|
|
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
|
|
|
|
}
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-06-25 19:18:11 +02:00
|
|
|
buf = buf[:c]
|
|
|
|
sl := make([]float64, resamplingBufferSize)
|
|
|
|
sr := make([]float64, resamplingBufferSize)
|
2017-01-14 17:42:25 +01:00
|
|
|
for i := 0; i < len(buf)/4; i++ {
|
2017-06-25 19:18:11 +02:00
|
|
|
sl[i] = float64(int16(buf[4*i])|(int16(buf[4*i+1])<<8)) / (1<<15 - 1)
|
|
|
|
sr[i] = float64(int16(buf[4*i+2])|(int16(buf[4*i+3])<<8)) / (1<<15 - 1)
|
|
|
|
}
|
|
|
|
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.
|
2017-12-27 10:05:55 +01:00
|
|
|
if len(r.lruSrcBlocks) >= 4 {
|
|
|
|
p := r.lruSrcBlocks[0]
|
2017-06-25 19:18:11 +02:00
|
|
|
delete(r.srcBufL, p)
|
|
|
|
delete(r.srcBufR, p)
|
2017-12-27 10:05:55 +01:00
|
|
|
r.lruSrcBlocks = r.lruSrcBlocks[1:]
|
2017-06-25 19:18:11 +02:00
|
|
|
}
|
2017-12-27 10:05:55 +01:00
|
|
|
r.lruSrcBlocks = append(r.lruSrcBlocks, r.srcBlock)
|
2017-06-25 19:18:11 +02:00
|
|
|
} else {
|
|
|
|
r.srcBlock = nextPos
|
|
|
|
idx := -1
|
2017-12-27 10:05:55 +01:00
|
|
|
for i, p := range r.lruSrcBlocks {
|
2017-06-25 19:18:11 +02:00
|
|
|
if p == r.srcBlock {
|
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx == -1 {
|
|
|
|
panic("not reach")
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-12-27 10:05:55 +01:00
|
|
|
r.lruSrcBlocks = append(r.lruSrcBlocks[:idx], r.lruSrcBlocks[idx+1:]...)
|
|
|
|
r.lruSrcBlocks = append(r.lruSrcBlocks, r.srcBlock)
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-06-25 19:18:11 +02:00
|
|
|
ii := i % resamplingBufferSize
|
|
|
|
return r.srcBufL[r.srcBlock][ii], r.srcBufR[r.srcBlock][ii], nil
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 14:37:59 +01:00
|
|
|
func (r *Resampling) at(t int64) (float64, float64, error) {
|
2018-01-05 11:40:12 +01:00
|
|
|
windowSize := 8.0
|
2017-01-15 14:37:59 +01:00
|
|
|
tInSrc := float64(t) * float64(r.from) / float64(r.to)
|
2018-01-05 09:39:47 +01:00
|
|
|
startN := int64(tInSrc - windowSize)
|
2017-01-14 17:42:25 +01:00
|
|
|
if startN < 0 {
|
|
|
|
startN = 0
|
|
|
|
}
|
2018-01-05 09:39:47 +01:00
|
|
|
if r.size/4 <= startN {
|
|
|
|
startN = r.size/4 - 1
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2018-01-05 09:39:47 +01:00
|
|
|
endN := int64(tInSrc + windowSize)
|
|
|
|
if r.size/4 <= endN {
|
|
|
|
endN = r.size/4 - 1
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
lv := 0.0
|
|
|
|
rv := 0.0
|
2018-01-05 09:39:47 +01:00
|
|
|
for n := startN; n <= endN; n++ {
|
2017-01-15 14:37:59 +01:00
|
|
|
srcL, srcR, err := r.src(int(n))
|
2017-01-14 17:42:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
2018-01-05 09:39:47 +01:00
|
|
|
d := tInSrc - float64(n)
|
2018-01-05 11:40:12 +01:00
|
|
|
w := 0.5 + 0.5*fastCos(2*math.Pi*d/(windowSize*2+1))
|
2018-01-05 09:39:47 +01:00
|
|
|
s := sinc(math.Pi*d) * w
|
2017-01-15 14:37:59 +01:00
|
|
|
lv += srcL * s
|
|
|
|
rv += srcR * s
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
if lv < -1 {
|
|
|
|
lv = -1
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
if lv > 1 {
|
|
|
|
lv = 1
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
if rv < -1 {
|
|
|
|
rv = -1
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
if rv > 1 {
|
|
|
|
rv = 1
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
return lv, rv, nil
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 14:37:59 +01:00
|
|
|
func (r *Resampling) Read(b []uint8) (int, error) {
|
|
|
|
if r.pos == r.Size() {
|
2017-01-14 17:42:25 +01:00
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n := len(b) / 4 * 4
|
2017-01-15 14:37:59 +01:00
|
|
|
if r.Size()-r.pos <= int64(n) {
|
|
|
|
n = int(r.Size() - r.pos)
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
for i := 0; i < n/4; i++ {
|
2017-01-15 14:37:59 +01:00
|
|
|
l, r, err := r.at(r.pos/4 + int64(i))
|
2017-01-14 17:42:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
l16 := int16(l * (1<<15 - 1))
|
|
|
|
r16 := int16(r * (1<<15 - 1))
|
|
|
|
b[4*i] = uint8(l16)
|
|
|
|
b[4*i+1] = uint8(l16 >> 8)
|
|
|
|
b[4*i+2] = uint8(r16)
|
|
|
|
b[4*i+3] = uint8(r16 >> 8)
|
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
r.pos += int64(n)
|
2017-01-14 17:42:25 +01:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2017-01-15 14:37:59 +01:00
|
|
|
func (r *Resampling) Seek(offset int64, whence int) (int64, error) {
|
2017-01-14 17:42:25 +01:00
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
2017-01-15 14:37:59 +01:00
|
|
|
r.pos = offset
|
2017-01-14 17:42:25 +01:00
|
|
|
case io.SeekCurrent:
|
2017-01-15 14:37:59 +01:00
|
|
|
r.pos += offset
|
2017-01-14 17:42:25 +01:00
|
|
|
case io.SeekEnd:
|
2017-01-15 14:37:59 +01:00
|
|
|
r.pos += r.Size() + offset
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
if r.pos < 0 {
|
|
|
|
r.pos = 0
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
if r.Size() <= r.pos {
|
|
|
|
r.pos = r.Size()
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-01-15 14:37:59 +01:00
|
|
|
return r.pos, nil
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 14:37:59 +01:00
|
|
|
func (r *Resampling) Close() error {
|
|
|
|
return r.source.Close()
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|