mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-27 04:08:53 +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
|
package internal
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
var audioEnabled = false
|
var audioEnabled = false
|
||||||
|
|
||||||
const SampleRate = 44100
|
const SampleRate = 44100
|
||||||
@ -34,9 +30,6 @@ var MaxChannel = 32
|
|||||||
|
|
||||||
var channels = make([]*channel, MaxChannel)
|
var channels = make([]*channel, MaxChannel)
|
||||||
|
|
||||||
// NOTE: In GopherJS, sync.Mutex blocks a function and requires gopherjs:blocking comments.
|
|
||||||
var channelsLock sync.Mutex
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
for i, _ := range channels {
|
for i, _ := range channels {
|
||||||
channels[i] = &channel{
|
channels[i] = &channel{
|
||||||
@ -72,41 +65,42 @@ func channelAt(i int) *channel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Play(channel int, l []int16, r []int16) bool {
|
func Play(channel int, l []int16, r []int16) bool {
|
||||||
channelsLock.Lock()
|
result := false
|
||||||
defer channelsLock.Unlock()
|
withChannels(func() {
|
||||||
|
if !audioEnabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !audioEnabled {
|
if len(l) != len(r) {
|
||||||
return false
|
panic("len(l) must equal to len(r)")
|
||||||
}
|
}
|
||||||
|
ch := channelAt(channel)
|
||||||
if len(l) != len(r) {
|
if ch == nil {
|
||||||
panic("len(l) must equal to len(r)")
|
return
|
||||||
}
|
}
|
||||||
ch := channelAt(channel)
|
ch.l = append(ch.l, make([]int16, ch.nextInsertionPosition-len(ch.l))...)
|
||||||
if ch == nil {
|
ch.r = append(ch.r, make([]int16, ch.nextInsertionPosition-len(ch.r))...)
|
||||||
return false
|
ch.l = append(ch.l, l...)
|
||||||
}
|
ch.r = append(ch.r, r...)
|
||||||
ch.l = append(ch.l, make([]int16, ch.nextInsertionPosition-len(ch.l))...)
|
result = true
|
||||||
ch.r = append(ch.r, make([]int16, ch.nextInsertionPosition-len(ch.r))...)
|
return
|
||||||
ch.l = append(ch.l, l...)
|
})
|
||||||
ch.r = append(ch.r, r...)
|
return result
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Queue(channel int, l []int16, r []int16) {
|
func Queue(channel int, l []int16, r []int16) {
|
||||||
channelsLock.Lock()
|
withChannels(func() {
|
||||||
defer channelsLock.Unlock()
|
if !audioEnabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !audioEnabled {
|
if len(l) != len(r) {
|
||||||
return
|
panic("len(l) must equal to len(r)")
|
||||||
}
|
}
|
||||||
|
ch := channels[channel]
|
||||||
if len(l) != len(r) {
|
ch.l = append(ch.l, l...)
|
||||||
panic("len(l) must equal to len(r)")
|
ch.r = append(ch.r, r...)
|
||||||
}
|
})
|
||||||
ch := channels[channel]
|
|
||||||
ch.l = append(ch.l, l...)
|
|
||||||
ch.r = append(ch.r, r...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Tick() {
|
func Tick() {
|
||||||
@ -123,48 +117,51 @@ func min(a, b int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isChannelsEmpty() bool {
|
func isChannelsEmpty() bool {
|
||||||
channelsLock.Lock()
|
result := false
|
||||||
defer channelsLock.Unlock()
|
withChannels(func() {
|
||||||
|
if !audioEnabled {
|
||||||
if !audioEnabled {
|
result = true
|
||||||
return true
|
return
|
||||||
}
|
|
||||||
|
|
||||||
for _, ch := range channels {
|
|
||||||
if 0 < len(ch.l) {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true
|
for _, ch := range channels {
|
||||||
|
if 0 < len(ch.l) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result = true
|
||||||
|
return
|
||||||
|
})
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadChannelBuffer(channel int, bufferSize int) (l, r []int16) {
|
func loadChannelBuffer(channel int, bufferSize int) (l, r []int16) {
|
||||||
channelsLock.Lock()
|
withChannels(func() {
|
||||||
defer channelsLock.Unlock()
|
if !audioEnabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if !audioEnabled {
|
ch := channels[channel]
|
||||||
return nil, nil
|
length := min(len(ch.l), bufferSize)
|
||||||
}
|
inputL := make([]int16, length)
|
||||||
|
inputR := make([]int16, length)
|
||||||
ch := channels[channel]
|
copy(inputL, ch.l[:length])
|
||||||
length := min(len(ch.l), bufferSize)
|
copy(inputR, ch.r[:length])
|
||||||
inputL := make([]int16, length)
|
ch.l = ch.l[length:]
|
||||||
inputR := make([]int16, length)
|
ch.r = ch.r[length:]
|
||||||
copy(inputL, ch.l[:length])
|
ch.nextInsertionPosition -= min(bufferSize, ch.nextInsertionPosition)
|
||||||
copy(inputR, ch.r[:length])
|
l, r = inputL, inputR
|
||||||
ch.l = ch.l[length:]
|
})
|
||||||
ch.r = ch.r[length:]
|
return
|
||||||
ch.nextInsertionPosition -= min(bufferSize, ch.nextInsertionPosition)
|
|
||||||
return inputL, inputR
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsPlaying(channel int) bool {
|
func IsPlaying(channel int) bool {
|
||||||
channelsLock.Lock()
|
result := false
|
||||||
defer channelsLock.Unlock()
|
withChannels(func() {
|
||||||
|
if !audioEnabled {
|
||||||
if !audioEnabled {
|
return
|
||||||
return false
|
}
|
||||||
}
|
result = isPlaying(channel)
|
||||||
|
})
|
||||||
return isPlaying(channel)
|
return result
|
||||||
}
|
}
|
||||||
|
@ -20,35 +20,41 @@ import (
|
|||||||
"github.com/gopherjs/gopherjs/js"
|
"github.com/gopherjs/gopherjs/js"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func withChannels(f func()) {
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
|
||||||
// Keep this so as not to be destroyed by GC.
|
// Keep this so as not to be destroyed by GC.
|
||||||
var nodes = []js.Object{}
|
var (
|
||||||
var context js.Object
|
nodes = []js.Object{}
|
||||||
|
dummies = []js.Object{} // Dummy source nodes for iOS.
|
||||||
|
context js.Object
|
||||||
|
)
|
||||||
|
|
||||||
const bufferSize = 1024
|
const bufferSize = 1024
|
||||||
|
|
||||||
func audioProcess(channel int) func(e js.Object) {
|
func audioProcess(channel int) func(e js.Object) {
|
||||||
return func(e js.Object) {
|
return func(e js.Object) {
|
||||||
go func() {
|
// Can't use 'go' here. Probably it may cause race conditions.
|
||||||
defer func() {
|
defer func() {
|
||||||
currentPosition += bufferSize
|
currentPosition += bufferSize
|
||||||
}()
|
|
||||||
|
|
||||||
b := e.Get("outputBuffer")
|
|
||||||
l := b.Call("getChannelData", 0)
|
|
||||||
r := b.Call("getChannelData", 1)
|
|
||||||
inputL, inputR := loadChannelBuffer(channel, bufferSize)
|
|
||||||
const max = 1 << 15
|
|
||||||
for i := 0; i < len(inputL); i++ {
|
|
||||||
// TODO: Use copyToChannel?
|
|
||||||
if len(inputL) <= i {
|
|
||||||
l.SetIndex(i, 0)
|
|
||||||
r.SetIndex(i, 0)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
l.SetIndex(i, float64(inputL[i])/max)
|
|
||||||
r.SetIndex(i, float64(inputR[i])/max)
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
b := e.Get("outputBuffer")
|
||||||
|
l := b.Call("getChannelData", 0)
|
||||||
|
r := b.Call("getChannelData", 1)
|
||||||
|
inputL, inputR := loadChannelBuffer(channel, bufferSize)
|
||||||
|
const max = 1 << 15
|
||||||
|
for i := 0; i < bufferSize; i++ {
|
||||||
|
// TODO: Use copyToChannel?
|
||||||
|
if len(inputL) <= i {
|
||||||
|
l.SetIndex(i, 0)
|
||||||
|
r.SetIndex(i, 0)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
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 := context.Call("createScriptProcessor", bufferSize, 0, 2)
|
||||||
node.Call("addEventListener", "audioprocess", audioProcess(i))
|
node.Call("addEventListener", "audioprocess", audioProcess(i))
|
||||||
nodes = append(nodes, node)
|
nodes = append(nodes, node)
|
||||||
|
|
||||||
|
dummy := context.Call("createBufferSource")
|
||||||
|
dummies = append(dummies, dummy)
|
||||||
}
|
}
|
||||||
audioEnabled = true
|
audioEnabled = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func start() {
|
func start() {
|
||||||
// TODO: For iOS, node should be connected with a buffer node.
|
destination := context.Get("destination")
|
||||||
for _, node := range nodes {
|
for i, node := range nodes {
|
||||||
node.Call("connect", context.Get("destination"))
|
dummy := dummies[i]
|
||||||
|
dummy.Call("connect", node)
|
||||||
|
node.Call("connect", destination)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,9 +23,18 @@ import (
|
|||||||
"github.com/timshannon/go-openal/openal"
|
"github.com/timshannon/go-openal/openal"
|
||||||
"log"
|
"log"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var channelsMutex = sync.Mutex{}
|
||||||
|
|
||||||
|
func withChannels(f func()) {
|
||||||
|
channelsMutex.Lock()
|
||||||
|
defer channelsMutex.Unlock()
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
|
||||||
func toBytesWithPadding(l, r []int16, size int) []byte {
|
func toBytesWithPadding(l, r []int16, size int) []byte {
|
||||||
if len(l) != len(r) {
|
if len(l) != len(r) {
|
||||||
panic("len(l) must equal to len(r)")
|
panic("len(l) must equal to len(r)")
|
||||||
|
Loading…
Reference in New Issue
Block a user