-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
37 lines (33 loc) · 806 Bytes
/
util.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
package audio
import (
"time"
)
// TimeBuffer blocks a speedy caller to be ahead of realtime by max amount.
type TimeBuffer struct {
max time.Duration
start time.Time
elapsed time.Duration
}
// NewTimeBuffer constructor.
func NewTimeBuffer(max time.Duration) *TimeBuffer {
return &TimeBuffer{
max: max,
start: time.Now(),
}
}
// Add an amount of time to buffer the caller thinks has passed.
// It will block if necessary to slow down the caller to be
// ahead of real time by max.
func (tb *TimeBuffer) Add(elapsed time.Duration) {
tb.elapsed += elapsed
if tb.elapsed > tb.max {
allowedAt := time.Now().Add(tb.max)
bufferedAt := tb.start.Add(tb.elapsed)
wait := bufferedAt.Sub(allowedAt)
if wait > 0 {
time.Sleep(wait)
tb.start = time.Now()
tb.elapsed = 0
}
}
}