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 <xiphcomment.h>
38 #include "utility/wide_string.h"
42 TagLib::StringList
tagList(const MPD::MutableSong
&s
, MPD::Song::GetFunction f
)
44 TagLib::StringList result
;
46 for (std::string value
; !(value
= (s
.*f
)(idx
)).empty(); ++idx
)
47 result
.append(ToWString(value
));
51 void readCommonTags(MPD::MutableSong
&s
, TagLib::Tag
*tag
)
53 s
.setTitle(tag
->title().to8Bit(true));
54 s
.setArtist(tag
->artist().to8Bit(true));
55 s
.setAlbum(tag
->album().to8Bit(true));
56 s
.setDate(boost::lexical_cast
<std::string
>(tag
->year()));
57 s
.setTrack(boost::lexical_cast
<std::string
>(tag
->track()));
58 s
.setGenre(tag
->genre().to8Bit(true));
59 s
.setComment(tag
->comment().to8Bit(true));
62 void readID3v1Tags(MPD::MutableSong
&s
, TagLib::ID3v1::Tag
*tag
)
64 readCommonTags(s
, tag
);
67 void readID3v2Tags(MPD::MutableSong
&s
, TagLib::ID3v2::Tag
*tag
)
69 auto readFrame
= [&s
](const TagLib::ID3v2::FrameList
&list
, MPD::MutableSong::SetFunction f
) {
71 for (auto it
= list
.begin(); it
!= list
.end(); ++it
, ++idx
)
73 if (auto textFrame
= dynamic_cast<TagLib::ID3v2::TextIdentificationFrame
*>(*it
))
75 auto values
= textFrame
->fieldList();
76 for (auto value
= values
.begin(); value
!= values
.end(); ++value
, ++idx
)
77 (s
.*f
)(value
->to8Bit(true), idx
);
80 (s
.*f
)((*it
)->toString().to8Bit(true), idx
);
83 auto &frames
= tag
->frameListMap();
84 readFrame(frames
["TIT2"], &MPD::MutableSong::setTitle
);
85 readFrame(frames
["TPE1"], &MPD::MutableSong::setArtist
);
86 readFrame(frames
["TPE2"], &MPD::MutableSong::setAlbumArtist
);
87 readFrame(frames
["TALB"], &MPD::MutableSong::setAlbum
);
88 readFrame(frames
["TDRC"], &MPD::MutableSong::setDate
);
89 readFrame(frames
["TRCK"], &MPD::MutableSong::setTrack
);
90 readFrame(frames
["TCON"], &MPD::MutableSong::setGenre
);
91 readFrame(frames
["TCOM"], &MPD::MutableSong::setComposer
);
92 readFrame(frames
["TPE3"], &MPD::MutableSong::setPerformer
);
93 readFrame(frames
["TPOS"], &MPD::MutableSong::setDisc
);
94 readFrame(frames
["COMM"], &MPD::MutableSong::setComment
);
97 void readXiphComments(MPD::MutableSong
&s
, TagLib::Ogg::XiphComment
*tag
)
99 auto readField
= [&s
](const TagLib::StringList
&list
, MPD::MutableSong::SetFunction f
) {
101 for (auto it
= list
.begin(); it
!= list
.end(); ++it
, ++idx
)
102 (s
.*f
)(it
->to8Bit(true), idx
);
104 auto &fields
= tag
->fieldListMap();
105 readField(fields
["TITLE"], &MPD::MutableSong::setTitle
);
106 readField(fields
["ARTIST"], &MPD::MutableSong::setArtist
);
107 readField(fields
["ALBUMARTIST"], &MPD::MutableSong::setAlbumArtist
);
108 readField(fields
["ALBUM"], &MPD::MutableSong::setAlbum
);
109 readField(fields
["DATE"], &MPD::MutableSong::setDate
);
110 readField(fields
["TRACKNUMBER"], &MPD::MutableSong::setTrack
);
111 readField(fields
["GENRE"], &MPD::MutableSong::setGenre
);
112 readField(fields
["COMPOSER"], &MPD::MutableSong::setComposer
);
113 readField(fields
["PERFORMER"], &MPD::MutableSong::setPerformer
);
114 readField(fields
["DISCNUMBER"], &MPD::MutableSong::setDisc
);
115 readField(fields
["COMMENT"], &MPD::MutableSong::setComment
);
118 void clearID3v1Tags(TagLib::ID3v1::Tag
*tag
)
120 tag
->setTitle(TagLib::String::null
);
121 tag
->setArtist(TagLib::String::null
);
122 tag
->setAlbum(TagLib::String::null
);
125 tag
->setGenre(TagLib::String::null
);
126 tag
->setComment(TagLib::String::null
);
129 void writeCommonTags(const MPD::MutableSong
&s
, TagLib::Tag
*tag
)
131 tag
->setTitle(ToWString(s
.getTitle()));
132 tag
->setArtist(ToWString(s
.getArtist()));
133 tag
->setAlbum(ToWString(s
.getAlbum()));
135 tag
->setYear(boost::lexical_cast
<TagLib::uint
>(s
.getDate()));
136 } catch (boost::bad_lexical_cast
&) {
137 std::cerr
<< "writeCommonTags: couldn't write 'year' tag to '" << s
.getURI() << "' as it's not a positive integer\n";
140 tag
->setTrack(boost::lexical_cast
<TagLib::uint
>(s
.getTrack()));
141 } catch (boost::bad_lexical_cast
&) {
142 std::cerr
<< "writeCommonTags: couldn't write 'track' tag to '" << s
.getURI() << "' as it's not a positive integer\n";
144 tag
->setGenre(ToWString(s
.getGenre()));
145 tag
->setComment(ToWString(s
.getComment()));
148 void writeID3v2Tags(const MPD::MutableSong
&s
, TagLib::ID3v2::Tag
*tag
)
150 auto writeID3v2
= [&](const TagLib::ByteVector
&type
, const TagLib::StringList
&list
) {
151 tag
->removeFrames(type
);
154 auto frame
= new TagLib::ID3v2::TextIdentificationFrame(type
, TagLib::String::UTF8
);
155 frame
->setText(list
);
156 tag
->addFrame(frame
);
159 writeID3v2("TIT2", tagList(s
, &MPD::Song::getTitle
));
160 writeID3v2("TPE1", tagList(s
, &MPD::Song::getArtist
));
161 writeID3v2("TPE2", tagList(s
, &MPD::Song::getAlbumArtist
));
162 writeID3v2("TALB", tagList(s
, &MPD::Song::getAlbum
));
163 writeID3v2("TDRC", tagList(s
, &MPD::Song::getDate
));
164 writeID3v2("TRCK", tagList(s
, &MPD::Song::getTrack
));
165 writeID3v2("TCON", tagList(s
, &MPD::Song::getGenre
));
166 writeID3v2("TCOM", tagList(s
, &MPD::Song::getComposer
));
167 writeID3v2("TPE3", tagList(s
, &MPD::Song::getPerformer
));
168 writeID3v2("TPOS", tagList(s
, &MPD::Song::getDisc
));
169 writeID3v2("COMM", tagList(s
, &MPD::Song::getComment
));
172 void writeXiphComments(const MPD::MutableSong
&s
, TagLib::Ogg::XiphComment
*tag
)
174 auto writeXiph
= [&](const TagLib::String
&type
, const TagLib::StringList
&list
) {
175 tag
->removeField(type
);
176 for (auto it
= list
.begin(); it
!= list
.end(); ++it
)
177 tag
->addField(type
, *it
, false);
179 // remove field previously used as album artist
180 tag
->removeField("ALBUM ARTIST");
181 // remove field TRACK, some taggers use it as TRACKNUMBER
182 tag
->removeField("TRACK");
183 // remove field DISC, some taggers use it as DISCNUMBER
184 tag
->removeField("DISC");
185 writeXiph("TITLE", tagList(s
, &MPD::Song::getTitle
));
186 writeXiph("ARTIST", tagList(s
, &MPD::Song::getArtist
));
187 writeXiph("ALBUMARTIST", tagList(s
, &MPD::Song::getAlbumArtist
));
188 writeXiph("ALBUM", tagList(s
, &MPD::Song::getAlbum
));
189 writeXiph("DATE", tagList(s
, &MPD::Song::getDate
));
190 writeXiph("TRACKNUMBER", tagList(s
, &MPD::Song::getTrack
));
191 writeXiph("GENRE", tagList(s
, &MPD::Song::getGenre
));
192 writeXiph("COMPOSER", tagList(s
, &MPD::Song::getComposer
));
193 writeXiph("PERFORMER", tagList(s
, &MPD::Song::getPerformer
));
194 writeXiph("DISCNUMBER", tagList(s
, &MPD::Song::getDisc
));
195 writeXiph("COMMENT", tagList(s
, &MPD::Song::getComment
));
198 Tags::ReplayGainInfo
getReplayGain(TagLib::Ogg::XiphComment
*tag
)
200 auto first_or_empty
= [](const TagLib::StringList
&list
) {
203 result
= list
.front().to8Bit(true);
206 auto &fields
= tag
->fieldListMap();
207 return Tags::ReplayGainInfo(
208 first_or_empty(fields
["REPLAYGAIN_REFERENCE_LOUDNESS"]),
209 first_or_empty(fields
["REPLAYGAIN_TRACK_GAIN"]),
210 first_or_empty(fields
["REPLAYGAIN_TRACK_PEAK"]),
211 first_or_empty(fields
["REPLAYGAIN_ALBUM_GAIN"]),
212 first_or_empty(fields
["REPLAYGAIN_ALBUM_PEAK"])
220 bool extendedSetSupported(const TagLib::File
*f
)
222 return dynamic_cast<const TagLib::MPEG::File
*>(f
)
223 || dynamic_cast<const TagLib::Ogg::Vorbis::File
*>(f
)
224 || dynamic_cast<const TagLib::FLAC::File
*>(f
);
227 ReplayGainInfo
readReplayGain(TagLib::File
*f
)
229 ReplayGainInfo result
;
230 if (auto ogg_file
= dynamic_cast<TagLib::Ogg::Vorbis::File
*>(f
))
232 if (auto xiph
= ogg_file
->tag())
233 result
= getReplayGain(xiph
);
235 else if (auto flac_file
= dynamic_cast<TagLib::FLAC::File
*>(f
))
237 if (auto xiph
= flac_file
->xiphComment())
238 result
= getReplayGain(xiph
);
243 void read(MPD::MutableSong
&s
)
245 TagLib::FileRef
f(s
.getURI().c_str());
249 s
.setDuration(f
.audioProperties()->length());
251 if (auto mpeg_file
= dynamic_cast<TagLib::MPEG::File
*>(f
.file()))
253 if (auto id3v1
= mpeg_file
->ID3v1Tag())
254 readID3v1Tags(s
, id3v1
);
255 if (auto id3v2
= mpeg_file
->ID3v2Tag())
256 readID3v2Tags(s
, id3v2
);
258 else if (auto ogg_file
= dynamic_cast<TagLib::Ogg::Vorbis::File
*>(f
.file()))
260 if (auto xiph
= ogg_file
->tag())
261 readXiphComments(s
, xiph
);
263 else if (auto flac_file
= dynamic_cast<TagLib::FLAC::File
*>(f
.file()))
265 if (auto xiph
= flac_file
->xiphComment())
266 readXiphComments(s
, xiph
);
269 readCommonTags(s
, f
.tag());
272 bool write(MPD::MutableSong
&s
)
274 std::string old_name
;
275 if (s
.isFromDatabase())
276 old_name
+= Config
.mpd_music_dir
;
277 old_name
+= s
.getURI();
279 TagLib::FileRef
f(old_name
.c_str());
283 if (auto mp3_file
= dynamic_cast<TagLib::MPEG::File
*>(f
.file()))
285 clearID3v1Tags(mp3_file
->ID3v1Tag());
286 writeID3v2Tags(s
, mp3_file
->ID3v2Tag(true));
288 else if (auto ogg_file
= dynamic_cast<TagLib::Ogg::Vorbis::File
*>(f
.file()))
290 writeXiphComments(s
, ogg_file
->tag());
292 else if (auto flac_file
= dynamic_cast<TagLib::FLAC::File
*>(f
.file()))
294 writeXiphComments(s
, flac_file
->xiphComment(true));
297 writeCommonTags(s
, f
.tag());
302 if (!s
.getNewName().empty())
304 std::string new_name
;
305 if (s
.isFromDatabase())
306 new_name
+= Config
.mpd_music_dir
;
307 new_name
+= s
.getDirectory() + "/" + s
.getNewName();
308 if (std::rename(old_name
.c_str(), new_name
.c_str()) == 0 && !s
.isFromDatabase())
311 /*if (myTinyTagEditor == myPlaylist)
313 // if we rename local file, it won't get updated
314 // so just remove it from playlist and add again
315 size_t pos = myPlaylist->main().choice();
316 Mpd.StartCommandsList();
318 int id = Mpd.AddSong("file://" + new_name);
321 s = myPlaylist->main().back().value();
322 Mpd.Move(s.getPosition(), pos);
324 Mpd.CommitCommandsList();
326 else // only myBrowser->main()
327 myBrowser->GetDirectory(myBrowser->CurrentDir());*/
335 #endif // HAVE_TAGLIB_H