forked from nestorInc/jukebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3.rb
177 lines (155 loc) · 4.61 KB
/
mp3.rb
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
#!/usr/bin/env ruby
class Mp3Frame
attr_reader :version
attr_reader :layer
attr_reader :protection
attr_reader :bitrate
attr_reader :samplerate
attr_reader :padding
attr_reader :hprivate
attr_reader :channel
attr_reader :extension
attr_reader :copyright
attr_reader :original
attr_reader :emphasis
attr_reader :duration
def to_s()
@frame;
end
def Mp3Frame.fetch(data)
v = data.unpack("N")[0];
sync = (v >> 21) & 0x7FF;
return nil if(sync != 0x7FF);
version = (v >> 19) & 0x003;
layer = (v >> 17) & 0x003;
protection = (v >> 16) & 0x001;
bitrate = (v >> 12) & 0x00F;
samplerate = (v >> 10) & 0x003;
padding = (v >> 9) & 0x001;
hprivate = (v >> 8) & 0x001;
channel = (v >> 6) & 0x003;
extension = (v >> 4) & 0x003;
copyright = (v >> 3) & 0x001;
original = (v >> 2) & 0x001;
emphasis = (v >> 0) & 0x003;
if(version == MPEG1_VERSION_RESERVED ||
layer == MPEG1_LAYER_RESERVED ||
samplerate == 3)
return nil;
end
layer = case(layer)
when MPEG1_LAYER_1 then 1;
when MPEG1_LAYER_2 then 2;
when MPEG1_LAYER_3 then 3;
end
samplerate = case(samplerate)
when 0 then 44100;
when 1 then 48000;
when 2 then 32000;
end
case(version)
when MPEG1_VERSION_1
version = 1;
samplerate = samplerate;
when MPEG1_VERSION_2
version = 2;
samplerate = samplerate >> 1;
when MPEG1_VERSION_2_5
version = 2;
samplerate = samplerate >> 2;
end
bitrate = BITRATE_TABLE[bitrate][(layer-1)+((version-1)*3)];
return nil if(bitrate == 0);
if(layer == 1)
size = (12 * bitrate / samplerate + padding) * 4;
else
size = 144 * bitrate / samplerate + padding;
end
return nil if(data.bytesize < size);
frame = data[0..size-1];
data.replace(data[size..-1]);
Mp3Frame.new(version,
layer,
protection,
bitrate,
samplerate,
padding,
hprivate,
channel,
extension,
copyright,
original,
emphasis,
frame);
end
private
MPEG1_VERSION_1 = 3;
MPEG1_VERSION_2 = 2;
MPEG1_VERSION_2_5 = 0;
MPEG1_VERSION_RESERVED = 1;
MPEG1_LAYER_1 = 3;
MPEG1_LAYER_2 = 2;
MPEG1_LAYER_3 = 1;
MPEG1_LAYER_RESERVED = 0;
BITRATE_TABLE =
[[ 0, 0, 0, 0, 0, 0 ],
[ 32000, 32000, 32000, 32000, 8000, 8000 ],
[ 64000, 48000, 40000, 48000, 16000, 16000 ],
[ 96000, 56000, 48000, 56000, 24000, 24000 ],
[ 128000, 64000, 56000, 64000, 32000, 32000 ],
[ 160000, 80000, 64000, 80000, 40000, 40000 ],
[ 192000, 96000, 80000, 96000, 48000, 48000 ],
[ 224000, 112000, 96000, 112000, 56000, 56000 ],
[ 256000, 128000, 112000, 128000, 64000, 64000 ],
[ 288000, 160000, 128000, 144000, 80000, 80000 ],
[ 320000, 192000, 160000, 160000, 96000, 96000 ],
[ 352000, 224000, 192000, 176000, 112000, 112000 ],
[ 384000, 256000, 224000, 192000, 128000, 128000 ],
[ 416000, 320000, 256000, 224000, 144000, 144000 ],
[ 448000, 384000, 320000, 256000, 160000, 160000 ],
[ 0, 0, 0, 0, 0, 0 ]];
def initialize(version,
layer,
protection,
bitrate,
samplerate,
padding,
hprivate,
channel,
extension,
copyright,
original,
emphasis,
data)
@version = version;
@layer = layer;
@protection = protection;
@bitrate = bitrate;
@samplerate = samplerate;
@padding = padding;
@private = hprivate;
@channel = channel;
@extension = extension;
@copyright = copyright;
@original = original;
@emphasis = emphasis;
@frame = data;
@duration = data.bytesize().to_f * 8 / bitrate;
end
end
class Mp3File
def self.open(file)
frames = [];
data = File.open(file) { |fd| fd.read(); }
data.force_encoding("BINARY");
while(data.size >= 4)
frame = Mp3Frame.fetch(data);
if(frame)
frames.push(frame.to_s.bytesize);
else
data.replace(data[1, -1]);
end
end
frames;
end
end