audio: Grace returning when audio is not available

This commit is contained in:
Hajime Hoshi 2015-01-25 14:28:24 +09:00
parent 1ff738e730
commit c2313c10c1
4 changed files with 30 additions and 4 deletions

View File

@ -9,7 +9,7 @@ before_install:
- sudo add-apt-repository 'deb http://us.archive.ubuntu.com/ubuntu/ utopic main restricted universe multiverse'
- sudo add-apt-repository 'deb http://us.archive.ubuntu.com/ubuntu/ utopic-updates main restricted universe multiverse'
- sudo apt-get update -qq
- sudo apt-get install -qq libglew-dev libglfw3-dev libopenal1 libopenal-dev
- sudo apt-get install -qq libglew-dev libglfw3-dev libopenal-dev libalut-dev
- export NODE_PATH=$(npm config get prefix)/lib/node_modules
- npm install --global gl

View File

@ -18,6 +18,8 @@ import (
"sync"
)
var audioEnabled = false
const bufferSize = 1024
const SampleRate = 44100
@ -75,6 +77,10 @@ func Play(channel int, l []float32, r []float32) bool {
channelsLock.Lock()
defer channelsLock.Unlock()
if !audioEnabled {
return false
}
if len(l) != len(r) {
panic("len(l) must equal to len(r)")
}
@ -93,6 +99,10 @@ func Queue(channel int, l []float32, r []float32) {
channelsLock.Lock()
defer channelsLock.Unlock()
if !audioEnabled {
return
}
if len(l) != len(r) {
panic("len(l) must equal to len(r)")
}
@ -116,6 +126,10 @@ func isChannelsEmpty() bool {
channelsLock.Lock()
defer channelsLock.Unlock()
if !audioEnabled {
return true
}
for _, ch := range channels {
if 0 < len(ch.l) {
return false
@ -128,6 +142,10 @@ func loadChannelBuffers() (l, r []float32) {
channelsLock.Lock()
defer channelsLock.Unlock()
if !audioEnabled {
return nil, nil
}
inputL := make([]float32, bufferSize)
inputR := make([]float32, bufferSize)
for _, ch := range channels {
@ -150,5 +168,9 @@ func IsPlaying(channel int) bool {
channelsLock.Lock()
defer channelsLock.Unlock()
if !audioEnabled {
return false
}
return isPlaying(channel)
}

View File

@ -49,6 +49,7 @@ func initialize() {
r.SetIndex(i, inputR[i])
}
})
audioEnabled = true
}
func start() {

View File

@ -21,6 +21,7 @@ import (
"encoding/binary"
"fmt"
"github.com/timshannon/go-openal/openal"
"log"
"math"
"runtime"
"time"
@ -55,12 +56,14 @@ func initialize() {
context := device.CreateContext()
context.Activate()
source := openal.NewSource()
if alErr := openal.GetError(); alErr != 0 {
panic(fmt.Sprintf("OpenAL initialize error: %d", alErr))
log.Printf("OpenAL initialize error: %d", alErr)
close(ch)
return
}
audioEnabled = true
source := openal.NewSource()
close(ch)
emptyBytes := make([]byte, 4*bufferSize)