audio: Use a dummy driver for testing

Fixes #750

This also fixes an out-of-index bug when all players are skipped.
This commit is contained in:
Hajime Hoshi 2018-12-17 23:06:56 +09:00
parent 9628e629ae
commit b9eba54115
2 changed files with 46 additions and 1 deletions

View File

@ -74,10 +74,13 @@ func (p *players) Read(b []byte) (int, error) {
l := len(b)
l &= mask
allSkipped := true
for player := range p.players {
if player.shouldSkip() {
continue
}
allSkipped = false
s := player.bufferSizeInBytes()
if l > s {
l = s
@ -85,6 +88,10 @@ func (p *players) Read(b []byte) (int, error) {
}
}
if allSkipped {
l = 0
}
if l == 0 {
// If l is 0, all the players might reach EOF at the next update.
// However, this Read might block forever and never causes context switch
@ -236,6 +243,15 @@ func CurrentContext() *Context {
return c
}
var driverForTesting io.WriteCloser
func newDriver(sampleRate int) (io.WriteCloser, error) {
if driverForTesting != nil {
return driverForTesting, nil
}
return oto.NewPlayer(sampleRate, channelNum, bytesPerSample/channelNum, bufferSize())
}
func (c *Context) loop() {
initCh := make(chan struct{})
@ -258,7 +274,7 @@ func (c *Context) loop() {
// e.g. a variable for JVM on Android might not be set.
<-initCh
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample/channelNum, bufferSize())
p, err := newDriver(c.sampleRate)
if err != nil {
c.err = err
return

29
audio/driver_test.go Normal file
View File

@ -0,0 +1,29 @@
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package audio
type dummyDriver struct{}
func (d *dummyDriver) Write(b []byte) (int, error) {
return len(b), nil
}
func (d *dummyDriver) Close() error {
return nil
}
func init() {
driverForTesting = &dummyDriver{}
}