forked from jacereda/webm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadecoder.go
68 lines (61 loc) · 1.3 KB
/
adecoder.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
package webm
import (
"github.com/jacereda/ffvorbis"
"time"
)
type Samples struct {
Data []float32
Timecode time.Duration
Rebase bool
EOS bool
}
type AudioDecoder struct {
Chan chan Samples
dec *ffvorbis.Decoder
goodtc time.Duration
duration int
emitted int
chans int
}
func NewAudioDecoder(track *TrackEntry) *AudioDecoder {
return &AudioDecoder{
Chan: make(chan Samples, 4),
dec: ffvorbis.NewDecoder(track.CodecPrivate,
int(track.Channels),
int(track.SamplingFrequency)),
duration: int(time.Duration(time.Second) /
time.Duration(track.Audio.SamplingFrequency)),
chans: int(track.Channels),
}
}
func (d *AudioDecoder) estimate() time.Duration {
return d.goodtc + time.Duration(d.duration*d.emitted)
}
func (d *AudioDecoder) Decode(pkt *Packet) bool {
sent := false
var data []float32
if pkt.Data == nil {
eos := Samples{nil, BadTC, false, true}
d.Chan <- eos
} else {
data = d.dec.Decode(pkt.Data)
}
if data != nil {
smp := Samples{data, pkt.Timecode, pkt.Rebase, false}
if smp.Timecode == BadTC {
smp.Timecode = d.estimate()
} else {
d.goodtc = smp.Timecode
d.emitted = 0
}
d.emitted += len(smp.Data) / d.chans
if !pkt.Invisible {
d.Chan <- smp
sent = true
}
}
return sent
}
func (d *AudioDecoder) Close() {
close(d.Chan)
}