forked from dhowden/tag
/
mp3.go
244 lines (214 loc) · 5.73 KB
/
mp3.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package tag
import (
"encoding/binary"
"errors"
"io"
"math"
)
// Some documentation :
// http://id3.org/mp3Frame
// http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header
// the number of frames to scan in fast mode
type mp3Infos struct {
Version string
Layer string
Type string
Mode string
Bitrate int
Sampling uint
Size int64
Length float64
vbr int
}
func getMp3Infos(r io.ReadSeeker, slow bool) (*mp3Infos, error) {
h := new(mp3Infos)
var err error
var nbscan, bitrateSum, frameCount int
var pos, start int64
var buf [8]byte
nbscan = 50
// skip the padding at the start
for ; buf[0] == 0; _, err = r.Read(buf[0:1]) {
if err != nil {
return nil, err
}
}
// no more padding, we are now at the start of the actual data
start, err = r.Seek(-1, 1)
if err != nil {
return nil, err
}
// we read the first frame. Maybe a xing header
j, err := r.Read(buf[:4])
if j < 4 || err != nil {
return nil, errors.New("not a MP3 file")
}
offset := h.readHeader(buf)
if offset == 5 {
return nil, errors.New("not a MP3 file")
}
if !(buf[0] == 255 && buf[1] >= 224) {
return nil, errors.New("not a MP3 file")
}
_, err = r.Seek(xingoffset(h.Version, h.Mode), 1)
if err != nil {
return nil, err
}
_, err = r.Read(buf[:8])
if err != nil {
return nil, err
}
if !slow && (string(buf[:4]) == "Xing" || string(buf[:4]) == "Info") {
flags := buf[7]
if (1&flags != 0) && (2&flags != 0) {
var frames, size uint32
binary.Read(r, binary.BigEndian, &frames)
binary.Read(r, binary.BigEndian, &size)
h.Length = float64(frames) * samplePerFrame(h.Version, h.Layer) / float64(h.Sampling)
h.Size = int64(size)
bitrate := getNearestBitrate(float64(h.Size/125)/h.Length, h.Version, h.Layer)
if bitrate != h.Bitrate {
h.Bitrate = bitrate
h.Type = "VBR"
}
return h, nil
}
}
//TODO support VBRI Header and LAME extension
// go to the next frame
_, err = r.Seek(start+offset, 0)
for i := 0; err != io.EOF && (slow || frameCount < nbscan); {
i, err = r.Read(buf[:4])
if i < 4 {
break
}
// looking for the synchronization bits
switch {
case (buf[0] == 255) && (buf[1] >= 224):
// found a valid mp3 frame. we read the header to know where the
// next one is
offset := h.readHeader(buf) - 4
if offset != 1 { // only count valid frames
bitrateSum += h.Bitrate
frameCount++
if h.vbr > 2 {
nbscan = 100 // the file is VBR, use more sample to estimate its size
}
}
pos, _ = r.Seek(offset, 1) // jump to the next frame
case string(buf[:3]) == "TAG":
pos, _ = r.Seek(128-4, 1) // id3v1 tag, bypass it
default:
r.Seek(-3, 1) // looking for the next header
}
}
// Extrapolate the total length base on the nbscan readHeaders
if err == io.EOF {
h.Size = pos
} else {
end, err := r.Seek(0, 2)
if err != nil {
return h, err
}
h.Length = h.Length * float64(end-int64(start)) / float64(pos-int64(start))
h.Size = end
}
// For VBR, choose the closest match
if frameCount > 1 || h.Type == "VBR" {
h.Bitrate = getNearestBitrate(float64(bitrateSum/frameCount), h.Version, h.Layer)
}
return h, nil
}
func getNearestBitrate(s float64, v string, l string) int {
diff := s
result := int(s)
for _, v := range mp3Bitrate[v+l] {
if math.Abs(float64(v)-s) < diff {
result = v
diff = math.Abs(float64(v) - s)
}
}
return result
}
func (h *mp3Infos) readHeader(buf [8]byte) int64 {
v := buf[1] & 24 >> 3
l := buf[1] & 6 >> 1
b := buf[2] & 240 >> 4
s := buf[2] & 12 >> 2
c := buf[3] & 192 >> 6
// if the values are off, try 1 byte after
if l == 0 || b == 15 || v == 1 || b == 0 || s == 3 {
return 5
}
if h.Version == "" {
h.Version = mp3Version[v]
h.Layer = mp3Layer[l]
h.Sampling = mp3Sampling[mp3Version[v]][s]
h.Mode = mp3Channel[c]
h.Type = "CBR"
}
bitrate := mp3Bitrate[mp3Version[v]+mp3Layer[l]][b]
mult := frameLengthMult[mp3Version[v]+mp3Layer[l]]
switch {
case h.vbr > 2:
h.Type = "VBR"
case bitrate != h.Bitrate:
h.vbr++
}
h.Bitrate = bitrate
samples := samplePerFrame(mp3Version[v], mp3Layer[l])
h.Length += samples / float64(h.Sampling)
return int64(mult * bitrate * 1000 / int(h.Sampling))
}
func xingoffset(v string, m string) int64 {
switch {
case v == "2" && m == "mono":
return 9
case v == "1" && m != "mono":
return 32
default:
return 17
}
}
func samplePerFrame(v string, l string) float64 {
switch {
case v == "1" && l == "I":
return 384
case (v == "2" || v == "2.5") && l == "III":
return 576
}
return 1152
}
// constants for decoding frame header
var (
mp3Version = [4]string{"2.5", "x", "2", "1"}
mp3Layer = [4]string{"r", "III", "II", "I"}
mp3Bitrate = map[string][16]int{
"1I": {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448},
"1II": {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},
"1III": {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320},
"2I": {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
"2II": {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
"2III": {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
"2.5I": {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256},
"2.5II": {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
"2.5III": {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},
}
mp3Sampling = map[string][4]uint{
"1": {44100, 48000, 32000, 0},
"2": {22050, 24000, 16000, 0},
"2.5": {11025, 12000, 8000, 0},
}
mp3Channel = [4]string{"Stereo", "Join Stereo", "Dual", "Mono"}
frameLengthMult = map[string]int{
"1I": 48,
"1II": 144,
"1III": 144,
"2I": 24,
"2II": 144,
"2III": 72,
"2.5I": 24,
"2.5II": 72,
"2.5III": 144,
}
)