audio: remove locks from (*timeStream).position

This commit is contained in:
Hajime Hoshi 2024-11-06 22:12:16 +09:00
parent 0981355b61
commit 20014ad5bc

View File

@ -389,7 +389,7 @@ type timeStream struct {
r io.Reader r io.Reader
seekable bool seekable bool
sampleRate int sampleRate int
pos int64 pos atomic.Int64
bytesPerSample int bytesPerSample int
// m is a mutex for this stream. // m is a mutex for this stream.
@ -410,7 +410,7 @@ func newTimeStream(r io.Reader, seekable bool, sampleRate int, bitDepthInBytes i
if err != nil { if err != nil {
return nil, err return nil, err
} }
s.pos = pos s.pos.Store(pos)
} }
return s, nil return s, nil
} }
@ -420,7 +420,7 @@ func (s *timeStream) Read(buf []byte) (int, error) {
defer s.m.Unlock() defer s.m.Unlock()
n, err := s.r.Read(buf) n, err := s.r.Read(buf)
s.pos += int64(n) s.pos.Add(int64(n))
return n, err return n, err
} }
@ -437,33 +437,24 @@ func (s *timeStream) Seek(offset int64, whence int) (int64, error) {
return pos, err return pos, err
} }
s.pos = pos s.pos.Store(pos)
return pos, nil return pos, nil
} }
func (s *timeStream) timeDurationToPos(offset time.Duration) int64 { func (s *timeStream) timeDurationToPos(offset time.Duration) int64 {
s.m.Lock()
defer s.m.Unlock()
o := int64(offset) * int64(s.bytesPerSample) * int64(s.sampleRate) / int64(time.Second) o := int64(offset) * int64(s.bytesPerSample) * int64(s.sampleRate) / int64(time.Second)
// Align the byte position with the samples. // Align the byte position with the samples.
o -= o % int64(s.bytesPerSample) o -= o % int64(s.bytesPerSample)
o += s.pos % int64(s.bytesPerSample) o += s.pos.Load() % int64(s.bytesPerSample)
return o return o
} }
func (s *timeStream) position() int64 { func (s *timeStream) position() int64 {
s.m.Lock() return s.pos.Load()
defer s.m.Unlock()
return s.pos
} }
func (s *timeStream) positionInTimeDuration() time.Duration { func (s *timeStream) positionInTimeDuration() time.Duration {
s.m.Lock() return time.Duration(s.pos.Load()) * time.Second / (time.Duration(s.sampleRate * s.bytesPerSample))
defer s.m.Unlock()
return time.Duration(s.pos) * time.Second / (time.Duration(s.sampleRate * s.bytesPerSample))
} }