audio: Introduce channels

This commit is contained in:
Hajime Hoshi 2015-01-23 03:02:23 +09:00
parent ba3a612ce4
commit 4b74411922
4 changed files with 91 additions and 44 deletions

View File

@ -18,10 +18,15 @@ import (
"github.com/hajimehoshi/ebiten/internal/audio"
)
func AppendToAudioBuffer(l []float32, r []float32) {
audio.Append(l, r)
func AudioSampleRate() int {
return audio.SampleRate
}
func AddToAudioBuffer(l []float32, r []float32) {
audio.Add(l, r)
func AppendToAudioBuffer(channel int, l []float32, r []float32) bool {
return audio.Append(channel, l, r)
}
// TODO: better name
func CurrentAudioTime() int {
return audio.CurrentBytes()
}

View File

@ -43,15 +43,12 @@ const (
freqGS = 830.6
)
// TODO: Need API to get sample rate?
const sampleRate = 44100
const score = `CCGGAAGR FFEEDDCR GGFFEEDR GGFFEEDR CCGGAAGR FFEEDDCR`
var scoreIndex = 0
func square(out []float32, volume float64, freq float64, sequence float64) {
length := int(sampleRate / freq)
length := int(float64(ebiten.AudioSampleRate()) / freq)
if length == 0 {
panic("invalid freq")
}
@ -65,15 +62,15 @@ func square(out []float32, volume float64, freq float64, sequence float64) {
}
func addNote() {
const size = sampleRate / 60
size := ebiten.AudioSampleRate() / 60
notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2}
defer func() {
scoreIndex++
scoreIndex %= len(score)
}()
l := make([]float32, size*30)
r := make([]float32, size*30)
l := make([]float32, size*30*2)
r := make([]float32, size*30*2)
note := score[scoreIndex]
for note == ' ' {
scoreIndex++
@ -92,7 +89,7 @@ func addNote() {
vol := 1.0 / 32.0
square(l, vol, freq, 0.5)
square(r, vol, freq, 0.5)
ebiten.AddToAudioBuffer(l, r)
ebiten.AppendToAudioBuffer(-1, l, r)
}
func update(screen *ebiten.Image) error {

View File

@ -16,6 +16,8 @@
package audio
const SampleRate = 44100
func Init() {
// TODO: Implement
}
@ -24,10 +26,12 @@ func Start() {
// TODO: Implement
}
func Append(l []float32, r []float32) {
func Append(channel int, l []float32, r []float32) bool {
// TODO: Implement
return false
}
func Add(l []float32, r []float32) {
func CurrentBytes() int {
// TODO: Implement
return 0
}

View File

@ -25,11 +25,23 @@ var node js.Object
var context js.Object
const bufferSize = 1024
const SampleRate = 44100
var (
bufferL = make([]float32, 0)
bufferR = make([]float32, 0)
)
type channel struct {
l []float32
r []float32
}
var channels = make([]*channel, 16)
func init() {
for i, _ := range channels {
channels[i] = &channel{
l: []float32{},
r: []float32{},
}
}
}
func min(a, b int) int {
if a < b {
@ -38,29 +50,50 @@ func min(a, b int) int {
return b
}
var currentBytes = 0
func CurrentBytes() int {
return currentBytes
}
func Init() {
context = js.Global.Get("AudioContext").New()
// TODO: ScriptProcessorNode will be replaced Audio WebWorker.
// TODO: ScriptProcessorNode will be replaced with Audio WebWorker.
// https://developer.mozilla.org/ja/docs/Web/API/ScriptProcessorNode
const bufLen = 1024
node = context.Call("createScriptProcessor", bufLen, 0, 2)
node = context.Call("createScriptProcessor", bufferSize, 0, 2)
node.Call("addEventListener", "audioprocess", func(e js.Object) {
defer func() {
currentBytes += bufferSize
}()
l := e.Get("outputBuffer").Call("getChannelData", 0)
r := e.Get("outputBuffer").Call("getChannelData", 1)
for i := 0; i < bufLen; i++ {
inputL := make([]float32, bufferSize)
inputR := make([]float32, bufferSize)
for _, ch := range channels {
if len(ch.l) == 0 {
continue
}
l := min(len(ch.l), bufferSize)
for i := 0; i < l; i++ {
inputL[i] += ch.l[i]
inputR[i] += ch.r[i]
}
// TODO: Use copyFromChannel?
if len(bufferL) <= i {
usedLen := min(bufferSize, len(ch.l))
ch.l = ch.l[usedLen:]
ch.r = ch.r[usedLen:]
}
for i := 0; i < bufferSize; i++ {
// TODO: Use copyFromChannel?
if len(inputL) <= i {
l.SetIndex(i, 0)
r.SetIndex(i, 0)
continue
}
l.SetIndex(i, bufferL[i])
r.SetIndex(i, bufferR[i])
l.SetIndex(i, inputL[i])
r.SetIndex(i, inputR[i])
}
// TODO: Will the array heads be released properly on GopherJS?
usedLen := min(bufLen, len(bufferL))
bufferL = bufferL[usedLen:]
bufferR = bufferR[usedLen:]
})
}
@ -69,26 +102,34 @@ func Start() {
node.Call("connect", context.Get("destination"))
}
func Append(l []float32, r []float32) {
if len(l) != len(r) {
panic("len(l) must equal to len(r)")
func channelAt(i int) *channel {
if i == -1 {
for _, ch := range channels {
if 0 < len(ch.l) {
continue
}
return ch
}
return nil
}
bufferL = append(bufferL, l...)
bufferR = append(bufferR, r...)
ch := channels[i]
// TODO: Can we append even though all data is not consumed? Need game timer?
if 0 < len(ch.l) {
return nil
}
return ch
}
func Add(l []float32, r []float32) {
// TODO: Adjust timing for frame?
func Append(i int, l []float32, r []float32) bool {
// TODO: Mutex (especially for OpenAL)
if len(l) != len(r) {
panic("len(l) must equal to len(r)")
}
m := min(len(l), len(bufferL))
for i := 0; i < m; i++ {
bufferL[i] += l[i]
bufferR[i] += r[i]
}
if m < len(l) {
bufferL = append(bufferL, l[m:]...)
bufferR = append(bufferR, r[m:]...)
ch := channelAt(i)
if ch == nil {
return false
}
ch.l = append(ch.l, l...)
ch.r = append(ch.r, r...)
return true
}