File: FlacInfo.cc

package info (click to toggle)
flactag 2.0.4-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,096 kB
  • ctags: 477
  • sloc: sh: 10,777; cpp: 3,283; makefile: 62; sed: 7
file content (446 lines) | stat: -rwxr-xr-x 10,065 bytes parent folder | download | duplicates (3)
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
/* --------------------------------------------------------------------------

   flactag -- A tagger for single album FLAC files with embedded CUE sheets
   						using data retrieved from the MusicBrainz service

   Copyright (C) 2006-2012 Andrew Hawkins
   Copyright (C) 2011-2012 Daniel Pocock

   This file is part of flactag.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
	 the Free Software Foundation, either version 3 of the License, or
	 (at your option) any later version.

   Flactag is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

     $Id$

----------------------------------------------------------------------------*/

#include "FlacInfo.h"

#include "TagName.h"

#include "ErrorLog.h"

#include <sstream>

CFlacInfo::CFlacInfo()
:	m_TagBlock(0),
	m_PictureBlock(0),
	m_CuesheetFound(false)
{
}

CFlacInfo::~CFlacInfo()
{
	if (m_TagBlock)
		delete m_TagBlock;

	if (m_PictureBlock)
		delete m_PictureBlock;
}

bool CFlacInfo::CuesheetFound() const
{
	return m_CuesheetFound;
}

void CFlacInfo::SetFileName(const std::string& FileName)
{
	m_Tags.clear();
	m_Cuesheet.Clear();
	m_CoverArt.Clear();

	m_FileName=FileName;

	if (m_TagBlock)
		delete m_TagBlock;

	m_TagBlock=0;

	if (m_PictureBlock)
		delete m_PictureBlock;

	m_PictureBlock=0;

	m_CuesheetFound=false;
}

bool CFlacInfo::Read()
{
	bool RetVal=false;

	m_Tags.clear();
	m_Cuesheet.Clear();
	m_CoverArt.Clear();

	if (m_TagBlock)
		delete m_TagBlock;

	m_TagBlock=0;

	if (m_PictureBlock)
		delete m_PictureBlock;

	m_PictureBlock=0;

	m_CuesheetFound=false;

	if (!m_FileName.empty())
	{
		if (m_Chain.read(m_FileName.c_str()))
		{
			if (m_Chain.is_valid())
			{
				FLAC::Metadata::Iterator Iterator;

				Iterator.init(m_Chain);

				if (Iterator.is_valid())
				{
					do
					{
						switch (Iterator.get_block_type())
						{
							case FLAC__METADATA_TYPE_STREAMINFO:
								break;

							case FLAC__METADATA_TYPE_PADDING:
								break;

							case FLAC__METADATA_TYPE_APPLICATION:
								break;

							case FLAC__METADATA_TYPE_SEEKTABLE:
								break;

							case FLAC__METADATA_TYPE_VORBIS_COMMENT:
								m_TagBlock=(FLAC::Metadata::VorbisComment *)Iterator.get_block();

								if (m_TagBlock->is_valid())
								{
									for (unsigned count=0;count<m_TagBlock->get_num_comments();count++)
									{
										FLAC::Metadata::VorbisComment::Entry Entry=m_TagBlock->get_comment(count);

										std::string Name(Entry.get_field_name(),Entry.get_field_name_length());
										std::string Value(Entry.get_field_value(),Entry.get_field_value_length());

										m_Tags[CTagName(Name)]=CUTF8Tag(Value);
									}
								}

								break;

							case FLAC__METADATA_TYPE_CUESHEET:
							{
								m_CuesheetFound=true;

								FLAC::Metadata::CueSheet *Cuesheet=(FLAC::Metadata::CueSheet *)Iterator.get_block();
								for (unsigned count=0;count<Cuesheet->get_num_tracks();count++)
								{
									FLAC::Metadata::CueSheet::Track Track=Cuesheet->get_track(count);

									if (Track.get_number()==170)
										m_Cuesheet.SetLeadout(CalculateOffset(Track));
									else
										m_Cuesheet.AddTrack(CCuesheetTrack(Track.get_number(),CalculateOffset(Track)));
								}

								delete Cuesheet;

								break;
							}

							case FLAC__METADATA_TYPE_UNDEFINED:
								break;

#ifdef FLAC_API_VERSION_CURRENT
							case FLAC__METADATA_TYPE_PICTURE:
								m_PictureBlock=(FLAC::Metadata::Picture *)Iterator.get_block();
								if (m_PictureBlock->is_valid())
									m_CoverArt.SetArt(m_PictureBlock->get_data(),m_PictureBlock->get_data_length(),true);
								break;
#endif

						}
					} while (Iterator.next());
				}
			}
		}
	}

	if(m_CuesheetFound)
		UpdateCuesheet();

	return RetVal;
}

bool CFlacInfo::UpdateCuesheet()
{
	std::map<int, std::string> Titles;
	std::map<int, std::string> Artists;
	std::map<int, int> trackMappings;
	tTagMap::iterator iterator;
	std::map<int, int>::iterator it2;

	for(iterator=m_Tags.begin(); iterator!=m_Tags.end(); iterator++)
	{
		const CTagName& tagName = iterator->first;
		const CUTF8Tag& tagValue = iterator->second;

		if(tagName == CTagName("ALBUM"))
			m_Cuesheet.setTitle(tagValue.UTF8Value());
		else if(tagName == CTagName("ARTIST"))
			m_Cuesheet.setPerformer(tagValue.UTF8Value());

		if(tagName.Number() > -1)
		{
			if(tagName.Name() == "TRACKNUMBER")
				trackMappings[tagName.Number()] = atoi(tagValue.DisplayValue().c_str());
			else if(tagName.Name() == "ARTIST")
				Artists[tagName.Number()] = tagValue.DisplayValue();
			else if(tagName.Name() == "TITLE")
				Titles[tagName.Number()] = tagValue.DisplayValue();
		}
	}

	for(it2=trackMappings.begin(); it2!=trackMappings.end(); it2++)
	{
		int trackNum = it2->second;
		m_Cuesheet.setTrackPerformer(trackNum, Artists[it2->first]);
		m_Cuesheet.setTrackTitle(trackNum, Titles[it2->first]);
	}
	return true;
}

tTagMap CFlacInfo::Tags() const
{
	return m_Tags;
}

CCoverArt CFlacInfo::CoverArt() const
{
	return m_CoverArt;
}

CCuesheet CFlacInfo::Cuesheet() const
{
	return m_Cuesheet;
}

std::string CFlacInfo::WriteError() const
{
	return m_WriteError;
}

int CFlacInfo::CalculateOffset(const FLAC::Metadata::CueSheet::Track& Track) const
{
	FLAC__uint64 Offset=Track.get_offset();
	FLAC__uint64 MaxIndexOffset=0;

	for (unsigned count=0;count<Track.get_num_indices();count++)
	{
		FLAC__StreamMetadata_CueSheet_Index Index=Track.get_index(count);

		if (Index.number==1)
			MaxIndexOffset=Index.offset;
	}

	Offset+=MaxIndexOffset;
	Offset/=588;
	Offset+=150;

	return Offset;
}

bool CFlacInfo::WriteInfo(const CWriteInfo& WriteInfo)
{
	bool RetVal=true;

	if (m_TagBlock)
	{
		while (m_TagBlock->get_num_comments())
			m_TagBlock->delete_comment(0);

		tTagMap Tags=WriteInfo.Tags();
		tTagMapConstIterator ThisTag=Tags.begin();

		while (Tags.end()!=ThisTag)
		{
			CTagName Name=(*ThisTag).first;
			CUTF8Tag Value=(*ThisTag).second;

			if (!Value.empty())
			{
				FLAC::Metadata::VorbisComment::Entry NewEntry;

				if (!NewEntry.set_field_name(Name.String().c_str()))
				{
					std::stringstream os;
					os << "Error setting field name: '" << Name.String() << "'";
					CErrorLog::Log(os.str());
				}

				if (!NewEntry.set_field_value(Value.UTF8Value().c_str(),Value.UTF8Value().length()))
				{
					std::stringstream os;
					os << "Error setting field value: '" << Value.DisplayValue() << "' for '" << Name.String() << "'";
					CErrorLog::Log(os.str());
				}

				if (!m_TagBlock->insert_comment(m_TagBlock->get_num_comments(),NewEntry))
				{
					std::stringstream os;
					os << "Error inserting comment: '" << Value.DisplayValue() << "' for '" << Name.String() << "'";
					CErrorLog::Log(os.str());
				}
			}

			++ThisTag;
		}

#ifdef FLAC_API_VERSION_CURRENT
		if (WriteInfo.CoverArt())
		{
			if (m_PictureBlock)
				SetPictureBlock(WriteInfo.CoverArt());
			else
			{
				FLAC::Metadata::Iterator Iterator;

				Iterator.init(m_Chain);

				if (Iterator.is_valid())
				{
					//Move to the end
					while (Iterator.next())
						;

					m_PictureBlock = new FLAC::Metadata::Picture;
					SetPictureBlock(WriteInfo.CoverArt());

					//insert_block_after takes ownership of the block
					//we need to get it back out again so we have ownership

					if (Iterator.insert_block_after(m_PictureBlock))
					{
						Iterator.next();

						if (Iterator.get_block_type()==FLAC__METADATA_TYPE_PICTURE)
						{
							m_PictureBlock=(FLAC::Metadata::Picture *)Iterator.get_block();
						}
					}
				}
			}
		}
		else
		{
			if (m_PictureBlock)
			{
				FLAC::Metadata::Iterator Iterator;

				Iterator.init(m_Chain);

				if (Iterator.is_valid())
				{
					bool Found=false;

					do
					{
						switch (Iterator.get_block_type())
						{
							case FLAC__METADATA_TYPE_PICTURE:
							{
								Found=true;
								RetVal=Iterator.delete_block(true);
								break;
							}

							default:
								break;
						}
					} while (!Found && Iterator.next());
				}
			}
		}

#endif
		if (RetVal)
		{
			RetVal=m_Chain.write();

			if (!RetVal)
			{
				FLAC::Metadata::Chain::Status Status=m_Chain.status();
				m_WriteError=Status.as_cstring();
			}
		}
	}
	else
	{
		m_WriteError.assign("no tag block exists in the FLAC file, create one with metaflac --set-tag=ARTIST=unset <filename.flac>");
		RetVal=false;
	}

	return RetVal;
}

void CFlacInfo::SetTag(const CTagName& Name, const std::string& Value)
{
	bool Found=false;

	for (unsigned count=0;count<m_TagBlock->get_num_comments();count++)
	{
		FLAC::Metadata::VorbisComment::Entry Entry=m_TagBlock->get_comment(count);
		std::string ThisField=Entry.get_field_name();

		if (ThisField==Name.String())
			Found=true;

		if (Found)
		{
			Entry.set_field_name(Name.String().c_str());
			Entry.set_field_value(Value.c_str(),Value.length());
			m_TagBlock->set_comment(count,Entry);
		}
	}

	if (!Found)
	{
		FLAC::Metadata::VorbisComment::Entry NewEntry;

		NewEntry.set_field_name(Name.String().c_str());
		NewEntry.set_field_value(Value.c_str(),Value.length());

		m_TagBlock->insert_comment(m_TagBlock->get_num_comments(),NewEntry);
	}
}

void CFlacInfo::SetPictureBlock(const CCoverArt& CoverArt)
{
	if (m_PictureBlock)
	{
		m_PictureBlock->set_type(FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER);
		unsigned char *TmpData=new unsigned char[CoverArt.Length()];

		memcpy(TmpData,CoverArt.Data(),CoverArt.Length());
		m_PictureBlock->set_data(TmpData,CoverArt.Length());
		delete[] TmpData;

		m_PictureBlock->set_width(CoverArt.Width());
		m_PictureBlock->set_height(CoverArt.Height());
		m_PictureBlock->set_mime_type("image/jpeg");
	}
}