MythTV  master
metaiowavpack.cpp
Go to the documentation of this file.
1 #include <cmath>
2 
3 #include <apetag.h>
4 #include <apeitem.h>
5 
6 #include <mythcontext.h>
7 
8 // libmythmetadata
9 #include "metaiowavpack.h"
10 #include "musicmetadata.h"
11 #include "musicutils.h"
12 
19 TagLib::WavPack::File *MetaIOWavPack::OpenFile(const QString &filename)
20 {
21  QByteArray fname = filename.toLocal8Bit();
22  TagLib::WavPack::File *wpfile = new TagLib::WavPack::File(fname.constData());
23 
24  if (!wpfile->isOpen())
25  {
26  delete wpfile;
27  wpfile = nullptr;
28  }
29 
30  return wpfile;
31 }
32 
33 
37 bool MetaIOWavPack::write(const QString &filename, MusicMetadata* mdata)
38 {
39  if (!mdata)
40  return false;
41 
42  if (filename.isEmpty())
43  return false;
44 
45  m_filename = filename;
46 
47  TagLib::WavPack::File *wpfile = OpenFile(m_filename);
48 
49  if (!wpfile)
50  return false;
51 
52  TagLib::APE::Tag *tag = wpfile->APETag();
53 
54  if (!tag)
55  {
56  delete wpfile;
57  return false;
58  }
59 
60  WriteGenericMetadata(tag, mdata);
61 
62  // Compilation Artist ("Album artist")
63  if (mdata->Compilation())
64  {
65  TagLib::String key = "Album artist";
66  TagLib::APE::Item item = TagLib::APE::Item(key,
68  tag->setItem(key, item);
69  }
70  else
71  tag->removeItem("Album artist");
72 
74  bool result = wpfile->save();
76 
77  delete wpfile;
78 
79  return (result);
80 }
81 
85 MusicMetadata* MetaIOWavPack::read(const QString &filename)
86 {
87  TagLib::WavPack::File *wpfile = OpenFile(filename);
88 
89  if (!wpfile)
90  return nullptr;
91 
92  TagLib::APE::Tag *tag = wpfile->APETag();
93 
94  if (!tag)
95  {
96  delete wpfile;
97  return nullptr;
98  }
99 
100  MusicMetadata *metadata = new MusicMetadata(filename);
101 
102  ReadGenericMetadata(tag, metadata);
103 
104  bool compilation = false;
105 
106  // Compilation Artist ("Album artist")
107  if(tag->itemListMap().contains("Album artist"))
108  {
109  compilation = true;
110  QString compilation_artist = TStringToQString(
111  tag->itemListMap()["Album artist"].toString()).trimmed();
112  metadata->setCompilationArtist(compilation_artist);
113  }
114 
115  metadata->setCompilation(compilation);
116 
117  if (metadata->Length() <= 0)
118  metadata->setLength(getTrackLength(wpfile));
119 
120  delete wpfile;
121 
122  return metadata;
123 }
bool write(const QString &filename, MusicMetadata *mdata) override
Writes all metadata back to a file.
void setLength(int llength)
int Length() const
MusicMetadata * read(const QString &filename) override
Reads MusicMetadata from a file.
#define TStringToQString(s)
Definition: musicutils.h:16
void WriteGenericMetadata(TagLib::Tag *tag, const MusicMetadata *metadata)
Writes metadata common to all tag formats to the tag.
void setCompilation(bool state)
TagLib::WavPack::File * OpenFile(const QString &filename)
Open the file to read the tag.
void saveTimeStamps(void)
Definition: metaio.cpp:218
void ReadGenericMetadata(TagLib::Tag *tag, MusicMetadata *metadata)
Writes metadata common to all tag formats to the tag.
bool Compilation() const
int getTrackLength(TagLib::File *file)
Find the length of the track (in seconds)
QString m_filename
Definition: metaio.h:181
QString CompilationArtist() const
void setCompilationArtist(const QString &lcompilation_artist, const QString &lcompilation_artist_sort=nullptr)
#define QStringToTString(s)
Definition: musicutils.h:14
void restoreTimeStamps(void)
Definition: metaio.cpp:227