mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
audio: Use channel num and bytes per sample on OpenAL
This commit is contained in:
parent
bbdb522b15
commit
2ba5105ba9
@ -34,21 +34,37 @@ type Player struct {
|
|||||||
source io.Reader
|
source io.Reader
|
||||||
sampleRate int
|
sampleRate int
|
||||||
isClosed bool
|
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) {
|
func NewPlayer(src io.Reader, sampleRate, channelNum, bytesPerSample int) (*Player, error) {
|
||||||
if e := al.OpenDevice(); e != nil {
|
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)
|
s := al.GenSources(1)
|
||||||
if err := al.Error(); err != 0 {
|
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{
|
p := &Player{
|
||||||
alSource: s[0],
|
alSource: s[0],
|
||||||
alBuffers: []al.Buffer{},
|
alBuffers: []al.Buffer{},
|
||||||
source: src,
|
source: src,
|
||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
|
alFormat: alFormat(channelNum, bytesPerSample),
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(p, (*Player).Close)
|
runtime.SetFinalizer(p, (*Player).Close)
|
||||||
|
|
||||||
@ -56,7 +72,7 @@ func NewPlayer(src io.Reader, sampleRate, channelNum, bytesPerSample int) (*Play
|
|||||||
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(al.FormatStereo16, emptyBytes, int32(p.sampleRate))
|
b.BufferData(p.alFormat, emptyBytes, int32(p.sampleRate))
|
||||||
p.alSource.QueueBuffers(b)
|
p.alSource.QueueBuffers(b)
|
||||||
}
|
}
|
||||||
al.PlaySources(p.alSource)
|
al.PlaySources(p.alSource)
|
||||||
@ -74,14 +90,14 @@ var (
|
|||||||
|
|
||||||
func (p *Player) Proceed() error {
|
func (p *Player) Proceed() error {
|
||||||
if err := al.Error(); err != 0 {
|
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()
|
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 := 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...)
|
p.alBuffers = append(p.alBuffers, bufs...)
|
||||||
}
|
}
|
||||||
@ -91,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.BufferData(al.FormatStereo16, tmpBuffer[:n], int32(p.sampleRate))
|
buf.BufferData(p.alFormat, tmpBuffer[:n], int32(p.sampleRate))
|
||||||
p.alSource.QueueBuffers(buf)
|
p.alSource.QueueBuffers(buf)
|
||||||
if err := al.Error(); err != 0 {
|
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 {
|
if err != nil {
|
||||||
@ -106,7 +122,7 @@ func (p *Player) Proceed() error {
|
|||||||
al.RewindSources(p.alSource)
|
al.RewindSources(p.alSource)
|
||||||
al.PlaySources(p.alSource)
|
al.PlaySources(p.alSource)
|
||||||
if err := al.Error(); err != 0 {
|
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 {
|
func (p *Player) Close() error {
|
||||||
if err := al.Error(); err != 0 {
|
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 {
|
if p.isClosed {
|
||||||
return nil
|
return nil
|
||||||
@ -130,7 +146,7 @@ func (p *Player) Close() error {
|
|||||||
}
|
}
|
||||||
p.isClosed = true
|
p.isClosed = true
|
||||||
if err := al.Error(); err != 0 {
|
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)
|
runtime.SetFinalizer(p, nil)
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user