1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
31 #include <vorbisfile.h>
33 #include <textidentificationframe.h>
34 #include <commentsframe.h>
35 #include <xiphcomment.h>
39 #include "utility/string.h"
40 #include "utility/wide_string.h"
44 TagLib::StringList
tagList(const MPD::MutableSong
&s
, MPD::Song::GetFunction f
)
46 TagLib::StringList result
;
48 for (std::string value
; !(value
= (s
.*f
)(idx
)).empty(); ++idx
)
49 result
.append(ToWString(value
));
53 void readCommonTags(MPD::MutableSong
&s
, TagLib::Tag
*tag
)
55 s
.setTitle(tag
->title().to8Bit(true));
56 s
.setArtist(tag
->artist().to8Bit(true));
57 s
.setAlbum(tag
->album().to8Bit(true));
58 s
.setDate(boost::lexical_cast
<std::string
>(tag
->year()));
59 s
.setTrack(boost::lexical_cast
<std::string
>(tag
->track()));
60 s
.setGenre(tag
->genre().to8Bit(true));
61 s
.setComment(tag
->comment().to8Bit(true));
64 void readID3v1Tags(MPD::MutableSong
&s
, TagLib::ID3v1::Tag
*tag
)
66 readCommonTags(s
, tag
);
69 void readID3v2Tags(MPD::MutableSong
&s
, TagLib::ID3v2::Tag
*tag
)
71 auto readFrame
= [&s
](const TagLib::ID3v2::FrameList
&list
, MPD::MutableSong::SetFunction f
) {
73 for (auto it
= list
.begin(); it
!= list
.end(); ++it
, ++idx
)
75 if (auto textFrame
= dynamic_cast<TagLib::ID3v2::TextIdentificationFrame
*>(*it
))
77 auto values
= textFrame
->fieldList();
78 for (auto value
= values
.begin(); value
!= values
.end(); ++value
, ++idx
)
79 (s
.*f
)(value
->to8Bit(true), idx
);
82 (s
.*f
)((*it
)->toString().to8Bit(true), idx
);
85 auto &frames
= tag
->frameListMap();
86 readFrame(frames
["TIT2"], &MPD::MutableSong::setTitle
);
87 readFrame(frames
["TPE1"], &MPD::MutableSong::setArtist
);
88 readFrame(frames
["TPE2"], &MPD::MutableSong::setAlbumArtist
);
89 readFrame(frames
["TALB"], &MPD::MutableSong::setAlbum
);
90 readFrame(frames
["TDRC"], &MPD::MutableSong::setDate
);
91 readFrame(frames
["TRCK"], &MPD::MutableSong::setTrack
);
92 readFrame(frames
["TCON"], &MPD::MutableSong::setGenre
);
93 readFrame(frames
["TCOM"], &MPD::MutableSong::setComposer
);
94 readFrame(frames
["TPE3"], &MPD::MutableSong::setPerformer
);
95 readFrame(frames
["TPOS"], &MPD::MutableSong::setDisc
);
96 readFrame(frames
["COMM"], &MPD::MutableSong::setComment
);
99 void readXiphComments(MPD::MutableSong
&s
, TagLib::Ogg::XiphComment
*tag
)
101 auto readField
= [&s
](const TagLib::StringList
&list
, MPD::MutableSong::SetFunction f
) {
103 for (auto it
= list
.begin(); it
!= list
.end(); ++it
, ++idx
)
104 (s
.*f
)(it
->to8Bit(true), idx
);
106 auto &fields
= tag
->fieldListMap();
107 readField(fields
["TITLE"], &MPD::MutableSong::setTitle
);
108 readField(fields
["ARTIST"], &MPD::MutableSong::setArtist
);
109 readField(fields
["ALBUMARTIST"], &MPD::MutableSong::setAlbumArtist
);
110 readField(fields
["ALBUM"], &MPD::MutableSong::setAlbum
);
111 readField(fields
["DATE"], &MPD::MutableSong::setDate
);
112 readField(fields
["TRACKNUMBER"], &MPD::MutableSong::setTrack
);
113 readField(fields
["GENRE"], &MPD::MutableSong::setGenre
);
114 readField(fields
["COMPOSER"], &MPD::MutableSong::setComposer
);
115 readField(fields
["PERFORMER"], &MPD::MutableSong::setPerformer
);
116 readField(fields
["DISCNUMBER"], &MPD::MutableSong::setDisc
);
117 readField(fields
["COMMENT"], &MPD::MutableSong::setComment
);
120 void writeCommonTags(const MPD::MutableSong
&s
, TagLib::Tag
*tag
)
122 tag
->setTitle(ToWString(s
.getTitle()));
123 tag
->setArtist(ToWString(s
.getArtist()));
124 tag
->setAlbum(ToWString(s
.getAlbum()));
126 tag
->setYear(boost::lexical_cast
<TagLib::uint
>(s
.getDate()));
127 } catch (boost::bad_lexical_cast
&) {
128 std::cerr
<< "writeCommonTags: couldn't write 'year' tag to '" << s
.getURI() << "' as it's not a positive integer\n";
131 tag
->setTrack(boost::lexical_cast
<TagLib::uint
>(s
.getTrack()));
132 } catch (boost::bad_lexical_cast
&) {
133 std::cerr
<< "writeCommonTags: couldn't write 'track' tag to '" << s
.getURI() << "' as it's not a positive integer\n";
135 tag
->setGenre(ToWString(s
.getGenre()));
136 tag
->setComment(ToWString(s
.getComment()));
139 void writeID3v2Tags(const MPD::MutableSong
&s
, TagLib::ID3v2::Tag
*tag
)
141 auto writeID3v2
= [&](const TagLib::ByteVector
&type
, const TagLib::StringList
&list
) {
142 tag
->removeFrames(type
);
145 if (type
== "COMM") // comment needs to be handled separately
147 auto frame
= new TagLib::ID3v2::CommentsFrame(TagLib::String::UTF8
);
148 // apparently there can't be multiple comments,
149 // so if there is more than one, join them.
150 frame
->setText(join(list
, TagLib::String(Config
.tags_separator
, TagLib::String::UTF8
)));
151 tag
->addFrame(frame
);
155 auto frame
= new TagLib::ID3v2::TextIdentificationFrame(type
, TagLib::String::UTF8
);
156 frame
->setText(list
);
157 tag
->addFrame(frame
);
161 writeID3v2("TIT2", tagList(s
, &MPD::Song::getTitle
));
162 writeID3v2("TPE1", tagList(s
, &MPD::Song::getArtist
));
163 writeID3v2("TPE2", tagList(s
, &MPD::Song::getAlbumArtist
));
164 writeID3v2("TALB", tagList(s
, &MPD::Song::getAlbum
));
165 writeID3v2("TDRC", tagList(s
, &MPD::Song::getDate
));
166 writeID3v2("TRCK", tagList(s
, &MPD::Song::getTrack
));
167 writeID3v2("TCON", tagList(s
, &MPD::Song::getGenre
));
168 writeID3v2("TCOM", tagList(s
, &MPD::Song::getComposer
));
169 writeID3v2("TPE3", tagList(s
, &MPD::Song::getPerformer
));
170 writeID3v2("TPOS", tagList(s
, &MPD::Song::getDisc
));
171 writeID3v2("COMM", tagList(s
, &MPD::Song::getComment
));
174 void writeXiphComments(const MPD::MutableSong
&s
, TagLib::Ogg::XiphComment
*tag
)
176 auto writeXiph
= [&](const TagLib::String
&type
, const TagLib::StringList
&list
) {
177 tag
->removeField(type
);
178 for (auto it
= list
.begin(); it
!= list
.end(); ++it
)
179 tag
->addField(type
, *it
, false);
181 // remove field previously used as album artist
182 tag
->removeField("ALBUM ARTIST");
183 // remove field TRACK, some taggers use it as TRACKNUMBER
184 tag
->removeField("TRACK");
185 // remove field DISC, some taggers use it as DISCNUMBER
186 tag
->removeField("DISC");
187 writeXiph("TITLE", tagList(s
, &MPD::Song::getTitle
));
188 writeXiph("ARTIST", tagList(s
, &MPD::Song::getArtist
));
189 writeXiph("ALBUMARTIST", tagList(s
, &MPD::Song::getAlbumArtist
));
190 writeXiph("ALBUM", tagList(s
, &MPD::Song::getAlbum
));
191 writeXiph("DATE", tagList(s
, &MPD::Song::getDate
));
192 writeXiph("TRACKNUMBER", tagList(s
, &MPD::Song::getTrack
));
193 writeXiph("GENRE", tagList(s
, &MPD::Song::getGenre
));
194 writeXiph("COMPOSER", tagList(s
, &MPD::Song::getComposer
));
195 writeXiph("PERFORMER", tagList(s
, &MPD::Song::getPerformer
));
196 writeXiph("DISCNUMBER", tagList(s
, &MPD::Song::getDisc
));
197 writeXiph("COMMENT", tagList(s
, &MPD::Song::getComment
));
200 Tags::ReplayGainInfo
getReplayGain(TagLib::Ogg::XiphComment
*tag
)
202 auto first_or_empty
= [](const TagLib::StringList
&list
) {
205 result
= list
.front().to8Bit(true);
208 auto &fields
= tag
->fieldListMap();
209 return Tags::ReplayGainInfo(
210 first_or_empty(fields
["REPLAYGAIN_REFERENCE_LOUDNESS"]),
211 first_or_empty(fields
["REPLAYGAIN_TRACK_GAIN"]),
212 first_or_empty(fields
["REPLAYGAIN_TRACK_PEAK"]),
213 first_or_empty(fields
["REPLAYGAIN_ALBUM_GAIN"]),
214 first_or_empty(fields
["REPLAYGAIN_ALBUM_PEAK"])
222 bool extendedSetSupported(const TagLib::File
*f
)
224 return dynamic_cast<const TagLib::MPEG::File
*>(f
)
225 || dynamic_cast<const TagLib::Ogg::Vorbis::File
*>(f
)
226 || dynamic_cast<const TagLib::FLAC::File
*>(f
);
229 ReplayGainInfo
readReplayGain(TagLib::File
*f
)
231 ReplayGainInfo result
;
232 if (auto ogg_file
= dynamic_cast<TagLib::Ogg::Vorbis::File
*>(f
))
234 if (auto xiph
= ogg_file
->tag())
235 result
= getReplayGain(xiph
);
237 else if (auto flac_file
= dynamic_cast<TagLib::FLAC::File
*>(f
))
239 if (auto xiph
= flac_file
->xiphComment())
240 result
= getReplayGain(xiph
);
245 void read(MPD::MutableSong
&s
)
247 TagLib::FileRef
f(s
.getURI().c_str());
251 s
.setDuration(f
.audioProperties()->length());
253 if (auto mpeg_file
= dynamic_cast<TagLib::MPEG::File
*>(f
.file()))
255 if (auto id3v1
= mpeg_file
->ID3v1Tag())
256 readID3v1Tags(s
, id3v1
);
257 if (auto id3v2
= mpeg_file
->ID3v2Tag())
258 readID3v2Tags(s
, id3v2
);
260 else if (auto ogg_file
= dynamic_cast<TagLib::Ogg::Vorbis::File
*>(f
.file()))
262 if (auto xiph
= ogg_file
->tag())
263 readXiphComments(s
, xiph
);
265 else if (auto flac_file
= dynamic_cast<TagLib::FLAC::File
*>(f
.file()))
267 if (auto xiph
= flac_file
->xiphComment())
268 readXiphComments(s
, xiph
);
271 readCommonTags(s
, f
.tag());
274 bool write(MPD::MutableSong
&s
)
276 std::string old_name
;
277 if (s
.isFromDatabase())
278 old_name
+= Config
.mpd_music_dir
;
279 old_name
+= s
.getURI();
281 TagLib::FileRef
f(old_name
.c_str());
286 if (auto mpeg_file
= dynamic_cast<TagLib::MPEG::File
*>(f
.file()))
288 writeID3v2Tags(s
, mpeg_file
->ID3v2Tag(true));
289 // write id3v2.4 tags only
290 if (!mpeg_file
->save(TagLib::MPEG::File::ID3v2
, true, 4, false))
292 // do not call generic save() as it will duplicate tags
295 else if (auto ogg_file
= dynamic_cast<TagLib::Ogg::Vorbis::File
*>(f
.file()))
297 writeXiphComments(s
, ogg_file
->tag());
299 else if (auto flac_file
= dynamic_cast<TagLib::FLAC::File
*>(f
.file()))
301 writeXiphComments(s
, flac_file
->xiphComment(true));
304 writeCommonTags(s
, f
.tag());
306 if (!saved
&& !f
.save())
309 if (!s
.getNewName().empty())
311 std::string new_name
;
312 if (s
.isFromDatabase())
313 new_name
+= Config
.mpd_music_dir
;
314 new_name
+= s
.getDirectory() + "/" + s
.getNewName();
315 if (std::rename(old_name
.c_str(), new_name
.c_str()) == 0 && !s
.isFromDatabase())
318 /*if (myTinyTagEditor == myPlaylist)
320 // if we rename local file, it won't get updated
321 // so just remove it from playlist and add again
322 size_t pos = myPlaylist->main().choice();
323 Mpd.StartCommandsList();
325 int id = Mpd.AddSong("file://" + new_name);
328 s = myPlaylist->main().back().value();
329 Mpd.Move(s.getPosition(), pos);
331 Mpd.CommitCommandsList();
333 else // only myBrowser->main()
334 myBrowser->GetDirectory(myBrowser->CurrentDir());*/
342 #endif // HAVE_TAGLIB_H