mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
audio: Refactoring: Remove alSourceCache and add playerCache instead
This commit is contained in:
parent
16412aab30
commit
72e0080f65
@ -26,75 +26,67 @@ import (
|
|||||||
"golang.org/x/mobile/exp/audio/al"
|
"golang.org/x/mobile/exp/audio/al"
|
||||||
)
|
)
|
||||||
|
|
||||||
// alSourceCacheEntry represents a source.
|
const (
|
||||||
// In some environments, too many calls of alGenSources and alGenBuffers could cause errors.
|
maxSourceNum = 32
|
||||||
// To avoid this error, al.Source and al.Buffer are reused.
|
maxBufferNum = 8
|
||||||
type alSourceCacheEntry struct {
|
)
|
||||||
source al.Source
|
|
||||||
buffers []al.Buffer
|
|
||||||
sampleRate int
|
|
||||||
isClosed bool
|
|
||||||
}
|
|
||||||
|
|
||||||
const maxSourceNum = 32
|
var totalBufferNum = 0
|
||||||
|
|
||||||
var alSourceCache = []*alSourceCacheEntry{}
|
var playerCache = []*player{}
|
||||||
|
|
||||||
type player struct {
|
type player struct {
|
||||||
alSource al.Source
|
alSource al.Source
|
||||||
alBuffers []al.Buffer
|
alBuffers []al.Buffer
|
||||||
source io.ReadSeeker
|
source io.ReadSeeker
|
||||||
sampleRate int
|
sampleRate int
|
||||||
|
isClosed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var m sync.Mutex
|
var m sync.Mutex
|
||||||
|
|
||||||
func newAlSource(sampleRate int) (al.Source, []al.Buffer, error) {
|
func newPlayerFromCache(src io.ReadSeeker, sampleRate int) (*player, error) {
|
||||||
for _, e := range alSourceCache {
|
for _, p := range playerCache {
|
||||||
if e.sampleRate != sampleRate {
|
if p.sampleRate != sampleRate {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !e.isClosed {
|
if !p.isClosed {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
e.isClosed = false
|
p.source = src
|
||||||
return e.source, e.buffers, nil
|
p.isClosed = false
|
||||||
|
return p, nil
|
||||||
}
|
}
|
||||||
if maxSourceNum <= len(alSourceCache) {
|
if maxSourceNum <= len(playerCache) {
|
||||||
return 0, nil, ErrTooManyPlayers
|
return nil, ErrTooManyPlayers
|
||||||
}
|
}
|
||||||
s := al.GenSources(1)
|
s := al.GenSources(1)
|
||||||
if err := al.Error(); err != 0 {
|
if err := al.Error(); err != 0 {
|
||||||
panic(fmt.Sprintf("audio: al.GenSources error: %d", err))
|
panic(fmt.Sprintf("audio: al.GenSources error: %d", err))
|
||||||
}
|
}
|
||||||
e := &alSourceCacheEntry{
|
|
||||||
source: s[0],
|
|
||||||
buffers: []al.Buffer{},
|
|
||||||
sampleRate: sampleRate,
|
|
||||||
}
|
|
||||||
alSourceCache = append(alSourceCache, e)
|
|
||||||
return s[0], e.buffers, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func newPlayer(src io.ReadSeeker, sampleRate int) (*Player, error) {
|
|
||||||
m.Lock()
|
|
||||||
if e := al.OpenDevice(); e != nil {
|
|
||||||
m.Unlock()
|
|
||||||
return nil, fmt.Errorf("audio: OpenAL initialization failed: %v", e)
|
|
||||||
}
|
|
||||||
s, b, err := newAlSource(sampleRate)
|
|
||||||
if err != nil {
|
|
||||||
m.Unlock()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
m.Unlock()
|
|
||||||
p := &player{
|
p := &player{
|
||||||
alSource: s,
|
alSource: s[0],
|
||||||
alBuffers: b,
|
alBuffers: []al.Buffer{},
|
||||||
source: src,
|
source: src,
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(p, (*player).close)
|
runtime.SetFinalizer(p, (*player).close)
|
||||||
|
playerCache = append(playerCache, p)
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPlayer(src io.ReadSeeker, sampleRate int) (*Player, error) {
|
||||||
|
m.Lock()
|
||||||
|
defer m.Unlock()
|
||||||
|
|
||||||
|
if e := al.OpenDevice(); e != nil {
|
||||||
|
m.Unlock()
|
||||||
|
return nil, fmt.Errorf("audio: OpenAL initialization failed: %v", e)
|
||||||
|
}
|
||||||
|
p, err := newPlayerFromCache(src, sampleRate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &Player{p}, nil
|
return &Player{p}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,6 +107,7 @@ func (p *player) proceed() error {
|
|||||||
p.alBuffers = append(p.alBuffers, bufs...)
|
p.alBuffers = append(p.alBuffers, bufs...)
|
||||||
}
|
}
|
||||||
m.Unlock()
|
m.Unlock()
|
||||||
|
|
||||||
for 0 < len(p.alBuffers) {
|
for 0 < len(p.alBuffers) {
|
||||||
b := make([]byte, bufferSize)
|
b := make([]byte, bufferSize)
|
||||||
n, err := p.source.Read(b)
|
n, err := p.source.Read(b)
|
||||||
@ -133,6 +126,7 @@ func (p *player) proceed() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Lock()
|
m.Lock()
|
||||||
if p.alSource.State() == al.Stopped {
|
if p.alSource.State() == al.Stopped {
|
||||||
al.RewindSources(p.alSource)
|
al.RewindSources(p.alSource)
|
||||||
@ -147,12 +141,15 @@ func (p *player) proceed() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *player) play() error {
|
func (p *player) play() error {
|
||||||
const bufferMaxNum = 8
|
|
||||||
// TODO: What if play is already called?
|
// TODO: What if play is already called?
|
||||||
m.Lock()
|
m.Lock()
|
||||||
n := bufferMaxNum - int(p.alSource.BuffersQueued()) - len(p.alBuffers)
|
n := maxBufferNum - int(p.alSource.BuffersQueued()) - len(p.alBuffers)
|
||||||
if 0 < n {
|
if 0 < n {
|
||||||
p.alBuffers = append(p.alBuffers, al.GenBuffers(n)...)
|
p.alBuffers = append(p.alBuffers, al.GenBuffers(n)...)
|
||||||
|
totalBufferNum += n
|
||||||
|
if maxSourceNum*maxBufferNum < totalBufferNum {
|
||||||
|
panic("audio: too many buffers are created")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if 0 < len(p.alBuffers) {
|
if 0 < len(p.alBuffers) {
|
||||||
emptyBytes := make([]byte, bufferSize)
|
emptyBytes := make([]byte, bufferSize)
|
||||||
@ -165,6 +162,7 @@ func (p *player) play() error {
|
|||||||
}
|
}
|
||||||
al.PlaySources(p.alSource)
|
al.PlaySources(p.alSource)
|
||||||
m.Unlock()
|
m.Unlock()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer p.close()
|
defer p.close()
|
||||||
for {
|
for {
|
||||||
@ -184,44 +182,27 @@ func (p *player) play() error {
|
|||||||
|
|
||||||
func (p *player) close() error {
|
func (p *player) close() error {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
|
defer m.Unlock()
|
||||||
|
|
||||||
if err := al.Error(); err != 0 {
|
if err := al.Error(); err != 0 {
|
||||||
panic(fmt.Sprintf("audio: error before closing: %d", err))
|
panic(fmt.Sprintf("audio: error before closing: %d", err))
|
||||||
}
|
}
|
||||||
s := p.alSource
|
if p.isClosed {
|
||||||
if p.alSource != 0 {
|
return nil
|
||||||
var bs []al.Buffer
|
|
||||||
al.RewindSources(p.alSource)
|
|
||||||
al.StopSources(p.alSource)
|
|
||||||
n := p.alSource.BuffersQueued()
|
|
||||||
if 0 < n {
|
|
||||||
bs = make([]al.Buffer, n)
|
|
||||||
p.alSource.UnqueueBuffers(bs...)
|
|
||||||
p.alBuffers = append(p.alBuffers, bs...)
|
|
||||||
}
|
|
||||||
p.alSource = 0
|
|
||||||
}
|
}
|
||||||
|
var bs []al.Buffer
|
||||||
|
al.RewindSources(p.alSource)
|
||||||
|
al.StopSources(p.alSource)
|
||||||
|
n := p.alSource.BuffersQueued()
|
||||||
|
if 0 < n {
|
||||||
|
bs = make([]al.Buffer, n)
|
||||||
|
p.alSource.UnqueueBuffers(bs...)
|
||||||
|
p.alBuffers = append(p.alBuffers, bs...)
|
||||||
|
}
|
||||||
|
p.isClosed = true
|
||||||
if err := al.Error(); err != 0 {
|
if err := al.Error(); err != 0 {
|
||||||
panic(fmt.Sprintf("audio: closing error: %d", err))
|
panic(fmt.Sprintf("audio: error after closing: %d", err))
|
||||||
}
|
}
|
||||||
if s != 0 {
|
|
||||||
found := false
|
|
||||||
for _, e := range alSourceCache {
|
|
||||||
if e.source != s {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if e.isClosed {
|
|
||||||
panic("audio: cache state is invalid: source is already closed?")
|
|
||||||
}
|
|
||||||
e.buffers = p.alBuffers
|
|
||||||
e.isClosed = true
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
panic("audio: cache state is invalid: source is not cached?")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.Unlock()
|
|
||||||
runtime.SetFinalizer(p, nil)
|
runtime.SetFinalizer(p, nil)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user