audio: Remove channel argument from audio.Queue

This commit is contained in:
Hajime Hoshi 2016-02-09 22:55:18 +09:00
parent 34691dabbf
commit 70fe6d8169
5 changed files with 17 additions and 22 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.Queue(-1, toBytes(l, r))
audio.Queue(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.Queue(-1, n)
audio.Queue(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.Queue(-1, n)
audio.Queue(n)
}
var keys = []ebiten.Key{

View File

@ -28,16 +28,10 @@ const MaxChannel = audio.MaxChannel
// The given data is queued to the end of the buffer.
// This may not be played immediately when data already exists in the buffer.
//
// 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)
// without a header (e.g. RIFF header).
func Queue(channel int, data []byte) bool {
return audio.Queue(channel, data)
func Queue(data []byte) bool {
return audio.Queue(data)
}
// IsPlaying returns a boolean value which indicates if the channel buffer has data to play.

View File

@ -70,14 +70,14 @@ func Tick() {
}
// TODO: Accept sample rate
func Queue(channel int, data []byte) bool {
func Queue(data []byte) bool {
result := true
withChannels(func() {
if !audioEnabled {
result = false
return
}
ch := channelAt(channel)
ch := channelAt(-1)
if ch == nil {
result = false
return

View File

@ -28,8 +28,9 @@ var (
)
type audioProcessor struct {
channel int
position float64
channel int
sampleRate int
position float64
}
var audioProcessors [MaxChannel]*audioProcessor
@ -44,13 +45,13 @@ func toLR(data []byte) ([]int16, []int16) {
return l, r
}
func (a *audioProcessor) playChunk(buf []byte, sampleRate int) {
func (a *audioProcessor) playChunk(buf []byte) {
if len(buf) == 0 {
return
}
const channelNum = 2
const bytesPerSample = channelNum * 16 / 8
b := context.Call("createBuffer", channelNum, len(buf)/bytesPerSample, sampleRate)
b := context.Call("createBuffer", channelNum, len(buf)/bytesPerSample, a.sampleRate)
l := b.Call("getChannelData", 0)
r := b.Call("getChannelData", 1)
il, ir := toLR(buf)
@ -67,7 +68,7 @@ func (a *audioProcessor) playChunk(buf []byte, sampleRate int) {
a.position = c
}
s.Call("start", a.position)
a.position += float64(len(il)) / float64(sampleRate)
a.position += float64(len(il)) / float64(a.sampleRate)
}
func isPlaying(channel int) bool {
@ -77,9 +78,8 @@ func isPlaying(channel int) bool {
func tick() {
const bufferSize = 1024
const sampleRate = 44100 // TODO: This should be changeable
for _, a := range audioProcessors {
a.playChunk(loadChannelBuffer(a.channel, bufferSize*4), sampleRate)
a.playChunk(loadChannelBuffer(a.channel, bufferSize*4))
}
}
@ -100,8 +100,9 @@ func initialize() {
audioEnabled = true
for i := 0; i < len(audioProcessors); i++ {
audioProcessors[i] = &audioProcessor{
channel: i,
position: 0,
channel: i,
sampleRate: 44100, // TODO: Change this for each chunks
position: 0,
}
}
}