2021-01-06 19:20:36 +01:00
|
|
|
// Copyright 2021 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.
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
package readerdriver
|
2021-01-06 19:20:36 +01:00
|
|
|
|
|
|
|
import (
|
2021-02-20 17:47:39 +01:00
|
|
|
"errors"
|
2021-01-07 03:52:34 +01:00
|
|
|
"io"
|
2021-02-20 17:47:39 +01:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2021-05-09 17:47:03 +02:00
|
|
|
"sync"
|
2021-01-06 19:20:36 +01:00
|
|
|
"syscall/js"
|
2021-02-20 17:47:39 +01:00
|
|
|
"unsafe"
|
2021-01-07 03:52:34 +01:00
|
|
|
|
2021-01-09 10:21:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/internal/go2cpp"
|
2021-01-06 19:20:36 +01:00
|
|
|
)
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
type context struct {
|
2021-02-20 17:47:39 +01:00
|
|
|
audioContext js.Value
|
|
|
|
ready bool
|
|
|
|
callbacks map[string]js.Func
|
2021-03-28 11:07:38 +02:00
|
|
|
|
|
|
|
sampleRate int
|
|
|
|
channelNum int
|
|
|
|
bitDepthInBytes int
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:21:06 +02:00
|
|
|
func NewContext(sampleRate int, channelNum int, bitDepthInBytes int) (Context, chan struct{}, error) {
|
|
|
|
ready := make(chan struct{})
|
2021-02-20 17:47:39 +01:00
|
|
|
if js.Global().Get("go2cpp").Truthy() {
|
2021-04-29 11:21:06 +02:00
|
|
|
close(ready)
|
|
|
|
return &go2cppDriverWrapper{go2cpp.NewContext(sampleRate, channelNum, bitDepthInBytes)}, ready, nil
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class := js.Global().Get("AudioContext")
|
|
|
|
if !class.Truthy() {
|
|
|
|
class = js.Global().Get("webkitAudioContext")
|
|
|
|
}
|
|
|
|
if !class.Truthy() {
|
2021-04-29 11:21:06 +02:00
|
|
|
return nil, nil, errors.New("readerdriver: AudioContext or webkitAudioContext was not found")
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
options := js.Global().Get("Object").New()
|
2021-03-28 11:07:38 +02:00
|
|
|
options.Set("sampleRate", sampleRate)
|
2021-02-20 17:47:39 +01:00
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
d := &context{
|
2021-03-28 11:07:38 +02:00
|
|
|
audioContext: class.New(options),
|
|
|
|
sampleRate: sampleRate,
|
|
|
|
channelNum: channelNum,
|
|
|
|
bitDepthInBytes: bitDepthInBytes,
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setCallback := func(event string) js.Func {
|
|
|
|
var f js.Func
|
|
|
|
f = js.FuncOf(func(this js.Value, arguments []js.Value) interface{} {
|
|
|
|
if !d.ready {
|
|
|
|
d.audioContext.Call("resume")
|
|
|
|
d.ready = true
|
2021-04-29 11:21:06 +02:00
|
|
|
close(ready)
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
js.Global().Get("document").Call("removeEventListener", event, f)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
js.Global().Get("document").Call("addEventListener", event, f)
|
|
|
|
d.callbacks[event] = f
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browsers require user interaction to start the audio.
|
|
|
|
// https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio
|
|
|
|
d.callbacks = map[string]js.Func{}
|
|
|
|
setCallback("touchend")
|
|
|
|
setCallback("keyup")
|
|
|
|
setCallback("mouseup")
|
|
|
|
|
2021-04-29 11:21:06 +02:00
|
|
|
return d, ready, nil
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
type player struct {
|
|
|
|
context *context
|
2021-03-28 11:07:38 +02:00
|
|
|
src io.Reader
|
|
|
|
eof bool
|
2021-03-28 16:27:47 +02:00
|
|
|
state playerState
|
2021-03-28 11:07:38 +02:00
|
|
|
gain js.Value
|
|
|
|
err error
|
2021-04-19 17:57:27 +02:00
|
|
|
buf []byte
|
2021-02-20 17:47:39 +01:00
|
|
|
|
|
|
|
nextPos float64
|
|
|
|
bufferSourceNodes []js.Value
|
|
|
|
appendBufferFunc js.Func
|
2021-05-09 17:47:03 +02:00
|
|
|
|
|
|
|
cond *sync.Cond
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
func (c *context) NewPlayer(src io.Reader) Player {
|
|
|
|
p := &player{
|
2021-03-28 11:07:38 +02:00
|
|
|
context: c,
|
|
|
|
src: src,
|
|
|
|
gain: c.audioContext.Call("createGain"),
|
2021-05-09 17:47:03 +02:00
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
p.appendBufferFunc = js.FuncOf(p.appendBuffer)
|
2021-03-28 11:07:38 +02:00
|
|
|
p.gain.Call("connect", c.audioContext.Get("destination"))
|
2021-05-01 09:25:40 +02:00
|
|
|
runtime.SetFinalizer(p, (*player).Close)
|
2021-02-20 17:47:39 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-05-04 16:18:16 +02:00
|
|
|
func (c *context) Suspend() error {
|
2021-05-04 11:49:34 +02:00
|
|
|
c.audioContext.Call("suspend")
|
2021-05-04 16:18:16 +02:00
|
|
|
return nil
|
2021-05-04 11:49:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 16:18:16 +02:00
|
|
|
func (c *context) Resume() error {
|
2021-05-04 11:49:34 +02:00
|
|
|
c.audioContext.Call("resume")
|
2021-05-04 16:18:16 +02:00
|
|
|
return nil
|
2021-05-04 11:49:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 16:09:15 +02:00
|
|
|
func (p *player) Play() {
|
2021-05-09 17:47:03 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
2021-05-09 16:09:15 +02:00
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.state != playerPaused {
|
|
|
|
return
|
|
|
|
}
|
2021-05-09 17:47:03 +02:00
|
|
|
|
|
|
|
buf := make([]byte, p.context.maxBufferSize())
|
|
|
|
for len(p.buf) < p.context.maxBufferSize() {
|
|
|
|
n, err := p.src.Read(buf)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.buf = append(p.buf, buf[:n]...)
|
|
|
|
if err == io.EOF {
|
|
|
|
p.eof = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-09 16:09:15 +02:00
|
|
|
p.state = playerPlay
|
2021-05-09 17:47:03 +02:00
|
|
|
p.appendBufferImpl(js.Undefined())
|
|
|
|
p.appendBufferImpl(js.Undefined())
|
|
|
|
|
|
|
|
go p.loop()
|
2021-05-09 16:09:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
func (p *player) Pause() {
|
2021-05-09 17:47:03 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
p.pauseImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) pauseImpl() {
|
2021-05-09 16:09:15 +02:00
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-03-28 16:27:47 +02:00
|
|
|
if p.state != playerPlay {
|
2021-02-20 17:47:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the state first. appendBuffer is called as an 'ended' callback.
|
2021-05-05 14:46:19 +02:00
|
|
|
var data [2][]float32
|
2021-02-20 17:47:39 +01:00
|
|
|
for _, n := range p.bufferSourceNodes {
|
2021-05-05 14:46:19 +02:00
|
|
|
for ch := 0; ch < 2; ch++ {
|
|
|
|
t := n.Get("buffer").Call("getChannelData", ch)
|
|
|
|
data[ch] = append(data[ch], float32ArrayToFloat32Slice(t)...)
|
|
|
|
}
|
2021-02-20 17:47:39 +01:00
|
|
|
n.Set("onended", nil)
|
|
|
|
n.Call("stop")
|
|
|
|
n.Call("disconnect")
|
|
|
|
}
|
2021-05-05 14:46:19 +02:00
|
|
|
p.buf = append(fromLR(data[0], data[1]), p.buf...)
|
2021-03-28 16:27:47 +02:00
|
|
|
p.state = playerPaused
|
2021-02-20 17:47:39 +01:00
|
|
|
p.bufferSourceNodes = p.bufferSourceNodes[:0]
|
|
|
|
p.nextPos = 0
|
2021-05-09 17:47:03 +02:00
|
|
|
p.cond.Signal()
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
func (p *player) appendBuffer(this js.Value, args []js.Value) interface{} {
|
2021-05-09 17:47:03 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
p.appendBufferImpl(this)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) appendBufferImpl(audioBuffer js.Value) {
|
2021-02-20 17:47:39 +01:00
|
|
|
// appendBuffer is called as the 'ended' callback of a buffer.
|
|
|
|
// 'this' is an AudioBufferSourceNode that already finishes its playing.
|
|
|
|
for i, n := range p.bufferSourceNodes {
|
2021-06-07 14:43:25 +02:00
|
|
|
if n.Equal(audioBuffer) {
|
2021-02-20 17:47:39 +01:00
|
|
|
p.bufferSourceNodes = append(p.bufferSourceNodes[:i], p.bufferSourceNodes[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 16:27:47 +02:00
|
|
|
if p.state != playerPlay {
|
2021-05-09 17:47:03 +02:00
|
|
|
return
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-09 17:47:03 +02:00
|
|
|
if p.eof && len(p.buf) == 0 {
|
2021-02-20 17:47:39 +01:00
|
|
|
if len(p.bufferSourceNodes) == 0 {
|
2021-05-09 17:47:03 +02:00
|
|
|
p.pauseImpl()
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
2021-05-09 17:47:03 +02:00
|
|
|
return
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
c := p.context.audioContext.Get("currentTime").Float()
|
2021-02-20 17:47:39 +01:00
|
|
|
if p.nextPos < c {
|
|
|
|
// The exact current time might be too early. Add some delay on purpose to avoid buffer overlapping.
|
|
|
|
p.nextPos = c + 1.0/60.0
|
|
|
|
}
|
|
|
|
|
2021-05-09 17:47:03 +02:00
|
|
|
bs := make([]byte, p.context.oneBufferSize())
|
|
|
|
n := copy(bs, p.buf)
|
|
|
|
p.buf = p.buf[n:]
|
|
|
|
if len(p.buf) < p.context.maxBufferSize() {
|
|
|
|
p.cond.Signal()
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
2021-04-19 17:57:27 +02:00
|
|
|
|
2021-05-06 19:22:51 +02:00
|
|
|
if len(bs) == 0 {
|
2021-05-09 17:47:03 +02:00
|
|
|
// createBuffer fails with 0 bytes. Add some zeros instead.
|
|
|
|
bs = make([]byte, 4096)
|
2021-05-06 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 17:47:39 +01:00
|
|
|
l, r := toLR(bs)
|
|
|
|
tl, tr := float32SliceToTypedArray(l), float32SliceToTypedArray(r)
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
buf := p.context.audioContext.Call("createBuffer", p.context.channelNum, len(bs)/p.context.channelNum/p.context.bitDepthInBytes, p.context.sampleRate)
|
2021-02-20 17:47:39 +01:00
|
|
|
if buf.Get("copyToChannel").Truthy() {
|
|
|
|
buf.Call("copyToChannel", tl, 0, 0)
|
|
|
|
buf.Call("copyToChannel", tr, 1, 0)
|
|
|
|
} else {
|
|
|
|
// copyToChannel is not defined on Safari 11.
|
|
|
|
buf.Call("getChannelData", 0).Call("set", tl)
|
|
|
|
buf.Call("getChannelData", 1).Call("set", tr)
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
s := p.context.audioContext.Call("createBufferSource")
|
2021-02-20 17:47:39 +01:00
|
|
|
s.Set("buffer", buf)
|
|
|
|
s.Set("onended", p.appendBufferFunc)
|
|
|
|
s.Call("connect", p.gain)
|
|
|
|
s.Call("start", p.nextPos)
|
|
|
|
p.nextPos += buf.Get("duration").Float()
|
|
|
|
p.bufferSourceNodes = append(p.bufferSourceNodes, s)
|
|
|
|
|
2021-05-09 17:47:03 +02:00
|
|
|
return
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
func (p *player) IsPlaying() bool {
|
2021-03-28 16:27:47 +02:00
|
|
|
return p.state == playerPlay
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
func (p *player) Reset() {
|
2021-05-09 17:47:03 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-05-09 18:10:42 +02:00
|
|
|
p.resetImpl()
|
|
|
|
}
|
2021-05-09 17:47:03 +02:00
|
|
|
|
2021-05-09 18:10:42 +02:00
|
|
|
func (p *player) resetImpl() {
|
2021-05-09 16:09:15 +02:00
|
|
|
if p.err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-03-28 16:27:47 +02:00
|
|
|
if p.state == playerClosed {
|
2021-02-20 17:47:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-09 17:47:03 +02:00
|
|
|
p.pauseImpl()
|
2021-02-20 17:47:39 +01:00
|
|
|
p.eof = false
|
2021-04-19 17:57:27 +02:00
|
|
|
p.buf = p.buf[:0]
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
func (p *player) Volume() float64 {
|
2021-02-20 17:47:39 +01:00
|
|
|
return p.gain.Get("gain").Get("value").Float()
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
func (p *player) SetVolume(volume float64) {
|
2021-02-20 17:47:39 +01:00
|
|
|
p.gain.Get("gain").Set("value", volume)
|
|
|
|
}
|
|
|
|
|
2021-05-09 20:22:19 +02:00
|
|
|
func (p *player) UnplayedBufferSize() int {
|
2021-05-09 17:47:03 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
2021-02-20 17:47:39 +01:00
|
|
|
// This is not an accurate buffer size as part of the buffers might already be consumed.
|
|
|
|
var sec float64
|
|
|
|
for _, n := range p.bufferSourceNodes {
|
|
|
|
sec += n.Get("buffer").Get("duration").Float()
|
|
|
|
}
|
2021-05-09 20:22:19 +02:00
|
|
|
return len(p.buf) + int(sec*float64(p.context.sampleRate*p.context.channelNum*p.context.bitDepthInBytes))
|
2021-02-20 17:47:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
func (p *player) Err() error {
|
2021-05-09 17:47:03 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
2021-03-28 14:57:39 +02:00
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:25:40 +02:00
|
|
|
func (p *player) Close() error {
|
2021-05-09 18:10:42 +02:00
|
|
|
runtime.SetFinalizer(p, nil)
|
2021-05-09 17:47:03 +02:00
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
return p.closeImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) closeImpl() error {
|
2021-05-09 18:10:42 +02:00
|
|
|
p.resetImpl()
|
2021-03-28 16:27:47 +02:00
|
|
|
p.state = playerClosed
|
2021-02-20 17:47:39 +01:00
|
|
|
p.appendBufferFunc.Release()
|
2021-05-09 17:47:03 +02:00
|
|
|
p.cond.Signal()
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
2021-05-25 20:45:12 +02:00
|
|
|
func (p *player) setError(err error) {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
p.setErrorImpl(err)
|
|
|
|
}
|
|
|
|
|
2021-05-09 17:47:03 +02:00
|
|
|
func (p *player) setErrorImpl(err error) {
|
|
|
|
p.err = err
|
|
|
|
p.closeImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) shouldWait() bool {
|
|
|
|
switch p.state {
|
|
|
|
case playerPaused:
|
|
|
|
// Even when the player is paused, the loop immediately ends.
|
|
|
|
// WebAudio doesn't have a notion of pause.
|
|
|
|
return false
|
|
|
|
case playerPlay:
|
2021-05-15 15:39:54 +02:00
|
|
|
return len(p.buf) >= p.context.maxBufferSize()
|
2021-05-09 17:47:03 +02:00
|
|
|
case playerClosed:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) wait() bool {
|
|
|
|
p.cond.L.Lock()
|
|
|
|
defer p.cond.L.Unlock()
|
|
|
|
|
|
|
|
for p.shouldWait() {
|
|
|
|
p.cond.Wait()
|
|
|
|
}
|
|
|
|
return p.state == playerPlay && !p.eof
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) loop() {
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
for {
|
|
|
|
if !p.wait() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := p.src.Read(buf)
|
|
|
|
if err != nil && err != io.EOF {
|
2021-05-25 20:45:12 +02:00
|
|
|
p.setError(err)
|
2021-05-09 17:47:03 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-25 20:45:12 +02:00
|
|
|
p.cond.L.Lock()
|
2021-05-09 17:47:03 +02:00
|
|
|
p.buf = append(p.buf, buf[:n]...)
|
|
|
|
if err == io.EOF {
|
2021-05-25 20:45:12 +02:00
|
|
|
// p.eof can be true even if the buffer is not consumed yet.
|
|
|
|
// This might be different from the other drivers.
|
2021-05-09 17:47:03 +02:00
|
|
|
p.eof = true
|
|
|
|
p.cond.L.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.cond.L.Unlock()
|
|
|
|
}
|
2021-01-07 03:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type go2cppDriverWrapper struct {
|
|
|
|
c *go2cpp.Context
|
|
|
|
}
|
|
|
|
|
2021-03-28 11:07:38 +02:00
|
|
|
func (w *go2cppDriverWrapper) NewPlayer(r io.Reader) Player {
|
2021-01-07 03:52:34 +01:00
|
|
|
return w.c.NewPlayer(r)
|
|
|
|
}
|
|
|
|
|
2021-05-04 16:18:16 +02:00
|
|
|
func (w *go2cppDriverWrapper) Suspend() error {
|
2021-05-04 11:49:34 +02:00
|
|
|
// Do nothing so far.
|
2021-05-04 16:18:16 +02:00
|
|
|
return nil
|
2021-05-04 11:49:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 16:18:16 +02:00
|
|
|
func (w *go2cppDriverWrapper) Resume() error {
|
2021-05-04 11:49:34 +02:00
|
|
|
// Do nothing so far.
|
2021-05-04 16:18:16 +02:00
|
|
|
return nil
|
2021-05-04 11:49:34 +02:00
|
|
|
}
|
|
|
|
|
2021-02-20 17:47:39 +01:00
|
|
|
func toLR(data []byte) ([]float32, []float32) {
|
|
|
|
const max = 1 << 15
|
|
|
|
|
|
|
|
l := make([]float32, len(data)/4)
|
|
|
|
r := make([]float32, len(data)/4)
|
|
|
|
for i := 0; i < len(data)/4; i++ {
|
|
|
|
l[i] = float32(int16(data[4*i])|int16(data[4*i+1])<<8) / max
|
|
|
|
r[i] = float32(int16(data[4*i+2])|int16(data[4*i+3])<<8) / max
|
|
|
|
}
|
|
|
|
return l, r
|
|
|
|
}
|
|
|
|
|
2021-05-05 14:46:19 +02:00
|
|
|
func fromLR(l, r []float32) []byte {
|
|
|
|
const max = 1 << 15
|
|
|
|
|
|
|
|
if len(l) != len(r) {
|
|
|
|
panic("readerdriver: len(l) must equal to len(r) at fromLR")
|
|
|
|
}
|
|
|
|
bs := make([]byte, len(l)*4)
|
|
|
|
for i := range l {
|
|
|
|
lv := int16(l[i] * max)
|
|
|
|
bs[4*i] = byte(lv)
|
|
|
|
bs[4*i+1] = byte(lv >> 8)
|
|
|
|
rv := int16(r[i] * max)
|
|
|
|
bs[4*i+2] = byte(rv)
|
|
|
|
bs[4*i+3] = byte(rv >> 8)
|
|
|
|
}
|
|
|
|
return bs
|
|
|
|
}
|
|
|
|
|
2021-02-20 17:47:39 +01:00
|
|
|
func float32SliceToTypedArray(s []float32) js.Value {
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
|
|
|
|
h.Len *= 4
|
|
|
|
h.Cap *= 4
|
|
|
|
bs := *(*[]byte)(unsafe.Pointer(h))
|
|
|
|
|
|
|
|
a := js.Global().Get("Uint8Array").New(len(bs))
|
|
|
|
js.CopyBytesToJS(a, bs)
|
|
|
|
runtime.KeepAlive(s)
|
|
|
|
buf := a.Get("buffer")
|
|
|
|
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
|
|
|
|
}
|
2021-05-05 14:46:19 +02:00
|
|
|
|
|
|
|
func float32ArrayToFloat32Slice(v js.Value) []float32 {
|
|
|
|
bs := make([]byte, v.Get("byteLength").Int())
|
|
|
|
js.CopyBytesToGo(bs, js.Global().Get("Uint8Array").New(v.Get("buffer"), v.Get("byteOffset"), v.Get("byteLength")))
|
|
|
|
|
|
|
|
h := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
|
|
|
|
h.Len /= 4
|
|
|
|
h.Cap /= 4
|
|
|
|
f32s := *(*[]float32)(unsafe.Pointer(h))
|
|
|
|
runtime.KeepAlive(bs)
|
|
|
|
|
|
|
|
return f32s
|
|
|
|
}
|