Rename audio/internal/resampling -> audio/internal/convert

This commit is contained in:
Hajime Hoshi 2017-01-15 22:37:59 +09:00
parent adde2f459e
commit 5bdcd8825c
3 changed files with 64 additions and 68 deletions

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package resampling package convert
import ( import (
"io" "io"
@ -28,7 +28,7 @@ func sinc(x float64) float64 {
return math.Sin(x) / x return math.Sin(x) / x
} }
type Stream struct { type Resampling struct {
source audio.ReadSeekCloser source audio.ReadSeekCloser
size int64 size int64
from int from int
@ -39,35 +39,35 @@ type Stream struct {
srcCacheR []float64 srcCacheR []float64
} }
func NewStream(source audio.ReadSeekCloser, size int64, from, to int) *Stream { func NewResampling(source audio.ReadSeekCloser, size int64, from, to int) *Resampling {
s := &Stream{ r := &Resampling{
source: source, source: source,
size: size, size: size,
from: from, from: from,
to: to, to: to,
} }
s.srcCacheL = make([]float64, s.size/4) r.srcCacheL = make([]float64, r.size/4)
s.srcCacheR = make([]float64, s.size/4) r.srcCacheR = make([]float64, r.size/4)
return s return r
} }
func (s *Stream) Size() int64 { func (r *Resampling) Size() int64 {
return int64(float64(s.size) * float64(s.to) / float64(s.from)) return int64(float64(r.size) * float64(r.to) / float64(r.from))
} }
func (s *Stream) src(i int) (float64, float64, error) { func (r *Resampling) src(i int) (float64, float64, error) {
// Use int here since int64 is very slow on browsers. // Use int here since int64 is very slow on browsers.
// TODO: Resampling is too heavy on browsers. How about using OfflineAudioContext? // TODO: Resampling is too heavy on browsers. How about using OfflineAudioContext?
if i < 0 { if i < 0 {
return 0, 0, nil return 0, 0, nil
} }
if len(s.srcCacheL) <= i { if len(r.srcCacheL) <= i {
return 0, 0, nil return 0, 0, nil
} }
pos := int(s.srcPos) / 4 pos := int(r.srcPos) / 4
if pos <= i { if pos <= i {
buf := make([]uint8, 4096) buf := make([]uint8, 4096)
n, err := s.source.Read(buf) n, err := r.source.Read(buf)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
return 0, 0, err return 0, 0, err
} }
@ -76,65 +76,65 @@ func (s *Stream) src(i int) (float64, float64, error) {
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) srcL := 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) srcR := float64(int16(buf[4*i+2])|(int16(buf[4*i+3])<<8)) / (1<<15 - 1)
s.srcCacheL[pos+i] = srcL r.srcCacheL[pos+i] = srcL
s.srcCacheR[pos+i] = srcR r.srcCacheR[pos+i] = srcR
} }
s.srcPos += int64(n) r.srcPos += int64(n)
} }
return s.srcCacheL[i], s.srcCacheR[i], nil return r.srcCacheL[i], r.srcCacheR[i], nil
} }
func (s *Stream) at(t int64) (float64, float64, error) { func (r *Resampling) at(t int64) (float64, float64, error) {
const windowSize = 8 const windowSize = 8
tInSrc := float64(t) * float64(s.from) / float64(s.to) tInSrc := float64(t) * float64(r.from) / float64(r.to)
startN := int64(tInSrc) - windowSize startN := int64(tInSrc) - windowSize
if startN < 0 { if startN < 0 {
startN = 0 startN = 0
} }
if s.size/4 < startN { if r.size/4 < startN {
startN = s.size / 4 startN = r.size / 4
} }
endN := int64(tInSrc) + windowSize + 1 endN := int64(tInSrc) + windowSize + 1
if s.size/4 < endN { if r.size/4 < endN {
endN = s.size / 4 endN = r.size / 4
} }
l := 0.0 lv := 0.0
r := 0.0 rv := 0.0
for n := startN; n < endN; n++ { for n := startN; n < endN; n++ {
srcL, srcR, err := s.src(int(n)) srcL, srcR, err := r.src(int(n))
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
w := 0.5 + 0.5*math.Cos(2*math.Pi*(tInSrc-float64(n))/(windowSize*2+1)) w := 0.5 + 0.5*math.Cos(2*math.Pi*(tInSrc-float64(n))/(windowSize*2+1))
s := sinc(math.Pi*(tInSrc-float64(n))) * w s := sinc(math.Pi*(tInSrc-float64(n))) * w
l += srcL * s lv += srcL * s
r += srcR * s rv += srcR * s
} }
if l < -1 { if lv < -1 {
l = -1 lv = -1
} }
if l > 1 { if lv > 1 {
l = 1 lv = 1
} }
if r < -1 { if rv < -1 {
r = -1 rv = -1
} }
if r > 1 { if rv > 1 {
r = 1 rv = 1
} }
return l, r, nil return lv, rv, nil
} }
func (s *Stream) Read(b []uint8) (int, error) { func (r *Resampling) Read(b []uint8) (int, error) {
if s.pos == s.Size() { if r.pos == r.Size() {
return 0, io.EOF return 0, io.EOF
} }
n := len(b) / 4 * 4 n := len(b) / 4 * 4
if s.Size()-s.pos <= int64(n) { if r.Size()-r.pos <= int64(n) {
n = int(s.Size() - s.pos) n = int(r.Size() - r.pos)
} }
for i := 0; i < n/4; i++ { for i := 0; i < n/4; i++ {
l, r, err := s.at(s.pos/4 + int64(i)) l, r, err := r.at(r.pos/4 + int64(i))
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -145,28 +145,28 @@ func (s *Stream) Read(b []uint8) (int, error) {
b[4*i+2] = uint8(r16) b[4*i+2] = uint8(r16)
b[4*i+3] = uint8(r16 >> 8) b[4*i+3] = uint8(r16 >> 8)
} }
s.pos += int64(n) r.pos += int64(n)
return n, nil return n, nil
} }
func (s *Stream) Seek(offset int64, whence int) (int64, error) { func (r *Resampling) Seek(offset int64, whence int) (int64, error) {
switch whence { switch whence {
case io.SeekStart: case io.SeekStart:
s.pos = offset r.pos = offset
case io.SeekCurrent: case io.SeekCurrent:
s.pos += offset r.pos += offset
case io.SeekEnd: case io.SeekEnd:
s.pos += s.Size() + offset r.pos += r.Size() + offset
} }
if s.pos < 0 { if r.pos < 0 {
s.pos = 0 r.pos = 0
} }
if s.Size() <= s.pos { if r.Size() <= r.pos {
s.pos = s.Size() r.pos = r.Size()
} }
return s.pos, nil return r.pos, nil
} }
func (s *Stream) Close() error { func (r *Resampling) Close() error {
return s.source.Close() return r.source.Close()
} }

View File

@ -21,7 +21,7 @@ import (
"runtime" "runtime"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/internal/resampling" "github.com/hajimehoshi/ebiten/audio/internal/convert"
"github.com/jfreymuth/oggvorbis" "github.com/jfreymuth/oggvorbis"
) )
@ -189,7 +189,7 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
return nil, fmt.Errorf("vorbis: number of channels must be 2") return nil, fmt.Errorf("vorbis: number of channels must be 2")
} }
if sampleRate != context.SampleRate() { if sampleRate != context.SampleRate() {
s := resampling.NewStream(decoded, decoded.Size(), sampleRate, context.SampleRate()) s := convert.NewResampling(decoded, decoded.Size(), sampleRate, context.SampleRate())
return &Stream{s}, nil return &Stream{s}, nil
} }
return &Stream{decoded}, nil return &Stream{decoded}, nil

View File

@ -21,17 +21,13 @@ import (
"io" "io"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/internal/resampling" "github.com/hajimehoshi/ebiten/audio/internal/convert"
) )
type readSeekCloseSizer interface {
audio.ReadSeekCloser
Size() int64
}
// Stream is a decoded audio stream. // Stream is a decoded audio stream.
type Stream struct { type Stream struct {
inner readSeekCloseSizer inner audio.ReadSeekCloser
size int64
} }
// Read is implementation of io.Reader's Read. // Read is implementation of io.Reader's Read.
@ -53,7 +49,7 @@ func (s *Stream) Close() error {
// Size returns the size of decoded stream in bytes. // Size returns the size of decoded stream in bytes.
func (s *Stream) Size() int64 { func (s *Stream) Size() int64 {
return s.inner.Size() return s.size
} }
type stream struct { type stream struct {
@ -194,15 +190,15 @@ chunks:
headerSize += size headerSize += size
} }
} }
s := &stream{ var s audio.ReadSeekCloser
s = &stream{
src: src, src: src,
headerSize: headerSize, headerSize: headerSize,
dataSize: dataSize, dataSize: dataSize,
remaining: dataSize, remaining: dataSize,
} }
if sampleRateFrom != sampleRateTo { if sampleRateFrom != sampleRateTo {
fixed := resampling.NewStream(s, s.dataSize, sampleRateFrom, sampleRateTo) s = convert.NewResampling(s, dataSize, sampleRateFrom, sampleRateTo)
return &Stream{fixed}, nil
} }
return &Stream{s}, nil return &Stream{s, dataSize}, nil
} }