mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
audio: Bug fix: Callback (audioprocess) can't treat goroutines (#119)
This commit is contained in:
parent
ba3feaf52f
commit
15739415c7
@ -14,10 +14,6 @@
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
var audioEnabled = false
|
||||
|
||||
const SampleRate = 44100
|
||||
@ -34,9 +30,6 @@ var MaxChannel = 32
|
||||
|
||||
var channels = make([]*channel, MaxChannel)
|
||||
|
||||
// NOTE: In GopherJS, sync.Mutex blocks a function and requires gopherjs:blocking comments.
|
||||
var channelsLock sync.Mutex
|
||||
|
||||
func init() {
|
||||
for i, _ := range channels {
|
||||
channels[i] = &channel{
|
||||
@ -72,11 +65,10 @@ func channelAt(i int) *channel {
|
||||
}
|
||||
|
||||
func Play(channel int, l []int16, r []int16) bool {
|
||||
channelsLock.Lock()
|
||||
defer channelsLock.Unlock()
|
||||
|
||||
result := false
|
||||
withChannels(func() {
|
||||
if !audioEnabled {
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
if len(l) != len(r) {
|
||||
@ -84,19 +76,20 @@ func Play(channel int, l []int16, r []int16) bool {
|
||||
}
|
||||
ch := channelAt(channel)
|
||||
if ch == nil {
|
||||
return false
|
||||
return
|
||||
}
|
||||
ch.l = append(ch.l, make([]int16, ch.nextInsertionPosition-len(ch.l))...)
|
||||
ch.r = append(ch.r, make([]int16, ch.nextInsertionPosition-len(ch.r))...)
|
||||
ch.l = append(ch.l, l...)
|
||||
ch.r = append(ch.r, r...)
|
||||
return true
|
||||
result = true
|
||||
return
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
func Queue(channel int, l []int16, r []int16) {
|
||||
channelsLock.Lock()
|
||||
defer channelsLock.Unlock()
|
||||
|
||||
withChannels(func() {
|
||||
if !audioEnabled {
|
||||
return
|
||||
}
|
||||
@ -107,6 +100,7 @@ func Queue(channel int, l []int16, r []int16) {
|
||||
ch := channels[channel]
|
||||
ch.l = append(ch.l, l...)
|
||||
ch.r = append(ch.r, r...)
|
||||
})
|
||||
}
|
||||
|
||||
func Tick() {
|
||||
@ -123,27 +117,28 @@ func min(a, b int) int {
|
||||
}
|
||||
|
||||
func isChannelsEmpty() bool {
|
||||
channelsLock.Lock()
|
||||
defer channelsLock.Unlock()
|
||||
|
||||
result := false
|
||||
withChannels(func() {
|
||||
if !audioEnabled {
|
||||
return true
|
||||
result = true
|
||||
return
|
||||
}
|
||||
|
||||
for _, ch := range channels {
|
||||
if 0 < len(ch.l) {
|
||||
return false
|
||||
return
|
||||
}
|
||||
}
|
||||
return true
|
||||
result = true
|
||||
return
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
func loadChannelBuffer(channel int, bufferSize int) (l, r []int16) {
|
||||
channelsLock.Lock()
|
||||
defer channelsLock.Unlock()
|
||||
|
||||
withChannels(func() {
|
||||
if !audioEnabled {
|
||||
return nil, nil
|
||||
return
|
||||
}
|
||||
|
||||
ch := channels[channel]
|
||||
@ -155,16 +150,18 @@ func loadChannelBuffer(channel int, bufferSize int) (l, r []int16) {
|
||||
ch.l = ch.l[length:]
|
||||
ch.r = ch.r[length:]
|
||||
ch.nextInsertionPosition -= min(bufferSize, ch.nextInsertionPosition)
|
||||
return inputL, inputR
|
||||
l, r = inputL, inputR
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func IsPlaying(channel int) bool {
|
||||
channelsLock.Lock()
|
||||
defer channelsLock.Unlock()
|
||||
|
||||
result := false
|
||||
withChannels(func() {
|
||||
if !audioEnabled {
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
return isPlaying(channel)
|
||||
result = isPlaying(channel)
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
@ -20,15 +20,22 @@ import (
|
||||
"github.com/gopherjs/gopherjs/js"
|
||||
)
|
||||
|
||||
func withChannels(f func()) {
|
||||
f()
|
||||
}
|
||||
|
||||
// Keep this so as not to be destroyed by GC.
|
||||
var nodes = []js.Object{}
|
||||
var context js.Object
|
||||
var (
|
||||
nodes = []js.Object{}
|
||||
dummies = []js.Object{} // Dummy source nodes for iOS.
|
||||
context js.Object
|
||||
)
|
||||
|
||||
const bufferSize = 1024
|
||||
|
||||
func audioProcess(channel int) func(e js.Object) {
|
||||
return func(e js.Object) {
|
||||
go func() {
|
||||
// Can't use 'go' here. Probably it may cause race conditions.
|
||||
defer func() {
|
||||
currentPosition += bufferSize
|
||||
}()
|
||||
@ -38,7 +45,7 @@ func audioProcess(channel int) func(e js.Object) {
|
||||
r := b.Call("getChannelData", 1)
|
||||
inputL, inputR := loadChannelBuffer(channel, bufferSize)
|
||||
const max = 1 << 15
|
||||
for i := 0; i < len(inputL); i++ {
|
||||
for i := 0; i < bufferSize; i++ {
|
||||
// TODO: Use copyToChannel?
|
||||
if len(inputL) <= i {
|
||||
l.SetIndex(i, 0)
|
||||
@ -48,7 +55,6 @@ func audioProcess(channel int) func(e js.Object) {
|
||||
l.SetIndex(i, float64(inputL[i])/max)
|
||||
r.SetIndex(i, float64(inputR[i])/max)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,13 +71,18 @@ func initialize() {
|
||||
node := context.Call("createScriptProcessor", bufferSize, 0, 2)
|
||||
node.Call("addEventListener", "audioprocess", audioProcess(i))
|
||||
nodes = append(nodes, node)
|
||||
|
||||
dummy := context.Call("createBufferSource")
|
||||
dummies = append(dummies, dummy)
|
||||
}
|
||||
audioEnabled = true
|
||||
}
|
||||
|
||||
func start() {
|
||||
// TODO: For iOS, node should be connected with a buffer node.
|
||||
for _, node := range nodes {
|
||||
node.Call("connect", context.Get("destination"))
|
||||
destination := context.Get("destination")
|
||||
for i, node := range nodes {
|
||||
dummy := dummies[i]
|
||||
dummy.Call("connect", node)
|
||||
node.Call("connect", destination)
|
||||
}
|
||||
}
|
||||
|
@ -23,9 +23,18 @@ import (
|
||||
"github.com/timshannon/go-openal/openal"
|
||||
"log"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var channelsMutex = sync.Mutex{}
|
||||
|
||||
func withChannels(f func()) {
|
||||
channelsMutex.Lock()
|
||||
defer channelsMutex.Unlock()
|
||||
f()
|
||||
}
|
||||
|
||||
func toBytesWithPadding(l, r []int16, size int) []byte {
|
||||
if len(l) != len(r) {
|
||||
panic("len(l) must equal to len(r)")
|
||||
|
Loading…
Reference in New Issue
Block a user