Audacity  2.3.1
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
Tags Class Referencefinal

ID3 Tags (for MP3) More...

#include <Tags.h>

Inheritance diagram for Tags:
XMLTagHandler

Public Types

using Iterators = IteratorRange< TagMap::const_iterator >
 

Public Member Functions

 Tags ()
 
 Tags (const Tags &)=default
 
virtual ~Tags ()
 
std::shared_ptr< TagsDuplicate () const
 
Tagsoperator= (const Tags &src)
 
bool ShowEditDialog (wxWindow *parent, const wxString &title, bool force=false)
 
bool HandleXMLTag (const wxChar *tag, const wxChar **attrs) override
 
XMLTagHandlerHandleXMLChild (const wxChar *tag) override
 
void WriteXML (XMLWriter &xmlFile) const
 
void AllowEditTitle (bool editTitle)
 
void AllowEditTrackNumber (bool editTrackNumber)
 
void LoadDefaultGenres ()
 
void LoadGenres ()
 
int GetNumUserGenres ()
 
wxString GetUserGenre (int value)
 
wxString GetGenre (int value)
 
int GetGenre (const wxString &name)
 
bool HasTag (const wxString &name) const
 
wxString GetTag (const wxString &name) const
 
Iterators GetRange () const
 
void SetTag (const wxString &name, const wxString &value)
 
void SetTag (const wxString &name, const int &value)
 
bool IsEmpty ()
 
void Clear ()
 
- Public Member Functions inherited from XMLTagHandler
 XMLTagHandler ()
 
virtual ~XMLTagHandler ()
 
virtual void HandleXMLEndTag (const wxChar *WXUNUSED(tag))
 
virtual void HandleXMLContent (const wxString &WXUNUSED(content))
 
bool ReadXMLTag (const char *tag, const char **attrs)
 
void ReadXMLEndTag (const char *tag)
 
void ReadXMLContent (const char *s, int len)
 
XMLTagHandlerReadXMLChild (const char *tag)
 

Private Member Functions

void LoadDefaults ()
 

Private Attributes

TagMap mXref
 
TagMap mMap
 
wxArrayString mGenres
 
bool mEditTitle
 
bool mEditTrackNumber
 

Friends

bool operator== (const Tags &lhs, const Tags &rhs)
 

Detailed Description

ID3 Tags (for MP3)

This class started as an ID3 tag

This class holds a few informational tags, such as Title, Author, etc. that can be associated with a project or other audio file. It is modeled after the ID3 format for MP3 files, and it can both import and export ID3 tags from/to MP2, MP3, and AIFF files.

It can present the user with a dialog for editing this information.

Use of this functionality requires that libid3tag be compiled in with Audacity.

Definition at line 70 of file Tags.h.

Member Typedef Documentation

using Tags::Iterators = IteratorRange<TagMap::const_iterator>

Definition at line 103 of file Tags.h.

Constructor & Destructor Documentation

Tags::Tags ( )

Definition at line 224 of file Tags.cpp.

References LoadDefaults(), LoadGenres(), mEditTitle, and mEditTrackNumber.

225 {
226  mEditTitle = true;
227  mEditTrackNumber = true;
228 
229  LoadDefaults();
230  LoadGenres();
231 }
void LoadGenres()
Definition: Tags.cpp:364
bool mEditTrackNumber
Definition: Tags.h:123
bool mEditTitle
Definition: Tags.h:122
void LoadDefaults()
Definition: Tags.cpp:258
Tags::Tags ( const Tags )
default
Tags::~Tags ( )
virtual

Definition at line 233 of file Tags.cpp.

234 {
235 }

Member Function Documentation

void Tags::AllowEditTitle ( bool  editTitle)

Definition at line 341 of file Tags.cpp.

References mEditTitle.

342 {
343  mEditTitle = editTitle;
344 }
bool mEditTitle
Definition: Tags.h:122
void Tags::AllowEditTrackNumber ( bool  editTrackNumber)

Definition at line 346 of file Tags.cpp.

References mEditTrackNumber.

347 {
348  mEditTrackNumber = editTrackNumber;
349 }
bool mEditTrackNumber
Definition: Tags.h:123
void Tags::Clear ( )
std::shared_ptr< Tags > Tags::Duplicate ( ) const

Definition at line 237 of file Tags.cpp.

Referenced by EditActions::DoEditMetadata().

238 {
239  return std::make_shared<Tags>(*this);
240 }
wxString Tags::GetGenre ( int  value)

Definition at line 391 of file Tags.cpp.

References DefaultGenres.

Referenced by PCMImportFileHandle::Import(), and MP3ImportFileHandle::ImportID3().

392 {
393  int cnt = WXSIZEOF(DefaultGenres);
394 
395  if (i >= 0 && i < cnt) {
396  return DefaultGenres[i];
397  }
398 
399  return wxT("");
400 }
static const wxChar * DefaultGenres[]
Definition: Tags.cpp:69
int Tags::GetGenre ( const wxString &  name)

Definition at line 402 of file Tags.cpp.

References DefaultGenres.

403 {
404  int cnt = WXSIZEOF(DefaultGenres);
405 
406  for (int i = 0; i < cnt; i++) {
407  if (name.CmpNoCase(DefaultGenres[i])) {
408  return i;
409  }
410  }
411 
412  return 255;
413 }
static const wxChar * DefaultGenres[]
Definition: Tags.cpp:69
wxString name
Definition: Tags.cpp:734
int Tags::GetNumUserGenres ( )

Definition at line 351 of file Tags.cpp.

References mGenres.

Referenced by GetUserGenre(), TagsEditor::OnEdit(), TagsEditor::OnReset(), and TagsEditor::PopulateGenres().

352 {
353  return mGenres.GetCount();
354 }
wxArrayString mGenres
Definition: Tags.h:120
Tags::Iterators Tags::GetRange ( ) const

Definition at line 444 of file Tags.cpp.

References mMap.

Referenced by ExportPCM::AddID3Chunk(), ExportMP3::AddTags(), ExportOGG::FillComment(), ExportFLAC::GetMetadata(), TagsEditor::OnSaveDefaults(), and TagsEditor::TransferDataToWindow().

445 {
446  return { mMap.begin(), mMap.end() };
447 }
TagMap mMap
Definition: Tags.h:118
wxString Tags::GetTag ( const wxString &  name) const

Definition at line 424 of file Tags.cpp.

References mMap, mXref, and name.

Referenced by ExportPCM::AddStrings(), PCMImportFileHandle::Import(), MP3ImportFileHandle::ImportID3(), TagsEditor::OnLoad(), TagsEditor::OnSave(), TagsEditor::OnSaveDefaults(), ExportFFmpeg::SetMetadata(), and TagsEditor::TransferDataToWindow().

425 {
426  wxString key = name;
427  key.UpperCase();
428 
429  auto iter = mXref.find(key);
430 
431  if (iter == mXref.end()) {
432  return wxEmptyString;
433  }
434 
435  auto iter2 = mMap.find(iter->second);
436  if (iter2 == mMap.end()) {
437  wxASSERT(false);
438  return wxEmptyString;
439  }
440  else
441  return iter2->second;
442 }
TagMap mXref
Definition: Tags.h:117
TagMap mMap
Definition: Tags.h:118
wxString name
Definition: Tags.cpp:734
wxString Tags::GetUserGenre ( int  value)

Definition at line 382 of file Tags.cpp.

References GetNumUserGenres(), and mGenres.

Referenced by TagsEditor::OnEdit(), TagsEditor::OnReset(), and TagsEditor::PopulateGenres().

383 {
384  if (i >= 0 && i < GetNumUserGenres()) {
385  return mGenres[i];
386  }
387 
388  return wxT("");
389 }
int GetNumUserGenres()
Definition: Tags.cpp:351
wxArrayString mGenres
Definition: Tags.h:120
XMLTagHandler * Tags::HandleXMLChild ( const wxChar *  tag)
overridevirtual

Implements XMLTagHandler.

Definition at line 546 of file Tags.cpp.

547 {
548  if (wxStrcmp(tag, wxT("tags")) == 0) {
549  return this;
550  }
551 
552  if (wxStrcmp(tag, wxT("tag")) == 0) {
553  return this;
554  }
555 
556  return NULL;
557 }
bool Tags::HandleXMLTag ( const wxChar *  tag,
const wxChar **  attrs 
)
overridevirtual

Implements XMLTagHandler.

Definition at line 505 of file Tags.cpp.

References XMLValueChecker::IsGoodString(), and SetTag().

506 {
507  if (wxStrcmp(tag, wxT("tags")) == 0) {
508  return true;
509  }
510 
511  if (wxStrcmp(tag, wxT("tag")) == 0) {
512  wxString n, v;
513 
514  while (*attrs) {
515  wxString attr = *attrs++;
516  if (attr.IsEmpty())
517  break;
518  wxString value = *attrs++;
519 
520  if (!XMLValueChecker::IsGoodString(attr) ||
522  break;
523  }
524 
525  if (attr == wxT("name")) {
526  n = value;
527  }
528  else if (attr == wxT("value")) {
529  v = value;
530  }
531  }
532 
533  if (n == wxT("id3v2")) {
534  // LLL: This is obsolete, but it must be handled and ignored.
535  }
536  else {
537  SetTag(n, v);
538  }
539 
540  return true;
541  }
542 
543  return false;
544 }
void SetTag(const wxString &name, const wxString &value)
Definition: Tags.cpp:449
static bool IsGoodString(const wxString &str)
bool Tags::HasTag ( const wxString &  name) const

Definition at line 415 of file Tags.cpp.

References mXref, and name.

Referenced by ExportPCM::AddStrings(), PCMImportFileHandle::Import(), OggImportFileHandle::Import(), FLACImportFileHandle::Import(), MP3ImportFileHandle::ImportID3(), IsEmpty(), and ExportFFmpeg::SetMetadata().

416 {
417  wxString key = name;
418  key.UpperCase();
419 
420  auto iter = mXref.find(key);
421  return (iter != mXref.end());
422 }
TagMap mXref
Definition: Tags.h:117
wxString name
Definition: Tags.cpp:734
bool Tags::IsEmpty ( )

Definition at line 289 of file Tags.cpp.

References HasTag(), TAG_ALBUM, TAG_ARTIST, and TAG_TITLE.

290 {
291  // At least one of these should be filled in, otherwise
292  // it's assumed that the tags have not been set...
294  return false;
295  }
296 
297  return true;
298 }
#define TAG_TITLE
Definition: Tags.h:60
#define TAG_ARTIST
Definition: Tags.h:61
#define TAG_ALBUM
Definition: Tags.h:62
bool HasTag(const wxString &name) const
Definition: Tags.cpp:415
void Tags::LoadDefaultGenres ( )

Definition at line 356 of file Tags.cpp.

References DefaultGenres, and mGenres.

Referenced by LoadGenres(), and TagsEditor::OnReset().

357 {
358  mGenres.Clear();
359  for (size_t i = 0; i < WXSIZEOF(DefaultGenres); i++) {
360  mGenres.Add(DefaultGenres[i]);
361  }
362 }
wxArrayString mGenres
Definition: Tags.h:120
static const wxChar * DefaultGenres[]
Definition: Tags.cpp:69
void Tags::LoadDefaults ( )
private

Definition at line 258 of file Tags.cpp.

References gPrefs, name, and SetTag().

Referenced by Tags().

259 {
260  wxString path;
261  wxString name;
262  wxString value;
263  long ndx;
264  bool cont;
265 
266  // Set the parent group
267  path = gPrefs->GetPath();
268  gPrefs->SetPath(wxT("/Tags"));
269 
270  // Process all entries in the group
271  cont = gPrefs->GetFirstEntry(name, ndx);
272  while (cont) {
273  gPrefs->Read(name, &value, wxT(""));
274 
275  if (name == wxT("ID3V2")) {
276  // LLL: This is obsolute, but it must be handled and ignored.
277  }
278  else {
279  SetTag(name, value);
280  }
281 
282  cont = gPrefs->GetNextEntry(name, ndx);
283  }
284 
285  // Restore original group
286  gPrefs->SetPath(path);
287 }
void SetTag(const wxString &name, const wxString &value)
Definition: Tags.cpp:449
AudacityPrefs * gPrefs
Definition: Prefs.cpp:73
wxString name
Definition: Tags.cpp:734
void Tags::LoadGenres ( )

Definition at line 364 of file Tags.cpp.

References FileNames::DataDir(), LoadDefaultGenres(), and mGenres.

Referenced by TagsEditor::OnEdit(), TagsEditor::OnReset(), and Tags().

365 {
366  wxFileName fn(FileNames::DataDir(), wxT("genres.txt"));
367  wxTextFile tf(fn.GetFullPath());
368 
369  if (!tf.Exists() || !tf.Open()) {
371  return;
372  }
373 
374  mGenres.Clear();
375 
376  int cnt = tf.GetLineCount();
377  for (int i = 0; i < cnt; i++) {
378  mGenres.Add(tf.GetLine(i));
379  }
380 }
void LoadDefaultGenres()
Definition: Tags.cpp:356
wxArrayString mGenres
Definition: Tags.h:120
static wxString DataDir()
Audacity user data directory.
Definition: FileNames.cpp:146
Tags & Tags::operator= ( const Tags src)

Definition at line 242 of file Tags.cpp.

References mEditTitle, mEditTrackNumber, mGenres, mMap, and mXref.

243 {
244  mEditTitle = src.mEditTitle;
246 
247  mXref.clear();
248  mXref = src.mXref;
249  mMap.clear();
250  mMap = src.mMap;
251 
252  mGenres.clear();
253  mGenres = src.mGenres;
254 
255  return *this;
256 }
TagMap mXref
Definition: Tags.h:117
bool mEditTrackNumber
Definition: Tags.h:123
wxArrayString mGenres
Definition: Tags.h:120
bool mEditTitle
Definition: Tags.h:122
TagMap mMap
Definition: Tags.h:118
void Tags::SetTag ( const wxString &  name,
const wxString &  value 
)

Definition at line 449 of file Tags.cpp.

References mMap, mXref, and name.

Referenced by ExportMultiple::ExportMultipleByLabel(), ExportMultiple::ExportMultipleByTrack(), FFmpegImportFileHandle::GetMetadata(), HandleXMLTag(), PCMImportFileHandle::Import(), OggImportFileHandle::Import(), FLACImportFileHandle::Import(), MP3ImportFileHandle::ImportID3(), LoadDefaults(), TagsEditor::OnLoad(), TagsEditor::OnSave(), TagsEditor::OnSaveDefaults(), GStreamerImportFileHandle::OnTag(), SetTag(), and TagsEditor::TransferDataFromWindow().

450 {
451  // We don't like empty names
452  if (name.IsEmpty()) {
453  return;
454  }
455 
456  // Tag name must be ascii
457  if (!name.IsAscii()) {
458  wxLogError("Tag rejected (Non-ascii character in name)");
459  return;
460  }
461 
462  // All keys are uppercase
463  wxString key = name;
464  key.UpperCase();
465 
466  // Look it up
467  TagMap::iterator iter = mXref.find(key);
468 
469  if (value.IsEmpty()) {
470  // Erase the tag
471  if (iter == mXref.end())
472  // nothing to do
473  ;
474  else {
475  mMap.erase(iter->second);
476  mXref.erase(iter);
477  }
478  }
479  else {
480  if (iter == mXref.end()) {
481  // Didn't find the tag
482 
483  // Add a NEW tag
484  mXref[key] = name;
485  mMap[name] = value;
486  }
487  else if (!iter->second.IsSameAs(name)) {
488  // Watch out for case differences!
489  mMap[name] = value;
490  mMap.erase(iter->second);
491  iter->second = name;
492  }
493  else {
494  // Update the value
495  mMap[iter->second] = value;
496  }
497  }
498 }
TagMap mXref
Definition: Tags.h:117
TagMap mMap
Definition: Tags.h:118
wxString name
Definition: Tags.cpp:734
void Tags::SetTag ( const wxString &  name,
const int &  value 
)

Definition at line 500 of file Tags.cpp.

References SetTag().

501 {
502  SetTag(name, wxString::Format(wxT("%d"), value));
503 }
void SetTag(const wxString &name, const wxString &value)
Definition: Tags.cpp:449
wxString name
Definition: Tags.cpp:734
bool Tags::ShowEditDialog ( wxWindow *  parent,
const wxString &  title,
bool  force = false 
)

Definition at line 576 of file Tags.cpp.

References mEditTitle, and mEditTrackNumber.

Referenced by ExportMultiple::ExportMultipleByLabel(), and ExportMultiple::ExportMultipleByTrack().

577 {
578  if (force) {
579  TagsEditor dlg(parent, title, this, mEditTitle, mEditTrackNumber);
580 
581  return dlg.ShowModal() == wxID_OK;
582  }
583 
584  return true;
585 }
bool mEditTrackNumber
Definition: Tags.h:123
bool mEditTitle
Definition: Tags.h:122
Derived from ExpandingToolBar, this dialog allows editing of Tags.
Definition: Tags.h:129
void Tags::WriteXML ( XMLWriter xmlFile) const

Definition at line 559 of file Tags.cpp.

Referenced by TagsEditor::OnSave().

561 {
562  xmlFile.StartTag(wxT("tags"));
563 
564  for (const auto &pair : GetRange()) {
565  const auto &n = pair.first;
566  const auto &v = pair.second;
567  xmlFile.StartTag(wxT("tag"));
568  xmlFile.WriteAttr(wxT("name"), n);
569  xmlFile.WriteAttr(wxT("value"), v);
570  xmlFile.EndTag(wxT("tag"));
571  }
572 
573  xmlFile.EndTag(wxT("tags"));
574 }
virtual void StartTag(const wxString &name)
Definition: XMLWriter.cpp:78
virtual void WriteAttr(const wxString &name, const wxString &value)
Definition: XMLWriter.cpp:131
virtual void EndTag(const wxString &name)
Definition: XMLWriter.cpp:101
Iterators GetRange() const
Definition: Tags.cpp:444

Friends And Related Function Documentation

bool operator== ( const Tags lhs,
const Tags rhs 
)
friend

Definition at line 324 of file Tags.cpp.

325 {
326  if (!EqualMaps(lhs.mXref, rhs.mXref))
327  return false;
328 
329  if (!EqualMaps(lhs.mMap, rhs.mMap))
330  return false;
331 
332  return
333  lhs.mGenres == rhs.mGenres
334  &&
335  lhs.mEditTitle == rhs.mEditTitle
336  &&
338  ;
339 }
TagMap mXref
Definition: Tags.h:117
bool mEditTrackNumber
Definition: Tags.h:123
wxArrayString mGenres
Definition: Tags.h:120
bool mEditTitle
Definition: Tags.h:122
TagMap mMap
Definition: Tags.h:118

Member Data Documentation

bool Tags::mEditTitle
private

Definition at line 122 of file Tags.h.

Referenced by AllowEditTitle(), operator=(), operator==(), ShowEditDialog(), and Tags().

bool Tags::mEditTrackNumber
private

Definition at line 123 of file Tags.h.

Referenced by AllowEditTrackNumber(), operator=(), operator==(), ShowEditDialog(), and Tags().

wxArrayString Tags::mGenres
private
TagMap Tags::mMap
private

Definition at line 118 of file Tags.h.

Referenced by Clear(), GetRange(), GetTag(), operator=(), operator==(), and SetTag().

TagMap Tags::mXref
private

Definition at line 117 of file Tags.h.

Referenced by Clear(), GetTag(), HasTag(), operator=(), operator==(), and SetTag().


The documentation for this class was generated from the following files: