diff --git a/example/audio/main.go b/example/audio/main.go index 971d03b14..679f858f6 100644 --- a/example/audio/main.go +++ b/example/audio/main.go @@ -112,7 +112,7 @@ func addNote() { vol := 1.0 / 16.0 square(l, vol, freq, 0.25) square(r, vol, freq, 0.25) - audio.Play(0, toBytes(l, r)) + audio.Queue(0, toBytes(l, r)) } func update(screen *ebiten.Image) error { diff --git a/example/piano/main.go b/example/piano/main.go index 0c39dfb06..d62ca4c38 100644 --- a/example/piano/main.go +++ b/example/piano/main.go @@ -71,7 +71,7 @@ func toBytes(l, r []int16) []byte { func addNote(freq float64, vol float64) { f := int(freq) if n, ok := noteCache[f]; ok { - audio.Play(-1, n) + audio.Queue(-1, n) return } length := len(pcm) * baseFreq / f @@ -88,7 +88,7 @@ func addNote(freq float64, vol float64) { } n := toBytes(l, r) noteCache[f] = n - audio.Play(-1, n) + audio.Queue(-1, n) } var keys = []ebiten.Key{ diff --git a/exp/audio/audio.go b/exp/audio/audio.go index cd74a91d2..797b8715d 100644 --- a/exp/audio/audio.go +++ b/exp/audio/audio.go @@ -26,28 +26,15 @@ func SampleRate() int { // MaxChannel is a max number of channels. var MaxChannel = audio.MaxChannel -// Play appends the given data to the given channel. +// Queue queues the given data to the given channel. +// The given data is queued to the end of the buffer and not played immediately. // // channel must be -1 or a channel index. If channel is -1, an empty channel is automatically selected. // If the channel is not empty, this function does nothing and returns false. This returns true otherwise. // // data's format must be linear PCM (44100Hz, 16bits, 2 channel stereo, little endian). -// -// This function is useful to play SE or a note of PCM synthesis immediately. -func Play(channel int, data []byte) bool { - return audio.Play(channel, data) -} - -// Queue queues the given data to the given channel. -// The given data is queued to the end of the buffer and not played immediately. -// -// channel must be a channel index. You can't give -1 to channel. -// -// data's format must be linear PCM (44100Hz, 16bits, 2 channel stereo, little endian). -// -// This function is useful to play streaming data. -func Queue(channel int, data []byte) { - audio.Queue(channel, data) +func Queue(channel int, data []byte) bool { + return audio.Queue(channel, data) } // IsPlaying returns a boolean value which indicates if the channel buffer has data to play. diff --git a/internal/audio/audio.go b/internal/audio/audio.go index 585b972b8..d8d95d7ea 100644 --- a/internal/audio/audio.go +++ b/internal/audio/audio.go @@ -46,52 +46,35 @@ func isPlaying(channel int) bool { } func channelAt(i int) *channel { - var ch *channel - withChannels(func() { - if i == -1 { - for i, _ := range channels { - if !isPlaying(i) { - ch = channels[i] - return - } + if i == -1 { + for i, _ := range channels { + if !isPlaying(i) { + return channels[i] } - return } - if !isPlaying(i) { - ch = channels[i] - return - } - return - }) - return ch -} - -func Play(channel int, data []byte) bool { - ch := channelAt(channel) - if ch == nil { - return false + return nil } - withChannels(func() { - if !audioEnabled { - return - } - d := ch.nextInsertionPosition - len(data) - if 0 < d { - ch.buffer = append(ch.buffer, make([]byte, d)...) - } - ch.buffer = append(ch.buffer, data...) - }) - return true + if !isPlaying(i) { + return channels[i] + } + return nil } -func Queue(channel int, data []byte) { +func Queue(channel int, data []byte) bool { + result := true withChannels(func() { if !audioEnabled { + result = false + return + } + ch := channelAt(channel) + if ch == nil { + result = false return } - ch := channels[channel] ch.buffer = append(ch.buffer, data...) }) + return result } func Tick() {