audio/vorbis: Add Stream interface

This commit is contained in:
Hajime Hoshi 2016-03-15 01:36:54 +09:00
parent ff2882799c
commit 79e1d1b1f7
3 changed files with 48 additions and 27 deletions

View File

@ -0,0 +1,44 @@
// Copyright 2016 Hajime Hoshi
//
// 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 (
"bytes"
"io"
"time"
)
type Stream interface {
io.ReadSeeker
Len() time.Duration
}
type stream struct {
buf *bytes.Reader
sampleRate int
}
func (s *stream) Read(p []byte) (int, error) {
return s.buf.Read(p)
}
func (s *stream) Seek(offset int64, whence int) (int64, error) {
return s.buf.Seek(offset, whence)
}
func (s *stream) Len() time.Duration {
const bytesPerSample = 4
return time.Duration(s.buf.Len() / bytesPerSample / s.sampleRate)
}

View File

@ -27,11 +27,7 @@ import (
"github.com/hajimehoshi/go-vorbis" "github.com/hajimehoshi/go-vorbis"
) )
type stream struct { func Decode(context *audio.Context, src io.Reader) (Stream, error) {
buf *bytes.Reader
}
func Decode(context *audio.Context, src io.Reader) (io.ReadSeeker, error) {
decoded, channels, sampleRate, err := vorbis.Decode(src) decoded, channels, sampleRate, err := vorbis.Decode(src)
if err != nil { if err != nil {
return nil, err return nil, err
@ -49,15 +45,8 @@ func Decode(context *audio.Context, src io.Reader) (io.ReadSeeker, error) {
return nil, err return nil, err
} }
s := &stream{ s := &stream{
buf: bytes.NewReader(b), buf: bytes.NewReader(b),
sampleRate: sampleRate,
} }
return s, nil return s, nil
} }
func (s *stream) Read(p []byte) (int, error) {
return s.buf.Read(p)
}
func (s *stream) Seek(offset int64, whence int) (int64, error) {
return s.buf.Seek(offset, whence)
}

View File

@ -25,14 +25,10 @@ import (
"github.com/hajimehoshi/ebiten/exp/audio" "github.com/hajimehoshi/ebiten/exp/audio"
) )
type stream struct {
buf *bytes.Reader
}
// TODO: This just uses decodeAudioData can treat audio files other than Ogg/Vorbis. // TODO: This just uses decodeAudioData can treat audio files other than Ogg/Vorbis.
// TODO: This doesn't work on iOS which doesn't have Ogg/Vorbis decoder. // TODO: This doesn't work on iOS which doesn't have Ogg/Vorbis decoder.
func Decode(context *audio.Context, src io.Reader) (io.ReadSeeker, error) { func Decode(context *audio.Context, src io.Reader) (Stream, error) {
b, err := ioutil.ReadAll(src) b, err := ioutil.ReadAll(src)
if err != nil { if err != nil {
return nil, err return nil, err
@ -60,11 +56,3 @@ func Decode(context *audio.Context, src io.Reader) (io.ReadSeeker, error) {
<-ch <-ch
return s, nil return s, nil
} }
func (s *stream) Read(p []byte) (int, error) {
return s.buf.Read(p)
}
func (s *stream) Seek(offset int64, whence int) (int64, error) {
return s.buf.Seek(offset, whence)
}