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-04-08 17:54:18 +02:00
|
|
|
package driver
|
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"
|
2015-06-07 15:01:14 +02:00
|
|
|
|
2016-04-09 15:56:25 +02:00
|
|
|
"golang.org/x/mobile/exp/audio/al"
|
2015-01-24 14:19:10 +01:00
|
|
|
)
|
|
|
|
|
2016-02-13 13:21:42 +01:00
|
|
|
const (
|
|
|
|
maxBufferNum = 8
|
|
|
|
)
|
2016-02-11 09:07:28 +01:00
|
|
|
|
2016-04-08 17:54:18 +02:00
|
|
|
type Player struct {
|
2016-04-09 15:56:25 +02: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
|
2016-02-13 13:21:42 +01:00
|
|
|
isClosed bool
|
2016-04-09 15:56:25 +02:00
|
|
|
alFormat uint32
|
2016-04-08 18:10:11 +02:00
|
|
|
}
|
|
|
|
|
2016-04-09 15:56:25 +02:00
|
|
|
func alFormat(channelNum, bytesPerSample int) uint32 {
|
2016-04-08 18:10:11 +02:00
|
|
|
switch {
|
|
|
|
case channelNum == 1 && bytesPerSample == 1:
|
2016-04-09 15:56:25 +02:00
|
|
|
return al.FormatMono8
|
2016-04-08 18:10:11 +02:00
|
|
|
case channelNum == 1 && bytesPerSample == 2:
|
2016-04-09 15:56:25 +02:00
|
|
|
return al.FormatMono16
|
2016-04-08 18:10:11 +02:00
|
|
|
case channelNum == 2 && bytesPerSample == 1:
|
2016-04-09 15:56:25 +02:00
|
|
|
return al.FormatStereo8
|
2016-04-08 18:10:11 +02:00
|
|
|
case channelNum == 2 && bytesPerSample == 2:
|
2016-04-09 15:56:25 +02:00
|
|
|
return al.FormatStereo16
|
2016-04-08 18:10:11 +02:00
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("driver: invalid channel num (%d) or bytes per sample (%d)", channelNum, bytesPerSample))
|
2016-02-11 10:43:11 +01:00
|
|
|
}
|
|
|
|
|
2016-04-08 17:54:18 +02:00
|
|
|
func NewPlayer(src io.Reader, sampleRate, channelNum, bytesPerSample int) (*Player, error) {
|
2016-04-09 15:56:25 +02:00
|
|
|
if e := al.OpenDevice(); e != nil {
|
|
|
|
return nil, fmt.Errorf("driver: OpenAL initialization failed: %v", e)
|
2016-03-04 16:57:35 +01:00
|
|
|
}
|
2016-04-09 15:56:25 +02:00
|
|
|
s := al.GenSources(1)
|
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
return nil, fmt.Errorf("driver: al.GenSources error: %d", err)
|
2016-02-11 19:06:04 +01:00
|
|
|
}
|
2016-04-08 17:54:18 +02:00
|
|
|
p := &Player{
|
2016-04-09 15:56:25 +02:00
|
|
|
alSource: s[0],
|
|
|
|
alBuffers: []al.Buffer{},
|
2016-02-13 13:21:42 +01:00
|
|
|
source: src,
|
2016-02-12 13:39:48 +01:00
|
|
|
sampleRate: sampleRate,
|
2016-04-08 18:10:11 +02:00
|
|
|
alFormat: alFormat(channelNum, bytesPerSample),
|
2016-02-12 13:39:48 +01:00
|
|
|
}
|
2016-04-08 17:54:18 +02:00
|
|
|
runtime.SetFinalizer(p, (*Player).Close)
|
2016-04-02 20:15:57 +02:00
|
|
|
|
2016-04-09 15:56:25 +02:00
|
|
|
bs := al.GenBuffers(maxBufferNum)
|
2016-04-05 18:26:21 +02:00
|
|
|
emptyBytes := make([]byte, bufferSize)
|
|
|
|
for _, b := range bs {
|
|
|
|
// Note that the third argument of only the first buffer is used.
|
2016-04-09 15:56:25 +02:00
|
|
|
b.BufferData(p.alFormat, emptyBytes, int32(p.sampleRate))
|
|
|
|
p.alSource.QueueBuffers(b)
|
2016-04-02 20:15:57 +02:00
|
|
|
}
|
2016-04-09 15:56:25 +02:00
|
|
|
al.PlaySources(p.alSource)
|
2016-04-04 16:42:44 +02:00
|
|
|
return p, nil
|
2016-02-12 13:39:48 +01:00
|
|
|
}
|
|
|
|
|
2016-03-13 15:13:50 +01:00
|
|
|
const (
|
|
|
|
bufferSize = 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tmpBuffer = make([]byte, bufferSize)
|
2016-04-09 15:56:25 +02:00
|
|
|
tmpAlBuffers = make([]al.Buffer, maxBufferNum)
|
2016-03-13 15:13:50 +01:00
|
|
|
)
|
2016-02-11 19:06:04 +01:00
|
|
|
|
2016-04-08 17:54:18 +02:00
|
|
|
func (p *Player) Proceed() error {
|
2016-04-09 15:56:25 +02:00
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
return fmt.Errorf("driver: 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 {
|
2016-03-13 15:13:50 +01:00
|
|
|
bufs := tmpAlBuffers[:processedNum]
|
2016-04-09 15:56:25 +02:00
|
|
|
p.alSource.UnqueueBuffers(bufs...)
|
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
return fmt.Errorf("driver: 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...)
|
|
|
|
}
|
2016-02-13 13:21:42 +01:00
|
|
|
|
2016-04-05 20:31:06 +02:00
|
|
|
if 0 < len(p.alBuffers) {
|
2016-03-13 15:13:50 +01:00
|
|
|
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:]
|
2016-04-09 15:56:25 +02:00
|
|
|
buf.BufferData(p.alFormat, tmpBuffer[:n], int32(p.sampleRate))
|
|
|
|
p.alSource.QueueBuffers(buf)
|
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
return fmt.Errorf("driver: 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-02-09 18:40:07 +01:00
|
|
|
}
|
2016-02-13 13:21:42 +01:00
|
|
|
|
2016-04-09 15:56:25 +02:00
|
|
|
if p.alSource.State() == al.Stopped || p.alSource.State() == al.Initial {
|
|
|
|
al.RewindSources(p.alSource)
|
|
|
|
al.PlaySources(p.alSource)
|
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
return fmt.Errorf("driver: 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-04-08 17:54:18 +02:00
|
|
|
func (p *Player) Close() error {
|
2016-04-09 15:56:25 +02:00
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
return fmt.Errorf("driver: error before closing: %d", err)
|
2016-02-13 12:43:27 +01:00
|
|
|
}
|
2016-02-13 13:21:42 +01:00
|
|
|
if p.isClosed {
|
|
|
|
return nil
|
2016-02-11 19:06:04 +01:00
|
|
|
}
|
2016-04-09 15:56:25 +02:00
|
|
|
var bs []al.Buffer
|
|
|
|
al.RewindSources(p.alSource)
|
|
|
|
al.StopSources(p.alSource)
|
2016-04-05 05:17:23 +02:00
|
|
|
if n := p.alSource.BuffersQueued(); 0 < n {
|
2016-04-09 15:56:25 +02:00
|
|
|
bs = make([]al.Buffer, n)
|
|
|
|
p.alSource.UnqueueBuffers(bs...)
|
2016-02-13 13:21:42 +01:00
|
|
|
p.alBuffers = append(p.alBuffers, bs...)
|
2016-02-11 19:06:04 +01:00
|
|
|
}
|
2016-02-13 13:21:42 +01:00
|
|
|
p.isClosed = true
|
2016-04-09 15:56:25 +02:00
|
|
|
if err := al.Error(); err != 0 {
|
|
|
|
return fmt.Errorf("driver: error after closing: %d", err)
|
2016-02-12 22:44:49 +01:00
|
|
|
}
|
2016-02-11 19:06:04 +01:00
|
|
|
runtime.SetFinalizer(p, nil)
|
|
|
|
return nil
|
2016-02-11 11:55:59 +01:00
|
|
|
}
|