audio: Revert "audio/vorbis: Use pure Go decoder (#247)": fixing a bug of noisy sound

This commit is contained in:
Hajime Hoshi 2016-09-27 00:56:56 +09:00
parent c634c588e5
commit a14b4c8046
2 changed files with 5673 additions and 46 deletions

View File

@ -17,89 +17,54 @@
package vorbis package vorbis
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io"
"math"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/audio"
"github.com/jfreymuth/go-vorbis/ogg/vorbis"
) )
// Stream is a decoded audio stream. // Stream is a decoded audio stream.
type Stream struct { type Stream struct {
buf *bytes.Reader decoded *decoded
}
func newStream(v *vorbis.Vorbis) (*Stream, error) {
data := []byte{}
// TODO: We can delay decoding.
for {
out, err := v.DecodePacket()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
const channelNum = 2
b := make([]byte, len(out[0])*2*channelNum)
for i := 0; i < len(out[0]); i++ {
vv := int16(out[0][i] * math.MaxInt16)
b[4*i] = byte(vv)
b[4*i+1] = byte(vv >> 8)
vv = int16(out[1][i] * math.MaxInt16)
b[4*i+2] = byte(vv)
b[4*i+3] = byte(vv >> 8)
}
data = append(data, b...)
}
s := &Stream{
buf: bytes.NewReader(data),
}
return s, nil
} }
// 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.buf.Read(p) return s.decoded.Read(p)
} }
// Seek is implementation of io.Seeker's Seek. // Seek is implementation of io.Seeker's Seek.
func (s *Stream) Seek(offset int64, whence int) (int64, error) { func (s *Stream) Seek(offset int64, whence int) (int64, error) {
return s.buf.Seek(offset, whence) return s.decoded.Seek(offset, whence)
} }
// Read is implementation of io.Closer's Close. // Read is implementation of io.Closer's Close.
func (s *Stream) Close() error { func (s *Stream) Close() error {
s.buf = nil return s.decoded.Close()
return nil
} }
// Size returns the size of decoded stream in bytes. // Size returns the size of decoded stream in bytes.
func (s *Stream) Size() int64 { func (s *Stream) Size() int64 {
return s.buf.Size() return s.decoded.Size()
} }
// Decode decodes Ogg/Vorbis data to playable stream. // Decode decodes Ogg/Vorbis data to playable stream.
// //
// The sample rate must be same as that of audio context. // The sample rate must be same as that of audio context.
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) { func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
v, err := vorbis.Open(src) decoded, channelNum, sampleRate, err := decode(src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// TODO: Remove this magic number // TODO: Remove this magic number
if v.Channels() != 2 { if channelNum != 2 {
return nil, errors.New("vorbis: number of channels must be 2") return nil, errors.New("vorbis: number of channels must be 2")
} }
if v.SampleRate() != context.SampleRate() { if sampleRate != context.SampleRate() {
return nil, fmt.Errorf("vorbis: sample rate must be %d but %d", context.SampleRate(), v.SampleRate()) return nil, fmt.Errorf("vorbis: sample rate must be %d but %d", context.SampleRate(), sampleRate)
} }
s, err := newStream(v) s := &Stream{
if err != nil { decoded: decoded,
return nil, err
} }
return s, nil return s, nil
} }

5662
audio/vorbis/vorbis.go Normal file

File diff suppressed because it is too large Load Diff