audio: Remove audio.Play

This commit is contained in:
Hajime Hoshi 2016-02-08 00:45:02 +09:00
parent 0b62a9af74
commit b63911e6f7
4 changed files with 25 additions and 55 deletions

View File

@ -112,7 +112,7 @@ func addNote() {
vol := 1.0 / 16.0 vol := 1.0 / 16.0
square(l, vol, freq, 0.25) square(l, vol, freq, 0.25)
square(r, 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 { func update(screen *ebiten.Image) error {

View File

@ -71,7 +71,7 @@ func toBytes(l, r []int16) []byte {
func addNote(freq float64, vol float64) { func addNote(freq float64, vol float64) {
f := int(freq) f := int(freq)
if n, ok := noteCache[f]; ok { if n, ok := noteCache[f]; ok {
audio.Play(-1, n) audio.Queue(-1, n)
return return
} }
length := len(pcm) * baseFreq / f length := len(pcm) * baseFreq / f
@ -88,7 +88,7 @@ func addNote(freq float64, vol float64) {
} }
n := toBytes(l, r) n := toBytes(l, r)
noteCache[f] = n noteCache[f] = n
audio.Play(-1, n) audio.Queue(-1, n)
} }
var keys = []ebiten.Key{ var keys = []ebiten.Key{

View File

@ -26,28 +26,15 @@ func SampleRate() int {
// MaxChannel is a max number of channels. // MaxChannel is a max number of channels.
var MaxChannel = audio.MaxChannel 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. // 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. // 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). // data's format must be linear PCM (44100Hz, 16bits, 2 channel stereo, little endian).
// func Queue(channel int, data []byte) bool {
// This function is useful to play SE or a note of PCM synthesis immediately. return audio.Queue(channel, data)
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)
} }
// IsPlaying returns a boolean value which indicates if the channel buffer has data to play. // IsPlaying returns a boolean value which indicates if the channel buffer has data to play.

View File

@ -46,52 +46,35 @@ func isPlaying(channel int) bool {
} }
func channelAt(i int) *channel { func channelAt(i int) *channel {
var ch *channel
withChannels(func() {
if i == -1 { if i == -1 {
for i, _ := range channels { for i, _ := range channels {
if !isPlaying(i) { if !isPlaying(i) {
ch = channels[i] return channels[i]
return
} }
} }
return return nil
} }
if !isPlaying(i) { if !isPlaying(i) {
ch = channels[i] return channels[i]
return
} }
return return nil
})
return ch
} }
func Play(channel int, data []byte) bool { func Queue(channel int, data []byte) bool {
result := true
withChannels(func() {
if !audioEnabled {
result = false
return
}
ch := channelAt(channel) ch := channelAt(channel)
if ch == nil { if ch == nil {
return false result = false
}
withChannels(func() {
if !audioEnabled {
return 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
}
func Queue(channel int, data []byte) {
withChannels(func() {
if !audioEnabled {
return
}
ch := channels[channel]
ch.buffer = append(ch.buffer, data...) ch.buffer = append(ch.buffer, data...)
}) })
return result
} }
func Tick() { func Tick() {