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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
importScripts('function_sequence.js');
importScripts('function_parallel.js');
importScripts('util.js');
function Id3Parser(parent) {
MetadataParser.apply(this, [parent]);
this.verbose = true;
}
Id3Parser.parserType = 'mpeg';
Id3Parser.prototype = {__proto__: MetadataParser.prototype};
Id3Parser.prototype.urlFilter = /\.(mp3)$/i;
/**
* id3v1 constants
*/
Id3Parser.v1 = {
/**
* Genres list as described in id3 documentation. We aren't going to
* localize this list, because at least in Russian (and I think most
* other languages), translation exists at least fo 10% and most time
* translation would degrade to transliteration.
*/
GENRES : [
'Blues',
'Classic Rock',
'Country',
'Dance',
'Disco',
'Funk',
'Grunge',
'Hip-Hop',
'Jazz',
'Metal',
'New Age',
'Oldies',
'Other',
'Pop',
'R&B',
'Rap',
'Reggae',
'Rock',
'Techno',
'Industrial',
'Alternative',
'Ska',
'Death Metal',
'Pranks',
'Soundtrack',
'Euro-Techno',
'Ambient',
'Trip-Hop',
'Vocal',
'Jazz+Funk',
'Fusion',
'Trance',
'Classical',
'Instrumental',
'Acid',
'House',
'Game',
'Sound Clip',
'Gospel',
'Noise',
'AlternRock',
'Bass',
'Soul',
'Punk',
'Space',
'Meditative',
'Instrumental Pop',
'Instrumental Rock',
'Ethnic',
'Gothic',
'Darkwave',
'Techno-Industrial',
'Electronic',
'Pop-Folk',
'Eurodance',
'Dream',
'Southern Rock',
'Comedy',
'Cult',
'Gangsta',
'Top 40',
'Christian Rap',
'Pop/Funk',
'Jungle',
'Native American',
'Cabaret',
'New Wave',
'Psychadelic',
'Rave',
'Showtunes',
'Trailer',
'Lo-Fi',
'Tribal',
'Acid Punk',
'Acid Jazz',
'Polka',
'Retro',
'Musical',
'Rock & Roll',
'Hard Rock',
'Folk',
'Folk-Rock',
'National Folk',
'Swing',
'Fast Fusion',
'Bebob',
'Latin',
'Revival',
'Celtic',
'Bluegrass',
'Avantgarde',
'Gothic Rock',
'Progressive Rock',
'Psychedelic Rock',
'Symphonic Rock',
'Slow Rock',
'Big Band',
'Chorus',
'Easy Listening',
'Acoustic',
'Humour',
'Speech',
'Chanson',
'Opera',
'Chamber Music',
'Sonata',
'Symphony',
'Booty Bass',
'Primus',
'Porn Groove',
'Satire',
'Slow Jam',
'Club',
'Tango',
'Samba',
'Folklore',
'Ballad',
'Power Ballad',
'Rhythmic Soul',
'Freestyle',
'Duet',
'Punk Rock',
'Drum Solo',
'A capella',
'Euro-House',
'Dance Hall',
'Goa',
'Drum & Bass',
'Club-House',
'Hardcore',
'Terror',
'Indie',
'BritPop',
'Negerpunk',
'Polsk Punk',
'Beat',
'Christian Gangsta Rap',
'Heavy Metal',
'Black Metal',
'Crossover',
'Contemporary Christian',
'Christian Rock',
'Merengue',
'Salsa',
'Thrash Metal',
'Anime',
'Jpop',
'Synthpop'
]
};
/**
* Reads synchsafe integer.
* 'SynchSafe' term is taken from id3 documentation.
*
* @param {ByteReader} reader - reader to use
* @param {int} length - bytes to read
* @return {int}
*/
Id3Parser.readSynchSafe_ = function(reader, length) {
var rv = 0;
switch (length) {
case 4:
rv = reader.readScalar(1, false) << 21;
case 3:
rv |= reader.readScalar(1, false) << 14;
case 2:
rv |= reader.readScalar(1, false) << 7;
case 1:
rv |= reader.readScalar(1, false);
}
return rv;
};
/**
* Reads 3bytes integer.
*
* @param {ByteReader} reader - reader to use
* @return {int}
*/
Id3Parser.readUInt24_ = function(reader) {
return reader.readScalar(2, false) << 16 | reader.readScalar(1, false);
};
/**
* Reads string from reader with specified encoding
*
* @param {ByteReader} reader reader to use
* @param {int} encoding string encoding.
* @param {int} size maximum string size. Actual result may be shorter.
*
*/
Id3Parser.prototype.readString_ = function(reader, encoding, size) {
switch (encoding) {
case Id3Parser.v2.ENCODING.ISO_8859_1:
return reader.readNullTerminatedString(size);
case Id3Parser.v2.ENCODING.UTF_16BE:
case Id3Parser.v2.ENCODING.UTF_16:
case Id3Parser.v2.ENCODING.UTF_8:
default: {
// TODO: implement reading of unicode strings
this.error('Reading of unicode strings in ID3 tags is unimplemented');
return '';
}
}
};
/**
* Reads text frame from reader.
*
* @param {ByteReader} reader reader to use
* @param {int} majorVersion major id3 version to use
* @param {Object} frame frame so store data at
* @param {int} end frame end position in reader
*/
Id3Parser.prototype.readTextFrame_ = function(reader,
majorVersion,
frame,
end) {
frame.encoding = reader.readScalar(1, false, end);
frame.value = this.readString_(reader, frame.encoding, end - reader.tell());
};
/**
* Reads user defined text frame from reader.
*
* @param {ByteReader} reader reader to use
* @param {int} majorVersion major id3 version to use
* @param {Object} frame frame so store data at
* @param {int} end frame end position in reader
*/
Id3Parser.prototype.readUserDefinedTextFrame_ = function(reader,
majorVersion,
frame,
end) {
frame.encoding = reader.readScalar(1, false, end);
frame.description = this.readString_(
reader,
frame.encoding,
end - reader.tell());
frame.value = this.readString_(
reader,
frame.encoding,
end - reader.tell());
};
Id3Parser.prototype.readPIC_ = function(reader, majorVersion, frame, end) {
frame.encoding = reader.readScalar(1, false, end);
frame.format = reader.readNullTerminatedString(3, end - reader.tell());
frame.pictureType = reader.readScalar(1, false, end);
frame.description = this.readString_(reader,
frame.encoding,
end - reader.tell());
if (frame.format == '-->') {
frame.imageUrl = reader.readNullTerminatedString(end - reader.tell());
} else {
frame.imageUrl = reader.readImage(end - reader.tell());
}
};
Id3Parser.prototype.readAPIC_ = function(reader, majorVersion, frame, end) {
this.vlog('Extracting picture');
frame.encoding = reader.readScalar(1, false, end);
frame.mime = reader.readNullTerminatedString(end - reader.tell());
frame.pictureType = reader.readScalar(1, false, end);
frame.description = this.readString_(
reader,
frame.encoding,
end - reader.tell());
if (frame.mime == '-->') {
frame.imageUrl = reader.readNullTerminatedString(end - reader.tell());
} else {
frame.imageUrl = reader.readImage(end - reader.tell());
}
};
/**
* Reads string from reader with specified encoding
*
* @param {ByteReader} reader reader to use
* @return {Object} frame read
*/
Id3Parser.prototype.readFrame_ = function(reader, majorVersion) {
var frame = {};
reader.pushSeek(reader.tell(), ByteReader.SEEK_BEG);
var position = reader.tell();
frame.name = (majorVersion == 2)
? reader.readNullTerminatedString(3)
: reader.readNullTerminatedString(4);
if (frame.name == '')
return null;
this.vlog('Found frame ' + (frame.name) + ' at position ' + position );
var dataSize = 0;
switch (majorVersion) {
case 2:
frame.size = Id3Parser.readUInt24_(reader);
frame.headerSize = 6;
break;
case 3:
frame.size = reader.readScalar(4, false);
frame.headerSize = 10;
frame.flags = reader.readScalar(2, false);
break;
case 4:
frame.size = Id3Parser.readSynchSafe_(reader, 4);
frame.headerSize = 10;
frame.flags = reader.readScalar(2, false);
break;
}
this.vlog('Found frame [' + frame.name + '] with size ['+frame.size+']');
if (Id3Parser.v2.HANDLERS[frame.name]) {
Id3Parser.v2.HANDLERS[frame.name].call(
this,
reader,
majorVersion,
frame,
reader.tell() + frame.size);
} else if (frame.name.charAt(0) == 'T' || frame.name.charAt(0) == 'W') {
this.readTextFrame_(
reader,
majorVersion,
frame,
reader.tell() + frame.size);
}
reader.popSeek();
reader.seek(frame.size + frame.headerSize, ByteReader.SEEK_CUR);
return frame;
};
Id3Parser.prototype.parse = function (file, callback, onError) {
var metadata = {};
var self = this;
this.log('Starting id3 parser for ' + file.name);
var id3v1Parser = new FunctionSequence(
'id3v1parser',
[
/**
* Reads last 128 bytes of file in bytebuffer,
* which passes further.
* In last 128 bytes should be placed ID3v1 tag if available.
* @param file - file which bytes to read.
*/
function readTail(file) {
util.readFileBytes(file, file.size - 128, file.size,
this.nextStep, this.onError, this);
},
/**
* Attempts to extract ID3v1 tag from 128 bytes long ByteBuffer
* @param file file which tags are being extracted.
* Could be used for logging purposes.
* @param {ByteReader} reader ByteReader of 128 bytes.
*/
function extractId3v1(file, reader) {
if ( reader.readString(3) == 'TAG') {
this.logger.vlog('id3v1 found');
var id3v1 = metadata.id3v1 = {};
var title = reader.readNullTerminatedString(30).trim();
if (title.length > 0) {
id3v1.title = title;
}
reader.seek(3 + 30, ByteReader.SEEK_BEG);
var artist = reader.readNullTerminatedString(30).trim();
if (artist.length > 0) {
id3v1.artist = artist;
}
reader.seek(3 + 30 + 30, ByteReader.SEEK_BEG);
var album = reader.readNullTerminatedString(30).trim();
if (album.length > 0) {
id3v1.album = album;
}
}
this.nextStep();
}
],
this
);
var id3v2Parser = new FunctionSequence(
'id3v2parser',
[
function readHead(file) {
util.readFileBytes(file, 0, 10, this.nextStep, this.onError,
this);
},
/**
* Check if passed array of 10 bytes contains ID3 header.
* @param file to check and continue reading if ID3 metadata found
* @param {ByteReader} reader reader to fill with stream bytes.
*/
function checkId3v2(file, reader) {
if (reader.readString(3) == 'ID3') {
this.logger.vlog('id3v2 found');
var id3v2 = metadata.id3v2 = {};
id3v2.major = reader.readScalar(1, false);
id3v2.minor = reader.readScalar(1, false);
id3v2.flags = reader.readScalar(1, false);
id3v2.size = Id3Parser.readSynchSafe_(reader, 4);
util.readFileBytes(file, 10, 10 + id3v2.size, this.nextStep,
this.onError, this);
} else {
this.finish();
}
},
/**
* Extracts all ID3v2 frames from given bytebuffer.
* @param file being parsed.
* @param {ByteReader} reader to use for metadata extraction.
*/
function extractFrames(file, reader) {
var frameStart = 0;
var id3v2 = metadata.id3v2;
if ((id3v2.major > 2)
&& (id3v2.flags & Id3Parser.v2.FLAG_EXTENDED_HEADER != 0)) {
// Skip extended header if found
if (id3v2.major == 3) {
reader.seek(reader.readScalar(4, false) - 4);
} else if (id3v2.major == 4) {
reader.seek(Id3Parser.readSynchSafe_(reader, 4) - 4);
}
}
var frame;
while (frame = self.readFrame_(reader, id3v2.major)) {
metadata.id3v2[frame.name] = frame;
}
this.nextStep();
}
],
this
);
var metadataParser = new FunctionParallel(
'mp3metadataParser',
[id3v1Parser, id3v2Parser],
this,
function() {
callback.call(null, metadata);
},
onError
);
id3v1Parser.setCallback(metadataParser.nextStep);
id3v2Parser.setCallback(metadataParser.nextStep);
id3v1Parser.setFailureCallback(metadataParser.onError);
id3v2Parser.setFailureCallback(metadataParser.onError);
this.vlog('Passed argument : ' + file);
metadataParser.start(file);
};
/**
* id3v2 constants
*/
Id3Parser.v2 = {
FLAG_EXTENDED_HEADER: 1 << 5,
ENCODING: {
/**
* ISO-8859-1 [ISO-8859-1]. Terminated with $00.
*
* @const
* @type {int}
*/
ISO_8859_1 : 0,
/**
* UTF-16 [UTF-16] encoded Unicode [UNICODE] with BOM. All
* strings in the same frame SHALL have the same byteorder.
* Terminated with $00 00.
*
* @const
* @type {int}
*/
UTF_16BE : 1,
/**
* UTF-16BE [UTF-16] encoded Unicode [UNICODE] without BOM.
* Terminated with $00 00.
*
* @const
* @type {int}
*/
UTF_16 : 2,
/**
* UTF-8 [UTF-8] encoded Unicode [UNICODE]. Terminated with $00.
*
* @const
* @type {int}
*/
UTF_8 : 3
},
HANDLERS: {
//User defined text information frame
TXX: Id3Parser.prototype.readUserDefinedTextFrame_,
//User defined URL link frame
WXX: Id3Parser.prototype.readUserDefinedTextFrame_,
//User defined text information frame
TXXX: Id3Parser.prototype.readUserDefinedTextFrame_,
//User defined URL link frame
WXXX: Id3Parser.prototype.readUserDefinedTextFrame_,
//User attached image
PIC: Id3Parser.prototype.readPIC_,
//User attached image
APIC: Id3Parser.prototype.readAPIC_
}
};
MetadataDispatcher.registerParserClass(Id3Parser);
|