mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 11:12:44 +01:00
audio: Introduce channels
This commit is contained in:
parent
ba3a612ce4
commit
4b74411922
13
audio.go
13
audio.go
@ -18,10 +18,15 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/audio"
|
"github.com/hajimehoshi/ebiten/internal/audio"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AppendToAudioBuffer(l []float32, r []float32) {
|
func AudioSampleRate() int {
|
||||||
audio.Append(l, r)
|
return audio.SampleRate
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddToAudioBuffer(l []float32, r []float32) {
|
func AppendToAudioBuffer(channel int, l []float32, r []float32) bool {
|
||||||
audio.Add(l, r)
|
return audio.Append(channel, l, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: better name
|
||||||
|
func CurrentAudioTime() int {
|
||||||
|
return audio.CurrentBytes()
|
||||||
}
|
}
|
||||||
|
@ -43,15 +43,12 @@ const (
|
|||||||
freqGS = 830.6
|
freqGS = 830.6
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Need API to get sample rate?
|
|
||||||
const sampleRate = 44100
|
|
||||||
|
|
||||||
const score = `CCGGAAGR FFEEDDCR GGFFEEDR GGFFEEDR CCGGAAGR FFEEDDCR`
|
const score = `CCGGAAGR FFEEDDCR GGFFEEDR GGFFEEDR CCGGAAGR FFEEDDCR`
|
||||||
|
|
||||||
var scoreIndex = 0
|
var scoreIndex = 0
|
||||||
|
|
||||||
func square(out []float32, volume float64, freq float64, sequence float64) {
|
func square(out []float32, volume float64, freq float64, sequence float64) {
|
||||||
length := int(sampleRate / freq)
|
length := int(float64(ebiten.AudioSampleRate()) / freq)
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
panic("invalid freq")
|
panic("invalid freq")
|
||||||
}
|
}
|
||||||
@ -65,15 +62,15 @@ func square(out []float32, volume float64, freq float64, sequence float64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addNote() {
|
func addNote() {
|
||||||
const size = sampleRate / 60
|
size := ebiten.AudioSampleRate() / 60
|
||||||
notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2}
|
notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
scoreIndex++
|
scoreIndex++
|
||||||
scoreIndex %= len(score)
|
scoreIndex %= len(score)
|
||||||
}()
|
}()
|
||||||
l := make([]float32, size*30)
|
l := make([]float32, size*30*2)
|
||||||
r := make([]float32, size*30)
|
r := make([]float32, size*30*2)
|
||||||
note := score[scoreIndex]
|
note := score[scoreIndex]
|
||||||
for note == ' ' {
|
for note == ' ' {
|
||||||
scoreIndex++
|
scoreIndex++
|
||||||
@ -92,7 +89,7 @@ func addNote() {
|
|||||||
vol := 1.0 / 32.0
|
vol := 1.0 / 32.0
|
||||||
square(l, vol, freq, 0.5)
|
square(l, vol, freq, 0.5)
|
||||||
square(r, 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 {
|
func update(screen *ebiten.Image) error {
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package audio
|
package audio
|
||||||
|
|
||||||
|
const SampleRate = 44100
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
}
|
}
|
||||||
@ -24,10 +26,12 @@ func Start() {
|
|||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
}
|
}
|
||||||
|
|
||||||
func Append(l []float32, r []float32) {
|
func Append(channel int, l []float32, r []float32) bool {
|
||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func Add(l []float32, r []float32) {
|
func CurrentBytes() int {
|
||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
@ -25,11 +25,23 @@ var node js.Object
|
|||||||
var context js.Object
|
var context js.Object
|
||||||
|
|
||||||
const bufferSize = 1024
|
const bufferSize = 1024
|
||||||
|
const SampleRate = 44100
|
||||||
|
|
||||||
var (
|
type channel struct {
|
||||||
bufferL = make([]float32, 0)
|
l []float32
|
||||||
bufferR = make([]float32, 0)
|
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 {
|
func min(a, b int) int {
|
||||||
if a < b {
|
if a < b {
|
||||||
@ -38,29 +50,50 @@ func min(a, b int) int {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var currentBytes = 0
|
||||||
|
|
||||||
|
func CurrentBytes() int {
|
||||||
|
return currentBytes
|
||||||
|
}
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
context = js.Global.Get("AudioContext").New()
|
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
|
// https://developer.mozilla.org/ja/docs/Web/API/ScriptProcessorNode
|
||||||
const bufLen = 1024
|
node = context.Call("createScriptProcessor", bufferSize, 0, 2)
|
||||||
node = context.Call("createScriptProcessor", bufLen, 0, 2)
|
|
||||||
node.Call("addEventListener", "audioprocess", func(e js.Object) {
|
node.Call("addEventListener", "audioprocess", func(e js.Object) {
|
||||||
|
defer func() {
|
||||||
|
currentBytes += bufferSize
|
||||||
|
}()
|
||||||
|
|
||||||
l := e.Get("outputBuffer").Call("getChannelData", 0)
|
l := e.Get("outputBuffer").Call("getChannelData", 0)
|
||||||
r := e.Get("outputBuffer").Call("getChannelData", 1)
|
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?
|
// 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)
|
l.SetIndex(i, 0)
|
||||||
r.SetIndex(i, 0)
|
r.SetIndex(i, 0)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
l.SetIndex(i, bufferL[i])
|
l.SetIndex(i, inputL[i])
|
||||||
r.SetIndex(i, bufferR[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"))
|
node.Call("connect", context.Get("destination"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Append(l []float32, r []float32) {
|
func channelAt(i int) *channel {
|
||||||
if len(l) != len(r) {
|
if i == -1 {
|
||||||
panic("len(l) must equal to len(r)")
|
for _, ch := range channels {
|
||||||
|
if 0 < len(ch.l) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
bufferL = append(bufferL, l...)
|
ch := channels[i]
|
||||||
bufferR = append(bufferR, r...)
|
// 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) {
|
func Append(i int, l []float32, r []float32) bool {
|
||||||
// TODO: Adjust timing for frame?
|
// 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)")
|
||||||
}
|
}
|
||||||
m := min(len(l), len(bufferL))
|
ch := channelAt(i)
|
||||||
for i := 0; i < m; i++ {
|
if ch == nil {
|
||||||
bufferL[i] += l[i]
|
return false
|
||||||
bufferR[i] += r[i]
|
|
||||||
}
|
|
||||||
if m < len(l) {
|
|
||||||
bufferL = append(bufferL, l[m:]...)
|
|
||||||
bufferR = append(bufferR, r[m:]...)
|
|
||||||
}
|
}
|
||||||
|
ch.l = append(ch.l, l...)
|
||||||
|
ch.r = append(ch.r, r...)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user