From b454ff654703c7baa0919f13452b9f57499f0503 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 29 Nov 2016 01:01:52 +0900 Subject: [PATCH] audio/vorbis: Use oggvorbis for browsers --- audio/vorbis/decode.go | 2 - audio/vorbis/decode_js.go | 127 -------------------------------------- audio/vorbis/vorbis.go | 47 ++++++++++---- 3 files changed, 35 insertions(+), 141 deletions(-) delete mode 100644 audio/vorbis/decode_js.go diff --git a/audio/vorbis/decode.go b/audio/vorbis/decode.go index 9722f20aa..364f0203b 100644 --- a/audio/vorbis/decode.go +++ b/audio/vorbis/decode.go @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -// +build !js - package vorbis import ( diff --git a/audio/vorbis/decode_js.go b/audio/vorbis/decode_js.go deleted file mode 100644 index a21d36f7c..000000000 --- a/audio/vorbis/decode_js.go +++ /dev/null @@ -1,127 +0,0 @@ -// 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. - -// +build js - -package vorbis - -import ( - "errors" - "io" - "io/ioutil" - - "github.com/gopherjs/gopherjs/js" - "github.com/hajimehoshi/ebiten/audio" -) - -// TODO: This just uses decodeAudioData can treat audio files other than Ogg/Vorbis. -// TODO: This doesn't work on iOS which doesn't have Ogg/Vorbis decoder. - -type Stream struct { - leftData []float32 - rightData []float32 - posInBytes int -} - -func (s *Stream) Read(b []byte) (int, error) { - l := len(s.leftData)*4 - s.posInBytes - if l > len(b) { - l = len(b) - } - l = l / 4 * 4 - for i := 0; i < l/4; i++ { - l := s.leftData[s.posInBytes/4+i] - r := s.rightData[s.posInBytes/4+i] - if 1 < l { - l = 1 - } - if l < -1 { - l = -1 - } - if 1 < r { - r = 1 - } - if r < -1 { - r = -1 - } - il := int16(l * ((1 << 15) - 1)) - ir := int16(r * ((1 << 15) - 1)) - b[4*i] = uint8(il) - b[4*i+1] = uint8(il >> 8) - b[4*i+2] = uint8(ir) - b[4*i+3] = uint8(ir >> 8) - } - s.posInBytes += l - if s.posInBytes == len(s.leftData)*4 { - return l, io.EOF - } - return l, nil -} - -func (s *Stream) Seek(offset int64, whence int) (int64, error) { - next := int64(0) - switch whence { - case io.SeekStart: - next = offset - case io.SeekCurrent: - next = int64(s.posInBytes) + offset - case io.SeekEnd: - next = int64(len(s.leftData)*4) + offset - } - s.posInBytes = int(next) - return next, nil -} - -func (s *Stream) Close() error { - return nil -} - -func (s *Stream) Size() int64 { - return int64(len(s.leftData) * 4) -} - -func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) { - b, err := ioutil.ReadAll(src) - if err != nil { - return nil, err - } - if err := src.Close(); err != nil { - return nil, err - } - s := &Stream{} - ch := make(chan error) - - klass := js.Global.Get("OfflineAudioContext") - if klass == js.Undefined { - klass = js.Global.Get("webkitOfflineAudioContext") - } - if klass == js.Undefined { - return nil, errors.New("vorbis: OfflineAudioContext is not available") - } - // TODO: 1 is a correct second argument? - // At least, 1 works on Chrome and Firefox but not Safari. - oc := klass.New(2, 1, context.SampleRate()) - oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) { - s.leftData = buf.Call("getChannelData", 0).Interface().([]float32) - s.rightData = buf.Call("getChannelData", 1).Interface().([]float32) - close(ch) - }, func() { - ch <- errors.New("vorbis: decodeAudioData failed (Safari might not be able to decode Ogg/Vorbis).") - close(ch) - }) - if err := <-ch; err != nil { - return nil, err - } - return s, nil -} diff --git a/audio/vorbis/vorbis.go b/audio/vorbis/vorbis.go index 4d8be8aae..5a18a38d2 100644 --- a/audio/vorbis/vorbis.go +++ b/audio/vorbis/vorbis.go @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -// +build !js - // Package vorbis provides Ogg/Vorbis decoder. package vorbis @@ -21,22 +19,46 @@ import ( "io" "runtime" + "github.com/hajimehoshi/ebiten/audio" "github.com/jfreymuth/oggvorbis" ) type decoded struct { data []float32 + totalBytes int posInBytes int + source io.Closer + decoder *oggvorbis.Reader } func (d *decoded) Read(b []byte) (int, error) { - total := len(d.data) * 2 + total := d.totalBytes l := total - d.posInBytes if l > len(b) { l = len(b) } + if l < 0 { + return 0, io.EOF + } // l must be even so that d.posInBytes is always even. l = l / 2 * 2 + for len(d.data) < (d.posInBytes+l)/2 { + // TODO: What if the channel is closed? + buffer := make([]float32, 4096) + n, err := d.decoder.Read(buffer) + if n > 0 { + d.data = append(d.data, buffer[:n]...) + } + if err == io.EOF { + if err := d.source.Close(); err != nil { + return 0, err + } + break + } + if err != nil { + return 0, err + } + } for i := 0; i < l/2; i++ { f := d.data[d.posInBytes/2+i] s := int16(f * (1<<15 - 1)) @@ -58,7 +80,7 @@ func (d *decoded) Seek(offset int64, whence int) (int64, error) { case io.SeekCurrent: next = int64(d.posInBytes) + offset case io.SeekEnd: - next = int64(len(d.data)*2) + offset + next = int64(d.totalBytes) + offset } d.posInBytes = int(next) return next, nil @@ -70,22 +92,23 @@ func (d *decoded) Close() error { } func (d *decoded) Size() int64 { - return int64(len(d.data) * 2) + return int64(d.totalBytes) } // decode accepts an ogg stream and returns a decorded stream. -func decode(in io.ReadCloser) (*decoded, int, int, error) { - data, format, err := oggvorbis.ReadAll(in) +func decode(in audio.ReadSeekCloser) (*decoded, int, int, error) { + // TODO: Lazy evaluation + r, err := oggvorbis.NewReader(in) if err != nil { return nil, 0, 0, err } - if err := in.Close(); err != nil { - return nil, 0, 0, err - } d := &decoded{ - data: data, + data: []float32{}, + totalBytes: int(r.Length()) * 4, // TODO: What if length is 0? posInBytes: 0, + source: in, + decoder: r, } runtime.SetFinalizer(d, (*decoded).Close) - return d, format.Channels, format.SampleRate, nil + return d, r.Channels(), r.SampleRate(), nil }