audio/internal/convert: refactoring: move utilities to audio/vorbis

This commit is contained in:
Hajime Hoshi 2024-07-07 16:59:45 +09:00
parent 343fd7084f
commit f8e3bef368
4 changed files with 46 additions and 27 deletions

View File

@ -0,0 +1,23 @@
// Copyright 2024 The Ebitengine 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.
package vorbis
import "io"
type Float32Reader = float32Reader
func NewInt16BytesReaderFromFloat32Reader(r float32Reader) io.Reader {
return newInt16BytesReaderFromFloat32Reader(r)
}

View File

@ -12,22 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package convert
package vorbis
import (
"io"
)
type Float32Reader interface {
type float32Reader interface {
Read([]float32) (int, error)
}
func NewReaderFromFloat32Reader(r Float32Reader) io.Reader {
return &f32Reader{r: r}
func newInt16BytesReaderFromFloat32Reader(r float32Reader) io.Reader {
return &int16BytesReader{r: r}
}
type f32Reader struct {
r Float32Reader
type int16BytesReader struct {
r float32Reader
eof bool
hasRemain bool
remain byte
@ -41,30 +41,30 @@ func max(a, b int) int {
return a
}
func (f *f32Reader) Read(buf []byte) (int, error) {
if f.eof {
func (r *int16BytesReader) Read(buf []byte) (int, error) {
if r.eof {
return 0, io.EOF
}
if len(buf) == 0 {
return 0, nil
}
if f.hasRemain {
buf[0] = f.remain
f.hasRemain = false
if r.hasRemain {
buf[0] = r.remain
r.hasRemain = false
return 1, nil
}
l := max(len(buf)/2, 1)
if cap(f.fbuf) < l {
f.fbuf = make([]float32, l)
if cap(r.fbuf) < l {
r.fbuf = make([]float32, l)
}
n, err := f.r.Read(f.fbuf[:l])
n, err := r.r.Read(r.fbuf[:l])
if err != nil && err != io.EOF {
return 0, err
}
if err == io.EOF {
f.eof = true
r.eof = true
}
b := buf
@ -72,7 +72,7 @@ func (f *f32Reader) Read(buf []byte) (int, error) {
b = make([]byte, 2)
}
for i := 0; i < n; i++ {
f := f.fbuf[i]
f := r.fbuf[i]
s := int16(f * (1<<15 - 1))
b[2*i] = byte(s)
b[2*i+1] = byte(s >> 8)
@ -80,8 +80,8 @@ func (f *f32Reader) Read(buf []byte) (int, error) {
if len(buf) == 1 && len(b) == 2 {
buf[0] = b[0]
f.remain = b[1]
f.hasRemain = true
r.remain = b[1]
r.hasRemain = true
return 1, err
}
return n * 2, err

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package convert_test
package vorbis_test
import (
"bytes"
@ -20,7 +20,7 @@ import (
"math"
"testing"
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
)
type f32reader struct {
@ -37,11 +37,7 @@ func (f *f32reader) Read(buf []float32) (int, error) {
return n, nil
}
func newFloat32Reader(data []float32) convert.Float32Reader {
return &f32reader{data: data}
}
func TestFloat32Reader(t *testing.T) {
func TestInt16BytesReader(t *testing.T) {
in1 := make([]float32, 256)
for i := range in1 {
in1[i] = float32(math.Sin(float64(i)))
@ -82,7 +78,7 @@ func TestFloat32Reader(t *testing.T) {
}
for i, c := range cases {
r := convert.NewReaderFromFloat32Reader(newFloat32Reader(c.In))
r := vorbis.NewInt16BytesReaderFromFloat32Reader(&f32reader{data: c.In})
got := []byte{}
for {

View File

@ -65,7 +65,7 @@ type i16Stream struct {
func (s *i16Stream) Read(b []byte) (int, error) {
if s.i16Reader == nil {
s.i16Reader = convert.NewReaderFromFloat32Reader(s.vorbisReader)
s.i16Reader = newInt16BytesReaderFromFloat32Reader(s.vorbisReader)
}
l := s.totalBytes - s.posInBytes