Tag Parser  8.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
id3v2tag.h
Go to the documentation of this file.
1 #ifndef TAG_PARSER_ID3V2TAG_H
2 #define TAG_PARSER_ID3V2TAG_H
3 
4 #include "./id3v2frame.h"
5 
6 #include "../fieldbasedtag.h"
7 
8 #include <map>
9 
10 namespace TagParser {
11 
12 class Id3v2Tag;
13 
15  bool operator()(uint32 lhs, uint32 rhs) const;
16 };
17 
19  friend class Id3v2Tag;
20 
21 public:
22  void make(std::ostream &stream, uint32 padding, Diagnostics &diag);
23  const Id3v2Tag &tag() const;
24  uint64 requiredSize() const;
25 
26 private:
27  Id3v2TagMaker(Id3v2Tag &tag, Diagnostics &diag);
28 
29  Id3v2Tag &m_tag;
30  uint32 m_framesSize;
31  uint32 m_requiredSize;
32  std::vector<Id3v2FrameMaker> m_maker;
33 };
34 
38 inline const Id3v2Tag &Id3v2TagMaker::tag() const
39 {
40  return m_tag;
41 }
42 
47 inline uint64 Id3v2TagMaker::requiredSize() const
48 {
49  return m_requiredSize;
50 }
51 
56 public:
59 };
60 
61 class TAG_PARSER_EXPORT Id3v2Tag : public FieldMapBasedTag<Id3v2Tag> {
62  friend class FieldMapBasedTag<Id3v2Tag>;
63 
64 public:
65  Id3v2Tag();
66 
67  static constexpr TagType tagType = TagType::Id3v2Tag;
68  static constexpr const char *tagName = "ID3v2 tag";
69  static constexpr TagTextEncoding defaultTextEncoding = TagTextEncoding::Utf16LittleEndian;
70  TagTextEncoding proposedTextEncoding() const override;
71  bool canEncodingBeUsed(TagTextEncoding encoding) const override;
72  bool supportsDescription(KnownField field) const override;
73  bool supportsMimeType(KnownField field) const override;
74  bool supportsMultipleValues(KnownField field) const override;
75 
76  void parse(std::istream &sourceStream, const uint64 maximalSize, Diagnostics &diag);
77  Id3v2TagMaker prepareMaking(Diagnostics &diag);
78  void make(std::ostream &targetStream, uint32 padding, Diagnostics &diag);
79 
80  byte majorVersion() const;
81  byte revisionVersion() const;
82  void setVersion(byte majorVersion, byte revisionVersion);
83  bool isVersionSupported() const;
84  byte flags() const;
85  bool isUnsynchronisationUsed() const;
86  bool hasExtendedHeader() const;
87  bool isExperimental() const;
88  bool hasFooter() const;
89  uint32 extendedHeaderSize() const;
90  uint32 paddingSize() const;
91 
92 protected:
93  IdentifierType internallyGetFieldId(KnownField field) const;
94  KnownField internallyGetKnownField(const IdentifierType &id) const;
95  TagDataType internallyGetProposedDataType(const uint32 &id) const;
96  std::vector<const TagValue *> internallyGetValues(const IdentifierType &id) const;
97  bool internallySetValues(const IdentifierType &id, const std::vector<TagValue> &values);
98 
99 private:
100  byte m_majorVersion;
101  byte m_revisionVersion;
102  byte m_flags;
103  uint32 m_sizeExcludingHeader;
104  uint32 m_extendedHeaderSize;
105  uint32 m_paddingSize;
106 };
107 
112  : m_majorVersion(4)
113  , m_revisionVersion(0)
114  , m_flags(0)
115  , m_sizeExcludingHeader(0)
116  , m_extendedHeaderSize(0)
117  , m_paddingSize(0)
118 {
119 }
120 
122 {
123  return m_majorVersion > 3 ? TagTextEncoding::Utf8 : TagTextEncoding::Utf16LittleEndian;
124 }
125 
126 inline bool Id3v2Tag::canEncodingBeUsed(TagTextEncoding encoding) const
127 {
128  return encoding == TagTextEncoding::Latin1 || (encoding == TagTextEncoding::Utf8 && m_majorVersion > 3)
130 }
131 
133 {
134  switch (field) {
135  case KnownField::Cover:
136  case KnownField::Lyrics:
138  return true;
139  default:
140  return false;
141  }
142 }
143 
144 inline bool Id3v2Tag::supportsMimeType(KnownField field) const
145 {
146  return field == KnownField::Cover;
147 }
148 
152 inline byte Id3v2Tag::majorVersion() const
153 {
154  return m_majorVersion;
155 }
156 
160 inline byte Id3v2Tag::revisionVersion() const
161 {
162  return m_revisionVersion;
163 }
164 
171 inline bool Id3v2Tag::isVersionSupported() const
172 {
173  return m_majorVersion == 2 || m_majorVersion == 3 || m_majorVersion == 4;
174 }
175 
179 inline byte Id3v2Tag::flags() const
180 {
181  return m_flags;
182 }
183 
188 {
189  return m_flags & 0x80;
190 }
191 
195 inline bool Id3v2Tag::hasExtendedHeader() const
196 {
197  return (m_majorVersion >= 3) && (m_flags & 0x40);
198 }
199 
203 inline bool Id3v2Tag::isExperimental() const
204 {
205  return (m_majorVersion >= 3) && (m_flags & 0x20);
206 }
207 
211 inline bool Id3v2Tag::hasFooter() const
212 {
213  return (m_majorVersion >= 3) && (m_flags & 0x10);
214 }
215 
219 inline uint32 Id3v2Tag::extendedHeaderSize() const
220 {
221  return m_extendedHeaderSize;
222 }
223 
228 inline uint32 Id3v2Tag::paddingSize() const
229 {
230  return m_paddingSize;
231 }
232 
233 } // namespace TagParser
234 
235 #endif // TAG_PARSER_ID3V2TAG_H
bool supportsDescription(KnownField field) const override
Returns an indications whether the specified field supports descriptions.
Definition: id3v2tag.h:132
The FieldMapBasedTag provides a generic implementation of Tag which stores the tag fields using std::...
Definition: fieldbasedtag.h:31
uint32 paddingSize() const
Returns the size of the padding between the tag and the first MPEG frame if one is present; otherwise...
Definition: id3v2tag.h:228
TagTextEncoding proposedTextEncoding() const override
Returns the proposed text encoding.
Definition: id3v2tag.h:121
bool isUnsynchronisationUsed() const
Returns an indication whether unsynchronisation is used.
Definition: id3v2tag.h:187
TagDataType
Specifies the data type.
Definition: tagvalue.h:53
byte flags() const
Returns the flags read from the ID3v2 header.
Definition: id3v2tag.h:179
const Id3v2Tag & tag() const
Returns the associated tag.
Definition: id3v2tag.h:38
KnownField
Specifies the field.
Definition: tag.h:40
byte revisionVersion() const
Returns the revision version if known; otherwise returns 0.
Definition: id3v2tag.h:160
Defines traits for the specified ImplementationType.
Definition: fieldbasedtag.h:17
The Id3v2Frame class is used by Id3v2Tag to store the fields.
Definition: id3v2frame.h:86
uint64 requiredSize() const
Returns the number of bytes which will be written when making the tag.
Definition: id3v2tag.h:47
The Id3v2TagMaker class helps writing ID3v2 tags.
Definition: id3v2tag.h:18
Id3v2Tag()
Constructs a new tag.
Definition: id3v2tag.h:111
bool isVersionSupported() const
Returns an indication whether the version is supported by the Id3v2Tag class.
Definition: id3v2tag.h:171
bool hasFooter() const
Returns an indication whether a footer is present.
Definition: id3v2tag.h:211
bool hasExtendedHeader() const
Returns an indication whether an extended header is used.
Definition: id3v2tag.h:195
bool supportsMimeType(KnownField field) const override
Returns an indications whether the specified field supports mime types.
Definition: id3v2tag.h:144
uint32 extendedHeaderSize() const
Returns the size of the extended header if one is present; otherwise returns 0.
Definition: id3v2tag.h:219
bool canEncodingBeUsed(TagTextEncoding encoding) const override
Returns an indication whether the specified encoding can be used to provide string values for the tag...
Definition: id3v2tag.h:126
byte majorVersion() const
Returns the major version if known; otherwise returns 0.
Definition: id3v2tag.h:152
bool isExperimental() const
Returns an indication whether the tag is labeled as experimental.
Definition: id3v2tag.h:203
Implementation of TagParser::Tag for ID3v2 tags.
Definition: id3v2tag.h:61
Defines the order which is used to store ID3v2 frames.
Definition: id3v2tag.h:14
TagType
Specifies the tag type.
Definition: tag.h:20
TagTextEncoding
Specifies the text encoding.
Definition: tagvalue.h:24
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156