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 {
|
2024-07-06 16:43:27 +02:00
|
|
|
src io.ReadSeeker
|
|
|
|
lstart int64
|
|
|
|
llength int64
|
|
|
|
pos int64
|
|
|
|
bitDepthInBytes int
|
|
|
|
bytesPerSample int
|
|
|
|
|
|
|
|
// extra is the remainder in the case when the read byte sizes are not multiple of the bit depth.
|
2021-11-29 15:01:19 +01:00
|
|
|
extra []byte
|
|
|
|
|
|
|
|
// afterLoop is data after the loop.
|
|
|
|
afterLoop []byte
|
|
|
|
|
|
|
|
// blending represents whether the loop start and afterLoop are blended or not.
|
|
|
|
blending bool
|
|
|
|
|
|
|
|
noBlendForTesting bool
|
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.
|
2021-11-30 13:56:44 +01:00
|
|
|
//
|
|
|
|
// If the loop's total length is exactly the same as src's length, you might hear noises around the loop joint.
|
|
|
|
// This noise can be heard especially when src is decoded from a lossy compression format like Ogg/Vorbis and MP3.
|
|
|
|
// In this case, try to add more (about 0.1[s]) data to src after the loop end.
|
|
|
|
// If src has data after the loop end, an InfiniteLoop uses part of the data to blend with the loop start
|
|
|
|
// to make the loop joint smooth.
|
2020-10-07 16:09:02 +02:00
|
|
|
func NewInfiniteLoop(src io.ReadSeeker, length int64) *InfiniteLoop {
|
2018-07-23 20:35:21 +02:00
|
|
|
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.
|
2021-11-29 16:28:40 +01:00
|
|
|
//
|
|
|
|
// If the loop's total length is exactly the same as src's length, you might hear noises around the loop joint.
|
|
|
|
// This noise can be heard especially when src is decoded from a lossy compression format like Ogg/Vorbis and MP3.
|
|
|
|
// In this case, try to add more (about 0.1[s]) data to src after the loop end.
|
|
|
|
// If src has data after the loop end, an InfiniteLoop uses part of the data to blend with the loop start
|
|
|
|
// to make the loop joint smooth.
|
2020-10-07 16:09:02 +02:00
|
|
|
func NewInfiniteLoopWithIntro(src io.ReadSeeker, introLength int64, loopLength int64) *InfiniteLoop {
|
2024-07-06 16:43:27 +02:00
|
|
|
return newInfiniteLoopWithIntro(src, introLength, loopLength, bitDepthInBytesInt16)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInfiniteLoopWithIntro(src io.ReadSeeker, introLength int64, loopLength int64, bitDepthInBytes int) *InfiniteLoop {
|
|
|
|
bytesPerSample := bitDepthInBytes * channelCount
|
2017-01-14 13:00:20 +01:00
|
|
|
return &InfiniteLoop{
|
2024-07-06 16:43:27 +02:00
|
|
|
src: src,
|
|
|
|
lstart: introLength / int64(bytesPerSample) * int64(bytesPerSample),
|
|
|
|
llength: loopLength / int64(bytesPerSample) * int64(bytesPerSample),
|
|
|
|
pos: -1,
|
|
|
|
bitDepthInBytes: bitDepthInBytes,
|
|
|
|
bytesPerSample: bytesPerSample,
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-29 15:01:19 +01:00
|
|
|
func (i *InfiniteLoop) blendRate(pos int64) float64 {
|
|
|
|
if pos < i.lstart {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if pos >= i.lstart+int64(len(i.afterLoop)) {
|
|
|
|
return 0
|
|
|
|
}
|
2024-07-06 16:43:27 +02:00
|
|
|
p := (pos - i.lstart) / int64(i.bytesPerSample)
|
|
|
|
l := len(i.afterLoop) / i.bytesPerSample
|
2021-11-29 15:01:19 +01:00
|
|
|
return 1 - float64(p)/float64(l)
|
|
|
|
}
|
|
|
|
|
2021-11-09 17:27:11 +01:00
|
|
|
// Read is implementation of ReadSeeker'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]
|
|
|
|
}
|
|
|
|
|
2021-11-29 15:01:19 +01:00
|
|
|
extralen := len(i.extra)
|
|
|
|
copy(b, i.extra)
|
|
|
|
i.extra = i.extra[:0]
|
|
|
|
|
|
|
|
n, err := i.src.Read(b[extralen:])
|
|
|
|
n += extralen
|
2018-07-23 20:35:21 +02:00
|
|
|
i.pos += int64(n)
|
|
|
|
if i.pos > i.length() {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic(fmt.Sprintf("audio: position must be <= length but not at (*InfiniteLoop).Read: pos: %d, length: %d", i.pos, i.length()))
|
2018-07-23 20:35:21 +02:00
|
|
|
}
|
|
|
|
|
2021-11-29 15:01:19 +01:00
|
|
|
// Save the remainder part to extra. This will be used at the next Read.
|
2024-07-06 16:43:27 +02:00
|
|
|
if rem := n % i.bitDepthInBytes; rem != 0 {
|
2021-11-29 15:01:19 +01:00
|
|
|
i.extra = append(i.extra, b[n-rem:n]...)
|
|
|
|
b = b[:n-rem]
|
|
|
|
n = n - rem
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blend afterLoop and the loop start to reduce noises (#1888).
|
|
|
|
// Ideally, afterLoop and the loop start should be identical, but they can have very slight differences.
|
|
|
|
if !i.noBlendForTesting && i.blending && i.pos >= i.lstart && i.pos-int64(n) < i.lstart+int64(len(i.afterLoop)) {
|
2024-07-06 16:43:27 +02:00
|
|
|
if n%i.bitDepthInBytes != 0 {
|
|
|
|
panic(fmt.Sprintf("audio: n must be a multiple of bit depth %d [bytes] but not: %d", i.bitDepthInBytes, n))
|
2021-11-29 15:01:19 +01:00
|
|
|
}
|
2024-07-06 16:43:27 +02:00
|
|
|
for idx := 0; idx < n/i.bitDepthInBytes; idx++ {
|
|
|
|
abspos := i.pos - int64(n) + int64(idx)*int64(i.bitDepthInBytes)
|
2021-11-29 15:01:19 +01:00
|
|
|
rate := i.blendRate(abspos)
|
|
|
|
if rate == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
relpos := abspos - i.lstart
|
2024-07-06 16:43:27 +02:00
|
|
|
switch i.bitDepthInBytes {
|
|
|
|
case 2:
|
|
|
|
afterLoop := int16(i.afterLoop[relpos]) | (int16(i.afterLoop[relpos+1]) << 8)
|
|
|
|
orig := int16(b[2*idx]) | (int16(b[2*idx+1]) << 8)
|
|
|
|
newval := int16(float64(afterLoop)*rate + float64(orig)*(1-rate))
|
|
|
|
b[2*idx] = byte(newval)
|
|
|
|
b[2*idx+1] = byte(newval >> 8)
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
2021-11-29 15:01:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-23 20:35:21 +02:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-11-29 15:01:19 +01:00
|
|
|
// Read the afterLoop part if necessary.
|
|
|
|
if i.pos == i.length() && err == nil {
|
|
|
|
if i.afterLoop == nil {
|
2024-07-06 16:43:27 +02:00
|
|
|
buflen := int64(256 * i.bytesPerSample)
|
2021-11-29 15:01:19 +01:00
|
|
|
if buflen > i.length() {
|
|
|
|
buflen = i.length()
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, buflen)
|
|
|
|
pos := 0
|
|
|
|
for pos < len(buf) {
|
|
|
|
n, err := i.src.Read(buf[pos:])
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
pos += n
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i.afterLoop = buf[:pos]
|
|
|
|
}
|
|
|
|
if len(i.afterLoop) > 0 {
|
|
|
|
i.blending = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.pos == i.length() || err == io.EOF {
|
2021-11-29 12:56:39 +01:00
|
|
|
// 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(i.lstart, io.SeekStart); err != nil {
|
2017-01-14 13:00:20 +01:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-11-29 12:56:39 +01:00
|
|
|
i.pos = i.lstart
|
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
|
|
|
}
|
|
|
|
|
2021-11-09 17:27:11 +01:00
|
|
|
// Seek is implementation of ReadSeeker's Seek.
|
2017-01-14 13:00:20 +01:00
|
|
|
func (i *InfiniteLoop) Seek(offset int64, whence int) (int64, error) {
|
2021-11-29 15:01:19 +01:00
|
|
|
i.blending = false
|
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")
|
|
|
|
}
|
2021-11-29 12:56:39 +01:00
|
|
|
if next > i.lstart {
|
2018-07-23 20:35:21 +02:00
|
|
|
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
|
|
|
}
|