audio/vorbis: Remove the GopherJS part

Updates #1129
This commit is contained in:
Hajime Hoshi 2020-10-07 01:25:00 +09:00
parent a22fdee171
commit 426cb5948a
7 changed files with 3 additions and 263 deletions

View File

@ -1,141 +0,0 @@
// 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.
// +build js,!wasm
package stb
import (
"fmt"
"io"
"syscall/js"
)
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;
})`)
func init() {
// Eval wasm.js first to set the Wasm binary to Module.
js.Global().Get("window").Call("eval", string(stbvorbis_js))
}
type Samples struct {
samples [][]float32
channels int
lengthInSamples int64
posInSamples int64
}
func (s *Samples) Read(buf []float32) (int, error) {
if s.posInSamples == s.lengthInSamples {
return 0, io.EOF
}
if len(buf) == 0 {
return 0, nil
}
var p int64
idx := 0
for idx < len(s.samples) {
l := int64(len(s.samples[idx])) / int64(s.channels)
if p+l > s.posInSamples {
break
}
p += l
idx++
}
start := (s.posInSamples - p) * int64(s.channels)
if start == int64(len(s.samples[idx])) {
idx++
start = 0
}
if len(s.samples[idx]) == 0 {
panic(fmt.Sprintf("stb: len(samples[%d]) must be > 0", idx))
}
n := copy(buf, s.samples[idx][start:])
s.posInSamples += int64(n) / int64(s.channels)
return n, nil
}
func (s *Samples) Length() int64 {
return s.lengthInSamples
}
func (s *Samples) SetPosition(pos int64) error {
s.posInSamples = pos
return nil
}
func DecodeVorbis(buf []byte) (*Samples, int, int, error) {
ch := make(chan error)
samples := &Samples{}
sampleRate := 0
f := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
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)
return nil
}
if r.Get("eof").Bool() {
close(ch)
return nil
}
if samples.channels == 0 {
samples.channels = r.Get("data").Length()
}
if sampleRate == 0 {
sampleRate = r.Get("sampleRate").Int()
}
flattened := flatten.Invoke(r.Get("data"))
if flattened.Length() == 0 {
return nil
}
s := make([]float32, flattened.Length())
arr := js.TypedArrayOf(s)
arr.Call("set", flattened)
arr.Release()
samples.samples = append(samples.samples, s)
samples.lengthInSamples += int64(len(s)) / int64(samples.channels)
return nil
})
defer f.Release()
arr := js.TypedArrayOf(buf)
js.Global().Get("stbvorbis").Call("decode", arr, f)
arr.Release()
if err := <-ch; err != nil {
return nil, 0, 0, err
}
return samples, samples.channels, sampleRate, nil
}

View File

@ -1,22 +0,0 @@
// 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
// stbvorbis.js
// URL: https://github.com/hajimehoshi/stbvorbis.js
// License: Apache License 2.0
// Commit: ac1c2ee9d24eb6085eb1e968f55e0fb32cacc03a
//go:generate file2byteslice -package=stb -input=stbvorbis.js -output=stbvorbis.js.go -var=stbvorbis_js -buildtags "js,!wasm"

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -20,6 +20,8 @@ import (
"io"
"runtime"
"github.com/jfreymuth/oggvorbis"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
)
@ -136,7 +138,7 @@ func (d *decoded) Length() int64 {
// decode accepts an ogg stream and returns a decorded stream.
func decode(in audio.ReadSeekCloser) (*decoded, int, int, error) {
r, err := newDecoder(in)
r, err := oggvorbis.NewReader(in)
if err != nil {
return nil, 0, 0, err
}

View File

@ -1,64 +0,0 @@
// 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.
// +build js
// +build !wasm
package vorbis
import (
"io/ioutil"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis/internal/stb"
)
type decoderImpl struct {
samples *stb.Samples
channels int
sampleRate int
}
func (d *decoderImpl) Read(buf []float32) (int, error) {
return d.samples.Read(buf)
}
func (d *decoderImpl) Length() int64 {
return d.samples.Length()
}
func (d *decoderImpl) Channels() int {
return d.channels
}
func (d *decoderImpl) SampleRate() int {
return d.sampleRate
}
func (d *decoderImpl) SetPosition(pos int64) error {
return d.samples.SetPosition(pos)
}
func newDecoder(in audio.ReadSeekCloser) (decoder, error) {
buf, err := ioutil.ReadAll(in)
if err != nil {
return nil, err
}
samples, channels, sampleRate, err := stb.DecodeVorbis(buf)
if err != nil {
return nil, err
}
return &decoderImpl{samples, channels, sampleRate}, nil
}

View File

@ -1,27 +0,0 @@
// 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.
// +build !js wasm
package vorbis
import (
"github.com/jfreymuth/oggvorbis"
"github.com/hajimehoshi/ebiten/v2/audio"
)
func newDecoder(in audio.ReadSeekCloser) (decoder, error) {
return oggvorbis.NewReader(in)
}