mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 19:58:54 +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,11 +65,10 @@ 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 {
|
if !audioEnabled {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(l) != len(r) {
|
if len(l) != len(r) {
|
||||||
@ -84,19 +76,20 @@ func Play(channel int, l []int16, r []int16) bool {
|
|||||||
}
|
}
|
||||||
ch := channelAt(channel)
|
ch := channelAt(channel)
|
||||||
if ch == nil {
|
if ch == nil {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
ch.l = append(ch.l, make([]int16, ch.nextInsertionPosition-len(ch.l))...)
|
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.r = append(ch.r, make([]int16, ch.nextInsertionPosition-len(ch.r))...)
|
||||||
ch.l = append(ch.l, l...)
|
ch.l = append(ch.l, l...)
|
||||||
ch.r = append(ch.r, r...)
|
ch.r = append(ch.r, r...)
|
||||||
return true
|
result = true
|
||||||
|
return
|
||||||
|
})
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
if !audioEnabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -107,6 +100,7 @@ func Queue(channel int, l []int16, r []int16) {
|
|||||||
ch := channels[channel]
|
ch := channels[channel]
|
||||||
ch.l = append(ch.l, l...)
|
ch.l = append(ch.l, l...)
|
||||||
ch.r = append(ch.r, r...)
|
ch.r = append(ch.r, r...)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Tick() {
|
func Tick() {
|
||||||
@ -123,27 +117,28 @@ 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 {
|
||||||
return true
|
result = true
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ch := range channels {
|
for _, ch := range channels {
|
||||||
if 0 < len(ch.l) {
|
if 0 < len(ch.l) {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
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 {
|
if !audioEnabled {
|
||||||
return nil, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ch := channels[channel]
|
ch := channels[channel]
|
||||||
@ -155,16 +150,18 @@ func loadChannelBuffer(channel int, bufferSize int) (l, r []int16) {
|
|||||||
ch.l = ch.l[length:]
|
ch.l = ch.l[length:]
|
||||||
ch.r = ch.r[length:]
|
ch.r = ch.r[length:]
|
||||||
ch.nextInsertionPosition -= min(bufferSize, ch.nextInsertionPosition)
|
ch.nextInsertionPosition -= min(bufferSize, ch.nextInsertionPosition)
|
||||||
return inputL, inputR
|
l, r = inputL, inputR
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsPlaying(channel int) bool {
|
func IsPlaying(channel int) bool {
|
||||||
channelsLock.Lock()
|
result := false
|
||||||
defer channelsLock.Unlock()
|
withChannels(func() {
|
||||||
|
|
||||||
if !audioEnabled {
|
if !audioEnabled {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
|
result = isPlaying(channel)
|
||||||
return isPlaying(channel)
|
})
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
@ -20,15 +20,22 @@ 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
|
||||||
}()
|
}()
|
||||||
@ -38,7 +45,7 @@ func audioProcess(channel int) func(e js.Object) {
|
|||||||
r := b.Call("getChannelData", 1)
|
r := b.Call("getChannelData", 1)
|
||||||
inputL, inputR := loadChannelBuffer(channel, bufferSize)
|
inputL, inputR := loadChannelBuffer(channel, bufferSize)
|
||||||
const max = 1 << 15
|
const max = 1 << 15
|
||||||
for i := 0; i < len(inputL); i++ {
|
for i := 0; i < bufferSize; i++ {
|
||||||
// TODO: Use copyToChannel?
|
// TODO: Use copyToChannel?
|
||||||
if len(inputL) <= i {
|
if len(inputL) <= i {
|
||||||
l.SetIndex(i, 0)
|
l.SetIndex(i, 0)
|
||||||
@ -48,7 +55,6 @@ func audioProcess(channel int) func(e js.Object) {
|
|||||||
l.SetIndex(i, float64(inputL[i])/max)
|
l.SetIndex(i, float64(inputL[i])/max)
|
||||||
r.SetIndex(i, float64(inputR[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