From 85f8423345b921156ddf5761a1aebd8df3bd95e3 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 8 Apr 2023 19:14:24 +0900 Subject: [PATCH] audio: rename constants Updates #2630 --- audio/audio.go | 6 +++--- audio/context.go | 2 +- audio/loop.go | 24 ++++++++++++------------ audio/player.go | 12 ++++++------ 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/audio/audio.go b/audio/audio.go index 3d163c89c..969f212d9 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -46,9 +46,9 @@ import ( ) const ( - channelCount = 2 - bitDepthInBytes = 2 - bytesPerSample = bitDepthInBytes * channelCount + channelCount = 2 + bitDepthInBytesInt16 = 2 + bytesPerSampleInt16 = bitDepthInBytesInt16 * channelCount ) // A Context represents a current state of audio. diff --git a/audio/context.go b/audio/context.go index e2d173085..106e125f4 100644 --- a/audio/context.go +++ b/audio/context.go @@ -24,7 +24,7 @@ func newContext(sampleRate int) (context, chan struct{}, error) { ctx, ready, err := oto.NewContextWithOptions(&oto.NewContextOptions{ SampleRate: sampleRate, ChannelCount: channelCount, - Format: bitDepthInBytes, + Format: oto.FormatSignedInt16LE, }) err = addErrorInfoForContextCreation(err) return &contextProxy{ctx}, ready, err diff --git a/audio/loop.go b/audio/loop.go index 09f37a848..6ab3b4477 100644 --- a/audio/loop.go +++ b/audio/loop.go @@ -26,7 +26,7 @@ type InfiniteLoop struct { llength 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 // 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 { return &InfiniteLoop{ src: src, - lstart: introLength / bytesPerSample * bytesPerSample, - llength: loopLength / bytesPerSample * bytesPerSample, + lstart: introLength / bytesPerSampleInt16 * bytesPerSampleInt16, + llength: loopLength / bytesPerSampleInt16 * bytesPerSampleInt16, pos: -1, } } @@ -92,8 +92,8 @@ func (i *InfiniteLoop) blendRate(pos int64) float64 { if pos >= i.lstart+int64(len(i.afterLoop)) { return 0 } - p := (pos - i.lstart) / bytesPerSample - l := len(i.afterLoop) / bytesPerSample + p := (pos - i.lstart) / bytesPerSampleInt16 + l := len(i.afterLoop) / bytesPerSampleInt16 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. - if rem := n % bitDepthInBytes; rem != 0 { + if rem := n % bitDepthInBytesInt16; rem != 0 { i.extra = append(i.extra, b[n-rem:n]...) b = b[: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). // 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 n%bitDepthInBytes != 0 { - panic(fmt.Sprintf("audio: n must be a multiple of bitDepthInBytes but not: %d", n)) + if n%bitDepthInBytesInt16 != 0 { + panic(fmt.Sprintf("audio: n must be a multiple of bitDepthInBytesInt16 but not: %d", n)) } - for idx := 0; idx < n/bitDepthInBytes; idx++ { - abspos := i.pos - int64(n) + int64(idx)*bitDepthInBytes + for idx := 0; idx < n/bitDepthInBytesInt16; idx++ { + abspos := i.pos - int64(n) + int64(idx)*bitDepthInBytesInt16 rate := i.blendRate(abspos) if rate == 0 { continue } - // This assumes that bitDepthInBytes is 2. + // This assumes that bitDepthInBytesInt16 is 2. relpos := abspos - i.lstart afterLoop := int16(i.afterLoop[relpos]) | (int16(i.afterLoop[relpos+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. if i.pos == i.length() && err == nil { if i.afterLoop == nil { - buflen := int64(256 * bytesPerSample) + buflen := int64(256 * bytesPerSampleInt16) if buflen > i.length() { buflen = i.length() } diff --git a/audio/player.go b/audio/player.go index 7db66b380..f10e2f092 100644 --- a/audio/player.go +++ b/audio/player.go @@ -251,7 +251,7 @@ func (p *playerImpl) Current() time.Duration { 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) } @@ -288,8 +288,8 @@ func (p *playerImpl) SetBufferSize(bufferSize time.Duration) { p.m.Lock() defer p.m.Unlock() - bufferSizeInBytes := int(bufferSize * bytesPerSample * time.Duration(p.factory.sampleRate) / time.Second) - bufferSizeInBytes = bufferSizeInBytes / bytesPerSample * bytesPerSample + bufferSizeInBytes := int(bufferSize * bytesPerSampleInt16 * time.Duration(p.factory.sampleRate) / time.Second) + bufferSizeInBytes = bufferSizeInBytes / bytesPerSampleInt16 * bytesPerSampleInt16 if p.player == nil { p.initBufferSize = bufferSizeInBytes return @@ -358,11 +358,11 @@ func (s *timeStream) timeDurationToPos(offset time.Duration) int64 { s.m.Lock() 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. - o -= o % bytesPerSample - o += s.pos % bytesPerSample + o -= o % bytesPerSampleInt16 + o += s.pos % bytesPerSampleInt16 return o }