export_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "sync"
  18. "time"
  19. )
  20. type (
  21. dummyContext struct{}
  22. dummyPlayer struct {
  23. r io.Reader
  24. playing bool
  25. volume float64
  26. m sync.Mutex
  27. }
  28. )
  29. func (c *dummyContext) NewPlayer(r io.Reader) player {
  30. return &dummyPlayer{
  31. r: r,
  32. volume: 1,
  33. }
  34. }
  35. func (c *dummyContext) MaxBufferSize() int {
  36. return 48000 * channelCount * bitDepthInBytesInt16 / 4
  37. }
  38. func (c *dummyContext) Suspend() error {
  39. return nil
  40. }
  41. func (c *dummyContext) Resume() error {
  42. return nil
  43. }
  44. func (c *dummyContext) Err() error {
  45. return nil
  46. }
  47. func (p *dummyPlayer) Pause() {
  48. p.m.Lock()
  49. p.playing = false
  50. p.m.Unlock()
  51. }
  52. func (p *dummyPlayer) Play() {
  53. p.m.Lock()
  54. p.playing = true
  55. p.m.Unlock()
  56. go func() {
  57. var buf [4096]byte
  58. for {
  59. _, err := p.r.Read(buf[:])
  60. if err != nil {
  61. if err != io.EOF {
  62. panic(err)
  63. }
  64. break
  65. }
  66. time.Sleep(time.Millisecond)
  67. }
  68. p.m.Lock()
  69. p.playing = false
  70. p.m.Unlock()
  71. }()
  72. }
  73. func (p *dummyPlayer) IsPlaying() bool {
  74. p.m.Lock()
  75. defer p.m.Unlock()
  76. return p.playing
  77. }
  78. func (p *dummyPlayer) Volume() float64 {
  79. return p.volume
  80. }
  81. func (p *dummyPlayer) SetVolume(volume float64) {
  82. p.volume = volume
  83. }
  84. func (p *dummyPlayer) BufferedSize() int {
  85. return 0
  86. }
  87. func (p *dummyPlayer) Err() error {
  88. return nil
  89. }
  90. func (p *dummyPlayer) SetBufferSize(bufferSize int) {
  91. }
  92. func (p *dummyPlayer) Seek(offset int64, whence int) (int64, error) {
  93. return 0, nil
  94. }
  95. func (p *dummyPlayer) Close() error {
  96. p.m.Lock()
  97. defer p.m.Unlock()
  98. p.playing = false
  99. return nil
  100. }
  101. func init() {
  102. driverForTesting = &dummyContext{}
  103. }
  104. type dummyHook struct {
  105. updates []func() error
  106. }
  107. func (h *dummyHook) OnSuspendAudio(f func() error) {
  108. }
  109. func (h *dummyHook) OnResumeAudio(f func() error) {
  110. }
  111. func (h *dummyHook) AppendHookOnBeforeUpdate(f func() error) {
  112. h.updates = append(h.updates, f)
  113. }
  114. func init() {
  115. hookerForTesting = &dummyHook{}
  116. }
  117. func UpdateForTesting() error {
  118. for _, f := range hookerForTesting.(*dummyHook).updates {
  119. if err := f(); err != nil {
  120. return err
  121. }
  122. }
  123. return nil
  124. }
  125. func PlayersCountForTesting() int {
  126. c := CurrentContext()
  127. c.m.Lock()
  128. n := len(c.playingPlayers)
  129. c.m.Unlock()
  130. return n
  131. }
  132. func ResetContextForTesting() {
  133. theContext = nil
  134. }
  135. func (i *InfiniteLoop) SetNoBlendForTesting(value bool) {
  136. i.noBlendForTesting = value
  137. }