mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 11:12:44 +01:00
parent
5b7ed246c0
commit
96ca0a7ff9
@ -137,16 +137,16 @@ func decode(in io.ReadSeeker) (*decoded, int, int, error) {
|
|||||||
return d, r.Channels(), r.SampleRate(), nil
|
return d, r.Channels(), r.SampleRate(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode decodes Ogg/Vorbis data to playable stream.
|
// DecodeWithSampleRate decodes Ogg/Vorbis data to playable stream.
|
||||||
//
|
//
|
||||||
// Decode returns error when decoding fails or IO error happens.
|
// DecodeWithSampleRate returns error when decoding fails or IO error happens.
|
||||||
//
|
//
|
||||||
// Decode automatically resamples the stream to fit with the audio context if necessary.
|
// DecodeWithSampleRate automatically resamples the stream to fit with the audio context if necessary.
|
||||||
//
|
//
|
||||||
// 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 Decode(context *audio.Context, src io.ReadSeeker) (*Stream, error) {
|
func DecodeWithSampleRate(sampleRate int, src io.ReadSeeker) (*Stream, error) {
|
||||||
decoded, channelNum, sampleRate, err := decode(src)
|
decoded, channelNum, origSampleRate, err := decode(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -159,11 +159,25 @@ func Decode(context *audio.Context, src io.ReadSeeker) (*Stream, error) {
|
|||||||
s = convert.NewStereo16(s, true, false)
|
s = convert.NewStereo16(s, true, false)
|
||||||
size *= 2
|
size *= 2
|
||||||
}
|
}
|
||||||
if sampleRate != context.SampleRate() {
|
if origSampleRate != sampleRate {
|
||||||
r := convert.NewResampling(s, size, sampleRate, context.SampleRate())
|
r := convert.NewResampling(s, size, sampleRate, sampleRate)
|
||||||
s = r
|
s = r
|
||||||
size = r.Length()
|
size = r.Length()
|
||||||
}
|
}
|
||||||
stream := &Stream{decoded: s, size: size}
|
stream := &Stream{decoded: s, size: size}
|
||||||
return stream, nil
|
return stream, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decode decodes Ogg/Vorbis data to playable stream.
|
||||||
|
//
|
||||||
|
// Decode returns error when decoding fails or IO error happens.
|
||||||
|
//
|
||||||
|
// Decode automatically resamples the stream to fit with the audio context if necessary.
|
||||||
|
//
|
||||||
|
// A Stream doesn't close src even if src implements io.Closer.
|
||||||
|
// Closing the source is src owner's responsibility.
|
||||||
|
//
|
||||||
|
// Deprecated: as of v2.1. Use DecodeWithSampleRate instead.
|
||||||
|
func Decode(context *audio.Context, src io.ReadSeeker) (*Stream, error) {
|
||||||
|
return DecodeWithSampleRate(context.SampleRate(), src)
|
||||||
|
}
|
||||||
|
@ -29,7 +29,7 @@ var audioContext = audio.NewContext(44100)
|
|||||||
func TestMono(t *testing.T) {
|
func TestMono(t *testing.T) {
|
||||||
bs := test_mono_ogg
|
bs := test_mono_ogg
|
||||||
|
|
||||||
s, err := Decode(audioContext, bytes.NewReader(bs))
|
s, err := DecodeWithSampleRate(audioContext.SampleRate(), bytes.NewReader(bs))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ func TestMono(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stream decoded by audio/vorbis.Decode() is always 16bit stereo.
|
// Stream decoded by audio/vorbis.DecodeWithSampleRate() is always 16bit stereo.
|
||||||
got := s.Length()
|
got := s.Length()
|
||||||
|
|
||||||
// On the other hand, the original vorbis package is monoral.
|
// On the other hand, the original vorbis package is monoral.
|
||||||
@ -55,7 +55,7 @@ func TestMono(t *testing.T) {
|
|||||||
func TestTooShort(t *testing.T) {
|
func TestTooShort(t *testing.T) {
|
||||||
bs := test_tooshort_ogg
|
bs := test_tooshort_ogg
|
||||||
|
|
||||||
s, err := Decode(audioContext, bytes.NewReader(bs))
|
s, err := DecodeWithSampleRate(audioContext.SampleRate(), bytes.NewReader(bs))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user