-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathMetadataCollector.ts
281 lines (228 loc) · 7.73 KB
/
MetadataCollector.ts
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
import {
FormatId,
IAudioMetadata, ICommonTagsResult,
IFormat,
INativeAudioMetadata,
INativeTags, IOptions,
MusicMetadataParser
} from "../index";
import * as _debug from "debug";
import {GenericTagId, IGenericTag, TagType, isSingleton, isUnique} from "./GenericTagTypes";
import {CombinedTagMapper} from "./CombinedTagMapper";
import {CommonTagMapper} from "./GenericTagMapper";
const debug = _debug("music-metadata:collector");
const TagPriority: TagType[] = ['APEv2', 'vorbis', 'ID3v2.4', 'ID3v2.3', 'ID3v2.2', 'exif', 'asf', 'iTunes MP4', 'ID3v1'];
/**
* Combines all generic-tag-mappers for each tag type
*/
export interface INativeMetadataCollector {
/**
* Only use this for reading
*/
readonly format: IFormat;
readonly native: INativeTags;
/**
* @returns {boolean} true if one or more tags have been found
*/
hasAny(): boolean;
setFormat(key: FormatId, value: any);
addTag(tagType: string, tagId: string, value: any);
}
/**
* Provided to the parser to uodate the metadata result.
* Responsible for triggering async updates
*/
export class MetadataCollector implements INativeMetadataCollector {
public readonly format: IFormat = {
tagTypes: []
};
public readonly native: INativeTags = {};
public readonly common: ICommonTagsResult = {
track: {no: null, of: null},
disk: {no: null, of: null}
};
/**
* Keeps track of origin priority for each mapped id
*/
private readonly commonOrigin: {
[id: string]: number;
} = {};
/**
* Maps a tag type to a priority
*/
private readonly originPriority: {
[tagType: string]: number;
} = {};
private tagMapper = new CombinedTagMapper();
public constructor(private opts: IOptions) {
let priority: number = 1;
for (const tagType of TagPriority) {
this.originPriority[tagType] = priority++;
}
this.originPriority.artificial = 500; // Filled using alternative tags
this.originPriority.id3v1 = 600; // Consider worst due to field length limit
}
/**
* @returns {boolean} true if one or more tags have been found
*/
public hasAny() {
for (const tagType in this.native) {
return true;
}
return false;
}
public setFormat(key: FormatId, value: any) {
debug(`format: ${key} = ${value}`);
(this.format as any)[key] = value; // as any to override readonly
if (this.opts.observer) {
this.opts.observer({metadata: this, tag: {type: 'format', id: key, value}});
}
}
public addTag(tagType: TagType, tagId: string, value: any) {
debug(`tag ${tagType}.${tagId} = ${value}`);
if (!this.native[tagType]) {
this.format.tagTypes.push(tagType);
this.native[tagType] = [];
}
this.native[tagType].push({id: tagId, value});
this.toCommon(tagType, tagId, value);
}
public getNativeMetadata(): INativeAudioMetadata {
return {
format: this.format,
native: this.native
};
}
public postMap(tagType: TagType | 'artificial', tag: IGenericTag) {
// Common tag (alias) found
// check if we need to do something special with common tag
// if the event has been aliased then we need to clean it before
// it is emitted to the user. e.g. genre (20) -> Electronic
switch (tag.id) {
case 'artist':
if (this.commonOrigin.artist === this.originPriority[tagType]) {
// Assume the artist field is used as artists
return this.postMap('artificial', {id: 'artists', value: tag.value});
}
if (!this.common.artists) {
// Fill artists using artist source
this.setGenericTag('artificial', {id: 'artists', value: tag.value});
}
break;
case 'artists':
if (!this.common.artist || this.commonOrigin.artist === this.originPriority.artificial) {
if (!this.common.artists || this.common.artists.indexOf(tag.value) === -1) {
// Fill artist using artists source
const artists = (this.common.artists || []).concat([tag.value]);
const value = MusicMetadataParser.joinArtists(artists);
const artistTag: IGenericTag = {id: 'artist', value};
this.setGenericTag('artificial', artistTag);
}
}
break;
case 'genre':
tag.value = CommonTagMapper.parseGenre(tag.value);
break;
case 'picture':
tag.value.format = CommonTagMapper.fixPictureMimeType(tag.value.format);
break;
case 'totaltracks':
this.common.track.of = CommonTagMapper.toIntOrNull(tag.value);
return;
case 'totaldiscs':
this.common.disk.of = CommonTagMapper.toIntOrNull(tag.value);
return;
case 'track':
case 'disk':
const of = this.common[tag.id].of; // store of value, maybe maybe overwritten
this.common[tag.id] = CommonTagMapper.normalizeTrack(tag.value);
this.common[tag.id].of = of != null ? of : this.common[tag.id].of;
return;
case 'year':
case 'originalyear':
tag.value = parseInt(tag.value, 10);
break;
case 'date':
// ToDo: be more strict on 'YYYY...'
const year = parseInt(tag.value.substr(0, 4), 10);
if (year && !isNaN(year)) {
this.common.year = year;
}
break;
case 'discogs_label_id':
case 'discogs_release_id':
case 'discogs_master_release_id':
case 'discogs_artist_id':
case 'discogs_votes':
tag.value = typeof tag.value === 'string' ? parseInt(tag.value, 10) : tag.value;
break;
case 'replaygain_track_peak':
tag.value = typeof tag.value === 'string' ? parseFloat(tag.value) : tag.value;
break;
case 'gapless': // iTunes gap-less flag
tag.value = tag.value === "1"; // boolean
break;
default:
// nothing to do
}
this.setGenericTag(tagType, tag);
}
/**
* Convert native tags to common tags
* @returns {IAudioMetadata} Native + common tags
*/
public toCommonMetadata(): IAudioMetadata {
return {
format: this.format,
native: this.opts.native ? this.native : undefined,
common: this.common
};
}
/**
* Convert native tag to common tags
*/
private toCommon(tagType: TagType, tagId: string, value: any) {
const tag = {id: tagId, value};
const genericTag = this.tagMapper.mapTag(tagType, tag);
if (genericTag) {
this.postMap(tagType, genericTag);
}
}
/**
* Set generic tag
* @param {GenericTagId} tagId
* @param {TagType} tagType originating header type, used to prioritize concurrent mappings
* @param value
*/
private setGenericTag(tagType: TagType | 'artificial', tag: IGenericTag) {
debug(`common.${tag.id} = ${tag.value}`);
const prio0 = this.commonOrigin[tag.id] || 1000;
const prio1 = this.originPriority[tagType];
if (isSingleton(tag.id)) {
if (prio1 <= prio0) {
this.common[tag.id] = tag.value;
this.commonOrigin[tag.id] = prio1;
} else {
return debug(`Ignore native tag (singleton): ${tagType}.${tag.id} = ${tag.value}`);
}
} else {
if (prio1 === prio0) {
if (!isUnique(tag.id) || this.common[tag.id].indexOf(tag.value) === -1) {
this.common[tag.id].push(tag.value);
} else {
debug(`Ignore duplicate value: ${tagType}.${tag.id} = ${tag.value}`);
}
// no effect? this.commonOrigin[tag.id] = prio1;
} else if (prio1 < prio0) {
this.common[tag.id] = [tag.value];
this.commonOrigin[tag.id] = prio1;
} else {
return debug(`Ignore native tag (list): ${tagType}.${tag.id} = ${tag.value}`);
}
}
if (this.opts.observer) {
this.opts.observer({metadata: this, tag: {type: 'common', id: tag.id, value: tag.value}});
}
// ToDo: trigger metadata event
}
}