2018-12-17 15:06:56 +01:00
|
|
|
// 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"
|
2021-03-21 18:57:52 +01:00
|
|
|
"sync"
|
2019-01-26 16:31:55 +01:00
|
|
|
)
|
2018-12-17 15:06:56 +01:00
|
|
|
|
2021-03-21 18:57:52 +01:00
|
|
|
type (
|
2021-08-21 12:52:11 +02:00
|
|
|
dummyContext struct{}
|
|
|
|
dummyPlayer struct {
|
2021-03-21 18:57:52 +01:00
|
|
|
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{
|
2021-03-21 18:57:52 +01:00
|
|
|
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-03-28 11:07:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-21 12:52:11 +02:00
|
|
|
func (c *dummyContext) Suspend() error {
|
2021-05-04 16:18:16 +02:00
|
|
|
return nil
|
2021-05-04 15:10:44 +02:00
|
|
|
}
|
|
|
|
|
2021-08-21 12:52:11 +02:00
|
|
|
func (c *dummyContext) Resume() error {
|
2021-05-04 16:18:16 +02:00
|
|
|
return nil
|
2021-05-04 15:10:44 +02:00
|
|
|
}
|
|
|
|
|
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() {
|
2021-03-21 18:57:52 +01:00
|
|
|
p.m.Lock()
|
|
|
|
p.playing = false
|
|
|
|
p.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-08-21 12:52:11 +02:00
|
|
|
func (p *dummyPlayer) Play() {
|
2021-03-21 18:57:52 +01:00
|
|
|
p.m.Lock()
|
|
|
|
p.playing = true
|
|
|
|
p.m.Unlock()
|
|
|
|
go func() {
|
2022-09-14 19:45:36 +02:00
|
|
|
if _, err := io.ReadAll(p.r); err != nil {
|
2021-03-21 18:57:52 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.m.Lock()
|
|
|
|
p.playing = false
|
|
|
|
p.m.Unlock()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-08-21 12:52:11 +02:00
|
|
|
func (p *dummyPlayer) IsPlaying() bool {
|
2021-03-21 18:57:52 +01:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
return p.playing
|
|
|
|
}
|
|
|
|
|
2021-08-21 12:52:11 +02:00
|
|
|
func (p *dummyPlayer) Volume() float64 {
|
2021-03-21 18:57:52 +01:00
|
|
|
return p.volume
|
|
|
|
}
|
|
|
|
|
2021-08-21 12:52:11 +02:00
|
|
|
func (p *dummyPlayer) SetVolume(volume float64) {
|
2021-03-21 18:57:52 +01:00
|
|
|
p.volume = volume
|
|
|
|
}
|
|
|
|
|
2023-07-30 09:05:17 +02:00
|
|
|
func (p *dummyPlayer) BufferedSize() int {
|
2021-03-21 18:57:52 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-08-21 12:52:11 +02:00
|
|
|
func (p *dummyPlayer) Err() error {
|
2021-03-28 15:57:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 19:03:46 +01:00
|
|
|
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 {
|
2021-03-21 18:57:52 +01:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
p.playing = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2021-08-21 12:52:11 +02:00
|
|
|
driverForTesting = &dummyContext{}
|
2018-12-17 15:06:56 +01:00
|
|
|
}
|
2019-04-28 12:35:59 +02:00
|
|
|
|
|
|
|
type dummyHook struct {
|
2019-04-28 12:53:22 +02:00
|
|
|
updates []func() error
|
2019-04-28 12:35:59 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 15:24:31 +02:00
|
|
|
func (h *dummyHook) OnSuspendAudio(f func() error) {
|
2019-04-28 12:35:59 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 15:24:31 +02:00
|
|
|
func (h *dummyHook) OnResumeAudio(f func() error) {
|
2019-04-28 12:35:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *dummyHook) AppendHookOnBeforeUpdate(f func() error) {
|
2019-04-28 12:53:22 +02:00
|
|
|
h.updates = append(h.updates, f)
|
2019-04-28 12:35:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
hookForTesting = &dummyHook{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateForTesting() error {
|
2019-04-28 12:53:22 +02:00
|
|
|
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
|
|
|
|
2022-07-12 18:29:28 +02: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
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:57:52 +01:00
|
|
|
func ResetContextForTesting() {
|
2021-01-06 17:37:29 +01:00
|
|
|
theContext = nil
|
|
|
|
}
|
2021-11-29 15:01:19 +01:00
|
|
|
|
|
|
|
func (i *InfiniteLoop) SetNoBlendForTesting(value bool) {
|
|
|
|
i.noBlendForTesting = value
|
|
|
|
}
|