mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-14 15:07:26 +01:00
audio: Add Close method
This commit is contained in:
parent
1fec0d8203
commit
6d3598c057
@ -37,3 +37,7 @@ func NewPlayer(src io.ReadSeeker, sampleRate int) *Player {
|
|||||||
func (p *Player) Play() error {
|
func (p *Player) Play() error {
|
||||||
return p.play()
|
return p.play()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Player) Close() error {
|
||||||
|
return p.close()
|
||||||
|
}
|
||||||
|
@ -29,6 +29,24 @@ type player struct {
|
|||||||
src io.ReadSeeker
|
src io.ReadSeeker
|
||||||
sampleRate int
|
sampleRate int
|
||||||
position float64
|
position float64
|
||||||
|
bufferSource *js.Object
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialize() bool {
|
||||||
|
// Do nothing in node.js.
|
||||||
|
if js.Global.Get("require") != js.Undefined {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
class := js.Global.Get("AudioContext")
|
||||||
|
if class == js.Undefined {
|
||||||
|
class = js.Global.Get("webkitAudioContext")
|
||||||
|
}
|
||||||
|
if class == js.Undefined {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
context = class.New()
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPlayer(src io.ReadSeeker, sampleRate int) *Player {
|
func newPlayer(src io.ReadSeeker, sampleRate int) *Player {
|
||||||
@ -42,6 +60,7 @@ func newPlayer(src io.ReadSeeker, sampleRate int) *Player {
|
|||||||
src: src,
|
src: src,
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
position: context.Get("currentTime").Float(),
|
position: context.Get("currentTime").Float(),
|
||||||
|
bufferSource: nil,
|
||||||
}
|
}
|
||||||
return &Player{p}
|
return &Player{p}
|
||||||
}
|
}
|
||||||
@ -65,6 +84,11 @@ func (p *player) play() error {
|
|||||||
if len(buf) == 0 {
|
if len(buf) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
// TODO: p.position should be updated
|
||||||
|
if p.bufferSource != nil {
|
||||||
|
p.bufferSource.Call("start", p.position)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
const channelNum = 2
|
const channelNum = 2
|
||||||
const bytesPerSample = channelNum * 16 / 8
|
const bytesPerSample = channelNum * 16 / 8
|
||||||
b := context.Call("createBuffer", channelNum, len(buf)/bytesPerSample, p.sampleRate)
|
b := context.Call("createBuffer", channelNum, len(buf)/bytesPerSample, p.sampleRate)
|
||||||
@ -76,27 +100,16 @@ func (p *player) play() error {
|
|||||||
l.SetIndex(i, float64(il[i])/max)
|
l.SetIndex(i, float64(il[i])/max)
|
||||||
r.SetIndex(i, float64(ir[i])/max)
|
r.SetIndex(i, float64(ir[i])/max)
|
||||||
}
|
}
|
||||||
s := context.Call("createBufferSource")
|
p.bufferSource = context.Call("createBufferSource")
|
||||||
s.Set("buffer", b)
|
p.bufferSource.Set("buffer", b)
|
||||||
s.Call("connect", context.Get("destination"))
|
p.bufferSource.Call("connect", context.Get("destination"))
|
||||||
s.Call("start", p.position)
|
p.bufferSource.Call("start", p.position)
|
||||||
p.position += b.Get("duration").Float()
|
p.position += b.Get("duration").Float()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initialize() bool {
|
func (p *player) close() error {
|
||||||
// Do nothing in node.js.
|
p.bufferSource.Call("stop")
|
||||||
if js.Global.Get("require") != js.Undefined {
|
p.bufferSource.Call("disconnect")
|
||||||
return false
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
class := js.Global.Get("AudioContext")
|
|
||||||
if class == js.Undefined {
|
|
||||||
class = js.Global.Get("webkitAudioContext")
|
|
||||||
}
|
|
||||||
if class == js.Undefined {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
context = class.New()
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
@ -50,4 +50,8 @@ func (p *player) play() error {
|
|||||||
return p.Play()
|
return p.Play()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *player) close() error {
|
||||||
|
return p.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement Close method
|
// TODO: Implement Close method
|
||||||
|
Loading…
Reference in New Issue
Block a user