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"
|
|
|
|
|
2016-04-18 18:24:44 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
2017-01-15 14:37:59 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/audio/internal/convert"
|
2016-03-27 12:10:16 +02:00
|
|
|
)
|
|
|
|
|
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 {
|
2017-01-15 14:37:59 +01:00
|
|
|
inner audio.ReadSeekCloser
|
|
|
|
size int64
|
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.
|
|
|
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
return s.inner.Seek(offset, whence)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read is implementation of io.Closer's Close.
|
|
|
|
func (s *Stream) Close() error {
|
|
|
|
return s.inner.Close()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-08 17:25:38 +01:00
|
|
|
// Size is deprecated as of version 1.6.0-alpha. Use Length instead.
|
|
|
|
func (s *Stream) Size() int64 {
|
|
|
|
return s.Length()
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:42:25 +01:00
|
|
|
type stream struct {
|
2016-03-29 18:30:40 +02:00
|
|
|
src audio.ReadSeekCloser
|
|
|
|
headerSize int64
|
|
|
|
dataSize int64
|
2016-09-26 17:37:40 +02:00
|
|
|
remaining int64
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:47:25 +02:00
|
|
|
// Read is implementation of io.Reader's Read.
|
2017-01-14 17:42:25 +01:00
|
|
|
func (s *stream) Read(p []byte) (int, error) {
|
2016-09-26 17:37:40 +02:00
|
|
|
if s.remaining <= 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
if s.remaining < int64(len(p)) {
|
|
|
|
p = p[0:s.remaining]
|
|
|
|
}
|
|
|
|
n, err := s.src.Read(p)
|
|
|
|
s.remaining -= int64(n)
|
|
|
|
return n, err
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:47:25 +02:00
|
|
|
// Seek is implementation of io.Seeker's Seek.
|
2017-01-14 17:42:25 +01:00
|
|
|
func (s *stream) Seek(offset int64, whence int) (int64, error) {
|
2017-01-16 14:29:20 +01:00
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
offset = offset + s.headerSize
|
|
|
|
case io.SeekCurrent:
|
|
|
|
case io.SeekEnd:
|
|
|
|
offset = s.headerSize + s.dataSize + offset
|
|
|
|
whence = io.SeekStart
|
2016-03-29 19:54:25 +02:00
|
|
|
}
|
2016-09-26 17:37:40 +02:00
|
|
|
n, err := s.src.Seek(offset, whence)
|
2016-09-26 19:13:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if n-s.headerSize < 0 {
|
|
|
|
return 0, fmt.Errorf("wav: invalid offset")
|
|
|
|
}
|
2016-09-26 17:37:40 +02:00
|
|
|
s.remaining = s.dataSize - (n - s.headerSize)
|
2017-01-14 17:30:28 +01:00
|
|
|
// There could be a tail in wav file.
|
|
|
|
if s.remaining < 0 {
|
|
|
|
s.remaining = 0
|
|
|
|
return s.dataSize, nil
|
|
|
|
}
|
2017-01-14 17:16:36 +01:00
|
|
|
return n - s.headerSize, nil
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2017-10-01 11:07:18 +02:00
|
|
|
// Close is implementation of io.Closer's Close.
|
2017-01-14 17:42:25 +01:00
|
|
|
func (s *stream) Close() error {
|
2016-03-29 18:30:40 +02:00
|
|
|
return s.src.Close()
|
2016-03-28 17:06:37 +02:00
|
|
|
}
|
|
|
|
|
2018-01-08 17:25:38 +01:00
|
|
|
// Length returns the size of decoded stream in bytes.
|
|
|
|
func (s *stream) Length() int64 {
|
2016-03-29 18:30:40 +02:00
|
|
|
return s.dataSize
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 19:00:16 +02:00
|
|
|
// Decode decodes WAV (RIFF) data to playable stream.
|
|
|
|
//
|
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
|
|
|
//
|
2017-10-01 11:07:18 +02:00
|
|
|
// Decode returns error when decoding fails or IO error happens.
|
2017-04-17 18:25:23 +02:00
|
|
|
//
|
2017-10-01 11:07:18 +02:00
|
|
|
// Decode automatically resamples the stream to fit with the audio context if necessary.
|
2016-03-29 18:30:40 +02:00
|
|
|
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
|
|
|
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) {
|
2016-03-27 12:10:16 +02:00
|
|
|
return nil, fmt.Errorf("wav: invalid header")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-29 18:30:40 +02:00
|
|
|
if !bytes.Equal(buf[0:4], []byte("RIFF")) {
|
|
|
|
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")) {
|
|
|
|
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
|
|
|
|
dataSize := int64(0)
|
2017-01-16 15:36:09 +01:00
|
|
|
headerSize := int64(len(buf))
|
2017-01-14 17:42:25 +01:00
|
|
|
sampleRateFrom := 0
|
|
|
|
sampleRateTo := 0
|
2017-01-15 16:03:18 +01:00
|
|
|
mono := false
|
|
|
|
bitsPerSample := 0
|
2016-03-29 18:30:40 +02:00
|
|
|
chunks:
|
|
|
|
for {
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
n, err := io.ReadFull(src, buf)
|
|
|
|
if n != len(buf) {
|
|
|
|
return nil, fmt.Errorf("wav: invalid header")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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 {
|
2016-03-29 18:30:40 +02:00
|
|
|
return nil, fmt.Errorf("wav: invalid header: maybe non-PCM file?")
|
|
|
|
}
|
|
|
|
buf := make([]byte, size)
|
|
|
|
n, err := io.ReadFull(src, buf)
|
|
|
|
if n != len(buf) {
|
|
|
|
return nil, fmt.Errorf("wav: invalid header")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-29 20:03:08 +02:00
|
|
|
format := int(buf[0]) | int(buf[1])<<8
|
|
|
|
if format != 1 {
|
|
|
|
return nil, fmt.Errorf("wav: format must be linear PCM")
|
|
|
|
}
|
|
|
|
channelNum := int(buf[2]) | int(buf[3])<<8
|
2017-01-15 16:03:18 +01:00
|
|
|
switch channelNum {
|
|
|
|
case 1:
|
|
|
|
mono = true
|
|
|
|
case 2:
|
|
|
|
mono = false
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("wav: channel num must be 1 or 2 but was %d", channelNum)
|
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 {
|
|
|
|
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
|
|
|
}
|
2016-05-13 09:19:02 +02:00
|
|
|
sampleRate := int64(buf[4]) | int64(buf[5])<<8 | int64(buf[6])<<16 | int64(buf[7])<<24
|
2016-03-29 18:30:40 +02:00
|
|
|
if int64(context.SampleRate()) != sampleRate {
|
2017-01-14 17:42:25 +01:00
|
|
|
sampleRateFrom = int(sampleRate)
|
|
|
|
sampleRateTo = context.SampleRate()
|
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) {
|
|
|
|
return nil, fmt.Errorf("wav: invalid header")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
headerSize += size
|
|
|
|
}
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2017-01-15 16:03:18 +01:00
|
|
|
var s audio.ReadSeekCloser = &stream{
|
2016-03-29 18:30:40 +02:00
|
|
|
src: src,
|
|
|
|
headerSize: headerSize,
|
|
|
|
dataSize: dataSize,
|
2016-09-26 17:37:40 +02:00
|
|
|
remaining: dataSize,
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2017-01-15 16:03:18 +01:00
|
|
|
if mono || bitsPerSample != 16 {
|
|
|
|
s = convert.NewStereo16(s, mono, bitsPerSample != 16)
|
|
|
|
if mono {
|
|
|
|
dataSize *= 2
|
|
|
|
}
|
|
|
|
if bitsPerSample != 16 {
|
|
|
|
dataSize *= 2
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 17:42:25 +01:00
|
|
|
if sampleRateFrom != sampleRateTo {
|
2017-07-11 17:51:36 +02:00
|
|
|
r := convert.NewResampling(s, dataSize, sampleRateFrom, sampleRateTo)
|
|
|
|
s = r
|
2018-01-08 17:25:38 +01:00
|
|
|
dataSize = r.Length()
|
2017-01-14 17:42:25 +01:00
|
|
|
}
|
2017-06-03 18:03:01 +02:00
|
|
|
return &Stream{inner: s, size: dataSize}, nil
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|