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"
|
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 {
|
2016-03-29 18:30:40 +02:00
|
|
|
src audio.ReadSeekCloser
|
|
|
|
headerSize int64
|
|
|
|
dataSize 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.
|
2016-03-27 12:10:16 +02:00
|
|
|
func (s *Stream) Read(p []byte) (int, error) {
|
2016-03-29 18:30:40 +02:00
|
|
|
return s.src.Read(p)
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:47:25 +02:00
|
|
|
// Seek is implementation of io.Seeker's Seek.
|
2016-03-27 12:10:16 +02:00
|
|
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
2016-03-29 19:54:25 +02:00
|
|
|
if whence == 0 {
|
|
|
|
offset += s.headerSize
|
|
|
|
}
|
|
|
|
return s.src.Seek(offset, whence)
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:47:25 +02:00
|
|
|
// Read is implementation of io.Closer's Close.
|
2016-03-28 17:06:37 +02: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
|
|
|
}
|
|
|
|
|
2016-04-18 19:00:16 +02:00
|
|
|
// Size returns the size of decoded stream in bytes.
|
2016-03-28 19:55:30 +02:00
|
|
|
func (s *Stream) Size() 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.
|
|
|
|
//
|
|
|
|
// The format must be 2 channels, 16bit little endian PCM.
|
|
|
|
// The sample rate must be same as that of audio context.
|
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)
|
|
|
|
headerSize := int64(0)
|
|
|
|
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 ")):
|
|
|
|
if size != 16 {
|
|
|
|
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
|
2016-03-29 18:30:40 +02:00
|
|
|
// TODO: Remove this magic number
|
2016-03-29 20:03:08 +02:00
|
|
|
if channelNum != 2 {
|
|
|
|
return nil, fmt.Errorf("wav: channel num must be 2")
|
2016-03-29 18:30:40 +02:00
|
|
|
}
|
2016-03-29 20:03:08 +02:00
|
|
|
bitsPerSample := int(buf[14]) | int(buf[15])<<8
|
2016-03-29 18:30:40 +02:00
|
|
|
// TODO: Remove this magic number
|
2016-03-29 20:03:08 +02:00
|
|
|
if bitsPerSample != 16 {
|
|
|
|
return nil, fmt.Errorf("wav: bits per sample must be 16")
|
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 {
|
|
|
|
return nil, fmt.Errorf("wav: sample rate must be %d but %d", context.SampleRate(), sampleRate)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
s := &Stream{
|
2016-03-29 18:30:40 +02:00
|
|
|
src: src,
|
|
|
|
headerSize: headerSize,
|
|
|
|
dataSize: dataSize,
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|