2015-01-24 06:50:41 +01:00
|
|
|
// 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.
|
|
|
|
|
2015-06-20 16:39:56 +02:00
|
|
|
// +build !js,!windows
|
2015-01-24 06:50:41 +01:00
|
|
|
|
2016-02-07 08:03:33 +01:00
|
|
|
package audio
|
2015-01-24 06:50:41 +01:00
|
|
|
|
2015-01-24 14:19:10 +01:00
|
|
|
import (
|
2016-02-11 19:06:04 +01:00
|
|
|
"fmt"
|
2016-02-11 09:07:28 +01:00
|
|
|
"io"
|
2016-02-11 19:06:04 +01:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-06-07 15:01:14 +02:00
|
|
|
|
2016-02-11 19:06:04 +01:00
|
|
|
"golang.org/x/mobile/exp/audio/al"
|
2015-01-24 14:19:10 +01:00
|
|
|
)
|
|
|
|
|
2016-02-13 12:43:27 +01:00
|
|
|
// alSourceCacheEntry represents a source.
|
|
|
|
// In some environments, too many calls of alGenSources and alGenBuffers could cause errors.
|
|
|
|
// To avoid this error, al.Source and al.Buffer are reused.
|
2016-02-12 13:39:48 +01:00
|
|
|
type alSourceCacheEntry struct {
|
|
|
|
source al.Source
|
2016-02-13 12:43:27 +01:00
|
|
|
buffers []al.Buffer
|
2016-02-12 13:39:48 +01:00
|
|
|
sampleRate int
|
2016-02-12 22:44:49 +01:00
|
|
|
isClosed bool
|
2016-02-11 09:07:28 +01:00
|
|
|
}
|
|
|
|
|
2016-02-12 13:39:48 +01:00
|
|
|
const maxSourceNum = 32
|
|
|
|
|
2016-02-12 22:44:49 +01:00
|
|
|
var alSourceCache = []*alSourceCacheEntry{}
|
2016-02-11 09:07:28 +01:00
|
|
|
|
2016-02-11 10:43:11 +01:00
|
|
|
type player struct {
|
2016-02-11 19:06:04 +01:00
|
|
|
alSource al.Source
|
|
|
|
alBuffers []al.Buffer
|
|
|
|
source io.ReadSeeker
|
|
|
|
sampleRate int
|
2016-02-11 10:43:11 +01:00
|
|
|
}
|
|
|
|
|
2016-02-11 19:06:04 +01:00
|
|
|
var m sync.Mutex
|
|
|
|
|
2016-02-13 12:43:27 +01:00
|
|
|
func newAlSource(sampleRate int) (al.Source, []al.Buffer, error) {
|
2016-02-12 13:39:48 +01:00
|
|
|
for _, e := range alSourceCache {
|
|
|
|
if e.sampleRate != sampleRate {
|
|
|
|
continue
|
|
|
|
}
|
2016-02-12 22:44:49 +01:00
|
|
|
if !e.isClosed {
|
2016-02-12 13:39:48 +01:00
|
|
|
continue
|
|
|
|
}
|
2016-02-12 22:44:49 +01:00
|
|
|
e.isClosed = false
|
2016-02-13 12:43:27 +01:00
|
|
|
return e.source, e.buffers, nil
|
2016-02-12 13:39:48 +01:00
|
|
|
}
|
|
|
|
if maxSourceNum <= len(alSourceCache) {
|
2016-02-13 12:43:27 +01:00
|
|
|
return 0, nil, ErrTooManyPlayers
|
2016-02-11 19:06:04 +01:00
|
|
|
}
|
|
|
|
s := al.GenSources(1)
|
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
panic(fmt.Sprintf("audio: al.GenSources error: %d", err))
|
|
|
|
}
|
2016-02-12 22:44:49 +01:00
|
|
|
e := &alSourceCacheEntry{
|
2016-02-12 13:39:48 +01:00
|
|
|
source: s[0],
|
2016-02-13 12:43:27 +01:00
|
|
|
buffers: []al.Buffer{},
|
2016-02-12 13:39:48 +01:00
|
|
|
sampleRate: sampleRate,
|
|
|
|
}
|
|
|
|
alSourceCache = append(alSourceCache, e)
|
2016-02-13 12:43:27 +01:00
|
|
|
return s[0], e.buffers, nil
|
2016-02-12 13:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2016-02-13 12:43:27 +01:00
|
|
|
s, b, err := newAlSource(sampleRate)
|
2016-02-12 13:39:48 +01:00
|
|
|
if err != nil {
|
|
|
|
m.Unlock()
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-11 19:06:04 +01:00
|
|
|
m.Unlock()
|
|
|
|
p := &player{
|
2016-02-12 13:39:48 +01:00
|
|
|
alSource: s,
|
2016-02-13 12:43:27 +01:00
|
|
|
alBuffers: b,
|
2016-02-11 19:06:04 +01:00
|
|
|
source: src,
|
|
|
|
sampleRate: sampleRate,
|
|
|
|
}
|
|
|
|
runtime.SetFinalizer(p, (*player).close)
|
2016-02-12 13:39:48 +01:00
|
|
|
return &Player{p}, nil
|
2016-02-11 19:06:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const bufferSize = 1024
|
|
|
|
|
|
|
|
func (p *player) proceed() error {
|
|
|
|
m.Lock()
|
2016-02-13 12:43:27 +01:00
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
panic(fmt.Sprintf("audio: before proceed: %d", err))
|
|
|
|
}
|
2016-02-11 19:06:04 +01:00
|
|
|
processedNum := p.alSource.BuffersProcessed()
|
|
|
|
if 0 < processedNum {
|
|
|
|
bufs := make([]al.Buffer, processedNum)
|
|
|
|
p.alSource.UnqueueBuffers(bufs...)
|
2016-02-13 12:43:27 +01:00
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
panic(fmt.Sprintf("audio: Unqueue in process: %d", err))
|
|
|
|
}
|
2016-02-11 19:06:04 +01:00
|
|
|
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)
|
2016-02-13 12:43:27 +01:00
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
panic(fmt.Sprintf("audio: Queue in process: %d", err))
|
|
|
|
}
|
2016-02-11 19:06:04 +01:00
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-09 18:40:07 +01:00
|
|
|
}
|
2016-02-11 19:06:04 +01:00
|
|
|
m.Lock()
|
|
|
|
if p.alSource.State() == al.Stopped {
|
|
|
|
al.RewindSources(p.alSource)
|
|
|
|
al.PlaySources(p.alSource)
|
2016-02-13 12:43:27 +01:00
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
panic(fmt.Sprintf("audio: PlaySource in process: %d", err))
|
|
|
|
}
|
2016-02-11 19:06:04 +01:00
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
return nil
|
2016-02-11 10:43:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) play() error {
|
2016-02-12 15:49:30 +01:00
|
|
|
const bufferMaxNum = 8
|
2016-02-11 19:06:04 +01:00
|
|
|
// TODO: What if play is already called?
|
|
|
|
m.Lock()
|
2016-02-12 16:45:48 +01:00
|
|
|
n := bufferMaxNum - int(p.alSource.BuffersQueued()) - len(p.alBuffers)
|
2016-02-12 13:43:00 +01:00
|
|
|
if 0 < n {
|
2016-02-12 16:45:48 +01:00
|
|
|
p.alBuffers = append(p.alBuffers, al.GenBuffers(n)...)
|
|
|
|
}
|
|
|
|
if 0 < len(p.alBuffers) {
|
|
|
|
emptyBytes := make([]byte, bufferSize)
|
|
|
|
for _, buf := range p.alBuffers {
|
2016-02-12 13:43:00 +01:00
|
|
|
// Note that the third argument of only the first buffer is used.
|
|
|
|
buf.BufferData(al.FormatStereo16, emptyBytes, int32(p.sampleRate))
|
|
|
|
p.alSource.QueueBuffers(buf)
|
|
|
|
}
|
2016-02-12 16:45:48 +01:00
|
|
|
p.alBuffers = []al.Buffer{}
|
2016-02-11 19:06:04 +01:00
|
|
|
}
|
|
|
|
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
|
2016-02-09 18:40:07 +01:00
|
|
|
}
|
2015-01-24 17:51:51 +01:00
|
|
|
|
2016-02-11 11:55:59 +01:00
|
|
|
func (p *player) close() error {
|
2016-02-11 19:06:04 +01:00
|
|
|
m.Lock()
|
2016-02-13 12:43:27 +01:00
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
panic(fmt.Sprintf("audio: error before closing: %d", err))
|
|
|
|
}
|
2016-02-12 22:44:49 +01:00
|
|
|
s := p.alSource
|
2016-02-13 12:43:27 +01:00
|
|
|
if p.alSource != 0 {
|
|
|
|
var bs []al.Buffer
|
2016-02-12 13:39:48 +01:00
|
|
|
al.RewindSources(p.alSource)
|
|
|
|
al.StopSources(p.alSource)
|
2016-02-12 15:49:30 +01:00
|
|
|
n := p.alSource.BuffersQueued()
|
|
|
|
if 0 < n {
|
|
|
|
bs = make([]al.Buffer, n)
|
|
|
|
p.alSource.UnqueueBuffers(bs...)
|
2016-02-13 12:43:27 +01:00
|
|
|
p.alBuffers = append(p.alBuffers, bs...)
|
2016-02-12 15:49:30 +01:00
|
|
|
}
|
2016-02-11 19:06:04 +01:00
|
|
|
p.alSource = 0
|
|
|
|
}
|
|
|
|
if err := al.Error(); err != 0 {
|
2016-02-12 13:39:48 +01:00
|
|
|
panic(fmt.Sprintf("audio: closing error: %d", err))
|
2016-02-11 19:06:04 +01:00
|
|
|
}
|
2016-02-12 22:44:49 +01:00
|
|
|
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?")
|
|
|
|
}
|
2016-02-13 12:43:27 +01:00
|
|
|
e.buffers = p.alBuffers
|
2016-02-12 22:44:49 +01:00
|
|
|
e.isClosed = true
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
panic("audio: cache state is invalid: source is not cached?")
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 19:06:04 +01:00
|
|
|
m.Unlock()
|
|
|
|
runtime.SetFinalizer(p, nil)
|
|
|
|
return nil
|
2016-02-11 11:55:59 +01:00
|
|
|
}
|