2017-06-11 11:12:12 +02:00
|
|
|
// Copyright 2017 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-06-18 16:49:05 +02:00
|
|
|
// +build !js
|
|
|
|
|
|
|
|
// Package mp3 provides MP3 decoder.
|
2017-10-01 11:07:18 +02:00
|
|
|
//
|
|
|
|
// On desktops and mobiles, a pure Go decoder is used.
|
|
|
|
// On browsers, a native decoder on the browser is used.
|
2017-06-11 11:12:12 +02:00
|
|
|
package mp3
|
|
|
|
|
|
|
|
import (
|
2018-03-27 03:58:13 +02:00
|
|
|
"runtime"
|
|
|
|
|
2017-06-18 16:09:20 +02:00
|
|
|
"github.com/hajimehoshi/go-mp3"
|
2017-06-18 13:42:59 +02:00
|
|
|
|
2017-06-18 16:09:20 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
2017-06-18 16:49:05 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/audio/internal/convert"
|
2017-06-18 16:09:20 +02:00
|
|
|
)
|
2017-06-18 13:42:59 +02:00
|
|
|
|
2017-10-01 11:07:18 +02:00
|
|
|
// Stream is a decoded stream.
|
2017-06-18 16:49:05 +02:00
|
|
|
type Stream struct {
|
2017-12-21 21:15:08 +01:00
|
|
|
orig *mp3.Decoder
|
|
|
|
resampling *convert.Resampling
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read is implementation of io.Reader's Read.
|
|
|
|
func (s *Stream) Read(buf []byte) (int, error) {
|
2017-12-21 21:15:08 +01:00
|
|
|
if s.resampling != nil {
|
|
|
|
return s.resampling.Read(buf)
|
|
|
|
}
|
|
|
|
return s.orig.Read(buf)
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek is implementation of io.Seeker's Seek.
|
|
|
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
2017-12-21 21:15:08 +01:00
|
|
|
if s.resampling != nil {
|
|
|
|
return s.resampling.Seek(offset, whence)
|
|
|
|
}
|
|
|
|
return s.orig.Seek(offset, whence)
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
|
|
|
|
2017-10-01 11:07:18 +02:00
|
|
|
// Close is implementation of io.Closer's Close.
|
2017-06-18 16:49:05 +02:00
|
|
|
func (s *Stream) Close() error {
|
2018-03-27 03:58:13 +02:00
|
|
|
runtime.SetFinalizer(s, nil)
|
2017-12-21 21:15:08 +01:00
|
|
|
if s.resampling != nil {
|
|
|
|
return s.resampling.Close()
|
|
|
|
}
|
2019-03-31 15:42:28 +02:00
|
|
|
return nil
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
|
|
|
|
2018-01-08 17:25:38 +01:00
|
|
|
// Length returns the size of decoded stream in bytes.
|
|
|
|
func (s *Stream) Length() int64 {
|
2017-12-21 21:15:08 +01:00
|
|
|
if s.resampling != nil {
|
2018-01-08 17:25:38 +01:00
|
|
|
return s.resampling.Length()
|
2017-12-21 21:15:08 +01:00
|
|
|
}
|
|
|
|
return s.orig.Length()
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
|
|
|
|
2018-01-08 17:25:38 +01:00
|
|
|
// Size is deprecated as of 1.6.0-alpha. Use Length instead.
|
|
|
|
func (s *Stream) Size() int64 {
|
|
|
|
return s.Length()
|
|
|
|
}
|
|
|
|
|
2017-10-01 11:07:18 +02:00
|
|
|
// Decode decodes MP3 source and returns a decoded stream.
|
|
|
|
//
|
|
|
|
// Decode returns error when decoding fails or IO error happens.
|
|
|
|
//
|
|
|
|
// Decode automatically resamples the stream to fit with the audio context if necessary.
|
2017-06-18 16:49:05 +02:00
|
|
|
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
2017-07-04 15:01:06 +02:00
|
|
|
d, err := mp3.NewDecoder(src)
|
2017-06-18 16:49:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-21 21:15:08 +01:00
|
|
|
var r *convert.Resampling
|
2017-06-18 16:49:05 +02:00
|
|
|
if d.SampleRate() != context.SampleRate() {
|
2017-12-21 21:15:08 +01:00
|
|
|
r = convert.NewResampling(d, d.Length(), d.SampleRate(), context.SampleRate())
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
2018-03-27 03:58:13 +02:00
|
|
|
s := &Stream{
|
2017-12-21 21:15:08 +01:00
|
|
|
orig: d,
|
|
|
|
resampling: r,
|
2018-03-27 03:58:13 +02:00
|
|
|
}
|
|
|
|
runtime.SetFinalizer(s, (*Stream).Close)
|
|
|
|
return s, nil
|
2017-06-11 11:12:12 +02:00
|
|
|
}
|