2017-01-14 13:00:20 +01:00
|
|
|
// Copyright 2017 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 (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2018-07-23 20:35:21 +02:00
|
|
|
// InfiniteLoop represents a looped stream which never ends.
|
2017-01-14 13:00:20 +01:00
|
|
|
type InfiniteLoop struct {
|
2018-07-23 20:35:21 +02:00
|
|
|
src ReadSeekCloser
|
|
|
|
lstart int64
|
|
|
|
llength int64
|
|
|
|
pos int64
|
2017-01-14 13:00:20 +01:00
|
|
|
}
|
|
|
|
|
2018-07-23 20:35:21 +02:00
|
|
|
// NewInfiniteLoop creates a new infinite loop stream with a source stream and length in bytes.
|
|
|
|
func NewInfiniteLoop(src ReadSeekCloser, length int64) *InfiniteLoop {
|
|
|
|
return NewInfiniteLoopWithIntro(src, 0, length)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInfiniteLoopWithIntro creates a new infinite loop stream with an intro part.
|
|
|
|
// NewInfiniteLoopWithIntro accepts a source stream src, introLength in bytes and loopLength in bytes.
|
|
|
|
func NewInfiniteLoopWithIntro(src ReadSeekCloser, introLength int64, loopLength int64) *InfiniteLoop {
|
2017-01-14 13:00:20 +01:00
|
|
|
return &InfiniteLoop{
|
2018-07-23 20:35:21 +02:00
|
|
|
src: src,
|
|
|
|
lstart: introLength,
|
|
|
|
llength: loopLength,
|
|
|
|
pos: -1,
|
2017-01-14 13:00:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 20:35:21 +02:00
|
|
|
func (i *InfiniteLoop) length() int64 {
|
|
|
|
return i.lstart + i.llength
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *InfiniteLoop) ensurePos() error {
|
|
|
|
if i.pos >= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pos, err := i.src.Seek(0, io.SeekCurrent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pos >= i.length() {
|
|
|
|
return fmt.Errorf("audio: stream position must be less than the specified length")
|
|
|
|
}
|
|
|
|
i.pos = pos
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-01 11:07:18 +02:00
|
|
|
// Read is implementation of ReadSeekCloser's Read.
|
2017-01-14 13:00:20 +01:00
|
|
|
func (i *InfiniteLoop) Read(b []byte) (int, error) {
|
2018-07-23 20:35:21 +02:00
|
|
|
if err := i.ensurePos(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.pos+int64(len(b)) > i.length() {
|
|
|
|
b = b[:i.length()-i.pos]
|
|
|
|
}
|
|
|
|
|
2018-07-23 18:22:05 +02:00
|
|
|
n, err := i.src.Read(b)
|
2018-07-23 20:35:21 +02:00
|
|
|
i.pos += int64(n)
|
|
|
|
if i.pos > i.length() {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == io.EOF || i.pos == i.length() {
|
|
|
|
pos, err := i.Seek(i.lstart, io.SeekStart)
|
|
|
|
if err != nil {
|
2017-01-14 13:00:20 +01:00
|
|
|
return 0, err
|
|
|
|
}
|
2018-07-23 20:35:21 +02:00
|
|
|
i.pos = pos
|
2017-01-14 13:00:20 +01:00
|
|
|
}
|
2018-07-23 20:35:21 +02:00
|
|
|
return n, nil
|
2017-01-14 13:00:20 +01:00
|
|
|
}
|
|
|
|
|
2017-10-01 11:07:18 +02:00
|
|
|
// Seek is implementation of ReadSeekCloser's Seek.
|
2017-01-14 13:00:20 +01:00
|
|
|
func (i *InfiniteLoop) Seek(offset int64, whence int) (int64, error) {
|
2018-07-23 20:35:21 +02:00
|
|
|
if err := i.ensurePos(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2017-01-14 13:00:20 +01:00
|
|
|
next := int64(0)
|
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
next = offset
|
|
|
|
case io.SeekCurrent:
|
2018-07-23 20:35:21 +02:00
|
|
|
next = i.pos + offset
|
2017-01-14 13:00:20 +01:00
|
|
|
case io.SeekEnd:
|
2018-07-22 19:28:07 +02:00
|
|
|
return 0, fmt.Errorf("audio: whence must be io.SeekStart or io.SeekCurrent for InfiniteLoop")
|
2017-01-14 13:00:20 +01:00
|
|
|
}
|
2018-07-22 21:01:43 +02:00
|
|
|
if next < 0 {
|
|
|
|
return 0, fmt.Errorf("audio: position must >= 0")
|
|
|
|
}
|
2018-07-23 20:35:21 +02:00
|
|
|
if next >= i.lstart {
|
|
|
|
next = ((next - i.lstart) % i.llength) + i.lstart
|
|
|
|
}
|
|
|
|
// Ignore the new position returned by Seek since the source position might not be match with the position
|
|
|
|
// managed by this.
|
|
|
|
if _, err := i.src.Seek(next, io.SeekStart); err != nil {
|
2017-01-14 13:00:20 +01:00
|
|
|
return 0, err
|
|
|
|
}
|
2018-07-23 20:35:21 +02:00
|
|
|
i.pos = next
|
|
|
|
return i.pos, nil
|
2017-01-14 13:00:20 +01:00
|
|
|
}
|
|
|
|
|
2017-10-01 11:07:18 +02:00
|
|
|
// Close is implementation of ReadSeekCloser's Close.
|
2017-01-14 13:00:20 +01:00
|
|
|
func (l *InfiniteLoop) Close() error {
|
2018-07-23 18:22:05 +02:00
|
|
|
return l.src.Close()
|
2017-01-14 13:00:20 +01:00
|
|
|
}
|