-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVorbisComment.cpp
More file actions
411 lines (361 loc) · 13.3 KB
/
VorbisComment.cpp
File metadata and controls
411 lines (361 loc) · 13.3 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
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
#include <fstream>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <sstream>
#include "Utils.h"
#include "UniTag.h"
using namespace std;
using namespace UniTag;
using namespace utils;
VorbisComment::VorbisComment(const std::string path) : Tag(path), _chunks(N_CHUNCKS)
{
parse();
}
std::string VorbisComment::parseFrameWidthIndex(const unsigned int index) const
{
stringstream ss;
if(_frames[index].flag == getFrameFlag(COVER_ART))
{
ss<<"[IMAGE (size : "<<_frames[index].data.size()<<")]";
return ss.str();
}
return toStr(_frames[index].data);
}
std::string VorbisComment::get(const std::string flag) const
{
auto framesIndexes = Tag::getFramesIndexes(flag);
if(framesIndexes.size() == 0)
return "";
std::string result;
for(int i = 0; i < framesIndexes.size(); i++)
{
if(i!=0)
result += " & ";
result += parseFrameWidthIndex(framesIndexes[i]);
}
return result;
}
std::vector <char> VorbisComment::getCoverArt() const
{
auto framesIndexes = Tag::getFramesIndexes(getFrameFlag(COVER_ART));
if(framesIndexes.size() == 0)
return std::vector <char>();
auto img = _frames[framesIndexes[0]].data;
unsigned int offset = 0;
unsigned int type = getInt(img, offset);
offset += 4;
unsigned int mimetypeSize = getInt(img, offset);
offset += 4;
if(mimetypeSize > 40)
{
cerr<<"ERROR : Wrong mimetype"<<endl;
return std::vector <char>();
}
string mimetype = string(img.begin() + offset, img.begin() + offset + mimetypeSize);
offset += mimetypeSize;
unsigned int descriptionSize = getInt(img, offset);
offset += 4 + descriptionSize + 4*4;//width in pixels//height in pixels//color depth bits-per-pixels//number of colors used
unsigned int imgSize = getInt(img, offset);
offset += 4;
return vector <char>(img.begin() + offset, img.end());
}
std::string VorbisComment::getFrameFlag(const unsigned int flag) const
{
if(flag == TITLE)
return "TITLE";
else if(flag == ARTIST)
return "ARTIST";
else if(flag == ALBUM)
return "ALBUM";
else if(flag == ALBUM_ARTIST)
return "ALBUMARTIST";
else if(flag == DATE)
return "DATE";
else if(flag == GENRE)
return "GENRE";
else if(flag == LABEL)
return "LABEL";
else if(flag == DESCRIPTION)
return "DESCRIPTION";
else if(flag == ENCODED_BY)
return "ENCODED-BY";
else if(flag == ENCODER)
return "ENCODER";
else if(flag == TRACK_NUM)
return "TRACKNUMBER";
else if(flag == DISC)
return "DISCNUMBER";
else if(flag == COMMENT)
return "COMMENT";
else if(flag == LYRICS)
return "LYRICS";
else if(flag == COVER_ART)
return "METADATA_BLOCK_PICTURE";
return "";
}
void VorbisComment::set(const unsigned int flag, const std::string &text)
{
if(getFrameFlag(flag) == "")
{
cerr<<"VorbisComment : set : flag invalid"<<endl;
return;
}
vector <char> data;
push(data, text);
Tag::set(getFrameFlag(flag), data);
}
void VorbisComment::setCoverArt(const std::vector <char> &img)
{
unsigned int x,y;
unsigned int type = getImageSizeWithData(img, x, y);
if(!type)
return;
vector <char> data;
std::string mimetype = "image/jpeg";
if(type == 2)
mimetype = "image/png";
else if(type == 3)
mimetype = "image/gif";
push(data, toBytes(3, false));//picture type
push(data, toBytes(mimetype.size(), false));//mimetype size
push(data, mimetype);
push(data, toBytes(0, false));//length of description
push(data, toBytes(x, false));//width
push(data, toBytes(y, false));//height
if(type == 2)
push(data, toBytes(32, false));//bits-per-pixels
else
push(data, toBytes(24, false));//bits-per-pixels
push(data, toBytes(0, false));
push(data, toBytes(img.size(), false));
push(data, img);
Tag::set(getFrameFlag(COVER_ART), data);
}
void VorbisComment::saveFrames(std::string savePath)
{
if(savePath == "")
savePath = _path;
if(_type == "OggS")
{
cout<<"Cannot edit .ogg file :/"<<endl;
}
else if(_type == "fLaC")
{
auto buffer = getDataFromFile(_path);
vector <char> newCOMMENT, newIMAGES;
unsigned int nF = 0, nImg = 0;
for(Frame f : _frames)
if(f.flag == getFrameFlag(COVER_ART))
nImg++;
else
nF++;
push(newCOMMENT, toBytes(nF, true));
for(Frame f : _frames)
{
if(f.flag != getFrameFlag(COVER_ART))
{
push(newCOMMENT, toBytes(f.flag.size() + 1 + f.data.size(), true));
push(newCOMMENT, f.flag + "=");
push(newCOMMENT, f.data);
}
}
bool wasLast = bitset<8>(buffer[_chunks[_chunks.size()-1].pos - 4])[7];//if the last image was the last chunck
const int diffSize = newCOMMENT.size() - _chunks[CHUNK_COMMENT_LIST].size;
ofstream of(savePath, ios::binary);
if(of)
{
of.write(&buffer[0], _chunks[CHUNK_COMMENT].pos - 4);
if(nImg == 0 && wasLast)
of.write(&toBytes(bitset<8>(std::string("10000100")).to_ulong(), true)[0], 1);
else
of.write(&toBytes(bitset<8>(std::string("00000100")).to_ulong(), true)[0], 1);
of.write(&toBytes(_chunks[CHUNK_COMMENT].size + diffSize, false)[1], 3);
of.write(&buffer[_chunks[CHUNK_COMMENT].pos], _chunks[CHUNK_COMMENT].size - _chunks[CHUNK_COMMENT_LIST].size);
of.write(&newCOMMENT[0], newCOMMENT.size());
unsigned int i = 0;
for(Frame f : _frames)
{
if(f.flag == getFrameFlag(COVER_ART))
{
i++;
if(i == nImg && wasLast)
of.write(&toBytes(bitset<8>(std::string("10000110")).to_ulong(), true)[0], 1);
else
of.write(&toBytes(bitset<8>(std::string("00000110")).to_ulong(), true)[0], 1);
of.write(&toBytes(f.data.size(), false)[1], 3);
of.write(&f.data[0], f.data.size());
}
}
of.write(&buffer[_chunks[_chunks.size()-1].end()], buffer.size() - _chunks[_chunks.size()-1].end());
of.close();
}
}
parse();
}
unsigned int VorbisComment::getDuration()
{
if(_duration != (unsigned int)-1)
return _duration;
else
return 0;
}
void VorbisComment::parse()
{
_frames.clear();
ifstream f(_path,ios::binary);
if(f)
{
f.seekg(0,f.end);
_fileSize = f.tellg();
f.seekg(0, f.beg);
vector <char> bytes(4);
char byte;
f.read(&bytes[0],4);
_type = toStr(bytes);
string flagStart;
vector <char> testFlagStart;
unsigned int startVC = 0, endVC = 0;
if(_type == "OggS")
{
flagStart = "vorbis";
testFlagStart = vector <char>(flagStart.size());
for(int i=f.tellg();i<600;i++)
{
f.seekg(i, f.beg);
/*f.read(&bytes[0],4);
if(bytesToInt(bytes)<600)
cout<<"pos : "<<f.tellg()-4<<", size : "<<bytesToInt(bytes)<<", end : "<<f.tellg() + bytesToInt(bytes)<<endl;*/
f.read(&testFlagStart[0],testFlagStart.size());
if(toStr(testFlagStart) == flagStart)
{
if(_type == "OggS")
{
f.seekg((unsigned int)f.tellg() - (unsigned int)testFlagStart.size() - 1, f.beg);
f.read(&byte,1);
f.seekg((unsigned int)f.tellg() + (unsigned int)testFlagStart.size(), f.beg);
if(byte == 0x01)
{
f.read(&bytes[0],4);
f.seekg(bytesToInt(bytes, true) + 1, f.cur);
f.read(&bytes[0],4);
f.seekg(4, f.cur);
_samplerate = bytesToInt(bytes, true);
f.read(&bytes[0],4);
f.seekg(4, f.cur);
_bitrate = bytesToInt(bytes, true);
}
else
{
f.read(&bytes[0],4);
f.seekg(bytesToInt(bytes, true) + 4, f.cur);
break;
}
}
}
}
}
else if(_type == "fLaC")
{
while(1)
{
f.read(&byte,1);
string bitsStr = bitset<8>(byte).to_string();
bitset<7> otherBits(bitsStr.substr(1));
unsigned int blockType = otherBits.to_ulong();
bytes.resize(3);
f.read(&bytes[0],bytes.size());
unsigned int sizeBlock = bytesToInt(bytes,false);
unsigned int endBlock = (unsigned int)f.tellg() + sizeBlock;
//cout<<"Pos : "<<f.tellg()<<", size : "<<sizeBlock<<", type : "<<blockType<<endl;
if(blockType == 127)
break;
else if(blockType == 0)//STREAMINFO
{
f.seekg(2 + 2 + 3 + 3,f.cur);
bytes.resize(3);
f.read(&bytes[0], bytes.size());
_samplerate = bitset<20>(bitset<8>(bytes[0]).to_string() +
bitset<8>(bytes[1]).to_string() +
bitset<8>(bytes[2]).to_string().substr(0,4)).to_ulong();
string temp = bitset<8>(bytes[2]).to_string().substr(7);
bytes.resize(5);
f.read(&bytes[0], bytes.size());
unsigned long long bitsPerSample = bitset<5>(temp + bitset<8>(bytes[0]).to_string().substr(0,4)).to_ulong() + 1;
unsigned long long nSamples = bitset<36>( bitset<8>(bytes[0]).to_string().substr(4) +
bitset<8>(bytes[1]).to_string() +
bitset<8>(bytes[2]).to_string() +
bitset<8>(bytes[3]).to_string() +
bitset<8>(bytes[4]).to_string()).to_ulong();
_duration = nSamples/_samplerate;
_bitrate = bitsPerSample*_samplerate;
}
else if(blockType == 1);//PADDING
else if(blockType == 4)//VORBIS_COMMENT
{
_chunks[CHUNK_COMMENT] = Chunk(f.tellg(), sizeBlock);
bytes.resize(4);
f.read(&bytes[0], bytes.size());
f.seekg(bytesToInt(bytes,true), f.cur);//ref
_chunks[CHUNK_COMMENT_LIST] = Chunk(f.tellg(), _chunks[CHUNK_COMMENT].end() - f.tellg());
f.read(&bytes[0], bytes.size());
//const unsigned int nFrames = bytesToInt(bytes,true);//useless
startVC = f.tellg();
endVC = endBlock;
}
else if(blockType == 6)//PICTURE
{
_chunks.push_back(Chunk(f.tellg(), sizeBlock));
bytes.resize(sizeBlock);
f.read(&bytes[0], bytes.size());
_frames.push_back(Frame(getFrameFlag(COVER_ART), bytes));
}
f.seekg(endBlock, f.beg);
if(bitsStr[0]=='1')//last block
break;
}
_posEndTag = f.tellg();
if(_duration == 0)
_duration = (_fileSize-_posEndTag)/2.0 * 1/1.22/_samplerate;//un peu au pif
}
else
{
cerr<<"Vorbis Comment : ERROR : No tag in : "<<_path<<endl;
_state = Tag::NO_TAG;
return;
}
if(startVC != 0)
f.seekg(startVC, f.beg);
bytes.resize(4);
while(1)
{
if(endVC != 0 && f.tellg()>=endVC)
break;
f.read(&bytes[0],4);
const unsigned int sizeFrame = bytesToInt(bytes, true);
if(sizeFrame == 1)//bit de fin
break;
else if(sizeFrame > 1000000)
{
cerr<<"Vorbis Comment : ERROR : frame : invalid size : (pos : "<<f.tellg()<<")"<<endl;
break;
}
else if(sizeFrame == 0)
{
cerr<<"Vorbis Comment : ERROR : framing bit : not set : (pos : "<<f.tellg()<<")"<<endl;
break;
}
vector <char> frame(sizeFrame);
f.read(&frame[0],sizeFrame);
auto it = find(frame.begin(),frame.end(),'=');
_frames.push_back(Frame(std::string(frame.begin(), it), vector <char>(it+1, frame.end())));
}
f.close();
_state = Tag::OK;
}
else
{
cerr<<"Vorbis Comment : ERROR : Cannot read file : "<<_path<<endl;
_state = Tag::NO_FILE;
}
}