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

This reverts commit 6e5b8a28dc.
This commit is contained in:
Hajime Hoshi 2016-04-09 22:56:25 +09:00
parent 6e5b8a28dc
commit 52aa9d4784

View File

@ -21,72 +21,61 @@ import (
"io" "io"
"runtime" "runtime"
"github.com/timshannon/go-openal/openal" "golang.org/x/mobile/exp/audio/al"
) )
// 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 {
alDevice *openal.Device alSource al.Source
alSource openal.Source alBuffers []al.Buffer
alBuffers []openal.Buffer
source io.Reader source io.Reader
sampleRate int sampleRate int
isClosed bool isClosed bool
alFormat openal.Format alFormat uint32
} }
func alFormat(channelNum, bytesPerSample int) openal.Format { func alFormat(channelNum, bytesPerSample int) uint32 {
switch { switch {
case channelNum == 1 && bytesPerSample == 1: case channelNum == 1 && bytesPerSample == 1:
return openal.FormatMono8 return al.FormatMono8
case channelNum == 1 && bytesPerSample == 2: case channelNum == 1 && bytesPerSample == 2:
return openal.FormatMono16 return al.FormatMono16
case channelNum == 2 && bytesPerSample == 1: case channelNum == 2 && bytesPerSample == 1:
return openal.FormatStereo8 return al.FormatStereo8
case channelNum == 2 && bytesPerSample == 2: case channelNum == 2 && bytesPerSample == 2:
return openal.FormatStereo16 return al.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) {
d := openal.OpenDevice("") if e := al.OpenDevice(); e != nil {
if err := openal.Err(); err != nil { return nil, fmt.Errorf("driver: OpenAL initialization failed: %v", e)
return nil, err
} }
c := d.CreateContext() s := al.GenSources(1)
if err := openal.Err(); err != nil { if err := al.Error(); err != 0 {
return nil, err return nil, fmt.Errorf("driver: al.GenSources error: %d", err)
}
c.Activate()
s := openal.NewSource()
if err := openal.Err(); err != nil {
return nil, err
} }
p := &Player{ p := &Player{
alDevice: d, alSource: s[0],
alSource: s, alBuffers: []al.Buffer{},
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 := openal.NewBuffers(maxBufferNum) bs := al.GenBuffers(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.SetData(p.alFormat, emptyBytes, int32(p.sampleRate)) b.BufferData(p.alFormat, emptyBytes, int32(p.sampleRate))
p.alSource.QueueBuffer(b) p.alSource.QueueBuffers(b)
} }
p.alSource.Play() al.PlaySources(p.alSource)
return p, nil return p, nil
} }
@ -96,19 +85,19 @@ const (
var ( var (
tmpBuffer = make([]byte, bufferSize) tmpBuffer = make([]byte, bufferSize)
tmpAlBuffers = make([]openal.Buffer, maxBufferNum) tmpAlBuffers = make([]al.Buffer, maxBufferNum)
) )
func (p *Player) Proceed() error { func (p *Player) Proceed() error {
if err := openal.Err(); err != nil { if err := al.Error(); err != 0 {
return err return fmt.Errorf("driver: before proceed: %d", 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 := openal.Err(); err != nil { if err := al.Error(); err != 0 {
return err return fmt.Errorf("driver: Unqueue in process: %d", err)
} }
p.alBuffers = append(p.alBuffers, bufs...) p.alBuffers = append(p.alBuffers, bufs...)
} }
@ -118,10 +107,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.SetData(p.alFormat, tmpBuffer[:n], int32(p.sampleRate)) buf.BufferData(p.alFormat, tmpBuffer[:n], int32(p.sampleRate))
p.alSource.QueueBuffer(buf) p.alSource.QueueBuffers(buf)
if err := openal.Err(); err != nil { if err := al.Error(); err != 0 {
return err return fmt.Errorf("driver: Queue in process: %d", err)
} }
} }
if err != nil { if err != nil {
@ -129,11 +118,11 @@ func (p *Player) Proceed() error {
} }
} }
if p.alSource.State() == openal.Stopped || p.alSource.State() == openal.Initial { if p.alSource.State() == al.Stopped || p.alSource.State() == al.Initial {
p.alSource.Rewind() al.RewindSources(p.alSource)
p.alSource.Play() al.PlaySources(p.alSource)
if err := openal.Err(); err != nil { if err := al.Error(); err != 0 {
return err return fmt.Errorf("driver: PlaySource in process: %d", err)
} }
} }
@ -141,24 +130,23 @@ func (p *Player) Proceed() error {
} }
func (p *Player) Close() error { func (p *Player) Close() error {
if err := openal.Err(); err != nil { if err := al.Error(); err != 0 {
return err return fmt.Errorf("driver: error before closing: %d", err)
} }
if p.isClosed { if p.isClosed {
return nil return nil
} }
var bs []openal.Buffer var bs []al.Buffer
p.alSource.Rewind() al.RewindSources(p.alSource)
p.alSource.Play() al.StopSources(p.alSource)
if n := p.alSource.BuffersQueued(); 0 < n { if n := p.alSource.BuffersQueued(); 0 < n {
bs = make([]openal.Buffer, n) bs = make([]al.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 := openal.Err(); err != nil { if err := al.Error(); err != 0 {
return err return fmt.Errorf("driver: error after closing: %d", err)
} }
runtime.SetFinalizer(p, nil) runtime.SetFinalizer(p, nil)
return nil return nil