forked from ebitengine/oto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_js.go
146 lines (126 loc) · 3.87 KB
/
player_js.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2015 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 oto
import (
"errors"
"strings"
"github.com/gopherjs/gopherjs/js"
)
type player struct {
sampleRate int
channelNum int
bytesPerSample int
nextPosInSamples int64
tmp []uint8
bufferSize int
context *js.Object
}
func isIOS() bool {
ua := js.Global.Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "iPhone") {
return false
}
return true
}
func isAndroidChrome() bool {
ua := js.Global.Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "Android") {
return false
}
if !strings.Contains(ua, "Chrome") {
return false
}
return true
}
func newPlayer(sampleRate, channelNum, bytesPerSample, bufferSize int) (*player, error) {
class := js.Global.Get("AudioContext")
if class == js.Undefined {
class = js.Global.Get("webkitAudioContext")
}
if class == js.Undefined {
return nil, errors.New("oto: audio couldn't be initialized")
}
p := &player{
sampleRate: sampleRate,
channelNum: channelNum,
bytesPerSample: bytesPerSample,
context: class.New(),
bufferSize: bufferSize,
}
// iOS and Android Chrome requires touch event to use AudioContext.
if isIOS() || isAndroidChrome() {
var f *js.Object
f = js.MakeFunc(func(this *js.Object, arguments []*js.Object) interface{} {
// Resuming is necessary as of Chrome 55+ in some cases like different
// domain page in an iframe.
p.context.Call("resume")
p.context.Call("createBufferSource").Call("start", 0)
p.nextPosInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
js.Global.Get("document").Call("removeEventListener", "touchend", f)
return nil
})
js.Global.Get("document").Call("addEventListener", "touchend", f)
}
p.nextPosInSamples = int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
return p, nil
}
func toLR(data []uint8) ([]int16, []int16) {
l := make([]int16, len(data)/4)
r := make([]int16, len(data)/4)
for i := 0; i < len(data)/4; i++ {
l[i] = int16(data[4*i]) | int16(data[4*i+1])<<8
r[i] = int16(data[4*i+2]) | int16(data[4*i+3])<<8
}
return l, r
}
func (p *player) SetUnderrunCallback(f func()) {
//TODO
}
func (p *player) Write(data []uint8) (int, error) {
n := min(len(data), p.bufferSize-len(p.tmp))
p.tmp = append(p.tmp, data[:n]...)
c := int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
if p.nextPosInSamples < c {
p.nextPosInSamples = c
}
sizeInSamples := p.bufferSize / p.bytesPerSample / p.channelNum
// It's too early to enqueue a buffer.
// Highly likely, there are two playing buffers now.
if c+int64(sizeInSamples) < p.nextPosInSamples {
return n, nil
}
if len(p.tmp) < p.bufferSize {
return n, nil
}
buf := p.context.Call("createBuffer", p.channelNum, sizeInSamples, p.sampleRate)
l := buf.Call("getChannelData", 0)
r := buf.Call("getChannelData", 1)
il, ir := toLR(p.tmp)
const max = 1 << 15
for i := 0; i < len(il); i++ {
l.SetIndex(i, float64(il[i])/max)
r.SetIndex(i, float64(ir[i])/max)
}
s := p.context.Call("createBufferSource")
s.Set("buffer", buf)
s.Call("connect", p.context.Get("destination"))
s.Call("start", float64(p.nextPosInSamples)/float64(p.sampleRate))
p.nextPosInSamples += int64(len(il))
p.tmp = nil
return n, nil
}
func (p *player) Close() error {
return nil
}