mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
audio: Bug fix: Reduce noise by using long buffers (#205)
This commit is contained in:
parent
9a18ffe563
commit
5d5f27bcbf
@ -27,6 +27,7 @@ type Player struct {
|
|||||||
channelNum int
|
channelNum int
|
||||||
bytesPerSample int
|
bytesPerSample int
|
||||||
positionInSamples int64
|
positionInSamples int64
|
||||||
|
bufferedData []byte
|
||||||
context *js.Object
|
context *js.Object
|
||||||
bufferSource *js.Object
|
bufferSource *js.Object
|
||||||
}
|
}
|
||||||
@ -43,6 +44,7 @@ func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
|
|||||||
sampleRate: sampleRate,
|
sampleRate: sampleRate,
|
||||||
channelNum: channelNum,
|
channelNum: channelNum,
|
||||||
bytesPerSample: bytesPerSample,
|
bytesPerSample: bytesPerSample,
|
||||||
|
bufferedData: []byte{},
|
||||||
bufferSource: nil,
|
bufferSource: nil,
|
||||||
context: class.New(),
|
context: class.New(),
|
||||||
}
|
}
|
||||||
@ -61,27 +63,36 @@ func toLR(data []byte) ([]int16, []int16) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Player) Proceed(data []byte) error {
|
func (p *Player) Proceed(data []byte) error {
|
||||||
|
p.bufferedData = append(p.bufferedData, data...)
|
||||||
c := int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
|
c := int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
|
||||||
if p.positionInSamples < c {
|
if p.positionInSamples < c {
|
||||||
p.positionInSamples = c
|
p.positionInSamples = c
|
||||||
}
|
}
|
||||||
n := len(data)
|
// Looks like low sample rate (e.g. 22050) requires a quite long buffer (#205).
|
||||||
buf := p.context.Call("createBuffer", p.channelNum, n/p.bytesPerSample/p.channelNum, p.sampleRate)
|
// 4096 is not perfect but it reduce noise to some extent.
|
||||||
l := buf.Call("getChannelData", 0)
|
const dataSize = 4096
|
||||||
r := buf.Call("getChannelData", 1)
|
for dataSize <= len(p.bufferedData) {
|
||||||
il, ir := toLR(data)
|
data := p.bufferedData[:dataSize]
|
||||||
const max = 1 << 15
|
size := len(data) / p.bytesPerSample / p.channelNum
|
||||||
for i := 0; i < len(il); i++ {
|
// TODO: size must be const or you'll get noise (e.g. sample rate is 22050)
|
||||||
l.SetIndex(i, float64(il[i])/max)
|
buf := p.context.Call("createBuffer", p.channelNum, size, p.sampleRate)
|
||||||
r.SetIndex(i, float64(ir[i])/max)
|
l := buf.Call("getChannelData", 0)
|
||||||
|
r := buf.Call("getChannelData", 1)
|
||||||
|
il, ir := toLR(data)
|
||||||
|
const max = 1 << 15
|
||||||
|
for i := 0; i < len(il); i++ {
|
||||||
|
l.SetIndex(i, float64(il[i])/max)
|
||||||
|
r.SetIndex(i, float64(ir[i])/max)
|
||||||
|
}
|
||||||
|
p.bufferSource = p.context.Call("createBufferSource")
|
||||||
|
p.bufferSource.Set("buffer", buf)
|
||||||
|
p.bufferSource.Call("connect", p.context.Get("destination"))
|
||||||
|
p.bufferSource.Call("start", float64(p.positionInSamples)/float64(p.sampleRate))
|
||||||
|
// Call 'stop' or we'll get noisy sound especially on Chrome.
|
||||||
|
//p.bufferSource.Call("stop", float64(p.positionInSamples+int64(len(il)))/float64(p.sampleRate))
|
||||||
|
p.positionInSamples += int64(len(il))
|
||||||
|
p.bufferedData = p.bufferedData[dataSize:]
|
||||||
}
|
}
|
||||||
p.bufferSource = p.context.Call("createBufferSource")
|
|
||||||
p.bufferSource.Set("buffer", buf)
|
|
||||||
p.bufferSource.Call("connect", p.context.Get("destination"))
|
|
||||||
p.bufferSource.Call("start", float64(p.positionInSamples)/float64(p.sampleRate))
|
|
||||||
// Call 'stop' or we'll get noisy sound especially on Chrome.
|
|
||||||
p.bufferSource.Call("stop", float64(p.positionInSamples+int64(len(il)))/float64(p.sampleRate))
|
|
||||||
p.positionInSamples += int64(len(il))
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user