2017-06-18 16:49:05 +02:00
|
|
|
// Copyright 2017 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
|
|
|
|
|
|
|
|
package mp3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-08-27 18:31:29 +02:00
|
|
|
"fmt"
|
2017-06-18 16:49:05 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-12-27 11:18:02 +01:00
|
|
|
"math"
|
2019-04-30 19:15:28 +02:00
|
|
|
"syscall/js"
|
2017-12-25 16:08:25 +01:00
|
|
|
"time"
|
2017-06-18 16:49:05 +02:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
|
|
|
)
|
|
|
|
|
2017-09-24 12:28:09 +02:00
|
|
|
// TODO: This just uses decodeAudioData, that can treat audio files other than MP3.
|
2017-06-18 16:49:05 +02:00
|
|
|
|
|
|
|
type Stream struct {
|
|
|
|
leftData []float32
|
|
|
|
rightData []float32
|
|
|
|
posInBytes int
|
|
|
|
}
|
|
|
|
|
2017-12-25 16:08:25 +01:00
|
|
|
var (
|
|
|
|
errTryAgain = errors.New("audio/mp3: try again")
|
|
|
|
errTimeout = errors.New("audio/mp3: timeout")
|
|
|
|
)
|
2017-09-27 20:10:51 +02:00
|
|
|
|
2017-06-18 16:49:05 +02:00
|
|
|
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
|
2017-06-18 18:04:37 +02:00
|
|
|
const max = 1<<15 - 1
|
2017-06-18 16:49:05 +02:00
|
|
|
for i := 0; i < l/4; i++ {
|
2017-06-18 18:04:37 +02:00
|
|
|
il := int32(s.leftData[s.posInBytes/4+i] * max)
|
|
|
|
if il > max {
|
|
|
|
il = max
|
|
|
|
}
|
|
|
|
if il < -max {
|
|
|
|
il = -max
|
|
|
|
}
|
|
|
|
ir := int32(s.rightData[s.posInBytes/4+i] * max)
|
|
|
|
if ir > max {
|
|
|
|
ir = max
|
|
|
|
}
|
|
|
|
if ir < -max {
|
|
|
|
ir = -max
|
|
|
|
}
|
2017-12-25 16:11:17 +01:00
|
|
|
b[4*i] = byte(il)
|
|
|
|
b[4*i+1] = byte(il >> 8)
|
|
|
|
b[4*i+2] = byte(ir)
|
|
|
|
b[4*i+3] = byte(ir >> 8)
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
|
|
|
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 {
|
2017-12-25 16:10:30 +01:00
|
|
|
case io.SeekStart:
|
2017-06-18 16:49:05 +02:00
|
|
|
next = offset
|
2017-12-25 16:10:30 +01:00
|
|
|
case io.SeekCurrent:
|
2017-06-18 16:49:05 +02:00
|
|
|
next = int64(s.posInBytes) + offset
|
2017-12-25 16:10:30 +01:00
|
|
|
case io.SeekEnd:
|
2018-01-08 17:25:38 +01:00
|
|
|
next = s.Length() + offset
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
|
|
|
s.posInBytes = int(next)
|
|
|
|
return next, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stream) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 17:25:38 +01:00
|
|
|
func (s *Stream) Length() int64 {
|
2017-06-18 16:49:05 +02:00
|
|
|
return int64(len(s.leftData) * 4)
|
|
|
|
}
|
|
|
|
|
2018-01-08 17:25:38 +01:00
|
|
|
// Size is deprecated as of 1.6.0-alpha. Use Length instead.
|
|
|
|
func (s *Stream) Size() int64 {
|
|
|
|
return s.Length()
|
|
|
|
}
|
|
|
|
|
2017-09-27 20:10:51 +02:00
|
|
|
// seekNextFrame seeks the next frame and returns the new buffer with the new position.
|
|
|
|
// seekNextFrame also returns true when seeking is successful, or false otherwise.
|
|
|
|
//
|
|
|
|
// Seeking is necessary when decoding fails. Safari's MP3 decoder can't treat IDs well (#438).
|
2017-12-23 19:13:14 +01:00
|
|
|
func seekNextFrame(buf []byte) ([]byte, bool) {
|
2017-09-27 20:10:51 +02:00
|
|
|
// TODO: Need to skip tags explicitly? (hajimehoshi/go-mp3#9)
|
|
|
|
|
|
|
|
if len(buf) < 1 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
buf = buf[1:]
|
|
|
|
|
|
|
|
for {
|
|
|
|
if buf[0] == 0xff && buf[1]&0xfe == 0xfe {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
buf = buf[1:]
|
|
|
|
if len(buf) < 2 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf, true
|
|
|
|
}
|
|
|
|
|
2017-06-18 16:49:05 +02:00
|
|
|
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
|
|
|
b, err := ioutil.ReadAll(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := src.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-27 20:10:51 +02:00
|
|
|
var s *Stream
|
2017-12-27 11:18:02 +01:00
|
|
|
try := 0
|
2017-09-27 20:10:51 +02:00
|
|
|
for {
|
2017-12-27 11:18:02 +01:00
|
|
|
s, err = decode(context, b, try)
|
2017-12-25 16:08:25 +01:00
|
|
|
switch err {
|
|
|
|
case errTryAgain:
|
2017-09-27 20:10:51 +02:00
|
|
|
buf, ok := seekNextFrame(b)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("audio/mp3: Decode failed: invalid format?")
|
|
|
|
}
|
|
|
|
b = buf
|
|
|
|
continue
|
2017-12-25 16:08:25 +01:00
|
|
|
case errTimeout:
|
2017-12-27 11:18:02 +01:00
|
|
|
try++
|
2017-12-25 16:08:25 +01:00
|
|
|
continue
|
|
|
|
default:
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-27 20:10:51 +02:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
2017-06-18 16:49:05 +02:00
|
|
|
|
2018-06-29 17:02:15 +02:00
|
|
|
var offlineAudioContextClass = js.Null()
|
2017-09-27 20:10:51 +02:00
|
|
|
|
|
|
|
func init() {
|
2018-06-29 17:02:15 +02:00
|
|
|
if klass := js.Global().Get("OfflineAudioContext"); klass != js.Undefined() {
|
2017-09-27 20:10:51 +02:00
|
|
|
offlineAudioContextClass = klass
|
|
|
|
return
|
|
|
|
}
|
2018-06-29 17:02:15 +02:00
|
|
|
if klass := js.Global().Get("webkitOfflineAudioContext"); klass != js.Undefined() {
|
2017-09-27 20:10:51 +02:00
|
|
|
offlineAudioContextClass = klass
|
|
|
|
return
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
2017-09-27 20:10:51 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 10:19:47 +02:00
|
|
|
func float32ArrayToSlice(arr js.Value) []float32 {
|
|
|
|
f := make([]float32, arr.Length())
|
|
|
|
a := js.TypedArrayOf(f)
|
|
|
|
a.Call("set", arr)
|
|
|
|
a.Release()
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-27 11:18:02 +01:00
|
|
|
func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
|
2018-06-29 17:02:15 +02:00
|
|
|
if offlineAudioContextClass == js.Null() {
|
2017-08-27 18:31:29 +02:00
|
|
|
return nil, errors.New("audio/mp3: OfflineAudioContext is not available")
|
2017-06-18 16:49:05 +02:00
|
|
|
}
|
2017-09-27 20:10:51 +02:00
|
|
|
|
|
|
|
s := &Stream{}
|
|
|
|
ch := make(chan error)
|
|
|
|
|
|
|
|
// TODO: webkitOfflineAudioContext's constructor might causes 'Syntax' error
|
|
|
|
// when the sample rate is not so usual like 22050 on Safari.
|
|
|
|
// Should this be handled before calling this?
|
|
|
|
|
2017-06-18 16:49:05 +02:00
|
|
|
// TODO: 1 is a correct second argument?
|
2017-09-27 20:10:51 +02:00
|
|
|
oc := offlineAudioContextClass.New(2, 1, context.SampleRate())
|
2018-06-23 19:17:08 +02:00
|
|
|
|
2018-07-07 10:19:47 +02:00
|
|
|
u8 := js.TypedArrayOf(buf)
|
2018-06-23 21:33:05 +02:00
|
|
|
a := u8.Get("buffer").Call("slice", u8.Get("byteOffset"), u8.Get("byteOffset").Int()+u8.Get("byteLength").Int())
|
2018-06-23 19:17:08 +02:00
|
|
|
|
2019-04-30 19:15:28 +02:00
|
|
|
succeeded := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
2018-06-23 18:42:16 +02:00
|
|
|
buf := args[0]
|
|
|
|
s.leftData = float32ArrayToSlice(buf.Call("getChannelData", 0))
|
2017-08-27 18:31:29 +02:00
|
|
|
switch n := buf.Get("numberOfChannels").Int(); n {
|
|
|
|
case 1:
|
|
|
|
s.rightData = s.leftData
|
2017-12-23 20:40:20 +01:00
|
|
|
close(ch)
|
2017-08-27 18:31:29 +02:00
|
|
|
case 2:
|
2018-06-23 18:42:16 +02:00
|
|
|
s.rightData = float32ArrayToSlice(buf.Call("getChannelData", 1))
|
2017-12-23 20:40:20 +01:00
|
|
|
close(ch)
|
2017-08-27 18:31:29 +02:00
|
|
|
default:
|
2017-09-27 17:55:18 +02:00
|
|
|
ch <- fmt.Errorf("audio/mp3: number of channels must be 1 or 2 but %d", n)
|
2017-08-27 18:31:29 +02:00
|
|
|
}
|
2019-04-30 19:15:28 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
defer succeeded.Release()
|
|
|
|
|
|
|
|
failed := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
2018-06-23 18:42:16 +02:00
|
|
|
err := args[0]
|
2018-06-29 17:02:15 +02:00
|
|
|
if err != js.Null() || err != js.Undefined() {
|
2017-10-17 19:11:09 +02:00
|
|
|
ch <- fmt.Errorf("audio/mp3: decodeAudioData failed: %v", err)
|
|
|
|
} else {
|
|
|
|
// On Safari, error value might be null and it is needed to retry decoding
|
|
|
|
// from the next frame (#438).
|
|
|
|
ch <- errTryAgain
|
|
|
|
}
|
2019-04-30 19:15:28 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
defer failed.Release()
|
|
|
|
|
|
|
|
oc.Call("decodeAudioData", a, succeeded, failed)
|
2018-07-07 10:19:47 +02:00
|
|
|
u8.Release()
|
2017-12-23 19:13:14 +01:00
|
|
|
|
2017-12-27 11:18:02 +01:00
|
|
|
timeout := time.Duration(math.Pow(2, float64(try))) * time.Second
|
|
|
|
|
2017-12-25 16:08:25 +01:00
|
|
|
select {
|
|
|
|
case err := <-ch:
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-27 11:18:02 +01:00
|
|
|
case <-time.After(timeout):
|
2017-12-25 16:08:25 +01:00
|
|
|
// Sometimes decode fails without calling the callbacks (#464).
|
|
|
|
// Let's just try again in this case.
|
|
|
|
return nil, errTimeout
|
2017-08-27 18:31:29 +02:00
|
|
|
}
|
2017-06-18 16:49:05 +02:00
|
|
|
return s, nil
|
|
|
|
}
|