mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
audio: Enable to play inifinite stream
This commit is contained in:
parent
60371deb3b
commit
f0d7a811e1
@ -19,7 +19,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Player struct {
|
type Player struct {
|
||||||
*player
|
player *player
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPlayer creates a new player with the given data to the given channel.
|
// NewPlayer creates a new player with the given data to the given channel.
|
||||||
@ -35,9 +35,9 @@ func NewPlayer(src io.ReadSeeker, sampleRate int) *Player {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) Play() error {
|
func (p *Player) Play() error {
|
||||||
return p.play()
|
return p.player.play()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) Close() error {
|
func (p *Player) Close() error {
|
||||||
return p.close()
|
return p.player.close()
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,13 @@
|
|||||||
package audio
|
package audio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"runtime"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/mobile/exp/audio"
|
"golang.org/x/mobile/exp/audio/al"
|
||||||
)
|
)
|
||||||
|
|
||||||
type readSeekCloser struct {
|
type readSeekCloser struct {
|
||||||
@ -31,27 +35,118 @@ func (r *readSeekCloser) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type player struct {
|
type player struct {
|
||||||
*audio.Player
|
alSource al.Source
|
||||||
|
alBuffers []al.Buffer
|
||||||
|
source io.ReadSeeker
|
||||||
|
sampleRate int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var m sync.Mutex
|
||||||
|
|
||||||
|
var sn = 0
|
||||||
|
|
||||||
func newPlayer(src io.ReadSeeker, sampleRate int) *Player {
|
func newPlayer(src io.ReadSeeker, sampleRate int) *Player {
|
||||||
p, err := audio.NewPlayer(&readSeekCloser{src}, audio.Stereo16, int64(sampleRate))
|
m.Lock()
|
||||||
if err != nil {
|
if err := al.OpenDevice(); err != nil {
|
||||||
// TODO: Should we return errors for this method?
|
panic(fmt.Sprintf("audio: OpenAL initialization failed: %v", err))
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
pp := &player{p}
|
// TODO: Too many generating sources may cause error. Limit the number of sources.
|
||||||
return &Player{pp}
|
s := al.GenSources(1)
|
||||||
|
if err := al.Error(); err != 0 {
|
||||||
|
panic(fmt.Sprintf("audio: al.GenSources error: %d", err))
|
||||||
|
}
|
||||||
|
m.Unlock()
|
||||||
|
p := &player{
|
||||||
|
alSource: s[0],
|
||||||
|
alBuffers: []al.Buffer{},
|
||||||
|
source: src,
|
||||||
|
sampleRate: sampleRate,
|
||||||
|
}
|
||||||
|
runtime.SetFinalizer(p, (*player).close)
|
||||||
|
return &Player{p}
|
||||||
|
}
|
||||||
|
|
||||||
|
const bufferSize = 1024
|
||||||
|
|
||||||
|
func (p *player) proceed() error {
|
||||||
|
m.Lock()
|
||||||
|
processedNum := p.alSource.BuffersProcessed()
|
||||||
|
if 0 < processedNum {
|
||||||
|
bufs := make([]al.Buffer, processedNum)
|
||||||
|
p.alSource.UnqueueBuffers(bufs...)
|
||||||
|
p.alBuffers = append(p.alBuffers, bufs...)
|
||||||
|
}
|
||||||
|
m.Unlock()
|
||||||
|
for 0 < len(p.alBuffers) {
|
||||||
|
b := make([]byte, bufferSize)
|
||||||
|
n, err := p.source.Read(b)
|
||||||
|
if 0 < n {
|
||||||
|
m.Lock()
|
||||||
|
buf := p.alBuffers[0]
|
||||||
|
p.alBuffers = p.alBuffers[1:]
|
||||||
|
buf.BufferData(al.FormatStereo16, b[:n], int32(p.sampleRate))
|
||||||
|
p.alSource.QueueBuffers(buf)
|
||||||
|
m.Unlock()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.Lock()
|
||||||
|
if p.alSource.State() == al.Stopped {
|
||||||
|
al.RewindSources(p.alSource)
|
||||||
|
al.PlaySources(p.alSource)
|
||||||
|
}
|
||||||
|
m.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) play() error {
|
func (p *player) play() error {
|
||||||
// TODO: audio.NewPlayer interprets WAV header, which we don't want.
|
// TODO: What if play is already called?
|
||||||
// Use OpenAL or native API instead.
|
emptyBytes := make([]byte, bufferSize)
|
||||||
return p.Play()
|
m.Lock()
|
||||||
|
bufs := al.GenBuffers(8)
|
||||||
|
for _, buf := range bufs {
|
||||||
|
// Note that the third argument of only the first buffer is used.
|
||||||
|
buf.BufferData(al.FormatStereo16, emptyBytes, int32(p.sampleRate))
|
||||||
|
p.alSource.QueueBuffers(buf)
|
||||||
|
}
|
||||||
|
al.PlaySources(p.alSource)
|
||||||
|
m.Unlock()
|
||||||
|
go func() {
|
||||||
|
defer p.close()
|
||||||
|
for {
|
||||||
|
err := p.proceed()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
// TODO: Record the last error
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
time.Sleep(1)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) close() error {
|
func (p *player) close() error {
|
||||||
return p.Close()
|
m.Lock()
|
||||||
|
if p.alSource != 0 {
|
||||||
|
al.DeleteSources(p.alSource)
|
||||||
|
p.alSource = 0
|
||||||
|
}
|
||||||
|
if 0 < len(p.alBuffers) {
|
||||||
|
al.DeleteBuffers(p.alBuffers...)
|
||||||
|
p.alBuffers = []al.Buffer{}
|
||||||
|
}
|
||||||
|
if err := al.Error(); err != 0 {
|
||||||
|
panic(fmt.Sprintf("audio: close error: %d", err))
|
||||||
|
}
|
||||||
|
m.Unlock()
|
||||||
|
runtime.SetFinalizer(p, nil)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement Close method
|
// TODO: Implement Close method
|
||||||
|
Loading…
Reference in New Issue
Block a user