ebiten/exp/audio/audio_openal.go

156 lines
3.7 KiB
Go
Raw Normal View History

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.
// +build !js,!windows
2015-01-24 06:50:41 +01:00
package audio
2015-01-24 06:50:41 +01:00
import (
2016-02-11 19:06:04 +01:00
"fmt"
"io"
2016-02-11 19:06:04 +01:00
"runtime"
2016-03-13 14:39:03 +01:00
"time"
2015-06-07 15:01:14 +02:00
2016-02-11 19:06:04 +01:00
"golang.org/x/mobile/exp/audio/al"
)
const (
maxBufferNum = 8
)
// TODO: This should be in player
var totalBufferNum = 0
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
2016-03-03 03:57:25 +01:00
source io.Reader
2016-02-11 19:06:04 +01:00
sampleRate int
isClosed bool
2016-02-11 10:43:11 +01:00
}
2016-04-04 16:42:44 +02:00
func newPlayer(src io.Reader, sampleRate int) (*player, error) {
if e := al.OpenDevice(); e != nil {
2016-04-04 16:42:44 +02:00
return nil, fmt.Errorf("audio: OpenAL initialization failed: %v", e)
}
2016-02-11 19:06:04 +01:00
s := al.GenSources(1)
if err := al.Error(); err != 0 {
2016-04-04 16:42:44 +02:00
return nil, fmt.Errorf("audio: al.GenSources error: %d", err)
2016-02-11 19:06:04 +01:00
}
p := &player{
alSource: s[0],
alBuffers: []al.Buffer{},
source: src,
sampleRate: sampleRate,
}
runtime.SetFinalizer(p, (*player).close)
n := maxBufferNum - int(p.alSource.BuffersQueued()) - len(p.alBuffers)
if 0 < n {
p.alBuffers = append(p.alBuffers, al.GenBuffers(n)...)
totalBufferNum += n
if maxBufferNum < totalBufferNum {
2016-04-03 09:13:00 +02:00
panic("audio: not reach: too many buffers are created")
}
}
if 0 < len(p.alBuffers) {
emptyBytes := make([]byte, bufferSize)
for _, buf := range p.alBuffers {
// Note that the third argument of only the first buffer is used.
buf.BufferData(al.FormatStereo16, emptyBytes, int32(p.sampleRate))
p.alSource.QueueBuffers(buf)
}
p.alBuffers = []al.Buffer{}
}
al.PlaySources(p.alSource)
2016-04-04 16:42:44 +02:00
return p, nil
}
const (
bufferSize = 1024
)
var (
tmpBuffer = make([]byte, bufferSize)
tmpAlBuffers = make([]al.Buffer, maxBufferNum)
)
2016-02-11 19:06:04 +01:00
func (p *player) proceed() error {
2016-02-13 12:43:27 +01:00
if err := al.Error(); err != 0 {
2016-04-03 09:13:00 +02:00
return fmt.Errorf("audio: before proceed: %d", err)
2016-02-13 12:43:27 +01:00
}
2016-02-11 19:06:04 +01:00
processedNum := p.alSource.BuffersProcessed()
if 0 < processedNum {
bufs := tmpAlBuffers[:processedNum]
2016-02-11 19:06:04 +01:00
p.alSource.UnqueueBuffers(bufs...)
2016-02-13 12:43:27 +01:00
if err := al.Error(); err != 0 {
2016-04-03 09:13:00 +02:00
return fmt.Errorf("audio: Unqueue in process: %d", err)
2016-02-13 12:43:27 +01:00
}
2016-02-11 19:06:04 +01:00
p.alBuffers = append(p.alBuffers, bufs...)
}
if 0 < len(p.alBuffers) {
n, err := p.source.Read(tmpBuffer)
2016-02-11 19:06:04 +01:00
if 0 < n {
buf := p.alBuffers[0]
p.alBuffers = p.alBuffers[1:]
buf.BufferData(al.FormatStereo16, tmpBuffer[:n], int32(p.sampleRate))
2016-02-11 19:06:04 +01:00
p.alSource.QueueBuffers(buf)
2016-02-13 12:43:27 +01:00
if err := al.Error(); err != 0 {
2016-04-03 09:13:00 +02:00
return fmt.Errorf("audio: Queue in process: %d", err)
2016-02-13 12:43:27 +01:00
}
2016-02-11 19:06:04 +01:00
}
if err != nil {
return err
}
2016-03-13 14:39:03 +01:00
if n == 0 {
2016-03-13 17:03:52 +01:00
time.Sleep(1 * time.Millisecond)
2016-03-13 14:39:03 +01:00
}
}
2016-02-11 19:06:04 +01:00
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 {
2016-04-03 09:13:00 +02:00
return fmt.Errorf("audio: PlaySource in process: %d", err)
2016-02-13 12:43:27 +01:00
}
2016-02-11 19:06:04 +01:00
}
return nil
2016-02-11 10:43:11 +01:00
}
2016-02-11 11:55:59 +01:00
func (p *player) close() error {
2016-02-13 12:43:27 +01:00
if err := al.Error(); err != 0 {
2016-04-03 09:13:00 +02:00
return fmt.Errorf("audio: error before closing: %d", err)
2016-02-13 12:43:27 +01:00
}
if p.isClosed {
return nil
2016-02-11 19:06:04 +01:00
}
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...)
2016-02-11 19:06:04 +01:00
}
p.isClosed = true
if err := al.Error(); err != 0 {
2016-04-03 09:13:00 +02:00
return fmt.Errorf("audio: error after closing: %d", err)
}
2016-02-11 19:06:04 +01:00
runtime.SetFinalizer(p, nil)
return nil
2016-02-11 11:55:59 +01:00
}