audio/vorbis: refactoring

This commit is contained in:
Hajime Hoshi 2024-05-21 02:34:54 +09:00
parent b0a4b6ebbf
commit 5942192b66

View File

@ -27,28 +27,28 @@ import (
// Stream is a decoded audio stream. // Stream is a decoded audio stream.
type Stream struct { type Stream struct {
decoded io.ReadSeeker readSeeker io.ReadSeeker
size int64 length int64
sampleRate int sampleRate int
} }
// Read is implementation of io.Reader's Read. // Read is implementation of io.Reader's Read.
func (s *Stream) Read(p []byte) (int, error) { func (s *Stream) Read(p []byte) (int, error) {
return s.decoded.Read(p) return s.readSeeker.Read(p)
} }
// Seek is implementation of io.Seeker's Seek. // Seek is implementation of io.Seeker's Seek.
// //
// Note that Seek can take long since decoding is a relatively heavy task. // Note that Seek can take long since decoding is a relatively heavy task.
func (s *Stream) Seek(offset int64, whence int) (int64, error) { func (s *Stream) Seek(offset int64, whence int) (int64, error) {
return s.decoded.Seek(offset, whence) return s.readSeeker.Seek(offset, whence)
} }
// Length returns the size of decoded stream in bytes. // Length returns the size of decoded stream in bytes.
// //
// If the source is not io.Seeker, Length returns 0. // If the source is not io.Seeker, Length returns 0.
func (s *Stream) Length() int64 { func (s *Stream) Length() int64 {
return s.size return s.length
} }
// SampleRate returns the sample rate of the decoded stream. // SampleRate returns the sample rate of the decoded stream.
@ -56,27 +56,19 @@ func (s *Stream) SampleRate() int {
return s.sampleRate return s.sampleRate
} }
type decoder interface { type i16Stream struct {
Read([]float32) (int, error)
SetPosition(int64) error
Length() int64
Channels() int
SampleRate() int
}
type decoded struct {
totalBytes int totalBytes int
posInBytes int posInBytes int
decoder decoder vorbisReader *oggvorbis.Reader
decoderr io.Reader i16Reader io.Reader
} }
func (d *decoded) Read(b []byte) (int, error) { func (s *i16Stream) Read(b []byte) (int, error) {
if d.decoderr == nil { if s.i16Reader == nil {
d.decoderr = convert.NewReaderFromFloat32Reader(d.decoder) s.i16Reader = convert.NewReaderFromFloat32Reader(s.vorbisReader)
} }
l := d.totalBytes - d.posInBytes l := s.totalBytes - s.posInBytes
if l > len(b) { if l > len(b) {
l = len(b) l = len(b)
} }
@ -85,7 +77,7 @@ func (d *decoded) Read(b []byte) (int, error) {
} }
retry: retry:
n, err := d.decoderr.Read(b[:l]) n, err := s.i16Reader.Read(b[:l])
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
return 0, err return 0, err
} }
@ -94,59 +86,63 @@ retry:
goto retry goto retry
} }
d.posInBytes += n s.posInBytes += n
if d.posInBytes == d.totalBytes || err == io.EOF { if s.posInBytes == s.totalBytes || err == io.EOF {
return n, io.EOF return n, io.EOF
} }
return n, nil return n, nil
} }
func (d *decoded) Seek(offset int64, whence int) (int64, error) { func (s *i16Stream) Seek(offset int64, whence int) (int64, error) {
next := int64(0) next := int64(0)
switch whence { switch whence {
case io.SeekStart: case io.SeekStart:
next = offset next = offset
case io.SeekCurrent: case io.SeekCurrent:
next = int64(d.posInBytes) + offset next = int64(s.posInBytes) + offset
case io.SeekEnd: case io.SeekEnd:
next = int64(d.totalBytes) + offset next = int64(s.totalBytes) + offset
} }
// pos should be always even // pos should be always even
next = next / 2 * 2 next = next / 2 * 2
d.posInBytes = int(next) s.posInBytes = int(next)
if err := d.decoder.SetPosition(next / int64(d.decoder.Channels()) / 2); err != nil { if err := s.vorbisReader.SetPosition(next / int64(s.vorbisReader.Channels()) / 2); err != nil {
return 0, err return 0, err
} }
d.decoderr = nil s.i16Reader = nil
return next, nil return next, nil
} }
func (d *decoded) Length() int64 { func (s *i16Stream) Length() int64 {
return int64(d.totalBytes) return int64(s.totalBytes)
} }
// decode accepts an ogg stream and returns a decorded stream. // decode accepts an ogg stream and returns a decorded stream.
func decode(in io.Reader) (*decoded, int, int, error) { func decode(in io.Reader) (*i16Stream, int, int, error) {
r, err := oggvorbis.NewReader(in) r, err := oggvorbis.NewReader(in)
if err != nil { if err != nil {
return nil, 0, 0, err return nil, 0, 0, err
} }
d := &decoded{ if r.Channels() != 1 && r.Channels() != 2 {
return nil, 0, 0, fmt.Errorf("vorbis: number of channels must be 1 or 2 but was %d", r.Channels())
}
s := &i16Stream{
// TODO: r.Length() returns 0 when the format is unknown. // TODO: r.Length() returns 0 when the format is unknown.
// Should we check that? // Should we check that?
totalBytes: int(r.Length()) * r.Channels() * 2, // 2 means 16bit per sample. totalBytes: int(r.Length()) * r.Channels() * 2, // 2 means 16bit per sample.
posInBytes: 0, posInBytes: 0,
decoder: r, vorbisReader: r,
} }
if _, ok := in.(io.Seeker); ok { if _, ok := in.(io.Seeker); ok {
if _, err := d.Read(make([]byte, 65536)); err != nil && err != io.EOF { if _, err := s.Read(make([]byte, 65536)); err != nil && err != io.EOF {
return nil, 0, 0, err return nil, 0, 0, err
} }
if _, err := d.Seek(0, io.SeekStart); err != nil { if _, err := s.Seek(0, io.SeekStart); err != nil {
return nil, 0, 0, err return nil, 0, 0, err
} }
} }
return d, r.Channels(), r.SampleRate(), nil return s, r.Channels(), r.SampleRate(), nil
} }
// DecodeWithoutResampling decodes Ogg/Vorbis data to playable stream. // DecodeWithoutResampling decodes Ogg/Vorbis data to playable stream.
@ -158,22 +154,21 @@ 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, sampleRate, err := decode(src) i16Stream, channelCount, sampleRate, err := decode(src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if channelCount != 1 && channelCount != 2 {
return nil, fmt.Errorf("vorbis: number of channels must be 1 or 2 but was %d", channelCount) var s io.ReadSeeker = i16Stream
} length := i16Stream.Length()
var s io.ReadSeeker = decoded
size := decoded.Length()
if channelCount == 1 { if channelCount == 1 {
s = convert.NewStereo16(s, true, false) s = convert.NewStereo16(s, true, false)
size *= 2 length *= 2
} }
stream := &Stream{ stream := &Stream{
decoded: s, readSeeker: s,
size: size, length: length,
sampleRate: sampleRate, sampleRate: sampleRate,
} }
return stream, nil return stream, nil
@ -193,27 +188,25 @@ func DecodeWithoutResampling(src io.Reader) (*Stream, error) {
// Resampling can be a very heavy task. Stream has a cache for resampling, but the size is limited. // Resampling can be a very heavy task. Stream has a cache for resampling, but the size is limited.
// Do not expect that Stream has a resampling cache even after whole data is played. // Do not expect that Stream has a resampling cache even after whole data is played.
func DecodeWithSampleRate(sampleRate int, src io.Reader) (*Stream, error) { func DecodeWithSampleRate(sampleRate int, src io.Reader) (*Stream, error) {
decoded, channelCount, origSampleRate, err := decode(src) i16Stream, channelCount, origSampleRate, err := decode(src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if channelCount != 1 && channelCount != 2 {
return nil, fmt.Errorf("vorbis: number of channels must be 1 or 2 but was %d", channelCount) var s io.ReadSeeker = i16Stream
} length := i16Stream.Length()
var s io.ReadSeeker = decoded
size := decoded.Length()
if channelCount == 1 { if channelCount == 1 {
s = convert.NewStereo16(s, true, false) s = convert.NewStereo16(s, true, false)
size *= 2 length *= 2
} }
if origSampleRate != sampleRate { if origSampleRate != sampleRate {
r := convert.NewResampling(s, size, origSampleRate, sampleRate) r := convert.NewResampling(s, length, origSampleRate, sampleRate)
s = r s = r
size = r.Length() length = r.Length()
} }
stream := &Stream{ stream := &Stream{
decoded: s, readSeeker: s,
size: size, length: length,
sampleRate: sampleRate, sampleRate: sampleRate,
} }
return stream, nil return stream, nil