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-02-07 08:03:33 +01:00
|
|
|
package audio
|
2015-01-24 06:50:41 +01:00
|
|
|
|
2015-01-24 14:19:10 +01:00
|
|
|
import (
|
2015-01-25 06:28:24 +01:00
|
|
|
"log"
|
2015-01-24 14:19:10 +01:00
|
|
|
"runtime"
|
2015-01-24 17:51:51 +01:00
|
|
|
"time"
|
2015-06-07 15:01:14 +02:00
|
|
|
|
2016-02-07 15:37:16 +01:00
|
|
|
"golang.org/x/mobile/exp/audio/al"
|
2015-01-24 14:19:10 +01:00
|
|
|
)
|
|
|
|
|
2016-02-09 14:35:55 +01:00
|
|
|
func isPlaying(channel int) bool {
|
|
|
|
ch := channels[channel]
|
|
|
|
return 0 < len(ch.buffer)
|
|
|
|
}
|
|
|
|
|
2015-01-24 06:50:41 +01:00
|
|
|
func initialize() {
|
2015-01-27 14:02:23 +01:00
|
|
|
// Creating OpenAL device must be done after initializing UI. I'm not sure the reason.
|
2015-01-24 14:19:10 +01:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
2016-02-07 15:37:16 +01:00
|
|
|
if err := al.OpenDevice(); err != nil {
|
2016-01-04 22:08:21 +01:00
|
|
|
log.Printf("OpenAL initialize error: %v", err)
|
2015-01-25 06:28:24 +01:00
|
|
|
close(ch)
|
2015-01-25 17:39:43 +01:00
|
|
|
// Graceful ending: Audio is not available on Travis CI.
|
2015-01-25 06:28:24 +01:00
|
|
|
return
|
2015-01-24 17:51:51 +01:00
|
|
|
}
|
2015-01-24 14:19:10 +01:00
|
|
|
|
2015-01-25 06:28:24 +01:00
|
|
|
audioEnabled = true
|
2016-02-07 15:37:16 +01:00
|
|
|
sources := al.GenSources(MaxChannel)
|
2015-01-24 14:19:10 +01:00
|
|
|
close(ch)
|
2015-01-24 17:51:51 +01:00
|
|
|
|
2015-06-13 19:37:20 +02:00
|
|
|
const bufferSize = 2048
|
|
|
|
emptyBytes := make([]byte, bufferSize)
|
2015-01-24 17:51:51 +01:00
|
|
|
|
2015-01-25 17:20:56 +01:00
|
|
|
for _, source := range sources {
|
2015-01-25 19:53:40 +01:00
|
|
|
// 3 is the least number?
|
|
|
|
// http://stackoverflow.com/questions/14932004/play-sound-with-openalstream
|
2015-01-26 02:14:03 +01:00
|
|
|
const bufferNum = 4
|
2016-02-07 15:37:16 +01:00
|
|
|
buffers := al.GenBuffers(bufferNum)
|
2015-01-25 17:20:56 +01:00
|
|
|
for _, buffer := range buffers {
|
2016-02-07 15:37:16 +01:00
|
|
|
buffer.BufferData(al.FormatStereo16, emptyBytes, SampleRate)
|
|
|
|
source.QueueBuffers(buffer)
|
2015-01-25 17:20:56 +01:00
|
|
|
}
|
2016-02-07 15:37:16 +01:00
|
|
|
al.PlaySources(source)
|
2015-01-24 17:51:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2015-01-25 17:20:56 +01:00
|
|
|
oneProcessed := false
|
2015-06-07 15:01:14 +02:00
|
|
|
for ch, source := range sources {
|
2015-01-25 17:20:56 +01:00
|
|
|
processed := source.BuffersProcessed()
|
|
|
|
if processed == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2015-01-25 17:58:21 +01:00
|
|
|
|
2015-01-25 17:20:56 +01:00
|
|
|
oneProcessed = true
|
2016-02-07 15:37:16 +01:00
|
|
|
buffers := make([]al.Buffer, processed)
|
|
|
|
source.UnqueueBuffers(buffers...)
|
2015-01-25 17:20:56 +01:00
|
|
|
for _, buffer := range buffers {
|
2015-06-13 19:37:20 +02:00
|
|
|
b := make([]byte, bufferSize)
|
|
|
|
copy(b, loadChannelBuffer(ch, bufferSize))
|
2016-02-07 15:37:16 +01:00
|
|
|
buffer.BufferData(al.FormatStereo16, b, SampleRate)
|
|
|
|
source.QueueBuffers(buffer)
|
2015-01-25 17:20:56 +01:00
|
|
|
}
|
2016-02-07 15:37:16 +01:00
|
|
|
if source.State() == al.Stopped {
|
|
|
|
al.RewindSources(source)
|
|
|
|
al.PlaySources(source)
|
2015-01-25 17:58:21 +01:00
|
|
|
}
|
2015-01-24 17:51:51 +01:00
|
|
|
}
|
2015-01-25 17:20:56 +01:00
|
|
|
if !oneProcessed {
|
2015-06-07 15:01:14 +02:00
|
|
|
time.Sleep(1 * time.Millisecond)
|
2015-01-24 17:51:51 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-24 14:19:10 +01:00
|
|
|
}()
|
|
|
|
<-ch
|
2015-01-24 06:50:41 +01:00
|
|
|
}
|