forked from zikichombo/sound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seek.go
69 lines (58 loc) · 1.73 KB
/
seek.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2018 The ZikiChombo Authors. All rights reserved. Use of this source
// code is governed by a license that can be found in the License file.
package sound
import "time"
// Interface Seeker provides common access to a
// source of sound with seek support.
type Seeker interface {
Form
Pos() int64 // Where we currently are in the source
Len() int64 // Length of the source in frames
Seek(f int64) error // Seek goes to the frame index f
}
// SourceSeeker is a Source with Seek support.
type SourceSeeker interface {
Seeker
Closer
// Receive is as specified in Source.Receive.
Receive(dst []float64) (int, error)
}
// SinkSeeker is a Sink with Seek support.
type SinkSeeker interface {
Seeker
Closer
// Send is as specified as in Sink.Send.
Send(src []float64) error
}
// RandomAccess is an interface for full read/write/seek
// support.
type RandomAccess interface {
Seeker
// Receive is as specified in Source.Receive.
Receive(dst []float64) (int, error)
// Send is as specified as in Sink.Send.
Send(src []float64) error
}
func d2s(v Form, d time.Duration) int64 {
return int64(d / v.SampleRate().Period())
}
func s2d(v Form, f int64) time.Duration {
return v.SampleRate().Period() * time.Duration(f)
}
// When takes a Seeker and returns its position in terms of time.
func When(s Seeker) time.Duration {
return s2d(s, s.Pos())
}
// Duration takes a Seeker and returns its total duration in
// time.
func Duration(s Seeker) time.Duration {
return s2d(s, s.Len())
}
// SeekDur takes a Seeker and seeks to the greatest frame
// not exceeding d time.
//
// SeekDur returns the error given by the call to s.Seek()
// implementing the seek.
func SeekDur(s Seeker, d time.Duration) error {
return s.Seek(d2s(s, d))
}