export_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2018 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package audio
  15. import (
  16. "io"
  17. "io/ioutil"
  18. "sync"
  19. "github.com/hajimehoshi/oto/v2"
  20. )
  21. type (
  22. dummyContext struct{}
  23. dummyPlayer struct {
  24. r io.Reader
  25. playing bool
  26. volume float64
  27. m sync.Mutex
  28. }
  29. )
  30. func (c *dummyContext) NewPlayer(r io.Reader) oto.Player {
  31. return &dummyPlayer{
  32. r: r,
  33. volume: 1,
  34. }
  35. }
  36. func (c *dummyContext) MaxBufferSize() int {
  37. return 48000 * channelNum * bitDepthInBytes / 4
  38. }
  39. func (c *dummyContext) Suspend() error {
  40. return nil
  41. }
  42. func (c *dummyContext) Resume() error {
  43. return nil
  44. }
  45. func (p *dummyPlayer) Pause() {
  46. p.m.Lock()
  47. p.playing = false
  48. p.m.Unlock()
  49. }
  50. func (p *dummyPlayer) Play() {
  51. p.m.Lock()
  52. p.playing = true
  53. p.m.Unlock()
  54. go func() {
  55. if _, err := ioutil.ReadAll(p.r); err != nil {
  56. panic(err)
  57. }
  58. p.m.Lock()
  59. p.playing = false
  60. p.m.Unlock()
  61. }()
  62. }
  63. func (p *dummyPlayer) IsPlaying() bool {
  64. p.m.Lock()
  65. defer p.m.Unlock()
  66. return p.playing
  67. }
  68. func (p *dummyPlayer) Reset() {
  69. p.m.Lock()
  70. defer p.m.Unlock()
  71. p.playing = false
  72. }
  73. func (p *dummyPlayer) Volume() float64 {
  74. return p.volume
  75. }
  76. func (p *dummyPlayer) SetVolume(volume float64) {
  77. p.volume = volume
  78. }
  79. func (p *dummyPlayer) UnplayedBufferSize() int {
  80. return 0
  81. }
  82. func (p *dummyPlayer) Err() error {
  83. return nil
  84. }
  85. func (p *dummyPlayer) Close() error {
  86. p.m.Lock()
  87. defer p.m.Unlock()
  88. p.playing = false
  89. return nil
  90. }
  91. func init() {
  92. driverForTesting = &dummyContext{}
  93. }
  94. type dummyHook struct {
  95. updates []func() error
  96. }
  97. func (h *dummyHook) OnSuspendAudio(f func() error) {
  98. }
  99. func (h *dummyHook) OnResumeAudio(f func() error) {
  100. }
  101. func (h *dummyHook) AppendHookOnBeforeUpdate(f func() error) {
  102. h.updates = append(h.updates, f)
  103. }
  104. func init() {
  105. hookForTesting = &dummyHook{}
  106. }
  107. func UpdateForTesting() error {
  108. for _, f := range hookForTesting.(*dummyHook).updates {
  109. if err := f(); err != nil {
  110. return err
  111. }
  112. }
  113. return nil
  114. }
  115. func PlayersNumForTesting() int {
  116. c := CurrentContext()
  117. c.m.Lock()
  118. n := len(c.players)
  119. c.m.Unlock()
  120. return n
  121. }
  122. func ResetContextForTesting() {
  123. theContext = nil
  124. }