diff --git a/exp/audio/internal/driver/driver_openal.go b/exp/audio/internal/driver/driver_openal.go index 2edff16ac..a359d300e 100644 --- a/exp/audio/internal/driver/driver_openal.go +++ b/exp/audio/internal/driver/driver_openal.go @@ -34,21 +34,37 @@ type Player struct { source io.Reader sampleRate int isClosed bool + alFormat uint32 +} + +func alFormat(channelNum, bytesPerSample int) uint32 { + switch { + case channelNum == 1 && bytesPerSample == 1: + return al.FormatMono8 + case channelNum == 1 && bytesPerSample == 2: + return al.FormatMono16 + case channelNum == 2 && bytesPerSample == 1: + return al.FormatStereo8 + case channelNum == 2 && bytesPerSample == 2: + return al.FormatStereo16 + } + 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) { if e := al.OpenDevice(); e != nil { - return nil, fmt.Errorf("audio: OpenAL initialization failed: %v", e) + return nil, fmt.Errorf("driver: OpenAL initialization failed: %v", e) } s := al.GenSources(1) if err := al.Error(); err != 0 { - return nil, fmt.Errorf("audio: al.GenSources error: %d", err) + return nil, fmt.Errorf("driver: al.GenSources error: %d", err) } p := &Player{ alSource: s[0], alBuffers: []al.Buffer{}, source: src, sampleRate: sampleRate, + alFormat: alFormat(channelNum, bytesPerSample), } runtime.SetFinalizer(p, (*Player).Close) @@ -56,7 +72,7 @@ func NewPlayer(src io.Reader, sampleRate, channelNum, bytesPerSample int) (*Play emptyBytes := make([]byte, bufferSize) for _, b := range bs { // Note that the third argument of only the first buffer is used. - b.BufferData(al.FormatStereo16, emptyBytes, int32(p.sampleRate)) + b.BufferData(p.alFormat, emptyBytes, int32(p.sampleRate)) p.alSource.QueueBuffers(b) } al.PlaySources(p.alSource) @@ -74,14 +90,14 @@ var ( func (p *Player) Proceed() error { if err := al.Error(); err != 0 { - return fmt.Errorf("audio: before proceed: %d", err) + return fmt.Errorf("driver: before proceed: %d", err) } processedNum := p.alSource.BuffersProcessed() if 0 < processedNum { bufs := tmpAlBuffers[:processedNum] p.alSource.UnqueueBuffers(bufs...) if err := al.Error(); err != 0 { - return fmt.Errorf("audio: Unqueue in process: %d", err) + return fmt.Errorf("driver: Unqueue in process: %d", err) } p.alBuffers = append(p.alBuffers, bufs...) } @@ -91,10 +107,10 @@ func (p *Player) Proceed() error { if 0 < n { buf := p.alBuffers[0] p.alBuffers = p.alBuffers[1:] - buf.BufferData(al.FormatStereo16, tmpBuffer[:n], int32(p.sampleRate)) + buf.BufferData(p.alFormat, tmpBuffer[:n], int32(p.sampleRate)) p.alSource.QueueBuffers(buf) if err := al.Error(); err != 0 { - return fmt.Errorf("audio: Queue in process: %d", err) + return fmt.Errorf("driver: Queue in process: %d", err) } } if err != nil { @@ -106,7 +122,7 @@ func (p *Player) Proceed() error { al.RewindSources(p.alSource) al.PlaySources(p.alSource) if err := al.Error(); err != 0 { - return fmt.Errorf("audio: PlaySource in process: %d", err) + return fmt.Errorf("driver: PlaySource in process: %d", err) } } @@ -115,7 +131,7 @@ func (p *Player) Proceed() error { func (p *Player) Close() error { if err := al.Error(); err != 0 { - return fmt.Errorf("audio: error before closing: %d", err) + return fmt.Errorf("driver: error before closing: %d", err) } if p.isClosed { return nil @@ -130,7 +146,7 @@ func (p *Player) Close() error { } p.isClosed = true if err := al.Error(); err != 0 { - return fmt.Errorf("audio: error after closing: %d", err) + return fmt.Errorf("driver: error after closing: %d", err) } runtime.SetFinalizer(p, nil) return nil