2016-03-27 12:10:16 +02:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2016-04-19 16:19:31 +02:00
|
|
|
// Package wav provides WAV (RIFF) decoder.
|
2016-03-27 12:10:16 +02:00
|
|
|
package wav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
|
2016-03-27 12:10:16 +02:00
|
|
|
)
|
|
|
|
|
2024-07-13 17:24:36 +02:00
|
|
|
const (
|
|
|
|
bitDepthInBytesInt16 = 2
|
|
|
|
)
|
|
|
|
|
2016-08-01 18:47:25 +02:00
|
|
|
// Stream is a decoded audio stream.
|
2016-03-27 12:10:16 +02:00
|
|
|
type Stream struct {
|
2024-05-18 13:19:27 +02:00
|
|
|
inner io.ReadSeeker
|
|
|
|
size int64
|
|
|
|
sampleRate int
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read is implementation of io.Reader's Read.
|
|
|
|
func (s *Stream) Read(p []byte) (int, error) {
|
|
|
|
return s.inner.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek is implementation of io.Seeker's Seek.
|
|
|
|
//
|
|
|
|
// Note that Seek can take long since decoding is a relatively heavy task.
|
2021-04-24 09:35:24 +02:00
|
|
|
//
|
|
|
|
// If the underlying source is not an io.Seeker, Seek panics.
|
2017-01-14 17:42:25 +01:00
|
|
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
return s.inner.Seek(offset, whence)
|
|
|
|
}
|
|
|
|
|
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 14:37:59 +01:00
|
|
|
return s.size
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
|
|
|
|
2024-05-18 13:19:27 +02:00
|
|
|
// SampleRate returns the sample rate of the decoded stream.
|
|
|
|
func (s *Stream) SampleRate() int {
|
|
|
|
return s.sampleRate
|
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
// DecodeWithoutResampling decodes WAV (RIFF) data to playable stream.
|
2022-06-30 18:37:07 +02:00
|
|
|
//
|
|
|
|
// The format must be 1 or 2 channels, 8bit or 16bit little endian PCM.
|
|
|
|
// The format is converted into 2 channels and 16bit.
|
|
|
|
//
|
2024-07-14 16:45:25 +02:00
|
|
|
// DecodeWithoutSampleRate returns error when decoding fails or IO error happens.
|
2022-06-30 18:37:07 +02:00
|
|
|
//
|
|
|
|
// 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-18 13:19:27 +02:00
|
|
|
s, err := decode(src)
|
2023-04-18 15:27:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s, nil
|
2022-06-30 18:37:07 +02:00
|
|
|
}
|
|
|
|
|
2021-01-06 15:09:09 +01:00
|
|
|
// DecodeWithSampleRate decodes WAV (RIFF) data to playable stream.
|
2016-04-18 19:00:16 +02:00
|
|
|
//
|
2017-01-15 16:03:18 +01:00
|
|
|
// The format must be 1 or 2 channels, 8bit or 16bit little endian PCM.
|
|
|
|
// The format is converted into 2 channels and 16bit.
|
2017-01-14 17:42:25 +01:00
|
|
|
//
|
2021-01-06 15:09:09 +01:00
|
|
|
// DecodeWithSampleRate returns error when decoding fails or IO error happens.
|
2017-04-17 18:25:23 +02:00
|
|
|
//
|
2021-01-06 15:09:09 +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:35:24 +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:35:24 +02:00
|
|
|
func DecodeWithSampleRate(sampleRate int, src io.Reader) (*Stream, error) {
|
2024-05-18 13:19:27 +02:00
|
|
|
s, err := decode(src)
|
2023-04-18 15:27:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-18 13:19:27 +02:00
|
|
|
if sampleRate == s.sampleRate {
|
2023-04-18 15:27:22 +02:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2024-07-13 17:24:36 +02:00
|
|
|
r := convert.NewResampling(s.inner, s.size, s.sampleRate, sampleRate, bitDepthInBytesInt16)
|
2023-04-18 15:27:22 +02:00
|
|
|
return &Stream{
|
2024-05-18 13:19:27 +02:00
|
|
|
inner: r,
|
|
|
|
size: r.Length(),
|
|
|
|
sampleRate: sampleRate,
|
2023-04-18 15:27:22 +02:00
|
|
|
}, nil
|
2022-06-30 18:37:07 +02:00
|
|
|
}
|
|
|
|
|
2024-05-18 13:19:27 +02:00
|
|
|
func decode(src io.Reader) (*Stream, error) {
|
2016-03-29 18:30:40 +02:00
|
|
|
buf := make([]byte, 12)
|
2016-03-27 12:10:16 +02:00
|
|
|
n, err := io.ReadFull(src, buf)
|
2016-03-29 18:30:40 +02:00
|
|
|
if n != len(buf) {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: invalid header")
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, err
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2016-03-29 18:30:40 +02:00
|
|
|
if !bytes.Equal(buf[0:4], []byte("RIFF")) {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: invalid header: 'RIFF' not found")
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2016-03-29 18:30:40 +02:00
|
|
|
if !bytes.Equal(buf[8:12], []byte("WAVE")) {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: invalid header: 'WAVE' not found")
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2016-03-29 18:30:40 +02:00
|
|
|
|
|
|
|
// Read chunks
|
2023-04-18 15:27:22 +02:00
|
|
|
var dataSize int64
|
2017-01-16 15:36:09 +01:00
|
|
|
headerSize := int64(len(buf))
|
2023-04-18 15:27:22 +02:00
|
|
|
var mono bool
|
|
|
|
var bitsPerSample int
|
|
|
|
var sampleRate int
|
2016-03-29 18:30:40 +02:00
|
|
|
chunks:
|
|
|
|
for {
|
2024-07-14 15:22:05 +02:00
|
|
|
var buf [8]byte
|
|
|
|
n, err := io.ReadFull(src, buf[:])
|
2016-03-29 18:30:40 +02:00
|
|
|
if n != len(buf) {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: invalid header")
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, err
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
|
|
|
headerSize += 8
|
2016-05-13 09:19:02 +02:00
|
|
|
size := int64(buf[4]) | int64(buf[5])<<8 | int64(buf[6])<<16 | int64(buf[7])<<24
|
2016-03-29 18:30:40 +02:00
|
|
|
switch {
|
|
|
|
case bytes.Equal(buf[0:4], []byte("fmt ")):
|
2017-01-15 14:02:59 +01:00
|
|
|
// Size of 'fmt' header is usually 16, but can be more than 16.
|
|
|
|
if size < 16 {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: invalid header: maybe non-PCM file?")
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
|
|
|
buf := make([]byte, size)
|
|
|
|
n, err := io.ReadFull(src, buf)
|
|
|
|
if n != len(buf) {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: invalid header")
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, err
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
2016-03-29 20:03:08 +02:00
|
|
|
format := int(buf[0]) | int(buf[1])<<8
|
|
|
|
if format != 1 {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: format must be linear PCM")
|
2016-03-29 20:03:08 +02:00
|
|
|
}
|
2022-07-12 19:08:38 +02:00
|
|
|
channelCount := int(buf[2]) | int(buf[3])<<8
|
|
|
|
switch channelCount {
|
2017-01-15 16:03:18 +01:00
|
|
|
case 1:
|
|
|
|
mono = true
|
|
|
|
case 2:
|
|
|
|
mono = false
|
|
|
|
default:
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: number of channels must be 1 or 2 but was %d", channelCount)
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
2017-01-15 16:03:18 +01:00
|
|
|
bitsPerSample = int(buf[14]) | int(buf[15])<<8
|
|
|
|
if bitsPerSample != 8 && bitsPerSample != 16 {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: bits per sample must be 8 or 16 but was %d", bitsPerSample)
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
2023-04-18 15:27:22 +02:00
|
|
|
sampleRate = int(buf[4]) | int(buf[5])<<8 | int(buf[6])<<16 | int(buf[7])<<24
|
2016-03-29 18:30:40 +02:00
|
|
|
headerSize += size
|
|
|
|
case bytes.Equal(buf[0:4], []byte("data")):
|
|
|
|
dataSize = size
|
|
|
|
break chunks
|
|
|
|
default:
|
|
|
|
buf := make([]byte, size)
|
|
|
|
n, err := io.ReadFull(src, buf)
|
|
|
|
if n != len(buf) {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, fmt.Errorf("wav: invalid header")
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2024-05-18 13:19:27 +02:00
|
|
|
return nil, err
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
|
|
|
headerSize += size
|
|
|
|
}
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2024-07-14 15:22:05 +02:00
|
|
|
|
|
|
|
var s io.ReadSeeker = newSectionReader(src, headerSize, dataSize)
|
2018-03-27 05:10:46 +02:00
|
|
|
|
2017-01-15 16:03:18 +01:00
|
|
|
if mono || bitsPerSample != 16 {
|
2024-07-21 02:59:06 +02:00
|
|
|
s = convert.NewStereoI16(s, mono, bitsPerSample != 16)
|
2017-01-15 16:03:18 +01:00
|
|
|
if mono {
|
|
|
|
dataSize *= 2
|
|
|
|
}
|
|
|
|
if bitsPerSample != 16 {
|
|
|
|
dataSize *= 2
|
|
|
|
}
|
|
|
|
}
|
2024-05-18 13:19:27 +02:00
|
|
|
return &Stream{
|
|
|
|
inner: s,
|
|
|
|
size: dataSize,
|
|
|
|
sampleRate: sampleRate,
|
|
|
|
}, nil
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2021-01-06 15:09:09 +01:00
|
|
|
|
|
|
|
// Decode decodes WAV (RIFF) data to playable stream.
|
|
|
|
//
|
|
|
|
// The format must be 1 or 2 channels, 8bit or 16bit little endian PCM.
|
|
|
|
// The format is converted into 2 channels and 16bit.
|
|
|
|
//
|
|
|
|
// 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:35:24 +02:00
|
|
|
// The returned Stream's Seek is available only when src is an io.Seeker.
|
|
|
|
//
|
2021-01-06 15:09:09 +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:35:24 +02:00
|
|
|
func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
|
2021-01-06 15:09:09 +01:00
|
|
|
return DecodeWithSampleRate(context.SampleRate(), src)
|
|
|
|
}
|