mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
#128 Bug fix: Delay when playing audio
This commit is contained in:
parent
47f31f75a0
commit
23cef1b715
@ -48,41 +48,47 @@ func isPlaying(channel int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func channelAt(i int) *channel {
|
func channelAt(i int) *channel {
|
||||||
if i == -1 {
|
var ch *channel
|
||||||
for i, _ := range channels {
|
withChannels(func() {
|
||||||
if !isPlaying(i) {
|
if i == -1 {
|
||||||
return channels[i]
|
for i, _ := range channels {
|
||||||
|
if !isPlaying(i) {
|
||||||
|
ch = channels[i]
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return nil
|
if !isPlaying(i) {
|
||||||
}
|
ch = channels[i]
|
||||||
if !isPlaying(i) {
|
return
|
||||||
return channels[i]
|
}
|
||||||
}
|
return
|
||||||
return nil
|
})
|
||||||
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func Play(channel int, l []int16, r []int16) bool {
|
func Play(channel int, l []int16, r []int16) bool {
|
||||||
result := false
|
ch := channelAt(channel)
|
||||||
|
if ch == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
withChannels(func() {
|
withChannels(func() {
|
||||||
if !audioEnabled {
|
if !audioEnabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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)")
|
||||||
}
|
}
|
||||||
ch := channelAt(channel)
|
d := ch.nextInsertionPosition - len(l)
|
||||||
if ch == nil {
|
if 0 < d {
|
||||||
return
|
ch.l = append(ch.l, make([]int16, d)...)
|
||||||
|
ch.r = append(ch.r, make([]int16, d)...)
|
||||||
}
|
}
|
||||||
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.l = append(ch.l, l...)
|
||||||
ch.r = append(ch.r, r...)
|
ch.r = append(ch.r, r...)
|
||||||
result = true
|
|
||||||
})
|
})
|
||||||
return result
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func Queue(channel int, l []int16, r []int16) {
|
func Queue(channel int, l []int16, r []int16) {
|
||||||
@ -90,7 +96,6 @@ func Queue(channel int, l []int16, r []int16) {
|
|||||||
if !audioEnabled {
|
if !audioEnabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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)")
|
||||||
}
|
}
|
||||||
@ -102,7 +107,11 @@ func Queue(channel int, l []int16, r []int16) {
|
|||||||
|
|
||||||
func Tick() {
|
func Tick() {
|
||||||
for _, ch := range channels {
|
for _, ch := range channels {
|
||||||
ch.nextInsertionPosition += SampleRate / 60
|
if 0 < len(ch.l) {
|
||||||
|
ch.nextInsertionPosition += SampleRate / 60 // FPS
|
||||||
|
} else {
|
||||||
|
ch.nextInsertionPosition = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,9 +151,12 @@ func loadChannelBuffer(channel int, bufferSize int) (l, r []int16) {
|
|||||||
length := min(len(ch.l), bufferSize)
|
length := min(len(ch.l), bufferSize)
|
||||||
inputL := ch.l[:length]
|
inputL := ch.l[:length]
|
||||||
inputR := ch.r[:length]
|
inputR := ch.r[:length]
|
||||||
|
ch.nextInsertionPosition -= length
|
||||||
|
if ch.nextInsertionPosition < 0 {
|
||||||
|
ch.nextInsertionPosition = 0
|
||||||
|
}
|
||||||
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)
|
|
||||||
l, r = inputL, inputR
|
l, r = inputL, inputR
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user