forked from aadsm/JavaScript-ID3-Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathid3v2frames.js
More file actions
174 lines (144 loc) · 5.65 KB
/
Copy pathid3v2frames.js
File metadata and controls
174 lines (144 loc) · 5.65 KB
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
/*
* Copyright (c) 2009 Opera Software ASA, António Afonso ([email protected])
* Modified by António Afonso <antonio.afonso gmail.com>
*/
(function() {
var pictureType = [
"32x32 pixels 'file icon' (PNG only)",
"Other file icon",
"Cover (front)",
"Cover (back)",
"Leaflet page",
"Media (e.g. lable side of CD)",
"Lead artist/lead performer/soloist",
"Artist/performer",
"Conductor",
"Band/Orchestra",
"Composer",
"Lyricist/text writer",
"Recording Location",
"During recording",
"During performance",
"Movie/video screen capture",
"A bright coloured fish",
"Illustration",
"Band/artist logotype",
"Publisher/Studio logotype"
];
function getTextEncoding( bite ) {
var charset;
switch( bite )
{
case 0x00:
charset = 'iso-8859-1';
break;
case 0x01:
charset = 'utf-16';
break;
case 0x02:
charset = 'utf-16be';
break;
case 0x03:
charset = 'utf-8';
break;
}
return charset;
}
function getTime( duration )
{
var duration = duration/1000,
seconds = Math.floor( duration ) % 60,
minutes = Math.floor( duration/60 ) % 60,
hours = Math.floor( duration/3600 );
return {
seconds : seconds,
minutes : minutes,
hours : hours
};
}
function formatTime( time )
{
var seconds = time.seconds < 10 ? '0'+time.seconds : time.seconds;
var minutes = (time.hours > 0 && time.minutes < 10) ? '0'+time.minutes : time.minutes;
return (time.hours>0?time.hours+':':'') + minutes + ':' + seconds;
}
ID3v2.readFrameData['APIC'] = function readPictureFrame(offset, length, data, flags, v) {
v = v || '3';
var start = offset;
var charset = getTextEncoding( data.getByteAt(offset) );
switch( v ) {
case '2':
var format = data.getStringAt(offset+1, 3);
offset += 4;
break;
case '3':
case '4':
var format = data.getStringWithCharsetAt(offset+1, length - (offset-start), charset);
offset += 1 + format.bytesReadCount;
break;
}
var bite = data.getByteAt(offset, 1);
var type = pictureType[bite];
var desc = data.getStringWithCharsetAt(offset+1, length - (offset-start), charset);
offset += 1 + desc.bytesReadCount;
return {
"format" : format.toString(),
"type" : type,
"description" : desc.toString(),
"data" : data.getBytesAt(offset, (start+length) - offset)
};
};
ID3v2.readFrameData['COMM'] = function readCommentsFrame(offset, length, data) {
var start = offset;
var charset = getTextEncoding( data.getByteAt(offset) );
var language = data.getStringAt( offset+1, 3 );
var shortdesc = data.getStringWithCharsetAt(offset+4, length-4, charset);
offset += 4 + shortdesc.bytesReadCount;
var text = data.getStringWithCharsetAt( offset, (start+length) - offset, charset );
return {
language : language,
short_description : shortdesc.toString(),
text : text.toString()
};
};
ID3v2.readFrameData['COM'] = ID3v2.readFrameData['COMM'];
ID3v2.readFrameData['PIC'] = function(offset, length, data, flags) {
return ID3v2.readFrameData['APIC'](offset, length, data, flags, '2');
};
ID3v2.readFrameData['PCNT'] = function readCounterFrame(offset, length, data) {
// FIXME: implement the rest of the spec
return data.getInteger32At(offset);
};
ID3v2.readFrameData['CNT'] = ID3v2.readFrameData['PCNT'];
ID3v2.readFrameData['T*'] = function readTextFrame(offset, length, data) {
var charset = getTextEncoding( data.getByteAt(offset) );
return data.getStringWithCharsetAt(offset+1, length-1, charset).toString();
};
ID3v2.readFrameData['TCON'] = function readGenreFrame(offset, length, data) {
var text = ID3v2.readFrameData['T*'].apply( this, arguments );
return text.replace(/^\(\d+\)/, '');
};
ID3v2.readFrameData['TCO'] = ID3v2.readFrameData['TCON'];
//ID3v2.readFrameData['TLEN'] = function readLengthFrame(offset, length, data) {
// var text = ID3v2.readFrameData['T*'].apply( this, arguments );
//
// return {
// text : text,
// parsed : formatTime( getTime(parseInt(text)) )
// };
//};
ID3v2.readFrameData['USLT'] = function readLyricsFrame(offset, length, data) {
var start = offset;
var charset = getTextEncoding( data.getByteAt(offset) );
var language = data.getStringAt( offset+1, 3 );
var descriptor = data.getStringWithCharsetAt( offset+4, length-4, charset );
offset += 4 + descriptor.bytesReadCount;
var lyrics = data.getStringWithCharsetAt( offset, (start+length) - offset, charset );
return {
language : language,
descriptor : descriptor.toString(),
lyrics : lyrics.toString()
};
};
ID3v2.readFrameData['ULT'] = ID3v2.readFrameData['USLT'];
})();