audio: rename constants

Updates #2630
This commit is contained in:
Hajime Hoshi 2023-04-08 19:14:24 +09:00
parent 6e22ebb73a
commit 85f8423345
4 changed files with 22 additions and 22 deletions

View File

@ -47,8 +47,8 @@ import (
const ( const (
channelCount = 2 channelCount = 2
bitDepthInBytes = 2 bitDepthInBytesInt16 = 2
bytesPerSample = bitDepthInBytes * channelCount bytesPerSampleInt16 = bitDepthInBytesInt16 * channelCount
) )
// A Context represents a current state of audio. // A Context represents a current state of audio.

View File

@ -24,7 +24,7 @@ func newContext(sampleRate int) (context, chan struct{}, error) {
ctx, ready, err := oto.NewContextWithOptions(&oto.NewContextOptions{ ctx, ready, err := oto.NewContextWithOptions(&oto.NewContextOptions{
SampleRate: sampleRate, SampleRate: sampleRate,
ChannelCount: channelCount, ChannelCount: channelCount,
Format: bitDepthInBytes, Format: oto.FormatSignedInt16LE,
}) })
err = addErrorInfoForContextCreation(err) err = addErrorInfoForContextCreation(err)
return &contextProxy{ctx}, ready, err return &contextProxy{ctx}, ready, err

View File

@ -26,7 +26,7 @@ type InfiniteLoop struct {
llength int64 llength int64
pos int64 pos int64
// extra is the remainder in the case when the read byte sizes are not multiple of bitDepthInBytes. // extra is the remainder in the case when the read byte sizes are not multiple of bitDepthInBytesInt16.
extra []byte extra []byte
// afterLoop is data after the loop. // afterLoop is data after the loop.
@ -60,8 +60,8 @@ func NewInfiniteLoop(src io.ReadSeeker, length int64) *InfiniteLoop {
func NewInfiniteLoopWithIntro(src io.ReadSeeker, introLength int64, loopLength int64) *InfiniteLoop { func NewInfiniteLoopWithIntro(src io.ReadSeeker, introLength int64, loopLength int64) *InfiniteLoop {
return &InfiniteLoop{ return &InfiniteLoop{
src: src, src: src,
lstart: introLength / bytesPerSample * bytesPerSample, lstart: introLength / bytesPerSampleInt16 * bytesPerSampleInt16,
llength: loopLength / bytesPerSample * bytesPerSample, llength: loopLength / bytesPerSampleInt16 * bytesPerSampleInt16,
pos: -1, pos: -1,
} }
} }
@ -92,8 +92,8 @@ func (i *InfiniteLoop) blendRate(pos int64) float64 {
if pos >= i.lstart+int64(len(i.afterLoop)) { if pos >= i.lstart+int64(len(i.afterLoop)) {
return 0 return 0
} }
p := (pos - i.lstart) / bytesPerSample p := (pos - i.lstart) / bytesPerSampleInt16
l := len(i.afterLoop) / bytesPerSample l := len(i.afterLoop) / bytesPerSampleInt16
return 1 - float64(p)/float64(l) return 1 - float64(p)/float64(l)
} }
@ -119,7 +119,7 @@ func (i *InfiniteLoop) Read(b []byte) (int, error) {
} }
// Save the remainder part to extra. This will be used at the next Read. // Save the remainder part to extra. This will be used at the next Read.
if rem := n % bitDepthInBytes; rem != 0 { if rem := n % bitDepthInBytesInt16; rem != 0 {
i.extra = append(i.extra, b[n-rem:n]...) i.extra = append(i.extra, b[n-rem:n]...)
b = b[:n-rem] b = b[:n-rem]
n = n - rem n = n - rem
@ -128,17 +128,17 @@ func (i *InfiniteLoop) Read(b []byte) (int, error) {
// Blend afterLoop and the loop start to reduce noises (#1888). // 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. // 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)) { if !i.noBlendForTesting && i.blending && i.pos >= i.lstart && i.pos-int64(n) < i.lstart+int64(len(i.afterLoop)) {
if n%bitDepthInBytes != 0 { if n%bitDepthInBytesInt16 != 0 {
panic(fmt.Sprintf("audio: n must be a multiple of bitDepthInBytes but not: %d", n)) panic(fmt.Sprintf("audio: n must be a multiple of bitDepthInBytesInt16 but not: %d", n))
} }
for idx := 0; idx < n/bitDepthInBytes; idx++ { for idx := 0; idx < n/bitDepthInBytesInt16; idx++ {
abspos := i.pos - int64(n) + int64(idx)*bitDepthInBytes abspos := i.pos - int64(n) + int64(idx)*bitDepthInBytesInt16
rate := i.blendRate(abspos) rate := i.blendRate(abspos)
if rate == 0 { if rate == 0 {
continue continue
} }
// This assumes that bitDepthInBytes is 2. // This assumes that bitDepthInBytesInt16 is 2.
relpos := abspos - i.lstart relpos := abspos - i.lstart
afterLoop := int16(i.afterLoop[relpos]) | (int16(i.afterLoop[relpos+1]) << 8) afterLoop := int16(i.afterLoop[relpos]) | (int16(i.afterLoop[relpos+1]) << 8)
orig := int16(b[2*idx]) | (int16(b[2*idx+1]) << 8) orig := int16(b[2*idx]) | (int16(b[2*idx+1]) << 8)
@ -156,7 +156,7 @@ func (i *InfiniteLoop) Read(b []byte) (int, error) {
// Read the afterLoop part if necessary. // Read the afterLoop part if necessary.
if i.pos == i.length() && err == nil { if i.pos == i.length() && err == nil {
if i.afterLoop == nil { if i.afterLoop == nil {
buflen := int64(256 * bytesPerSample) buflen := int64(256 * bytesPerSampleInt16)
if buflen > i.length() { if buflen > i.length() {
buflen = i.length() buflen = i.length()
} }

View File

@ -251,7 +251,7 @@ func (p *playerImpl) Current() time.Duration {
return 0 return 0
} }
samples := (p.stream.Current() - int64(p.player.UnplayedBufferSize())) / bytesPerSample samples := (p.stream.Current() - int64(p.player.UnplayedBufferSize())) / bytesPerSampleInt16
return time.Duration(samples) * time.Second / time.Duration(p.factory.sampleRate) return time.Duration(samples) * time.Second / time.Duration(p.factory.sampleRate)
} }
@ -288,8 +288,8 @@ func (p *playerImpl) SetBufferSize(bufferSize time.Duration) {
p.m.Lock() p.m.Lock()
defer p.m.Unlock() defer p.m.Unlock()
bufferSizeInBytes := int(bufferSize * bytesPerSample * time.Duration(p.factory.sampleRate) / time.Second) bufferSizeInBytes := int(bufferSize * bytesPerSampleInt16 * time.Duration(p.factory.sampleRate) / time.Second)
bufferSizeInBytes = bufferSizeInBytes / bytesPerSample * bytesPerSample bufferSizeInBytes = bufferSizeInBytes / bytesPerSampleInt16 * bytesPerSampleInt16
if p.player == nil { if p.player == nil {
p.initBufferSize = bufferSizeInBytes p.initBufferSize = bufferSizeInBytes
return return
@ -358,11 +358,11 @@ func (s *timeStream) timeDurationToPos(offset time.Duration) int64 {
s.m.Lock() s.m.Lock()
defer s.m.Unlock() defer s.m.Unlock()
o := int64(offset) * bytesPerSample * int64(s.sampleRate) / int64(time.Second) o := int64(offset) * bytesPerSampleInt16 * int64(s.sampleRate) / int64(time.Second)
// Align the byte position with the samples. // Align the byte position with the samples.
o -= o % bytesPerSample o -= o % bytesPerSampleInt16
o += s.pos % bytesPerSample o += s.pos % bytesPerSampleInt16
return o return o
} }