audio: Add comments

This commit is contained in:
Hajime Hoshi 2015-01-24 15:48:48 +09:00
parent edda664ca8
commit 2c7430171d
3 changed files with 37 additions and 6 deletions

View File

@ -96,7 +96,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.AppendToBuffer(0, l, r) audio.Play(0, l, r)
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {

View File

@ -18,13 +18,32 @@ import (
"github.com/hajimehoshi/ebiten/internal/audio" "github.com/hajimehoshi/ebiten/internal/audio"
) )
// SampleRate returns the sampling frequency (e.g. 44100).
func SampleRate() int { func SampleRate() int {
return audio.SampleRate return audio.SampleRate
} }
// TODO: better name // MaxChannel is a max number of channels.
func AppendToBuffer(channel int, l []float32, r []float32) bool { var MaxChannel = audio.MaxChannel
return audio.Append(channel, l, r)
// Play appends the given data to the given channel.
//
// 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.
//
// This function is useful to play SE or a note of PCM synthesis immediately.
func Play(channel int, l []float32, r []float32) bool {
return audio.Play(channel, l, r)
}
// 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.
//
// This function is useful to play streaming data.
func Queue(channel int, l []float32, r []float32) {
audio.Queue(channel, l, r)
} }
// TODO: Add funciton to append samples to the buffer without adjusting. // TODO: Add funciton to append samples to the buffer without adjusting.

View File

@ -25,7 +25,9 @@ type channel struct {
r []float32 r []float32
} }
var channels = make([]*channel, 16) var MaxChannel = 32
var channels = make([]*channel, MaxChannel)
func init() { func init() {
for i, _ := range channels { for i, _ := range channels {
@ -44,7 +46,7 @@ func Start() {
start() start()
} }
func Append(channel int, l []float32, r []float32) bool { func Play(channel int, l []float32, r []float32) bool {
// TODO: Mutex (especially for OpenAL) // TODO: Mutex (especially for OpenAL)
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)")
@ -60,6 +62,16 @@ func Append(channel int, l []float32, r []float32) bool {
return true return true
} }
func Queue(channel int, l []float32, r []float32) {
// TODO: Mutex (especially for OpenAL)
if len(l) != len(r) {
panic("len(l) must equal to len(r)")
}
ch := channels[channel]
ch.l = append(ch.l, l...)
ch.r = append(ch.r, r...)
}
func CurrentBytes() int { func CurrentBytes() int {
return currentBytes + nextInsertion return currentBytes + nextInsertion
} }