2018-07-03 14:09:12 +02:00
|
|
|
// Copyright 2018 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 stb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-05 16:59:30 +02:00
|
|
|
"io"
|
2018-07-03 14:09:12 +02:00
|
|
|
|
|
|
|
"github.com/gopherjs/gopherwasm/js"
|
|
|
|
)
|
|
|
|
|
2018-07-21 23:27:57 +02:00
|
|
|
var flatten = js.Global().Get("window").Call("eval", `(function(arr) {
|
|
|
|
var ch = arr.length;
|
|
|
|
var len = arr[0].length;
|
|
|
|
var result = new Float32Array(ch * len);
|
|
|
|
for (var j = 0; j < len; j++) {
|
|
|
|
for (var i = 0; i < ch; i++) {
|
|
|
|
result[j*ch+i] = arr[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
})`)
|
|
|
|
|
2018-07-03 14:09:12 +02:00
|
|
|
func init() {
|
|
|
|
// Eval wasm.js first to set the Wasm binary to Module.
|
|
|
|
js.Global().Get("window").Call("eval", string(stbvorbis_js))
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:59:30 +02:00
|
|
|
type Samples struct {
|
|
|
|
samples [][]float32
|
|
|
|
length int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Samples) Read(buf []float32) (int, error) {
|
|
|
|
if len(s.samples) == 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n := copy(buf, s.samples[0])
|
|
|
|
s.samples[0] = s.samples[0][n:]
|
|
|
|
if len(s.samples[0]) == 0 {
|
|
|
|
s.samples = s.samples[1:]
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Samples) Length() int64 {
|
|
|
|
return s.length
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeVorbis(buf []byte) (*Samples, int, int, error) {
|
2018-10-05 13:55:23 +02:00
|
|
|
ch := make(chan error)
|
2018-10-05 16:59:30 +02:00
|
|
|
samples := &Samples{}
|
2018-10-05 13:55:23 +02:00
|
|
|
channels := 0
|
|
|
|
sampleRate := 0
|
|
|
|
|
2018-07-21 23:27:57 +02:00
|
|
|
var f js.Callback
|
|
|
|
f = js.NewCallback(func(args []js.Value) {
|
2018-10-05 13:55:23 +02:00
|
|
|
r := args[0]
|
|
|
|
|
|
|
|
if e := r.Get("error"); e != js.Null() {
|
|
|
|
ch <- fmt.Errorf("audio/vorbis/internal/stb: decode error: %s", e.String())
|
|
|
|
close(ch)
|
|
|
|
f.Release()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Get("eof").Bool() {
|
|
|
|
close(ch)
|
|
|
|
f.Release()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if channels == 0 {
|
|
|
|
channels = r.Get("data").Length()
|
|
|
|
}
|
|
|
|
if sampleRate == 0 {
|
|
|
|
sampleRate = r.Get("sampleRate").Int()
|
|
|
|
}
|
|
|
|
|
|
|
|
flattened := flatten.Invoke(r.Get("data"))
|
2018-10-05 16:59:30 +02:00
|
|
|
s := make([]float32, flattened.Length())
|
|
|
|
arr := js.TypedArrayOf(s)
|
2018-10-05 13:55:23 +02:00
|
|
|
arr.Call("set", flattened)
|
|
|
|
arr.Release()
|
|
|
|
|
2018-10-05 16:59:30 +02:00
|
|
|
samples.samples = append(samples.samples, s)
|
|
|
|
samples.length += int64(len(s)) / int64(channels)
|
2018-07-21 23:27:57 +02:00
|
|
|
})
|
2018-10-05 13:55:23 +02:00
|
|
|
|
|
|
|
arr := js.TypedArrayOf(buf)
|
|
|
|
js.Global().Get("stbvorbis").Call("decode", arr, f)
|
2018-07-07 10:45:00 +02:00
|
|
|
arr.Release()
|
2018-07-21 23:27:57 +02:00
|
|
|
|
2018-10-05 13:55:23 +02:00
|
|
|
if err := <-ch; err != nil {
|
|
|
|
return nil, 0, 0, err
|
2018-07-03 14:09:12 +02:00
|
|
|
}
|
2018-07-07 10:45:00 +02:00
|
|
|
|
2018-10-05 16:59:30 +02:00
|
|
|
return samples, channels, sampleRate, nil
|
2018-07-03 14:09:12 +02:00
|
|
|
}
|