ebiten/audio/export_test.go

157 lines
2.7 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"
"sync"
2019-01-26 16:31:55 +01:00
)
type (
2021-08-21 12:52:11 +02:00
dummyContext struct{}
dummyPlayer struct {
r io.Reader
playing bool
volume float64
m sync.Mutex
}
)
2021-12-17 08:04:39 +01:00
func (c *dummyContext) NewPlayer(r io.Reader) player {
2021-08-21 12:52:11 +02:00
return &dummyPlayer{
r: r,
volume: 1,
}
}
2021-08-21 12:52:11 +02:00
func (c *dummyContext) MaxBufferSize() int {
2023-04-08 12:40:55 +02:00
return 48000 * channelCount * bitDepthInBytesInt16 / 4
}
2021-08-21 12:52:11 +02:00
func (c *dummyContext) Suspend() error {
return nil
}
2021-08-21 12:52:11 +02:00
func (c *dummyContext) Resume() error {
return nil
}
2021-10-22 08:35:05 +02:00
func (c *dummyContext) Err() error {
return nil
}
2021-08-21 12:52:11 +02:00
func (p *dummyPlayer) Pause() {
p.m.Lock()
p.playing = false
p.m.Unlock()
}
2021-08-21 12:52:11 +02:00
func (p *dummyPlayer) Play() {
p.m.Lock()
p.playing = true
p.m.Unlock()
go func() {
if _, err := io.ReadAll(p.r); err != nil {
panic(err)
}
p.m.Lock()
p.playing = false
p.m.Unlock()
}()
}
2021-08-21 12:52:11 +02:00
func (p *dummyPlayer) IsPlaying() bool {
p.m.Lock()
defer p.m.Unlock()
return p.playing
}
2021-08-21 12:52:11 +02:00
func (p *dummyPlayer) Volume() float64 {
return p.volume
}
2021-08-21 12:52:11 +02:00
func (p *dummyPlayer) SetVolume(volume float64) {
p.volume = volume
}
2023-07-30 09:05:17 +02:00
func (p *dummyPlayer) BufferedSize() int {
return 0
}
2021-08-21 12:52:11 +02:00
func (p *dummyPlayer) Err() error {
return nil
}
func (p *dummyPlayer) SetBufferSize(bufferSize int) {
}
2022-06-30 19:31:29 +02:00
func (p *dummyPlayer) Seek(offset int64, whence int) (int64, error) {
return 0, nil
}
2021-08-21 12:52:11 +02:00
func (p *dummyPlayer) Close() error {
p.m.Lock()
defer p.m.Unlock()
p.playing = false
return nil
}
func init() {
2021-08-21 12:52:11 +02:00
driverForTesting = &dummyContext{}
}
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 PlayersCountForTesting() int {
2021-01-06 17:37:29 +01:00
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
}
func (i *InfiniteLoop) SetNoBlendForTesting(value bool) {
i.noBlendForTesting = value
}