-
Notifications
You must be signed in to change notification settings - Fork 7
/
Audio.go
53 lines (46 loc) · 1.33 KB
/
Audio.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
package movie
import (
"github.com/inkyblackness/hacked/ss1/content/audio"
"github.com/inkyblackness/hacked/ss1/content/movie/internal/format"
)
const audioEntrySize = 0x2000
// Audio represents the sound part of a movie.
type Audio struct {
Sound audio.L8
}
func (a Audio) duration() format.Timestamp {
if a.Sound.SampleRate <= 0 {
return format.Timestamp{}
}
return format.TimestampFromSeconds(a.Sound.Duration())
}
func (a Audio) encode() []format.EntryBucket {
buckets := make([]format.EntryBucket, 0, (len(a.Sound.Samples)/audioEntrySize)+1)
addBucket := func(ts format.Timestamp, samples []byte) {
buckets = append(buckets,
format.EntryBucket{
Priority: format.EntryBucketPriorityAudio,
Timestamp: ts,
Entries: []format.Entry{
{
Timestamp: ts,
Data: format.AudioEntryData{
Samples: samples,
},
},
},
})
}
startOffset := 0
for (startOffset + audioEntrySize) <= len(a.Sound.Samples) {
ts := format.TimestampFromSeconds(float32(startOffset) / a.Sound.SampleRate)
endOffset := startOffset + audioEntrySize
addBucket(ts, a.Sound.Samples[startOffset:endOffset])
startOffset = endOffset
}
if startOffset < len(a.Sound.Samples) {
ts := format.TimestampFromSeconds(float32(startOffset) / a.Sound.SampleRate)
addBucket(ts, a.Sound.Samples[startOffset:])
}
return buckets
}