mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
audio: Convert 8bit monoral to 16bit stereo (#282)
This commit is contained in:
parent
5bdcd8825c
commit
ed8d87dd87
97
audio/internal/convert/stereo16.go
Normal file
97
audio/internal/convert/stereo16.go
Normal file
@ -0,0 +1,97 @@
|
||||
// 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.
|
||||
|
||||
package convert
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/audio"
|
||||
)
|
||||
|
||||
type Stereo16 struct {
|
||||
source audio.ReadSeekCloser
|
||||
mono bool
|
||||
eight bool
|
||||
}
|
||||
|
||||
func NewStereo16(source audio.ReadSeekCloser, mono, eight bool) *Stereo16 {
|
||||
return &Stereo16{
|
||||
source: source,
|
||||
mono: mono,
|
||||
eight: eight,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Stereo16) Read(b []uint8) (int, error) {
|
||||
l := len(b)
|
||||
if s.mono {
|
||||
l /= 2
|
||||
}
|
||||
if s.eight {
|
||||
l /= 2
|
||||
}
|
||||
buf := make([]uint8, l)
|
||||
n, err := s.source.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
return 0, err
|
||||
}
|
||||
switch {
|
||||
case s.mono && s.eight:
|
||||
for i := 0; i < n; i++ {
|
||||
v := (int16(buf[i]) - 128) << 8
|
||||
b[4*i] = uint8(v)
|
||||
b[4*i+1] = uint8(v >> 8)
|
||||
b[4*i+2] = uint8(v)
|
||||
b[4*i+3] = uint8(v >> 8)
|
||||
}
|
||||
case s.mono && !s.eight:
|
||||
for i := 0; i < n/2; i++ {
|
||||
b[4*i] = buf[2*i]
|
||||
b[4*i+1] = buf[2*i+1]
|
||||
b[4*i+2] = buf[2*i]
|
||||
b[4*i+3] = buf[2*i+1]
|
||||
}
|
||||
case !s.mono && s.eight:
|
||||
for i := 0; i < n/2; i++ {
|
||||
v0 := (int16(buf[2*i]) - 128) << 8
|
||||
v1 := (int16(buf[2*i+1]) - 128) << 8
|
||||
b[4*i] = uint8(v0)
|
||||
b[4*i+1] = uint8(v0 >> 8)
|
||||
b[4*i+2] = uint8(v1)
|
||||
b[4*i+3] = uint8(v1 >> 8)
|
||||
}
|
||||
}
|
||||
if s.mono {
|
||||
n *= 2
|
||||
}
|
||||
if s.eight {
|
||||
n *= 2
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (s *Stereo16) Seek(offset int64, whence int) (int64, error) {
|
||||
if s.mono {
|
||||
offset /= 2
|
||||
}
|
||||
if s.eight {
|
||||
offset /= 2
|
||||
}
|
||||
return s.source.Seek(offset, whence)
|
||||
}
|
||||
|
||||
func (s *Stereo16) Close() error {
|
||||
return s.source.Close()
|
||||
}
|
@ -25,14 +25,10 @@ import (
|
||||
"github.com/jfreymuth/oggvorbis"
|
||||
)
|
||||
|
||||
type readSeekCloseSizer interface {
|
||||
audio.ReadSeekCloser
|
||||
Size() int64
|
||||
}
|
||||
|
||||
// Stream is a decoded audio stream.
|
||||
type Stream struct {
|
||||
decoded readSeekCloseSizer
|
||||
decoded audio.ReadSeekCloser
|
||||
size int64
|
||||
}
|
||||
|
||||
// Read is implementation of io.Reader's Read.
|
||||
@ -54,7 +50,7 @@ func (s *Stream) Close() error {
|
||||
|
||||
// Size returns the size of decoded stream in bytes.
|
||||
func (s *Stream) Size() int64 {
|
||||
return s.decoded.Size()
|
||||
return s.size
|
||||
}
|
||||
|
||||
type decoded struct {
|
||||
@ -184,13 +180,17 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// TODO: Remove this magic number
|
||||
if channelNum != 2 {
|
||||
return nil, fmt.Errorf("vorbis: number of channels must be 2")
|
||||
if channelNum != 1 && channelNum != 2 {
|
||||
return nil, fmt.Errorf("vorbis: number of channels must be 1 or 2 but was %d", channelNum)
|
||||
}
|
||||
var s audio.ReadSeekCloser = decoded
|
||||
size := decoded.Size()
|
||||
if channelNum == 1 {
|
||||
s = convert.NewStereo16(s, true, false)
|
||||
size *= 2
|
||||
}
|
||||
if sampleRate != context.SampleRate() {
|
||||
s := convert.NewResampling(decoded, decoded.Size(), sampleRate, context.SampleRate())
|
||||
return &Stream{s}, nil
|
||||
s = convert.NewResampling(s, size, sampleRate, context.SampleRate())
|
||||
}
|
||||
return &Stream{decoded}, nil
|
||||
return &Stream{s, size}, nil
|
||||
}
|
||||
|
@ -105,7 +105,8 @@ func (s *stream) Size() int64 {
|
||||
|
||||
// Decode decodes WAV (RIFF) data to playable stream.
|
||||
//
|
||||
// The format must be 2 channels, 16bit little endian PCM.
|
||||
// The format must be 1 or 2 channels, 8bit or 16bit little endian PCM.
|
||||
// The format is converted into 2 channels and 16bit.
|
||||
//
|
||||
// Sample rate is automatically adjusted to fit with the audio context.
|
||||
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
||||
@ -129,6 +130,8 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
||||
headerSize := int64(0)
|
||||
sampleRateFrom := 0
|
||||
sampleRateTo := 0
|
||||
mono := false
|
||||
bitsPerSample := 0
|
||||
chunks:
|
||||
for {
|
||||
buf := make([]byte, 8)
|
||||
@ -160,14 +163,17 @@ chunks:
|
||||
return nil, fmt.Errorf("wav: format must be linear PCM")
|
||||
}
|
||||
channelNum := int(buf[2]) | int(buf[3])<<8
|
||||
// TODO: Remove this magic number
|
||||
if channelNum != 2 {
|
||||
return nil, fmt.Errorf("wav: channel num must be 2")
|
||||
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)
|
||||
}
|
||||
bitsPerSample := int(buf[14]) | int(buf[15])<<8
|
||||
// TODO: Remove this magic number
|
||||
if bitsPerSample != 16 {
|
||||
return nil, fmt.Errorf("wav: bits per sample must be 16")
|
||||
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)
|
||||
}
|
||||
sampleRate := int64(buf[4]) | int64(buf[5])<<8 | int64(buf[6])<<16 | int64(buf[7])<<24
|
||||
if int64(context.SampleRate()) != sampleRate {
|
||||
@ -190,13 +196,21 @@ chunks:
|
||||
headerSize += size
|
||||
}
|
||||
}
|
||||
var s audio.ReadSeekCloser
|
||||
s = &stream{
|
||||
var s audio.ReadSeekCloser = &stream{
|
||||
src: src,
|
||||
headerSize: headerSize,
|
||||
dataSize: dataSize,
|
||||
remaining: dataSize,
|
||||
}
|
||||
if mono || bitsPerSample != 16 {
|
||||
s = convert.NewStereo16(s, mono, bitsPerSample != 16)
|
||||
if mono {
|
||||
dataSize *= 2
|
||||
}
|
||||
if bitsPerSample != 16 {
|
||||
dataSize *= 2
|
||||
}
|
||||
}
|
||||
if sampleRateFrom != sampleRateTo {
|
||||
s = convert.NewResampling(s, dataSize, sampleRateFrom, sampleRateTo)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user