loop.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2017 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. "fmt"
  17. "io"
  18. )
  19. // InfiniteLoop represents a looped stream which never ends.
  20. type InfiniteLoop struct {
  21. src io.ReadSeeker
  22. lstart int64
  23. llength int64
  24. pos int64
  25. }
  26. // NewInfiniteLoop creates a new infinite loop stream with a source stream and length in bytes.
  27. func NewInfiniteLoop(src io.ReadSeeker, length int64) *InfiniteLoop {
  28. return NewInfiniteLoopWithIntro(src, 0, length)
  29. }
  30. // NewInfiniteLoopWithIntro creates a new infinite loop stream with an intro part.
  31. // NewInfiniteLoopWithIntro accepts a source stream src, introLength in bytes and loopLength in bytes.
  32. func NewInfiniteLoopWithIntro(src io.ReadSeeker, introLength int64, loopLength int64) *InfiniteLoop {
  33. return &InfiniteLoop{
  34. src: src,
  35. lstart: introLength / bytesPerSample * bytesPerSample,
  36. llength: loopLength / bytesPerSample * bytesPerSample,
  37. pos: -1,
  38. }
  39. }
  40. func (i *InfiniteLoop) length() int64 {
  41. return i.lstart + i.llength
  42. }
  43. func (i *InfiniteLoop) ensurePos() error {
  44. if i.pos >= 0 {
  45. return nil
  46. }
  47. pos, err := i.src.Seek(0, io.SeekCurrent)
  48. if err != nil {
  49. return err
  50. }
  51. if pos >= i.length() {
  52. return fmt.Errorf("audio: stream position must be less than the specified length")
  53. }
  54. i.pos = pos
  55. return nil
  56. }
  57. // Read is implementation of ReadSeekCloser's Read.
  58. func (i *InfiniteLoop) Read(b []byte) (int, error) {
  59. if err := i.ensurePos(); err != nil {
  60. return 0, err
  61. }
  62. if i.pos+int64(len(b)) > i.length() {
  63. b = b[:i.length()-i.pos]
  64. }
  65. n, err := i.src.Read(b)
  66. i.pos += int64(n)
  67. if i.pos > i.length() {
  68. panic(fmt.Sprintf("audio: position must be <= length but not at (*InfiniteLoop).Read: pos: %d, length: %d", i.pos, i.length()))
  69. }
  70. if err != nil && err != io.EOF {
  71. return 0, err
  72. }
  73. if err == io.EOF || i.pos == i.length() {
  74. pos, err := i.Seek(i.lstart, io.SeekStart)
  75. if err != nil {
  76. return 0, err
  77. }
  78. i.pos = pos
  79. }
  80. return n, nil
  81. }
  82. // Seek is implementation of ReadSeekCloser's Seek.
  83. func (i *InfiniteLoop) Seek(offset int64, whence int) (int64, error) {
  84. if err := i.ensurePos(); err != nil {
  85. return 0, err
  86. }
  87. next := int64(0)
  88. switch whence {
  89. case io.SeekStart:
  90. next = offset
  91. case io.SeekCurrent:
  92. next = i.pos + offset
  93. case io.SeekEnd:
  94. return 0, fmt.Errorf("audio: whence must be io.SeekStart or io.SeekCurrent for InfiniteLoop")
  95. }
  96. if next < 0 {
  97. return 0, fmt.Errorf("audio: position must >= 0")
  98. }
  99. if next >= i.lstart {
  100. next = ((next - i.lstart) % i.llength) + i.lstart
  101. }
  102. // Ignore the new position returned by Seek since the source position might not be match with the position
  103. // managed by this.
  104. if _, err := i.src.Seek(next, io.SeekStart); err != nil {
  105. return 0, err
  106. }
  107. i.pos = next
  108. return i.pos, nil
  109. }