-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-spec.cpp
390 lines (357 loc) · 9.48 KB
/
file-spec.cpp
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
/*
* Copyright (c) 2013-2019 amded workers, All rights reserved.
* Terms for redistribution and use can be found in LICENCE.
*/
/**
* @file file-spec.cpp
* @brief File specific routines and abstraction
*/
#include <algorithm>
#include <map>
#include <string>
#include <apetag.h>
#include <flacfile.h>
#include <id3v1tag.h>
#include <id3v2tag.h>
#include <mp4file.h>
#include <mpegfile.h>
#include <oggflacfile.h>
#include <opusfile.h>
#include <tfile.h>
#include <tpropertymap.h>
#include <vorbisfile.h>
#include "amded.h"
#include "file-spec.h"
#include "file-type.h"
#include "setup.h"
#include "tag-implementation.h"
#include "tag.h"
/**
* Map of file types that support multiple tag-types.
*
* This is used by both the read-map and the write-map. It also defines default
* values for those:
*
* - The read-map defaults to exactly this map.
*
* - The write-map defaults to the first entry in the tag-type vector.
*/
std::map< enum file_type, std::vector< enum tag_impl > > filetag_map {
{ FILE_T_MP3, { TAG_T_ID3V2, TAG_T_APETAG, TAG_T_ID3V1 } }
};
static std::vector< enum tag_impl >
get_vector_from_map(enum file_type type,
std::map< enum file_type, std::vector< enum tag_impl > > m)
{
auto rv = m.find(type);
if (rv == m.end()) {
return {};
}
return rv->second;
}
static std::vector< enum tag_impl >
get_readmap_vector(enum file_type type)
{
return get_vector_from_map(type, read_map);
}
static std::vector< enum tag_impl >
get_writemap_vector(enum file_type type)
{
return get_vector_from_map(type, write_map);
}
static std::vector< enum tag_impl >
get_multitag_vector(enum file_type type)
{
return get_vector_from_map(type, filetag_map);
}
bool
tag_impl_allowed_for_file_type(enum file_type ft, enum tag_impl ti)
{
static std::vector<enum tag_impl> ttypes = get_multitag_vector(ft);
for (auto &iter : ttypes) {
if (iter == ti) {
return true;
}
}
return false;
}
static bool
mp3_has_tag_type(TagLib::MPEG::File *fh, enum tag_impl type)
{
if (type == TAG_T_APETAG && fh->hasAPETag()) {
return true;
}
if (type == TAG_T_ID3V2 && fh->hasID3v2Tag()) {
return true;
}
if (type == TAG_T_ID3V1 && fh->hasID3v1Tag()) {
return true;
}
return false;
}
static bool
has_tag_type(const struct amded_file &file, enum tag_impl type)
{
switch (file.type.get_id()) {
case FILE_T_MP3:
return
mp3_has_tag_type(
reinterpret_cast<TagLib::MPEG::File *>(file.fh),
type);
break;
default:
return false;
}
}
bool
is_multitag_type(enum file_type type)
{
return (filetag_map.find(type) != filetag_map.end());
}
static std::map < std::string, enum file_type >
file_ext_map = {
{ "flac", FILE_T_FLAC },
{ "flc", FILE_T_FLAC },
{ "mp3", FILE_T_MP3 },
{ "mp4", FILE_T_M4A },
{ "m4a", FILE_T_M4A },
{ "ogg", FILE_T_OGG_VORBIS },
{ "oga", FILE_T_OGG_VORBIS },
{ "opus", FILE_T_OPUS }
};
void
list_extensions(void)
{
for (auto &iter : file_ext_map) {
std::cout << iter.first << std::endl;
}
}
enum file_type
get_ext_type(const std::string &filename)
{
int dotidx = filename.rfind('.');
std::string ext = filename.substr(dotidx + 1);
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
auto rv = file_ext_map.find(ext);
if (rv == file_ext_map.end()) {
return FILE_T_INVALID;
}
return rv->second;
}
static enum tag_impl
get_prefered_tag_impl(const struct amded_file &file)
{
auto types = get_readmap_vector(file.type.get_id());
for (auto &iter : types) {
if (has_tag_type(file, iter)) {
return iter;
}
}
return TAG_T_NONE;
}
bool
amded_open(struct amded_file &file)
{
switch (file.type.get_id()) {
case FILE_T_MP3:
file.fh = new TagLib::MPEG::File(file.name);
break;
case FILE_T_FLAC:
file.fh = new TagLib::FLAC::File(file.name);
break;
case FILE_T_OGG_VORBIS:
file.fh = new TagLib::Ogg::Vorbis::File(file.name);
break;
case FILE_T_M4A:
file.fh = new TagLib::MP4::File(file.name);
break;
case FILE_T_OPUS:
file.fh = new TagLib::Ogg::Opus::File(file.name);
break;
default:
std::cerr << "BUG: Missing implementation for file type: "
<< file.type.get_id() << std::endl
<< " This should not happen. Please report!"
<< std::endl;
return false;
}
if (!file.fh->isValid()) {
std::cerr << PROJECT ": Could not open file: `"
<< file.name << "'" << std::endl;
goto error;
}
if (file.fh->tag() == nullptr) {
std::cerr << PROJECT ": No tags in file: `"
<< file.name << "'" << std::endl;
goto error;
}
if (file.fh->tag() == nullptr) {
std::cerr << PROJECT ": Could not get audio properties for file: `"
<< file.name << "'" << std::endl;
goto error;
}
if (is_multitag_type(file.type.get_id())) {
file.multi_tag = true;
file.tagimpl = get_prefered_tag_impl(file);
} else {
file.multi_tag = false;
}
return true;
error:
delete file.fh;
return false;
}
std::string
get_tag_types(const struct amded_file &file)
{
std::string rv {};
bool first = true;
auto types = get_multitag_vector(file.type.get_id());
for (auto &iter : types) {
if (has_tag_type(file, iter)) {
Amded::TagImplementation s = iter;
if (!first) {
rv += ",";
} else {
first = false;
}
rv += s.get_label();
}
}
return first ? "none" : rv;
}
TagLib::PropertyMap
get_tags_for_file(const struct amded_file &file)
{
TagLib::MPEG::File *mp3fh;
switch (file.type.get_id()) {
case FILE_T_MP3:
mp3fh = reinterpret_cast<TagLib::MPEG::File *>(file.fh);
switch (file.tagimpl.get_id()) {
case TAG_T_ID3V2:
return mp3fh->ID3v2Tag()->properties();
case TAG_T_APETAG:
return mp3fh->APETag()->properties();
case TAG_T_ID3V1:
return mp3fh->ID3v1Tag()->properties();
default:
break;
}
/* FALL-THROUGH */
default:
return file.fh->properties();
}
}
static bool
amded_tag_mp3(TagLib::MPEG::File *fh,
const std::vector<enum tag_impl> &wm)
{
bool want_ape, want_v1, want_v2;
want_ape = want_v1 = want_v2 = false;
for (auto &iter : wm) {
switch (iter) {
case TAG_T_APETAG:
if (fh->hasAPETag() || !only_tag_delete()) {
want_ape = true;
}
break;
case TAG_T_ID3V1:
if (fh->hasID3v1Tag() || !only_tag_delete()) {
want_v1 = true;
}
break;
case TAG_T_ID3V2:
if (fh->hasID3v2Tag() || !only_tag_delete()) {
want_v2 = true;
}
break;
default:
break;
}
}
if (!(want_ape || want_v1 || want_v2)) {
return true;
}
int save_tags = TagLib::MPEG::File::NoTags;
if (want_ape) {
save_tags |= TagLib::MPEG::File::APE;
TagLib::APE::Tag *tag = fh->APETag(true);
TagLib::PropertyMap pm = tag->properties();
amded_amend_tags(pm);
tag->setProperties(pm);
}
if (want_v2) {
save_tags |= TagLib::MPEG::File::ID3v2;
TagLib::ID3v2::Tag *tag = fh->ID3v2Tag(true);
TagLib::PropertyMap pm = tag->properties();
amded_amend_tags(pm);
tag->setProperties(pm);
}
if (want_v1) {
save_tags |= TagLib::MPEG::File::ID3v1;
TagLib::ID3v1::Tag *tag = fh->ID3v1Tag(true);
TagLib::PropertyMap pm = tag->properties();
amded_amend_tags(pm);
tag->setProperties(pm);
}
return fh->save(save_tags, false, 4, false);
}
static bool
amded_strip_mp3(TagLib::MPEG::File *fh,
const std::vector<enum tag_impl> &wm)
{
int save_tags = TagLib::MPEG::File::NoTags;
for (auto &iter : wm) {
switch (iter) {
case TAG_T_APETAG:
save_tags |= TagLib::MPEG::File::APE;
break;
case TAG_T_ID3V1:
save_tags |= TagLib::MPEG::File::ID3v1;
break;
case TAG_T_ID3V2:
save_tags |= TagLib::MPEG::File::ID3v2;
break;
default:
break;
}
}
if (save_tags == TagLib::MPEG::File::NoTags) {
return true;
}
return fh->strip(save_tags);
}
void
tag_multitag(const struct amded_file &file)
{
bool rc;
switch (file.type.get_id()) {
case FILE_T_MP3:
rc = amded_tag_mp3(reinterpret_cast<TagLib::MPEG::File *>(file.fh),
get_writemap_vector(file.type.get_id()));
break;
default:
return;
}
if (!rc) {
std::cerr << PROJECT << ": Failed to save file `" << file.name << "'"
<< std::endl;
}
}
void
strip_multitag(const struct amded_file &file)
{
bool rc;
switch (file.type.get_id()) {
case FILE_T_MP3:
rc = amded_strip_mp3(reinterpret_cast<TagLib::MPEG::File *>(file.fh),
get_writemap_vector(file.type.get_id()));
break;
default:
return;
}
if (!rc) {
std::cerr << PROJECT << ": Failed to save file `" << file.name << "'"
<< std::endl;
}
}