diff --git a/example/audio/main.go b/example/audio/main.go index 208fae0e7..feae6992c 100644 --- a/example/audio/main.go +++ b/example/audio/main.go @@ -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 { diff --git a/example/piano/main.go b/example/piano/main.go index 6e2ac1c9c..1c7ef1102 100644 --- a/example/piano/main.go +++ b/example/piano/main.go @@ -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{ diff --git a/exp/audio/audio.go b/exp/audio/audio.go index 775f66b02..798254a28 100644 --- a/exp/audio/audio.go +++ b/exp/audio/audio.go @@ -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. diff --git a/internal/audio/audio.go b/internal/audio/audio.go index 2b7ebe8e9..cb792a76c 100644 --- a/internal/audio/audio.go +++ b/internal/audio/audio.go @@ -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 diff --git a/internal/audio/audio_js.go b/internal/audio/audio_js.go index c88775672..a010db630 100644 --- a/internal/audio/audio_js.go +++ b/internal/audio/audio_js.go @@ -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, } } }