ebiten/audio/vorbis/vorbis.go

234 lines
6.2 KiB
Go
Raw Normal View History

// 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"
"io"
"github.com/jfreymuth/oggvorbis"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
)
const (
bitDepthInBytesInt16 = 2
)
2016-12-11 17:53:37 +01:00
// Stream is a decoded audio stream.
type Stream struct {
2024-05-20 19:34:54 +02:00
readSeeker io.ReadSeeker
length int64
sampleRate int
2016-12-11 17:53:37 +01:00
}
// Read is implementation of io.Reader's Read.
func (s *Stream) Read(p []byte) (int, error) {
2024-05-20 19:34:54 +02:00
return s.readSeeker.Read(p)
2016-12-11 17:53:37 +01:00
}
// 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) {
2024-05-20 19:34:54 +02:00
return s.readSeeker.Seek(offset, whence)
2016-12-11 17:53:37 +01:00
}
// Length returns the size of decoded stream in bytes.
//
// If the source is not io.Seeker, Length returns 0.
func (s *Stream) Length() int64 {
2024-05-20 19:34:54 +02:00
return s.length
2016-12-11 17:53:37 +01:00
}
// SampleRate returns the sample rate of the decoded stream.
func (s *Stream) SampleRate() int {
return s.sampleRate
}
2024-05-20 19:34:54 +02:00
type i16Stream struct {
totalBytes int
posInBytes int
vorbisReader *oggvorbis.Reader
i16Reader io.Reader
}
2024-05-20 19:34:54 +02:00
func (s *i16Stream) Read(b []byte) (int, error) {
if s.i16Reader == nil {
s.i16Reader = newInt16BytesReaderFromFloat32Reader(s.vorbisReader)
}
2024-05-20 19:34:54 +02:00
l := s.totalBytes - s.posInBytes
2016-11-28 19:37:43 +01:00
if l > len(b) {
l = len(b)
}
if l < 0 {
return 0, io.EOF
}
retry:
2024-05-20 19:34:54 +02:00
n, err := s.i16Reader.Read(b[:l])
if err != nil && err != io.EOF {
2016-11-28 19:37:43 +01:00
return 0, err
}
if n == 0 && l > 0 && err != io.EOF {
// When l is too small, decoder's Read might return 0 for a while. Let's retry.
goto retry
}
2024-05-20 19:34:54 +02:00
s.posInBytes += n
if s.posInBytes == s.totalBytes || err == io.EOF {
return n, io.EOF
}
return n, nil
}
2024-05-20 19:34:54 +02:00
func (s *i16Stream) Seek(offset int64, whence int) (int64, error) {
next := int64(0)
switch whence {
case io.SeekStart:
next = offset
case io.SeekCurrent:
2024-05-20 19:34:54 +02:00
next = int64(s.posInBytes) + offset
case io.SeekEnd:
2024-05-20 19:34:54 +02:00
next = int64(s.totalBytes) + offset
}
2016-11-28 19:37:43 +01:00
// pos should be always even
next = next / 2 * 2
2024-05-20 19:34:54 +02:00
s.posInBytes = int(next)
if err := s.vorbisReader.SetPosition(next / int64(s.vorbisReader.Channels()) / 2); err != nil {
2021-05-03 16:11:14 +02:00
return 0, err
}
2024-05-20 19:34:54 +02:00
s.i16Reader = nil
return next, nil
}
2024-05-20 19:34:54 +02:00
func (s *i16Stream) Length() int64 {
return int64(s.totalBytes)
}
// decode accepts an ogg stream and returns a decorded stream.
2024-05-20 19:34:54 +02:00
func decode(in io.Reader) (*i16Stream, int, int, error) {
r, err := oggvorbis.NewReader(in)
if err != nil {
return nil, 0, 0, err
}
2024-05-20 19:34:54 +02:00
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.
// Should we check that?
2024-05-20 19:34:54 +02:00
totalBytes: int(r.Length()) * r.Channels() * 2, // 2 means 16bit per sample.
posInBytes: 0,
vorbisReader: r,
}
if _, ok := in.(io.Seeker); ok {
2024-05-20 19:34:54 +02:00
if _, err := s.Read(make([]byte, 65536)); err != nil && err != io.EOF {
return nil, 0, 0, err
}
2024-05-20 19:34:54 +02:00
if _, err := s.Seek(0, io.SeekStart); err != nil {
return nil, 0, 0, err
}
}
2024-05-20 19:34:54 +02:00
return s, r.Channels(), r.SampleRate(), nil
}
2016-12-11 17:53:37 +01:00
// DecodeWithoutResampling decodes Ogg/Vorbis data to playable stream.
//
// DecodeWithoutResampling returns error when decoding fails or IO error happens.
//
// The returned Stream's Seek is available only when src is an io.Seeker.
//
// A Stream doesn't close src even if src implements io.Closer.
// Closing the source is src owner's responsibility.
func DecodeWithoutResampling(src io.Reader) (*Stream, error) {
2024-05-20 19:34:54 +02:00
i16Stream, channelCount, sampleRate, err := decode(src)
if err != nil {
return nil, err
}
2024-05-20 19:34:54 +02:00
var s io.ReadSeeker = i16Stream
length := i16Stream.Length()
if channelCount == 1 {
s = convert.NewStereo16(s, true, false)
2024-05-20 19:34:54 +02:00
length *= 2
}
2024-05-20 19:34:54 +02:00
stream := &Stream{
2024-05-20 19:34:54 +02:00
readSeeker: s,
length: length,
sampleRate: sampleRate,
}
return stream, nil
}
// DecodeWithSampleRate decodes Ogg/Vorbis data to playable stream.
2016-12-11 17:53:37 +01:00
//
// DecodeWithSampleRate returns error when decoding fails or IO error happens.
//
2021-01-06 15:02:56 +01:00
// DecodeWithSampleRate automatically resamples the stream to fit with sampleRate if necessary.
//
// The returned Stream's Seek is available only when src is an io.Seeker.
//
// A Stream doesn't close src even if src implements io.Closer.
// Closing the source is src owner's responsibility.
//
// 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.
func DecodeWithSampleRate(sampleRate int, src io.Reader) (*Stream, error) {
2024-05-20 19:34:54 +02:00
i16Stream, channelCount, origSampleRate, err := decode(src)
2016-12-11 17:53:37 +01:00
if err != nil {
return nil, err
}
2024-05-20 19:34:54 +02:00
var s io.ReadSeeker = i16Stream
length := i16Stream.Length()
if channelCount == 1 {
s = convert.NewStereo16(s, true, false)
2024-05-20 19:34:54 +02:00
length *= 2
2016-12-11 17:53:37 +01:00
}
if origSampleRate != sampleRate {
r := convert.NewResampling(s, length, origSampleRate, sampleRate, bitDepthInBytesInt16)
s = r
2024-05-20 19:34:54 +02:00
length = r.Length()
2016-12-11 17:53:37 +01:00
}
stream := &Stream{
2024-05-20 19:34:54 +02:00
readSeeker: s,
length: length,
sampleRate: sampleRate,
}
return stream, nil
2016-12-11 17:53:37 +01:00
}
// Decode decodes Ogg/Vorbis data to playable stream.
//
// Decode returns error when decoding fails or IO error happens.
//
// Decode automatically resamples the stream to fit with the audio context if necessary.
//
// The returned Stream's Seek is available only when src is an io.Seeker.
//
// A Stream doesn't close src even if src implements io.Closer.
// Closing the source is src owner's responsibility.
//
// Deprecated: as of v2.1. Use DecodeWithSampleRate instead.
func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
return DecodeWithSampleRate(context.SampleRate(), src)
}