Refactoring: only one nextInsertion is needed

This commit is contained in:
Hajime Hoshi 2015-01-23 21:51:21 +09:00
parent 8d250c6b25
commit 88ac129dd6

View File

@ -27,10 +27,11 @@ var context js.Object
const bufferSize = 1024 const bufferSize = 1024
const SampleRate = 44100 const SampleRate = 44100
var nextInsertion = 0
type channel struct { type channel struct {
l []float32 l []float32
r []float32 r []float32
nextInsertion int
} }
var channels = make([]*channel, 16) var channels = make([]*channel, 16)
@ -84,8 +85,8 @@ func Init() {
usedLen := min(bufferSize, len(ch.l)) usedLen := min(bufferSize, len(ch.l))
ch.l = ch.l[usedLen:] ch.l = ch.l[usedLen:]
ch.r = ch.r[usedLen:] ch.r = ch.r[usedLen:]
ch.nextInsertion -= min(bufferSize, ch.nextInsertion)
} }
nextInsertion -= min(bufferSize, nextInsertion)
for i := 0; i < bufferSize; i++ { for i := 0; i < bufferSize; i++ {
// TODO: Use copyFromChannel? // TODO: Use copyFromChannel?
if len(inputL) <= i { if len(inputL) <= i {
@ -100,12 +101,7 @@ func Init() {
} }
func Update() { func Update() {
for _, ch := range channels { nextInsertion += SampleRate / 60
if len(ch.l) == 0 {
continue
}
ch.nextInsertion += SampleRate / 60
}
} }
func Start() { func Start() {
@ -116,14 +112,14 @@ func Start() {
func channelAt(i int) *channel { func channelAt(i int) *channel {
if i == -1 { if i == -1 {
for _, ch := range channels { for _, ch := range channels {
if len(ch.l) <= ch.nextInsertion { if len(ch.l) <= nextInsertion {
return ch return ch
} }
} }
return nil return nil
} }
ch := channels[i] ch := channels[i]
if len(ch.l) <= ch.nextInsertion { if len(ch.l) <= nextInsertion {
return ch return ch
} }
return nil return nil
@ -138,9 +134,8 @@ func Append(i int, l []float32, r []float32) bool {
if ch == nil { if ch == nil {
return false return false
} }
print(ch.nextInsertion) ch.l = append(ch.l, make([]float32, nextInsertion-len(ch.l))...)
ch.l = append(ch.l, make([]float32, ch.nextInsertion-len(ch.l))...) ch.r = append(ch.r, make([]float32, nextInsertion-len(ch.r))...)
ch.r = append(ch.r, make([]float32, ch.nextInsertion-len(ch.r))...)
ch.l = append(ch.l, l...) ch.l = append(ch.l, l...)
ch.r = append(ch.r, r...) ch.r = append(ch.r, r...)
return true return true