mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08: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,12 +63,19 @@ 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.
|
||||||
|
const dataSize = 4096
|
||||||
|
for dataSize <= len(p.bufferedData) {
|
||||||
|
data := p.bufferedData[:dataSize]
|
||||||
|
size := len(data) / p.bytesPerSample / p.channelNum
|
||||||
|
// TODO: size must be const or you'll get noise (e.g. sample rate is 22050)
|
||||||
|
buf := p.context.Call("createBuffer", p.channelNum, size, p.sampleRate)
|
||||||
l := buf.Call("getChannelData", 0)
|
l := buf.Call("getChannelData", 0)
|
||||||
r := buf.Call("getChannelData", 1)
|
r := buf.Call("getChannelData", 1)
|
||||||
il, ir := toLR(data)
|
il, ir := toLR(data)
|
||||||
@ -80,8 +89,10 @@ func (p *Player) Proceed(data []byte) error {
|
|||||||
p.bufferSource.Call("connect", p.context.Get("destination"))
|
p.bufferSource.Call("connect", p.context.Get("destination"))
|
||||||
p.bufferSource.Call("start", float64(p.positionInSamples)/float64(p.sampleRate))
|
p.bufferSource.Call("start", float64(p.positionInSamples)/float64(p.sampleRate))
|
||||||
// Call 'stop' or we'll get noisy sound especially on Chrome.
|
// 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.bufferSource.Call("stop", float64(p.positionInSamples+int64(len(il)))/float64(p.sampleRate))
|
||||||
p.positionInSamples += int64(len(il))
|
p.positionInSamples += int64(len(il))
|
||||||
|
p.bufferedData = p.bufferedData[dataSize:]
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user