/
mpeg.go
89 lines (70 loc) · 1.66 KB
/
mpeg.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package mpeg
// FIXED
import (
"os"
"time"
"github.com/tcolgate/mp3"
)
// Info holds the information.
// Don't print the whole thing. It's going to be bad.
type Info struct {
BitRate float64
SPF int
SampleRate int
Channels int
NumberOfFrames int
Skipped int
FramesToRead int
Frames []mp3.Frame
}
// GetInfo get the needed information about filename.
func GetInfo(filename string) (info Info, e error) {
r, err := os.Open(filename)
if err != nil {
e = err
return
}
d := mp3.NewDecoder(r)
skipped := 0
var first mp3.Frame
for {
s := 0
if err := d.Decode(&first, &s); err != nil {
if err.Error() != "EOF" {
e = err
return
}
break
}
info.Frames = append(info.Frames, first)
if s > skipped {
skipped = s
}
}
info.BitRate = float64(first.Header().BitRate()) / 1000
info.SampleRate = int(first.Header().SampleRate())
info.SPF = first.Samples()
info.NumberOfFrames = len(info.Frames)
info.Skipped = skipped
info.FramesToRead = (info.SampleRate / info.SPF) + 1
switch first.Header().ChannelMode().String() {
case "Stereo":
info.Channels = 2
case "Joint Stereo":
info.Channels = 1
case "Dual Channel":
info.Channels = 2
case "Single Channel":
info.Channels = 1
}
return
}
// TimeBetweenTracks returns the time to pause between tracks.
func (i Info) TimeBetweenTracks(began time.Time) time.Duration {
f := float64(i.NumberOfFrames)
spf := float64(i.SPF)
sr := float64(i.SampleRate)
s := float64((time.Now().Sub(began)).Seconds())
timeBetweenTracks := int(((f*spf)/sr)*1000) - int(s*1000)
return time.Duration(time.Millisecond) * time.Duration(timeBetweenTracks)
}