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
square(l, 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 {

View File

@ -18,13 +18,32 @@ import (
"github.com/hajimehoshi/ebiten/internal/audio"
)
// SampleRate returns the sampling frequency (e.g. 44100).
func SampleRate() int {
return audio.SampleRate
}
// TODO: better name
func AppendToBuffer(channel int, l []float32, r []float32) bool {
return audio.Append(channel, l, r)
// MaxChannel is a max number of channels.
var MaxChannel = audio.MaxChannel
// 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.

View File

@ -25,7 +25,9 @@ type channel struct {
r []float32
}
var channels = make([]*channel, 16)
var MaxChannel = 32
var channels = make([]*channel, MaxChannel)
func init() {
for i, _ := range channels {
@ -44,7 +46,7 @@ func 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)
if len(l) != 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
}
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 {
return currentBytes + nextInsertion
}