2016-09-26 17:56:56 +02:00
|
|
|
// Copyright 2015 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 provides Ogg/Vorbis decoder.
|
|
|
|
package vorbis
|
|
|
|
|
|
|
|
import (
|
2016-12-11 17:53:37 +01:00
|
|
|
"fmt"
|
2016-09-26 17:56:56 +02:00
|
|
|
"io"
|
|
|
|
"runtime"
|
2016-11-25 21:43:42 +01:00
|
|
|
|
2016-11-28 17:01:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
2017-01-15 14:37:59 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/audio/internal/convert"
|
2016-11-25 20:31:56 +01:00
|
|
|
)
|
2016-09-26 17:56:56 +02:00
|
|
|
|
2016-12-11 17:53:37 +01:00
|
|
|
// Stream is a decoded audio stream.
|
|
|
|
type Stream struct {
|
2017-01-15 16:03:18 +01:00
|
|
|
decoded audio.ReadSeekCloser
|
|
|
|
size int64
|
2016-12-11 17:53:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read is implementation of io.Reader's Read.
|
|
|
|
func (s *Stream) Read(p []byte) (int, error) {
|
|
|
|
return s.decoded.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek is implementation of io.Seeker's Seek.
|
|
|
|
//
|
|
|
|
// Note that Seek can take long since decoding is a relatively heavy task.
|
|
|
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
return s.decoded.Seek(offset, whence)
|
|
|
|
}
|
|
|
|
|
2017-10-01 11:07:18 +02:00
|
|
|
// Close is implementation of io.Closer's Close.
|
2016-12-11 17:53:37 +01:00
|
|
|
func (s *Stream) Close() error {
|
2018-03-27 03:51:21 +02:00
|
|
|
runtime.SetFinalizer(s, nil)
|
|
|
|
if err := s.decoded.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-12-11 17:53:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-08 17:25:38 +01:00
|
|
|
// Length returns the size of decoded stream in bytes.
|
|
|
|
func (s *Stream) Length() int64 {
|
2017-01-15 16:03:18 +01:00
|
|
|
return s.size
|
2016-12-11 17:53:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-08 17:25:38 +01:00
|
|
|
// Size is deprecated as of version 1.6.0-alpha. Use Length instead.
|
|
|
|
func (s *Stream) Size() int64 {
|
|
|
|
return s.Length()
|
|
|
|
}
|
|
|
|
|
2018-07-03 14:09:12 +02:00
|
|
|
type decoder interface {
|
|
|
|
Read([]float32) (int, error)
|
2019-01-14 17:07:21 +01:00
|
|
|
SetPosition(int64) error
|
2018-07-03 14:09:12 +02:00
|
|
|
Length() int64
|
|
|
|
Channels() int
|
|
|
|
SampleRate() int
|
|
|
|
}
|
|
|
|
|
2016-09-26 17:56:56 +02:00
|
|
|
type decoded struct {
|
2016-11-28 17:01:52 +01:00
|
|
|
totalBytes int
|
2016-12-07 18:14:34 +01:00
|
|
|
readBytes int
|
2016-11-25 20:31:56 +01:00
|
|
|
posInBytes int
|
2016-11-28 17:01:52 +01:00
|
|
|
source io.Closer
|
2018-07-03 14:09:12 +02:00
|
|
|
decoder decoder
|
2019-01-15 03:14:55 +01:00
|
|
|
decoderr io.Reader
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|
|
|
|
|
2018-09-02 16:55:59 +02:00
|
|
|
func (d *decoded) Read(b []byte) (int, error) {
|
2019-01-15 03:14:55 +01:00
|
|
|
if d.decoderr == nil {
|
|
|
|
d.decoderr = convert.NewReaderFromFloat32Reader(d.decoder)
|
|
|
|
}
|
|
|
|
|
2016-12-07 18:14:34 +01:00
|
|
|
l := d.totalBytes - d.posInBytes
|
2016-11-28 19:37:43 +01:00
|
|
|
if l > len(b) {
|
|
|
|
l = len(b)
|
|
|
|
}
|
|
|
|
if l < 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2019-01-14 17:07:21 +01:00
|
|
|
|
|
|
|
retry:
|
2019-01-15 03:14:55 +01:00
|
|
|
n, err := d.decoderr.Read(b[:l])
|
2019-01-14 17:07:21 +01:00
|
|
|
if err != nil && err != io.EOF {
|
2016-11-28 19:37:43 +01:00
|
|
|
return 0, err
|
2016-11-28 17:01:52 +01:00
|
|
|
}
|
2019-01-15 03:14:55 +01:00
|
|
|
if n == 0 && l > 0 && err != io.EOF {
|
2019-01-14 17:07:21 +01:00
|
|
|
// When l is too small, decoder's Read might return 0 for a while. Let's retry.
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
|
2019-01-15 03:14:55 +01:00
|
|
|
d.posInBytes += n
|
2019-01-14 17:07:21 +01:00
|
|
|
if d.posInBytes == d.totalBytes || err == io.EOF {
|
2019-01-15 03:14:55 +01:00
|
|
|
return n, io.EOF
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|
2019-01-15 03:14:55 +01:00
|
|
|
return n, nil
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoded) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
next := int64(0)
|
|
|
|
switch whence {
|
2016-11-25 20:31:56 +01:00
|
|
|
case io.SeekStart:
|
2016-09-26 17:56:56 +02:00
|
|
|
next = offset
|
2016-11-25 20:31:56 +01:00
|
|
|
case io.SeekCurrent:
|
2016-09-26 17:56:56 +02:00
|
|
|
next = int64(d.posInBytes) + offset
|
2016-11-25 20:31:56 +01:00
|
|
|
case io.SeekEnd:
|
2016-11-28 17:01:52 +01:00
|
|
|
next = int64(d.totalBytes) + offset
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|
2016-11-28 19:37:43 +01:00
|
|
|
// pos should be always even
|
|
|
|
next = next / 2 * 2
|
2016-09-26 17:56:56 +02:00
|
|
|
d.posInBytes = int(next)
|
2019-01-14 17:07:21 +01:00
|
|
|
d.decoder.SetPosition(next / int64(d.decoder.Channels()) / 2)
|
2019-01-15 03:14:55 +01:00
|
|
|
d.decoderr = nil
|
2016-09-26 17:56:56 +02:00
|
|
|
return next, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoded) Close() error {
|
|
|
|
runtime.SetFinalizer(d, nil)
|
2018-03-27 03:51:21 +02:00
|
|
|
if err := d.source.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-08 19:48:28 +01:00
|
|
|
d.decoder = nil
|
2019-01-15 03:14:55 +01:00
|
|
|
d.decoderr = nil
|
2016-09-26 17:56:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 17:25:38 +01:00
|
|
|
func (d *decoded) Length() int64 {
|
2016-11-28 17:01:52 +01:00
|
|
|
return int64(d.totalBytes)
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// decode accepts an ogg stream and returns a decorded stream.
|
2016-11-28 17:01:52 +01:00
|
|
|
func decode(in audio.ReadSeekCloser) (*decoded, int, int, error) {
|
2018-07-03 14:09:12 +02:00
|
|
|
r, err := newDecoder(in)
|
2016-09-26 17:56:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
d := &decoded{
|
2018-03-30 04:59:03 +02:00
|
|
|
// TODO: r.Length() returns 0 when the format is unknown.
|
|
|
|
// Should we check that?
|
|
|
|
totalBytes: int(r.Length()) * r.Channels() * 2, // 2 means 16bit per sample.
|
2016-11-25 20:31:56 +01:00
|
|
|
posInBytes: 0,
|
2016-11-28 17:01:52 +01:00
|
|
|
source: in,
|
|
|
|
decoder: r,
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|
|
|
|
runtime.SetFinalizer(d, (*decoded).Close)
|
2018-09-02 16:55:59 +02:00
|
|
|
if _, err := d.Read(make([]byte, 65536)); err != nil && err != io.EOF {
|
2016-11-30 04:00:53 +01:00
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
if _, err := d.Seek(0, io.SeekStart); err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
2016-11-28 17:01:52 +01:00
|
|
|
return d, r.Channels(), r.SampleRate(), nil
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|
2016-12-11 17:53:37 +01:00
|
|
|
|
|
|
|
// Decode decodes Ogg/Vorbis data to playable stream.
|
|
|
|
//
|
2017-10-01 11:07:18 +02:00
|
|
|
// Decode returns error when decoding fails or IO error happens.
|
2017-04-17 18:25:23 +02:00
|
|
|
//
|
2017-10-01 11:07:18 +02:00
|
|
|
// Decode automatically resamples the stream to fit with the audio context if necessary.
|
2019-03-31 18:32:11 +02:00
|
|
|
//
|
|
|
|
// Decode takes the ownership of src, and Stream's Close function closes src.
|
2016-12-11 17:53:37 +01:00
|
|
|
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
|
|
|
decoded, channelNum, sampleRate, err := decode(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-15 16:03:18 +01:00
|
|
|
if channelNum != 1 && channelNum != 2 {
|
|
|
|
return nil, fmt.Errorf("vorbis: number of channels must be 1 or 2 but was %d", channelNum)
|
|
|
|
}
|
|
|
|
var s audio.ReadSeekCloser = decoded
|
2018-01-08 17:25:38 +01:00
|
|
|
size := decoded.Length()
|
2017-01-15 16:03:18 +01:00
|
|
|
if channelNum == 1 {
|
|
|
|
s = convert.NewStereo16(s, true, false)
|
|
|
|
size *= 2
|
2016-12-11 17:53:37 +01:00
|
|
|
}
|
|
|
|
if sampleRate != context.SampleRate() {
|
2017-07-11 17:51:36 +02:00
|
|
|
r := convert.NewResampling(s, size, sampleRate, context.SampleRate())
|
|
|
|
s = r
|
2018-01-08 17:25:38 +01:00
|
|
|
size = r.Length()
|
2016-12-11 17:53:37 +01:00
|
|
|
}
|
2018-03-27 03:51:21 +02:00
|
|
|
stream := &Stream{decoded: s, size: size}
|
|
|
|
runtime.SetFinalizer(stream, (*Stream).Close)
|
|
|
|
return stream, nil
|
2016-12-11 17:53:37 +01:00
|
|
|
}
|