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

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

View File

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