mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
audio/vorbis: Stop copying streaming in JavaScript
This commit is contained in:
parent
764c755dd0
commit
03906148bb
@ -17,9 +17,8 @@
|
|||||||
package vorbis
|
package vorbis
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"github.com/gopherjs/gopherjs/js"
|
"github.com/gopherjs/gopherjs/js"
|
||||||
"github.com/hajimehoshi/ebiten/exp/audio"
|
"github.com/hajimehoshi/ebiten/exp/audio"
|
||||||
@ -28,35 +27,72 @@ import (
|
|||||||
// TODO: This just uses decodeAudioData can treat audio files other than Ogg/Vorbis.
|
// 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.
|
// TODO: This doesn't work on iOS which doesn't have Ogg/Vorbis decoder.
|
||||||
|
|
||||||
|
type Stream struct {
|
||||||
|
leftData []float32
|
||||||
|
rightData []float32
|
||||||
|
posInBytes int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) Read(b []byte) (int, error) {
|
||||||
|
l := len(s.leftData)*4 - s.posInBytes
|
||||||
|
if l > len(b) {
|
||||||
|
l = len(b)
|
||||||
|
}
|
||||||
|
l = l / 4 * 4
|
||||||
|
for i := 0; i < l/4; i++ {
|
||||||
|
il := int16(s.leftData[s.posInBytes/4+i] * (1 << 15))
|
||||||
|
ir := int16(s.rightData[s.posInBytes/4+i] * (1 << 15))
|
||||||
|
b[4*i] = uint8(il)
|
||||||
|
b[4*i+1] = uint8(il >> 8)
|
||||||
|
b[4*i+2] = uint8(ir)
|
||||||
|
b[4*i+3] = uint8(ir >> 8)
|
||||||
|
}
|
||||||
|
s.posInBytes += l
|
||||||
|
if s.posInBytes == len(s.leftData)*4 {
|
||||||
|
return l, io.EOF
|
||||||
|
}
|
||||||
|
return l, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
||||||
|
next := int64(0)
|
||||||
|
switch whence {
|
||||||
|
case 0:
|
||||||
|
next = offset
|
||||||
|
case 1:
|
||||||
|
next = int64(s.posInBytes) + offset
|
||||||
|
case 2:
|
||||||
|
next = int64(len(s.leftData)*4) + offset
|
||||||
|
}
|
||||||
|
s.posInBytes = int(next)
|
||||||
|
return next, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) Size() int64 {
|
||||||
|
return int64(len(s.leftData) * 4)
|
||||||
|
}
|
||||||
|
|
||||||
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
||||||
b, err := ioutil.ReadAll(src)
|
b, err := ioutil.ReadAll(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := src.Close(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
s := &Stream{}
|
s := &Stream{}
|
||||||
ch := make(chan struct{})
|
ch := make(chan struct{})
|
||||||
|
|
||||||
// TODO: 1 is a correct second argument?
|
// TODO: 1 is a correct second argument?
|
||||||
oc := js.Global.Get("OfflineAudioContext").New(2, 1, context.SampleRate())
|
oc := js.Global.Get("OfflineAudioContext").New(2, 1, context.SampleRate())
|
||||||
oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) {
|
oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Object) {
|
||||||
go func() {
|
s.leftData = buf.Call("getChannelData", 0).Interface().([]float32)
|
||||||
defer close(ch)
|
s.rightData = buf.Call("getChannelData", 1).Interface().([]float32)
|
||||||
il := buf.Call("getChannelData", 0).Interface().([]float32)
|
close(ch)
|
||||||
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)
|
|
||||||
if i%16384 == 0 {
|
|
||||||
runtime.Gosched()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.buf = bytes.NewReader(b)
|
|
||||||
}()
|
|
||||||
})
|
})
|
||||||
<-ch
|
<-ch
|
||||||
return s, nil
|
return s, nil
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
// 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
|
|
||||||
|
|
||||||
package vorbis
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Stream struct {
|
|
||||||
buf *bytes.Reader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stream) Read(p []byte) (int, error) {
|
|
||||||
return s.buf.Read(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
|
||||||
return s.buf.Seek(offset, whence)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stream) Close() error {
|
|
||||||
s.buf = nil
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stream) Size() int64 {
|
|
||||||
return s.buf.Size()
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user