audio: wav: more precise Position and Seek
This commit is contained in:
parent
7624d11cfc
commit
9a6fbad6ce
|
@ -94,15 +94,14 @@ func (s *decoder) Duration() time.Duration {
|
||||||
|
|
||||||
func (s *decoder) Position() time.Duration {
|
func (s *decoder) Position() time.Duration {
|
||||||
frameIndex := time.Duration(s.pos / int32(s.h.BytesPerFrame))
|
frameIndex := time.Duration(s.pos / int32(s.h.BytesPerFrame))
|
||||||
frameTime := time.Second / time.Duration(s.h.SampleRate)
|
return frameIndex * time.Second / time.Duration(s.h.SampleRate)
|
||||||
return frameIndex * frameTime
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *decoder) Seek(d time.Duration) error {
|
func (s *decoder) Seek(d time.Duration) error {
|
||||||
if d < 0 || s.Duration() < d {
|
if d < 0 || s.Duration() < d {
|
||||||
return fmt.Errorf("wav: seek duration %v out of range [%v, %v]", d, 0, s.Duration())
|
return fmt.Errorf("wav: seek duration %v out of range [%v, %v]", d, 0, s.Duration())
|
||||||
}
|
}
|
||||||
frame := int32(d / (time.Second / time.Duration(s.h.SampleRate)))
|
frame := int32(d * time.Duration(s.h.SampleRate) / time.Second)
|
||||||
pos := frame * int32(s.h.BytesPerFrame)
|
pos := frame * int32(s.h.BytesPerFrame)
|
||||||
_, err := s.rsc.Seek(int64(pos)+44, io.SeekStart) // 44 is the size of the header
|
_, err := s.rsc.Seek(int64(pos)+44, io.SeekStart) // 44 is the size of the header
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue