mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-27 04:08:53 +01:00
audio/mp3: Use gopherwasm
This commit is contained in:
parent
ff86f8bd93
commit
5aefa837db
@ -24,8 +24,8 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gopherjs/gopherjs/js"
|
|
||||||
"github.com/hajimehoshi/ebiten/audio"
|
"github.com/hajimehoshi/ebiten/audio"
|
||||||
|
"github.com/hajimehoshi/gopherwasm/js"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: This just uses decodeAudioData, that can treat audio files other than MP3.
|
// TODO: This just uses decodeAudioData, that can treat audio files other than MP3.
|
||||||
@ -159,7 +159,7 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
|||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var offlineAudioContextClass *js.Object
|
var offlineAudioContextClass = js.Null
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if klass := js.Global.Get("OfflineAudioContext"); klass != js.Undefined {
|
if klass := js.Global.Get("OfflineAudioContext"); klass != js.Undefined {
|
||||||
@ -173,7 +173,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
|
func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
|
||||||
if offlineAudioContextClass == nil {
|
if offlineAudioContextClass == js.Null {
|
||||||
return nil, errors.New("audio/mp3: OfflineAudioContext is not available")
|
return nil, errors.New("audio/mp3: OfflineAudioContext is not available")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,27 +186,30 @@ func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
|
|||||||
|
|
||||||
// TODO: 1 is a correct second argument?
|
// TODO: 1 is a correct second argument?
|
||||||
oc := offlineAudioContextClass.New(2, 1, context.SampleRate())
|
oc := offlineAudioContextClass.New(2, 1, context.SampleRate())
|
||||||
oc.Call("decodeAudioData", js.NewArrayBuffer(buf), func(buf *js.Object) {
|
a := js.ValueOf(buf).Get("buffer")
|
||||||
s.leftData = buf.Call("getChannelData", 0).Interface().([]float32)
|
oc.Call("decodeAudioData", a, js.NewCallback(func(args []js.Value) {
|
||||||
|
buf := args[0]
|
||||||
|
s.leftData = float32ArrayToSlice(buf.Call("getChannelData", 0))
|
||||||
switch n := buf.Get("numberOfChannels").Int(); n {
|
switch n := buf.Get("numberOfChannels").Int(); n {
|
||||||
case 1:
|
case 1:
|
||||||
s.rightData = s.leftData
|
s.rightData = s.leftData
|
||||||
close(ch)
|
close(ch)
|
||||||
case 2:
|
case 2:
|
||||||
s.rightData = buf.Call("getChannelData", 1).Interface().([]float32)
|
s.rightData = float32ArrayToSlice(buf.Call("getChannelData", 1))
|
||||||
close(ch)
|
close(ch)
|
||||||
default:
|
default:
|
||||||
ch <- fmt.Errorf("audio/mp3: number of channels must be 1 or 2 but %d", n)
|
ch <- fmt.Errorf("audio/mp3: number of channels must be 1 or 2 but %d", n)
|
||||||
}
|
}
|
||||||
}, func(err *js.Object) {
|
}), js.NewCallback(func(args []js.Value) {
|
||||||
if err != nil {
|
err := args[0]
|
||||||
|
if err != js.Null || err != js.Undefined {
|
||||||
ch <- fmt.Errorf("audio/mp3: decodeAudioData failed: %v", err)
|
ch <- fmt.Errorf("audio/mp3: decodeAudioData failed: %v", err)
|
||||||
} else {
|
} else {
|
||||||
// On Safari, error value might be null and it is needed to retry decoding
|
// On Safari, error value might be null and it is needed to retry decoding
|
||||||
// from the next frame (#438).
|
// from the next frame (#438).
|
||||||
ch <- errTryAgain
|
ch <- errTryAgain
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
|
|
||||||
timeout := time.Duration(math.Pow(2, float64(try))) * time.Second
|
timeout := time.Duration(math.Pow(2, float64(try))) * time.Second
|
||||||
|
|
||||||
|
26
audio/mp3/float32_notwasm.go
Normal file
26
audio/mp3/float32_notwasm.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// 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 mp3
|
||||||
|
|
||||||
|
import (
|
||||||
|
gjs "github.com/gopherjs/gopherjs/js"
|
||||||
|
"github.com/hajimehoshi/gopherwasm/js"
|
||||||
|
)
|
||||||
|
|
||||||
|
func float32ArrayToSlice(arr js.Value) []float32 {
|
||||||
|
return js.GetInternalObject(arr).(*gjs.Object).Interface().([]float32)
|
||||||
|
}
|
37
audio/mp3/float32_wasm.go
Normal file
37
audio/mp3/float32_wasm.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// 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 mp3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"syscall/js"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
func float32ArrayToSlice(arr js.Value) []float32 {
|
||||||
|
bytes := make([]byte, arr.Length()*4)
|
||||||
|
js.ValueOf(bytes).Call("set", arr.Get("buffer"))
|
||||||
|
|
||||||
|
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
|
||||||
|
var f []float32
|
||||||
|
fh := (*reflect.SliceHeader)(unsafe.Pointer(&f))
|
||||||
|
|
||||||
|
fh.Data = bh.Data
|
||||||
|
fh.Len = bh.Len / 4
|
||||||
|
fh.Cap = bh.Cap / 4
|
||||||
|
return f
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user