mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 19:22:49 +01:00
parent
fbd067c96e
commit
d2c58dac8c
@ -27,8 +27,9 @@ import (
|
|||||||
|
|
||||||
// Stream is a decoded audio stream.
|
// Stream is a decoded audio stream.
|
||||||
type Stream struct {
|
type Stream struct {
|
||||||
decoded io.ReadSeeker
|
decoded io.ReadSeeker
|
||||||
size int64
|
size int64
|
||||||
|
sampleRate int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read is implementation of io.Reader's Read.
|
// Read is implementation of io.Reader's Read.
|
||||||
@ -50,6 +51,11 @@ func (s *Stream) Length() int64 {
|
|||||||
return s.size
|
return s.size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SampleRate returns the sample rate of the decoded stream.
|
||||||
|
func (s *Stream) SampleRate() int {
|
||||||
|
return s.sampleRate
|
||||||
|
}
|
||||||
|
|
||||||
type decoder interface {
|
type decoder interface {
|
||||||
Read([]float32) (int, error)
|
Read([]float32) (int, error)
|
||||||
SetPosition(int64) error
|
SetPosition(int64) error
|
||||||
@ -152,7 +158,7 @@ func decode(in io.Reader) (*decoded, int, int, error) {
|
|||||||
// A Stream doesn't close src even if src implements io.Closer.
|
// A Stream doesn't close src even if src implements io.Closer.
|
||||||
// Closing the source is src owner's responsibility.
|
// Closing the source is src owner's responsibility.
|
||||||
func DecodeWithoutResampling(src io.Reader) (*Stream, error) {
|
func DecodeWithoutResampling(src io.Reader) (*Stream, error) {
|
||||||
decoded, channelCount, _, err := decode(src)
|
decoded, channelCount, sampleRate, err := decode(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -166,8 +172,9 @@ func DecodeWithoutResampling(src io.Reader) (*Stream, error) {
|
|||||||
size *= 2
|
size *= 2
|
||||||
}
|
}
|
||||||
stream := &Stream{
|
stream := &Stream{
|
||||||
decoded: s,
|
decoded: s,
|
||||||
size: size,
|
size: size,
|
||||||
|
sampleRate: sampleRate,
|
||||||
}
|
}
|
||||||
return stream, nil
|
return stream, nil
|
||||||
}
|
}
|
||||||
@ -204,7 +211,11 @@ func DecodeWithSampleRate(sampleRate int, src io.Reader) (*Stream, error) {
|
|||||||
s = r
|
s = r
|
||||||
size = r.Length()
|
size = r.Length()
|
||||||
}
|
}
|
||||||
stream := &Stream{decoded: s, size: size}
|
stream := &Stream{
|
||||||
|
decoded: s,
|
||||||
|
size: size,
|
||||||
|
sampleRate: sampleRate,
|
||||||
|
}
|
||||||
return stream, nil
|
return stream, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,16 +50,16 @@ func TestMono(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Stream decoded by audio/vorbis.DecodeWithSampleRate() is always 16bit stereo.
|
// Stream decoded by audio/vorbis.DecodeWithSampleRate() is always 16bit stereo.
|
||||||
got := s.Length()
|
|
||||||
|
|
||||||
// On the other hand, the original vorbis package is monoral.
|
// On the other hand, the original vorbis package is monoral.
|
||||||
// As Length() represents the number of samples,
|
// As Length() represents the number of samples,
|
||||||
// this needs to be doubled by 2 (= bytes in 16bits).
|
// this needs to be doubled by 2 (= bytes in 16bits).
|
||||||
want := r.Length() * 2 * 2
|
if got, want := s.Length(), r.Length()*2*2; got != want {
|
||||||
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("s.Length(): got: %d, want: %d", got, want)
|
t.Errorf("s.Length(): got: %d, want: %d", got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if got, want := s.SampleRate(), audioContext.SampleRate(); got != want {
|
||||||
|
t.Errorf("s.SampleRate(): got: %d, want: %d", got, want)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTooShort(t *testing.T) {
|
func TestTooShort(t *testing.T) {
|
||||||
@ -70,11 +70,13 @@ func TestTooShort(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
got := s.Length()
|
if got, want := s.Length(), int64(79424); got != want {
|
||||||
want := int64(79424)
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("s.Length(): got: %d, want: %d", got, want)
|
t.Errorf("s.Length(): got: %d, want: %d", got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if got, want := s.SampleRate(), audioContext.SampleRate(); got != want {
|
||||||
|
t.Errorf("s.SampleRate(): got: %d, want: %d", got, want)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type reader struct {
|
type reader struct {
|
||||||
@ -93,9 +95,11 @@ func TestNonSeeker(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
got := s.Length()
|
if got, want := s.Length(), int64(0); got != want {
|
||||||
want := int64(0)
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("s.Length(): got: %d, want: %d", got, want)
|
t.Errorf("s.Length(): got: %d, want: %d", got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if got, want := s.SampleRate(), audioContext.SampleRate(); got != want {
|
||||||
|
t.Errorf("s.SampleRate(): got: %d, want: %d", got, want)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user