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
|
|
|
// 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 (
|
2019-03-31 15:58:22 +02:00
|
|
|
"io"
|
2018-03-27 03:58:13 +02:00
|
|
|
|
2017-06-18 16:09:20 +02:00
|
|
|
"github.com/hajimehoshi/go-mp3"
|
2017-06-18 13:42:59 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
|
2017-06-18 16:09:20 +02:00
|
|
|
)
|
2017-06-18 13:42:59 +02:00
|
|
|
|
2024-07-13 17:24:36 +02:00
|
|
|
const (
|
2024-07-14 15:03:47 +02:00
|
|
|
bitDepthInBytesInt16 = 2
|
|
|
|
bitDepthInBytesFloat32 = 4
|
2024-07-13 17:24:36 +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 {
|
2024-07-21 10:00:46 +02:00
|
|
|
readSeeker io.ReadSeeker
|
|
|
|
length int64
|
2024-05-18 13:13:36 +02:00
|
|
|
sampleRate int
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read is implementation of io.Reader's Read.
|
|
|
|
func (s *Stream) Read(buf []byte) (int, error) {
|
2024-07-21 10:00:46 +02:00
|
|
|
return s.readSeeker.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) {
|
2024-07-21 10:00:46 +02:00
|
|
|
return s.readSeeker.Seek(offset, whence)
|
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 {
|
2024-07-21 10:00:46 +02:00
|
|
|
return s.length
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
|
|
|
|
2024-05-18 13:13:36 +02:00
|
|
|
// SampleRate returns the sample rate of the decoded stream.
|
|
|
|
func (s *Stream) SampleRate() int {
|
|
|
|
return s.sampleRate
|
|
|
|
}
|
|
|
|
|
2024-07-14 15:03:47 +02:00
|
|
|
// DecodeF32 decodes an MP3 source and returns a decoded stream in 32bit float, little endian, 2 channels (stereo) format.
|
|
|
|
//
|
|
|
|
// DecodeF32 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 DecodeF32(src io.Reader) (*Stream, error) {
|
|
|
|
d, err := mp3.NewDecoder(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := convert.NewFloat32BytesReadSeekerFromInt16BytesReadSeeker(d)
|
|
|
|
s := &Stream{
|
|
|
|
readSeeker: r,
|
|
|
|
length: d.Length() / bitDepthInBytesInt16 * bitDepthInBytesFloat32,
|
|
|
|
sampleRate: d.SampleRate(),
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeWithoutResampling decodes an MP3 source and returns a decoded stream in signed 16bit integer, little endian, 2 channels (stereo) format.
|
2022-06-30 18:37:07 +02:00
|
|
|
//
|
|
|
|
// 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) {
|
|
|
|
d, err := mp3.NewDecoder(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := &Stream{
|
2024-07-21 10:00:46 +02:00
|
|
|
readSeeker: d,
|
|
|
|
length: d.Length(),
|
2024-05-18 13:13:36 +02:00
|
|
|
sampleRate: d.SampleRate(),
|
2022-06-30 18:37:07 +02:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2024-07-14 15:03:47 +02:00
|
|
|
// DecodeWithSampleRate decodes an MP3 source and returns a decoded stream in signed 16bit integer, little endian, 2 channels (stereo) format.
|
2017-10-01 11:07:18 +02:00
|
|
|
//
|
2021-01-06 15:03:47 +01:00
|
|
|
// DecodeWithSampleRate returns error when decoding fails or IO error happens.
|
2017-10-01 11:07:18 +02:00
|
|
|
//
|
2021-01-06 15:03:47 +01:00
|
|
|
// DecodeWithSampleRate automatically resamples the stream to fit with sampleRate if necessary.
|
2019-03-31 18:32:11 +02:00
|
|
|
//
|
2021-04-24 09:39:46 +02:00
|
|
|
// The returned Stream's Seek is available only when src is an io.Seeker.
|
|
|
|
//
|
2020-10-07 16:09:02 +02:00
|
|
|
// A Stream doesn't close src even if src implements io.Closer.
|
|
|
|
// Closing the source is src owner's responsibility.
|
2024-05-04 10:30:15 +02:00
|
|
|
//
|
|
|
|
// 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.
|
2021-04-24 09:39:46 +02:00
|
|
|
func DecodeWithSampleRate(sampleRate int, src io.Reader) (*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
|
|
|
|
}
|
2019-03-31 15:58:22 +02:00
|
|
|
|
2024-07-21 10:00:46 +02:00
|
|
|
var r io.ReadSeeker = d
|
|
|
|
length := d.Length()
|
2021-01-06 15:03:47 +01:00
|
|
|
if d.SampleRate() != sampleRate {
|
2024-07-21 10:00:46 +02:00
|
|
|
r2 := convert.NewResampling(d, d.Length(), d.SampleRate(), sampleRate, bitDepthInBytesInt16)
|
|
|
|
r = r2
|
|
|
|
length = r2.Length()
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
2018-03-27 03:58:13 +02:00
|
|
|
s := &Stream{
|
2024-07-21 10:00:46 +02:00
|
|
|
readSeeker: r,
|
|
|
|
length: length,
|
2024-05-18 13:13:36 +02:00
|
|
|
sampleRate: sampleRate,
|
2018-03-27 03:58:13 +02:00
|
|
|
}
|
|
|
|
return s, nil
|
2017-06-11 11:12:12 +02:00
|
|
|
}
|
2021-01-06 15:03:47 +01:00
|
|
|
|
2024-07-14 15:03:47 +02:00
|
|
|
// Decode decodes MP3 source and returns a decoded stream in signed 16bit integer, little endian, 2 channels (stereo) format.
|
2021-01-06 15:03:47 +01:00
|
|
|
//
|
|
|
|
// Decode returns error when decoding fails or IO error happens.
|
|
|
|
//
|
|
|
|
// Decode automatically resamples the stream to fit with the audio context if necessary.
|
|
|
|
//
|
2021-04-24 09:39:46 +02:00
|
|
|
// The returned Stream's Seek is available only when src is an io.Seeker.
|
|
|
|
//
|
2021-01-06 15:03:47 +01:00
|
|
|
// 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.
|
2021-04-24 09:39:46 +02:00
|
|
|
func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
|
2021-01-06 15:03:47 +01:00
|
|
|
return DecodeWithSampleRate(context.SampleRate(), src)
|
|
|
|
}
|