audio: Use another OpenAL library for Mac OS X (#195)

This commit is contained in:
Hajime Hoshi 2016-04-09 22:15:00 +09:00
parent c2d1f99e7e
commit 6e5b8a28dc

View File

@ -21,61 +21,72 @@ import (
"io" "io"
"runtime" "runtime"
"golang.org/x/mobile/exp/audio/al" "github.com/timshannon/go-openal/openal"
) )
// As x/mobile/exp/audio/al is broken on Mac OS X (https://github.com/golang/go/issues/15075),
// let's use timshannon/go-openal.
const ( const (
maxBufferNum = 8 maxBufferNum = 8
) )
type Player struct { type Player struct {
alSource al.Source alDevice *openal.Device
alBuffers []al.Buffer alSource openal.Source
alBuffers []openal.Buffer
source io.Reader source io.Reader
sampleRate int sampleRate int
isClosed bool isClosed bool
alFormat uint32 alFormat openal.Format
} }
func alFormat(channelNum, bytesPerSample int) uint32 { func alFormat(channelNum, bytesPerSample int) openal.Format {
switch { switch {
case channelNum == 1 && bytesPerSample == 1: case channelNum == 1 && bytesPerSample == 1:
return al.FormatMono8 return openal.FormatMono8
case channelNum == 1 && bytesPerSample == 2: case channelNum == 1 && bytesPerSample == 2:
return al.FormatMono16 return openal.FormatMono16
case channelNum == 2 && bytesPerSample == 1: case channelNum == 2 && bytesPerSample == 1:
return al.FormatStereo8 return openal.FormatStereo8
case channelNum == 2 && bytesPerSample == 2: case channelNum == 2 && bytesPerSample == 2:
return al.FormatStereo16 return openal.FormatStereo16
} }
panic(fmt.Sprintf("driver: invalid channel num (%d) or bytes per sample (%d)", channelNum, bytesPerSample)) panic(fmt.Sprintf("driver: invalid channel num (%d) or bytes per sample (%d)", channelNum, bytesPerSample))
} }
func NewPlayer(src io.Reader, sampleRate, channelNum, bytesPerSample int) (*Player, error) { func NewPlayer(src io.Reader, sampleRate, channelNum, bytesPerSample int) (*Player, error) {
if e := al.OpenDevice(); e != nil { d := openal.OpenDevice("")
return nil, fmt.Errorf("driver: OpenAL initialization failed: %v", e) if err := openal.Err(); err != nil {
return nil, err
} }
s := al.GenSources(1) c := d.CreateContext()
if err := al.Error(); err != 0 { if err := openal.Err(); err != nil {
return nil, fmt.Errorf("driver: al.GenSources error: %d", err) return nil, err
}
c.Activate()
s := openal.NewSource()
if err := openal.Err(); err != nil {
return nil, err
} }
p := &Player{ p := &Player{
alSource: s[0], alDevice: d,
alBuffers: []al.Buffer{}, alSource: s,
alBuffers: []openal.Buffer{},
source: src, source: src,
sampleRate: sampleRate, sampleRate: sampleRate,
alFormat: alFormat(channelNum, bytesPerSample), alFormat: alFormat(channelNum, bytesPerSample),
} }
runtime.SetFinalizer(p, (*Player).Close) runtime.SetFinalizer(p, (*Player).Close)
bs := al.GenBuffers(maxBufferNum) bs := openal.NewBuffers(maxBufferNum)
emptyBytes := make([]byte, bufferSize) emptyBytes := make([]byte, bufferSize)
for _, b := range bs { for _, b := range bs {
// Note that the third argument of only the first buffer is used. // Note that the third argument of only the first buffer is used.
b.BufferData(p.alFormat, emptyBytes, int32(p.sampleRate)) b.SetData(p.alFormat, emptyBytes, int32(p.sampleRate))
p.alSource.QueueBuffers(b) p.alSource.QueueBuffer(b)
} }
al.PlaySources(p.alSource) p.alSource.Play()
return p, nil return p, nil
} }
@ -85,19 +96,19 @@ const (
var ( var (
tmpBuffer = make([]byte, bufferSize) tmpBuffer = make([]byte, bufferSize)
tmpAlBuffers = make([]al.Buffer, maxBufferNum) tmpAlBuffers = make([]openal.Buffer, maxBufferNum)
) )
func (p *Player) Proceed() error { func (p *Player) Proceed() error {
if err := al.Error(); err != 0 { if err := openal.Err(); err != nil {
return fmt.Errorf("driver: before proceed: %d", err) return err
} }
processedNum := p.alSource.BuffersProcessed() processedNum := p.alSource.BuffersProcessed()
if 0 < processedNum { if 0 < processedNum {
bufs := tmpAlBuffers[:processedNum] bufs := tmpAlBuffers[:processedNum]
p.alSource.UnqueueBuffers(bufs...) p.alSource.UnqueueBuffers(bufs)
if err := al.Error(); err != 0 { if err := openal.Err(); err != nil {
return fmt.Errorf("driver: Unqueue in process: %d", err) return err
} }
p.alBuffers = append(p.alBuffers, bufs...) p.alBuffers = append(p.alBuffers, bufs...)
} }
@ -107,10 +118,10 @@ func (p *Player) Proceed() error {
if 0 < n { if 0 < n {
buf := p.alBuffers[0] buf := p.alBuffers[0]
p.alBuffers = p.alBuffers[1:] p.alBuffers = p.alBuffers[1:]
buf.BufferData(p.alFormat, tmpBuffer[:n], int32(p.sampleRate)) buf.SetData(p.alFormat, tmpBuffer[:n], int32(p.sampleRate))
p.alSource.QueueBuffers(buf) p.alSource.QueueBuffer(buf)
if err := al.Error(); err != 0 { if err := openal.Err(); err != nil {
return fmt.Errorf("driver: Queue in process: %d", err) return err
} }
} }
if err != nil { if err != nil {
@ -118,11 +129,11 @@ func (p *Player) Proceed() error {
} }
} }
if p.alSource.State() == al.Stopped || p.alSource.State() == al.Initial { if p.alSource.State() == openal.Stopped || p.alSource.State() == openal.Initial {
al.RewindSources(p.alSource) p.alSource.Rewind()
al.PlaySources(p.alSource) p.alSource.Play()
if err := al.Error(); err != 0 { if err := openal.Err(); err != nil {
return fmt.Errorf("driver: PlaySource in process: %d", err) return err
} }
} }
@ -130,23 +141,24 @@ func (p *Player) Proceed() error {
} }
func (p *Player) Close() error { func (p *Player) Close() error {
if err := al.Error(); err != 0 { if err := openal.Err(); err != nil {
return fmt.Errorf("driver: error before closing: %d", err) return err
} }
if p.isClosed { if p.isClosed {
return nil return nil
} }
var bs []al.Buffer var bs []openal.Buffer
al.RewindSources(p.alSource) p.alSource.Rewind()
al.StopSources(p.alSource) p.alSource.Play()
if n := p.alSource.BuffersQueued(); 0 < n { if n := p.alSource.BuffersQueued(); 0 < n {
bs = make([]al.Buffer, n) bs = make([]openal.Buffer, n)
p.alSource.UnqueueBuffers(bs...) p.alSource.UnqueueBuffers(bs)
p.alBuffers = append(p.alBuffers, bs...) p.alBuffers = append(p.alBuffers, bs...)
} }
p.alDevice.CloseDevice()
p.isClosed = true p.isClosed = true
if err := al.Error(); err != 0 { if err := openal.Err(); err != nil {
return fmt.Errorf("driver: error after closing: %d", err) return err
} }
runtime.SetFinalizer(p, nil) runtime.SetFinalizer(p, nil)
return nil return nil