123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- // 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
- import (
- "io"
- "sync"
- "time"
- )
- type (
- dummyContext struct{}
- dummyPlayer struct {
- r io.Reader
- playing bool
- volume float64
- m sync.Mutex
- }
- )
- func (c *dummyContext) NewPlayer(r io.Reader) player {
- return &dummyPlayer{
- r: r,
- volume: 1,
- }
- }
- func (c *dummyContext) MaxBufferSize() int {
- return 48000 * channelCount * bitDepthInBytesInt16 / 4
- }
- func (c *dummyContext) Suspend() error {
- return nil
- }
- func (c *dummyContext) Resume() error {
- return nil
- }
- func (c *dummyContext) Err() error {
- return nil
- }
- func (p *dummyPlayer) Pause() {
- p.m.Lock()
- p.playing = false
- p.m.Unlock()
- }
- func (p *dummyPlayer) Play() {
- p.m.Lock()
- p.playing = true
- p.m.Unlock()
- go func() {
- var buf [4096]byte
- for {
- _, err := p.r.Read(buf[:])
- if err != nil {
- if err != io.EOF {
- panic(err)
- }
- break
- }
- time.Sleep(time.Millisecond)
- }
- p.m.Lock()
- p.playing = false
- p.m.Unlock()
- }()
- }
- func (p *dummyPlayer) IsPlaying() bool {
- p.m.Lock()
- defer p.m.Unlock()
- return p.playing
- }
- func (p *dummyPlayer) Volume() float64 {
- return p.volume
- }
- func (p *dummyPlayer) SetVolume(volume float64) {
- p.volume = volume
- }
- func (p *dummyPlayer) BufferedSize() int {
- return 0
- }
- func (p *dummyPlayer) Err() error {
- return nil
- }
- func (p *dummyPlayer) SetBufferSize(bufferSize int) {
- }
- func (p *dummyPlayer) Seek(offset int64, whence int) (int64, error) {
- return 0, nil
- }
- func (p *dummyPlayer) Close() error {
- p.m.Lock()
- defer p.m.Unlock()
- p.playing = false
- return nil
- }
- func init() {
- driverForTesting = &dummyContext{}
- }
- type dummyHook struct {
- updates []func() error
- }
- func (h *dummyHook) OnSuspendAudio(f func() error) {
- }
- func (h *dummyHook) OnResumeAudio(f func() error) {
- }
- func (h *dummyHook) AppendHookOnBeforeUpdate(f func() error) {
- h.updates = append(h.updates, f)
- }
- func init() {
- hookerForTesting = &dummyHook{}
- }
- func UpdateForTesting() error {
- for _, f := range hookerForTesting.(*dummyHook).updates {
- if err := f(); err != nil {
- return err
- }
- }
- return nil
- }
- func PlayersCountForTesting() int {
- c := CurrentContext()
- c.m.Lock()
- n := len(c.playingPlayers)
- c.m.Unlock()
- return n
- }
- func ResetContextForTesting() {
- theContext = nil
- }
- func (i *InfiniteLoop) SetNoBlendForTesting(value bool) {
- i.noBlendForTesting = value
- }
|