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
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 {

View File

@ -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{

View File

@ -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.

View File

@ -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() {