audio: Bug fix: noise on audio

This commit is contained in:
Hajime Hoshi 2017-07-09 04:09:21 +09:00
parent 16f84f6a89
commit 5cf7b2491a
2 changed files with 5 additions and 3 deletions

View File

@ -223,7 +223,8 @@ func (c *Context) Update() error {
// e.g. a variable for JVM on Android might not be set. // e.g. a variable for JVM on Android might not be set.
if c.playerWriteCh == nil { if c.playerWriteCh == nil {
init := make(chan error) init := make(chan error)
c.playerWriteCh = make(chan []uint8) // 4 is (buffer size) / (bytes for 1 frame).
c.playerWriteCh = make(chan []uint8, 4)
c.playerErrCh = make(chan error, 1) c.playerErrCh = make(chan error, 1)
c.playerCloseCh = make(chan struct{}) c.playerCloseCh = make(chan struct{})
go func() { go func() {
@ -430,7 +431,7 @@ func (p *Player) readToBuffer(length int) (int, error) {
if len(r.data) > 0 { if len(r.data) > 0 {
p.buf = append(p.buf, r.data...) p.buf = append(p.buf, r.data...)
} }
case <-timeoutIfPossible(15 * time.Millisecond): case <-timeoutIfPossible(10 * time.Millisecond):
if l := length - len(p.buf); l > 0 { if l := length - len(p.buf); l > 0 {
empty := make([]uint8, l) empty := make([]uint8, l)
p.buf = append(p.buf, empty...) p.buf = append(p.buf, empty...)

View File

@ -113,7 +113,7 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
mp3F, err := ebitenutil.OpenFile("_resources/audio/classic.mp3") mp3F, err := ebitenutil.OpenFile("_resources/audio/piece.mp3")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -289,6 +289,7 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
musicPlayer, err = NewPlayer(audioContext) musicPlayer, err = NewPlayer(audioContext)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)