2016-03-07 20:20:59 +01: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.
|
|
|
|
|
|
|
|
// +build js
|
|
|
|
|
2016-03-12 19:00:05 +01:00
|
|
|
package vorbis
|
2016-03-07 20:20:59 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/gopherjs/gopherjs/js"
|
2016-03-12 19:00:05 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/exp/audio"
|
2016-03-07 20:20:59 +01:00
|
|
|
)
|
|
|
|
|
2016-03-12 19:00:05 +01:00
|
|
|
type Stream struct {
|
2016-03-07 20:20:59 +01:00
|
|
|
buf *bytes.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2016-03-12 19:00:05 +01:00
|
|
|
func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
|
2016-03-07 20:20:59 +01:00
|
|
|
b, err := ioutil.ReadAll(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-12 19:00:05 +01:00
|
|
|
s := &Stream{}
|
2016-03-07 20:20:59 +01:00
|
|
|
ch := make(chan struct{})
|
|
|
|
|
|
|
|
// TODO: 1 is a correct second argument?
|
2016-03-12 19:00:05 +01:00
|
|
|
oc := js.Global.Get("OfflineAudioContext").New(2, 1, context.SampleRate())
|
2016-03-08 17:36:19 +01:00
|
|
|
oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) {
|
2016-03-07 20:20:59 +01:00
|
|
|
defer close(ch)
|
|
|
|
il := buf.Call("getChannelData", 0).Interface().([]float32)
|
|
|
|
ir := buf.Call("getChannelData", 1).Interface().([]float32)
|
|
|
|
b := make([]byte, len(il)*4)
|
|
|
|
for i := 0; i < len(il); i++ {
|
|
|
|
l := int16(il[i] * (1 << 15))
|
|
|
|
r := int16(ir[i] * (1 << 15))
|
|
|
|
b[4*i] = uint8(l)
|
|
|
|
b[4*i+1] = uint8(l >> 8)
|
|
|
|
b[4*i+2] = uint8(r)
|
|
|
|
b[4*i+3] = uint8(r >> 8)
|
|
|
|
}
|
|
|
|
s.buf = bytes.NewReader(b)
|
|
|
|
})
|
|
|
|
<-ch
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2016-03-12 19:00:05 +01:00
|
|
|
func (s *Stream) Read(p []byte) (int, error) {
|
2016-03-07 20:20:59 +01:00
|
|
|
return s.buf.Read(p)
|
|
|
|
}
|
|
|
|
|
2016-03-12 19:00:05 +01:00
|
|
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
2016-03-07 20:20:59 +01:00
|
|
|
return s.buf.Seek(offset, whence)
|
|
|
|
}
|