ebiten/audio/export_test.go

176 lines
3.1 KiB
Go
Raw Normal View History

// 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
2019-01-26 16:31:55 +01:00
import (
"io"
"io/ioutil"
"sync"
"github.com/hajimehoshi/ebiten/v2/audio/internal/readerdriver"
2019-01-26 16:31:55 +01:00
)
2019-01-26 16:31:55 +01:00
type (
dummyWriterContext struct{}
dummyWriterPlayer struct{}
2019-01-26 16:31:55 +01:00
)
func (c *dummyWriterContext) NewPlayer() io.WriteCloser {
return &dummyWriterPlayer{}
2019-01-26 16:31:55 +01:00
}
func (c *dummyWriterContext) Close() error {
2019-01-26 16:31:55 +01:00
return nil
}
func (p *dummyWriterPlayer) Write(b []byte) (int, error) {
return len(b), nil
}
func (p *dummyWriterPlayer) Close() error {
return nil
}
func init() {
writerDriverForTesting = &dummyWriterContext{}
}
type (
dummyReaderContext struct{}
dummyReaderPlayer struct {
r io.Reader
playing bool
volume float64
m sync.Mutex
}
)
func (c *dummyReaderContext) NewPlayer(r io.Reader) readerdriver.Player {
return &dummyReaderPlayer{
r: r,
volume: 1,
}
}
func (c *dummyReaderContext) MaxBufferSize() int {
return 48000 * channelNum * bitDepthInBytes / 4
}
func (c *dummyReaderContext) Suspend() error {
return nil
}
func (c *dummyReaderContext) Resume() error {
return nil
}
func (p *dummyReaderPlayer) Pause() {
p.m.Lock()
p.playing = false
p.m.Unlock()
}
func (p *dummyReaderPlayer) Play() {
p.m.Lock()
p.playing = true
p.m.Unlock()
go func() {
if _, err := ioutil.ReadAll(p.r); err != nil {
panic(err)
}
p.m.Lock()
p.playing = false
p.m.Unlock()
}()
}
func (p *dummyReaderPlayer) IsPlaying() bool {
p.m.Lock()
defer p.m.Unlock()
return p.playing
}
func (p *dummyReaderPlayer) Reset() {
p.m.Lock()
defer p.m.Unlock()
p.playing = false
}
func (p *dummyReaderPlayer) Volume() float64 {
return p.volume
}
func (p *dummyReaderPlayer) SetVolume(volume float64) {
p.volume = volume
}
func (p *dummyReaderPlayer) UnplayedBufferSize() int {
return 0
}
func (p *dummyReaderPlayer) Err() error {
return nil
}
func (p *dummyReaderPlayer) Close() error {
p.m.Lock()
defer p.m.Unlock()
p.playing = false
return nil
}
func init() {
readerDriverForTesting = &dummyReaderContext{}
}
2019-04-28 12:35:59 +02:00
type dummyHook struct {
updates []func() error
2019-04-28 12:35:59 +02:00
}
func (h *dummyHook) OnSuspendAudio(f func() error) {
2019-04-28 12:35:59 +02:00
}
func (h *dummyHook) OnResumeAudio(f func() error) {
2019-04-28 12:35:59 +02:00
}
func (h *dummyHook) AppendHookOnBeforeUpdate(f func() error) {
h.updates = append(h.updates, f)
2019-04-28 12:35:59 +02:00
}
func init() {
hookForTesting = &dummyHook{}
}
func UpdateForTesting() error {
for _, f := range hookForTesting.(*dummyHook).updates {
if err := f(); err != nil {
return err
}
}
return nil
2019-04-28 12:35:59 +02:00
}
2021-01-06 17:37:29 +01:00
func PlayersNumForTesting() int {
c := CurrentContext()
c.m.Lock()
n := len(c.players)
c.m.Unlock()
return n
}
func ResetContextForTesting() {
2021-01-06 17:37:29 +01:00
theContext = nil
}