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 (
|
|
|
|
"io"
|
|
|
|
"runtime"
|
2016-11-25 21:43:42 +01:00
|
|
|
|
2016-11-28 17:01:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
2016-11-25 20:31:56 +01:00
|
|
|
"github.com/jfreymuth/oggvorbis"
|
|
|
|
)
|
2016-09-26 17:56:56 +02:00
|
|
|
|
|
|
|
type decoded struct {
|
2016-11-25 20:31:56 +01:00
|
|
|
data []float32
|
2016-11-28 17:01:52 +01:00
|
|
|
totalBytes int
|
2016-11-25 20:31:56 +01:00
|
|
|
posInBytes int
|
2016-11-28 17:01:52 +01:00
|
|
|
source io.Closer
|
|
|
|
decoder *oggvorbis.Reader
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoded) Read(b []byte) (int, error) {
|
2016-11-28 17:01:52 +01:00
|
|
|
total := d.totalBytes
|
2016-11-25 20:31:56 +01:00
|
|
|
l := total - d.posInBytes
|
2016-09-26 17:56:56 +02:00
|
|
|
if l > len(b) {
|
|
|
|
l = len(b)
|
|
|
|
}
|
2016-11-28 17:01:52 +01:00
|
|
|
if l < 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2016-09-26 17:56:56 +02:00
|
|
|
// l must be even so that d.posInBytes is always even.
|
|
|
|
l = l / 2 * 2
|
2016-11-28 17:01:52 +01:00
|
|
|
for len(d.data) < (d.posInBytes+l)/2 {
|
|
|
|
// TODO: What if the channel is closed?
|
|
|
|
buffer := make([]float32, 4096)
|
|
|
|
n, err := d.decoder.Read(buffer)
|
|
|
|
if n > 0 {
|
|
|
|
d.data = append(d.data, buffer[:n]...)
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
if err := d.source.Close(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 17:56:56 +02:00
|
|
|
for i := 0; i < l/2; i++ {
|
2016-11-25 20:31:56 +01:00
|
|
|
f := d.data[d.posInBytes/2+i]
|
|
|
|
s := int16(f * (1<<15 - 1))
|
|
|
|
b[2*i] = uint8(s)
|
|
|
|
b[2*i+1] = uint8(s >> 8)
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|
|
|
|
d.posInBytes += l
|
2016-11-25 20:31:56 +01:00
|
|
|
if d.posInBytes == total {
|
2016-09-26 17:56:56 +02:00
|
|
|
return l, io.EOF
|
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
d.posInBytes = int(next)
|
|
|
|
return next, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoded) Close() error {
|
|
|
|
runtime.SetFinalizer(d, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoded) Size() 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) {
|
|
|
|
// TODO: Lazy evaluation
|
|
|
|
r, err := oggvorbis.NewReader(in)
|
2016-09-26 17:56:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
|
|
|
}
|
|
|
|
d := &decoded{
|
2016-11-28 17:01:52 +01:00
|
|
|
data: []float32{},
|
|
|
|
totalBytes: int(r.Length()) * 4, // TODO: What if length is 0?
|
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)
|
2016-11-28 17:01:52 +01:00
|
|
|
return d, r.Channels(), r.SampleRate(), nil
|
2016-09-26 17:56:56 +02:00
|
|
|
}
|